From 80ce5ea115f89148d189606daf1f77146a785f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Sch=C3=B6chlin?= Date: Wed, 25 Dec 2024 11:25:29 +0100 Subject: [PATCH] Add a docker workflow - use a shared workflow for the release and development builds - update base docker image to zabbix 7.2 - add docker build and release steps - add testing procedure to docker build - documentation: add reference to dockerhub --- .../continuous-integration-workflow.yml | 20 +-- .github/workflows/release.yml | 26 +--- .github/workflows/shared.yml | 54 ++++++++ DEVELOPMENT.md | 4 +- Dockerfile | 7 +- README.md | 8 +- create_docker | 118 ++++++++++++++++-- docker-scripts/01_phase_base.sh | 1 + docker-scripts/04_install_agent_extensions.sh | 1 - 9 files changed, 185 insertions(+), 54 deletions(-) create mode 100644 .github/workflows/shared.yml diff --git a/.github/workflows/continuous-integration-workflow.yml b/.github/workflows/continuous-integration-workflow.yml index 4110cd1..6f8ef37 100644 --- a/.github/workflows/continuous-integration-workflow.yml +++ b/.github/workflows/continuous-integration-workflow.yml @@ -4,19 +4,7 @@ on: branches: - '*' jobs: - build: - name: CI - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: Setup - run: | - sudo apt-get install ruby-dev build-essential debhelper devscripts rpm xalan wget -y - sudo gem install fpm - - name: Build Package - run: ./create_packages - - name: Test Package - run: sudo ./ci_test + CI_DEV: + uses: ./.github/workflows/shared.yml + with: + publish_steps: false diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a6274d6..913113b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,24 +6,8 @@ on: - '*' jobs: - build: - runs-on: ubuntu-latest - permissions: - contents: write - steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - fetch-depth: 0 - ref: refs/heads/main - - name: Setup - run: | - sudo apt-get install ruby-dev build-essential debhelper devscripts rpm xalan wget -y - sudo gem install fpm - - name: Build Package - run: ./create_packages - - name: Test Package - run: sudo ./ci_test - - uses: ncipollo/release-action@v1 - with: - artifacts: "zabbix-agent-extensions*.deb,zabbix-agent-extensions-*.rpm" + CI_RELEASE: + uses: ./.github/workflows/shared.yml + with: + publish_steps: true + diff --git a/.github/workflows/shared.yml b/.github/workflows/shared.yml new file mode 100644 index 0000000..e11173d --- /dev/null +++ b/.github/workflows/shared.yml @@ -0,0 +1,54 @@ +name: Shared Build Workflow + +on: + workflow_call: + inputs: + publish_steps: + description: "Publish artefacts" + required: false + default: false + type: boolean + +jobs: + build-pkg: + name: CI-PKGs + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Setup + run: | + sudo apt-get install ruby-dev build-essential debhelper devscripts rpm xalan wget -y + sudo gem install fpm + - name: Build Package + run: ./create_packages + - name: Test Package + run: sudo ./ci_test + - uses: ncipollo/release-action@v1 + if: ${{ inputs.publish_steps }} + with: + artifacts: "zabbix-agent-extensions*.deb,zabbix-agent-extensions-*.rpm" + build-docker: + name: CI-Docker + runs-on: ubuntu-latest + environment: "Docker Upload" + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Login to Docker Hub + if: ${{ inputs.publish_steps }} + uses: docker/login-action@v3 + with: + username: ${{ vars.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build + run: | + ./create_docker build + - name: Publish docker images + if: ${{ inputs.publish_steps }} + run: | + ./create_docker publish diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index da6496d..facb05b 100755 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -47,5 +47,7 @@ git push git push --tags ``` - * TravisCI builds and tests the release and uploads it to github + * Github builds and tests the release and uploads it to github + * RPM and DEB packages + * Docker Images diff --git a/Dockerfile b/Dockerfile index 825f51f..c863edc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ -FROM zabbix/zabbix-agent:ubuntu-6.4-latest -MAINTAINER operations@flipapp.de +# see https://github.com/zabbix/zabbix-docker/blob/7.2/Dockerfiles/agent/ubuntu/Dockerfile +FROM zabbix/zabbix-agent:ubuntu-7.2-latest +ARG BUILD_DATE USER 0 ADD /docker-scripts /tmp/setup RUN chmod 755 /tmp/setup/*.sh @@ -10,7 +11,7 @@ ADD zabbix-agent-extensions_*_all.deb /tmp/setup RUN /tmp/setup/04_install_agent_extensions.sh RUN /tmp/setup/05_perform_upgrade.sh -ENTRYPOINT ["/usr/bin/tini", "--", "/usr/bin/docker-entrypoint.sh"] +ENTRYPOINT ["/usr/bin/docker-entrypoint.sh"] USER 1997 diff --git a/README.md b/README.md index 2c1dea5..3cd5f18 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ zabbix-agent-extensions ======================= +[![Images at Dockerhub](https://www.docker.com/wp-content/uploads/2024/02/cropped-docker-logo-favicon-32x32.png)](https://hub.docker.com/repository/docker/scoopex666/zabbix-agent-with-agent-extensions/) [![Build package and test](https://github.com/scoopex/zabbix-agent-extensions/actions/workflows/continuous-integration-workflow.yml/badge.svg)](https://github.com/scoopex/zabbix-agent-extensions/actions/workflows/continuous-integration-workflow.yml) [![Releases](https://github.com/scoopex/zabbix-agent-extensions/actions/workflows/release.yml/badge.svg)](https://github.com/scoopex/zabbix-agent-extensions/actions/workflows/release.yml) @@ -9,7 +10,7 @@ zabbix-agent-extensions A set of zabbix UserParameter scripts and production ready monitoring templates for linux systems. * this project ist originated on: https://github.com/scoopex/zabbix-agent-extensions - * the ci is implmented with github actions + * the ci is implemented with github actions (creating packages, docker images and testing) * docker images of a zabbix agent can be obtained at https://hub.docker.com/repository/docker/scoopex666/zabbix-agent-with-agent-extensions # Monitoring details @@ -109,6 +110,8 @@ Install the agent extensions on kubernetes nodes --------------------------------------------- This procedure deploy zabbix agents on all worker nodes of your kubernetes cluster and provides autodiscovery for all nodes +It is based on the offical [image](https://hub.docker.com/r/zabbix/zabbix-agent). + * Make zabbix available to the deployed agent, i.e. by deploying a zabbix proxy in k8s * Import template [custom-os-kubernetes-node.xml](http://htmlpreview.github.io/?https://github.com/scoopex/zabbix-agent-extensions/blob/master/zabbix_templates/5.2/documentation/custom-os-kubernetes-node.html) @@ -123,7 +126,7 @@ This procedure deploy zabbix agents on all worker nodes of your kubernetes clust * "Link to templates: Custom - OS - Kubernetes Node" * Configure deployment * Download [kubernetes yaml file](https://raw.githubusercontent.com/scoopex/zabbix-agent-extensions/master/zabbix-agent-daemonset-kubernetes.yaml) - * Adapt the file to your own needs + * Adapt the file to your own needs, see [environment variables](https://hub.docker.com/r/zabbix/zabbix-agent) * Configure `ZBX_ACTIVESERVER` * Configure `ZBX_PASSIVESERVERS` * Configure the version of the image [check dockerhub](https://hub.docker.com/repository/docker/scoopex666/zabbix-agent-with-agent-extensions) @@ -134,7 +137,6 @@ This procedure deploy zabbix agents on all worker nodes of your kubernetes clust kubectl logs -n infra-zabbix-agent zabbix-agent-8n4ss -f ``` - How to configure the zabbix server/templates -------------------------------------------- diff --git a/create_docker b/create_docker index 4b5a3cb..8018bee 100755 --- a/create_docker +++ b/create_docker @@ -2,21 +2,121 @@ IMAGE_REPO="scoopex666" IMAGE_NAME="zabbix-agent-with-agent-extensions" +CONTAINER_TEST_NAME="zabbix-agent-extentions-test" VERSION="${2:-$(git describe --abbrev=0 --tags)}" -if [ "$1" = "build" ];then - docker build -t ${IMAGE_NAME}:${VERSION} -f Dockerfile . -elif [ "$1" = "inspect" ];then - docker run -ti ${IMAGE_NAME}:${VERSION} /bin/bash -elif [ "$1" = "publish" ];then + +FAILED_TESTS="" + +assertSuccess(){ + local STRATEGY="$1" + local CMD="docker exec -u root -ti ${CONTAINER_TEST_NAME} $2" + echo "****************************************************************************" + echo "** EXECUTE: $CMD"; + eval "$CMD 2>&1" + local RET="$?" + echo "**" + + if [ "$RET" != "0" ];then + echo "** ERROR: execution failed (returncode $RET)" + FAILED_TESTS="$FAILED_TESTS#assertSuccess => $CMD" + echo "****************************************************************************" + if [ "$STRATEGY" = "STOP_ON_ERROR" ];then + exit 100 + else + return 100 + fi + fi + echo "****************************************************************************" + return 0 +} + + + +testPackage(){ + buildDocker + docker stop ${CONTAINER_TEST_NAME} + set -x + docker run --privileged --rm \ + -d \ + --network host \ + -v $PWD:/src \ + -e ZBX_HOSTNAME=${CONTAINER_TEST_NAME} \ + -e ZBX_SERVER_HOST=127.0.0.1 \ + -e ZBX_PASSIVESERVERS=127.0.0.1 \ + --name ${CONTAINER_TEST_NAME} ${IMAGE_NAME}:${VERSION} + set +x + echo '{ "{#FOO}" : "BAR" }' | docker exec -i -u root ${CONTAINER_TEST_NAME} tee /etc/zabbix/zabbix-discovery-generic/foo-bar.json + assertSuccess STOP_ON_ERROR "ls -l /etc/zabbix/zabbix-discovery-generic/foo-bar.json" + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k 'generic.discovery[foo,json]' | tee /dev/stderr | grep -q -P 'BAR'" # Without sudo + + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k linux.dmesg | tee /dev/stderr | grep -q -P 'OK: ALL OK|ERROR:'" # Without sudo + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k linux.dmesg | tee /dev/stderr | grep -q -P 'OK: ALL OK|ERROR:'" # Without sudo + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k linux.multipath | tee /dev/stderr | grep -q 'OK:'" # With sudo + + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k 'custom.process[zabbix_agentd,minage]'|tee /dev/stderr | grep -q -P '^\d+'" + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k 'custom.process[zabbix_agentd,maxage]'|tee /dev/stderr | grep -q -P '^\d+'" + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k 'custom.process[nosuchprocess,maxage]'|tee /dev/stderr | grep -q -P '^\d+'" + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k 'zabbix.agent_extensions.version'|tee /dev/stderr | grep -q -P '^\d+'" + + docker exec -i -u root ${CONTAINER_TEST_NAME} mkdir -m 755 -p /opt/puppetlabs/puppet/cache/state/ + docker exec -i -u root ${CONTAINER_TEST_NAME} cp /src/extension-files/test/last_run_summary.yaml /opt/puppetlabs/puppet/cache/state/last_run_summary.yaml + docker exec -i -u root ${CONTAINER_TEST_NAME} cp /src/extension-files/test/last_run_report.yaml /opt/puppetlabs/puppet/cache/state/last_run_report.yaml + + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k 'puppet[state]'|tee /dev/stderr | grep -q -P '^OK: puppetrun successful, no changes'" + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k 'puppet[changes,total]'|tee /dev/stderr | grep -q -P '^\d+'" + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k 'puppet[environment]'|tee /dev/stderr | grep -q -P '^production_master'" + + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k vfs.dev.discovery | tee /dev/stderr | grep -q '#BLOCKDEVICE'" + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k vfs.fs.discovery | grep -v /proc" + assertSuccess STOP_ON_ERROR "zabbix_get -s 127.0.0.1 -k nfs.rpc.calls|grep -P '^\d+'" + + docker stop ${CONTAINER_TEST_NAME} + echo + echo + echo "*** SUMMARY" + if [ -z "$FAILED_TESTS" ];then + echo "ALL TESTS PASSED" + return 0 + else + echo "THE FOLLOWING TESTS FAILED" + echo "$FAILED_TESTS"|tr '#' '\n'|sed '~s,^, ,' + return 1 + fi +} + +buildDocker(){ + set -x + docker rmi ${IMAGE_NAME}:${VERSION} + set -e + docker build --progress plain --build-arg BUILD_DATE="$(date "+%Y-%m-%d")" -t ${IMAGE_NAME}:${VERSION} -f Dockerfile . + set +xe +} + +inspectDocker(){ + set -x + docker stop ${CONTAINER_TEST_NAME} + exec docker run --network host --rm --name ${CONTAINER_TEST_NAME} -ti ${IMAGE_NAME}:${VERSION} /bin/bash +} + +publishDocker(){ + buildDocker + set -xe docker tag ${IMAGE_NAME}:${VERSION} ${IMAGE_REPO}/${IMAGE_NAME}:${VERSION} docker push ${IMAGE_REPO}/${IMAGE_NAME}:${VERSION} docker tag ${IMAGE_NAME}:${VERSION} ${IMAGE_REPO}/${IMAGE_NAME}:latest docker push ${IMAGE_REPO}/${IMAGE_NAME}:latest + set +xe +} + +if [ "$1" = "build" ];then + buildDocker +elif [ "$1" = "inspect" ];then + inspectDocker +elif [ "$1" = "publish" ];then + publishDocker elif [ "$1" = "test" ];then - docker run ${IMAGE_NAME}:${VERSION} + testPackage else - echo "$0 build|inspect|publish|run" + echo "$0 build|inspect|publish|test" fi - - diff --git a/docker-scripts/01_phase_base.sh b/docker-scripts/01_phase_base.sh index e236cd1..f1c1dbf 100755 --- a/docker-scripts/01_phase_base.sh +++ b/docker-scripts/01_phase_base.sh @@ -12,6 +12,7 @@ apt-get dist-upgrade -y apt-get autoremove -y apt-get clean apt-get autoremove -y + rm -rf /var/lib/apt/lists/* echo "set nocompatible" > /var/lib/zabbix/.vimrc diff --git a/docker-scripts/04_install_agent_extensions.sh b/docker-scripts/04_install_agent_extensions.sh index c99f40a..fae2e8b 100755 --- a/docker-scripts/04_install_agent_extensions.sh +++ b/docker-scripts/04_install_agent_extensions.sh @@ -4,4 +4,3 @@ cd /tmp/setup ls -l /tmp/setup dpkg -i *.deb sed -i '~s,/var/lib/zabbix/:/sbin/nologin,/var/lib/zabbix/:/bin/bash,' /etc/passwd -#cat /etc/passwd