From 2b1f503a0e8a0b1b52ee058c9c4ce49de83a7dab Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Fri, 2 May 2025 20:42:31 +0330 Subject: [PATCH 01/13] added docker sock check --- .github/workflows/dev.yml | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/dev.yml diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml new file mode 100644 index 0000000..58f7a63 --- /dev/null +++ b/.github/workflows/dev.yml @@ -0,0 +1,46 @@ +name: CI + +on: + push: + paths-ignore: + - '**.md' + - 'extra/**' + - 'capture/**' + - 'ansible/**' + - 'dashboards/**' + - 'scripts/**' + - '.gitignore' + - 'LICENSE' + branches: + - dev + - ci + pull_request: + paths-ignore: + - 'extra/**' + - 'capture/**' + - 'ansible/**' + - 'dashboards/**' + - 'scripts/**' + - '.gitignore' + - 'LICENSE' + +permissions: + contents: write + pull-requests: read + +env: + ghcr_repo_addr: ghcr.io/shayan-ghani/container-exporter:latest + +jobs: + probe: + runs-on: ubuntu-latest + steps: + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: check for docker sock + run: | + sudo ls /var/run/ \ No newline at end of file From af26b1ae14c3bf1bb197c279799f9934734b6ae7 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Fri, 2 May 2025 21:11:37 +0330 Subject: [PATCH 02/13] added healtheck --- .github/workflows/dev.yml | 8 ++++++-- healthcheck.sh | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 healthcheck.sh diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 58f7a63..d0b162c 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -41,6 +41,10 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: check for docker sock + - name: deploy run: | - sudo ls /var/run/ \ No newline at end of file + docker compose -f container-exporter-local.yml up --build + + - name: check for health + run: | + bash ./healthcheck.sh diff --git a/healthcheck.sh b/healthcheck.sh new file mode 100644 index 0000000..cc16d1f --- /dev/null +++ b/healthcheck.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +log_dir="/opt/src/logs" +mkdir -p $log_dir + +if ! curl http://localhost:8000/ > "${log_dir}/index.txt"; then + echo "Port 8000 not responding" + exit 1 +fi + + +if ! curl --max-time 6 --silent --show-error http://localhost:8000/metrics > "${log_dir}/metrics.txt"; then + echo "/metrics endpoint not responding" + cat "${log_dir}/metrics.txt" + exit 1 +fi + + +exit 0 From f77f62a75321b8052ab799b8939802b23c677c58 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Fri, 2 May 2025 21:15:07 +0330 Subject: [PATCH 03/13] added checkout --- .github/workflows/dev.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index d0b162c..8cb7a60 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -35,6 +35,9 @@ jobs: probe: runs-on: ubuntu-latest steps: + - name: Checkout + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 + - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -43,7 +46,7 @@ jobs: - name: deploy run: | - docker compose -f container-exporter-local.yml up --build + docker compose -f ./container-exporter-local.yml up --build - name: check for health run: | From 9cac9102e47b522948a4d26af007e6d82f122631 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Fri, 2 May 2025 21:17:03 +0330 Subject: [PATCH 04/13] moved compose to bg --- .github/workflows/dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 8cb7a60..71a7333 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -46,7 +46,7 @@ jobs: - name: deploy run: | - docker compose -f ./container-exporter-local.yml up --build + docker compose -f ./container-exporter-local.yml up --build -d - name: check for health run: | From dd75b3c5dcc1c265768a8eb09c43f66c75a62303 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Fri, 2 May 2025 22:08:07 +0330 Subject: [PATCH 05/13] added container debugging --- .github/workflows/dev.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 71a7333..15cd8d6 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -50,4 +50,7 @@ jobs: - name: check for health run: | + sleep 5 + docker ps + bash -c " Date: Fri, 2 May 2025 22:40:11 +0330 Subject: [PATCH 06/13] updated healthcheck script --- .github/workflows/dev.yml | 5 +---- healthcheck.sh | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 15cd8d6..766c885 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -48,9 +48,6 @@ jobs: run: | docker compose -f ./container-exporter-local.yml up --build -d - - name: check for health + - name: check for metrics and overall api health run: | - sleep 5 - docker ps - bash -c " "${log_dir}/index.txt"; then - echo "Port 8000 not responding" - exit 1 -fi +for i in 1 2 +do + sleep 5 + + if ! curl http://localhost:8000/ > "${log_dir}/index.txt"; then + echo "Port 8000 not responding" + exit 1 + fi -if ! curl --max-time 6 --silent --show-error http://localhost:8000/metrics > "${log_dir}/metrics.txt"; then - echo "/metrics endpoint not responding" - cat "${log_dir}/metrics.txt" - exit 1 -fi + if ! curl --max-time 6 --silent --show-error http://localhost:8000/metrics > "${log_dir}/metrics.txt"; then + echo "/metrics endpoint not responding" + exit 1 + fi + + echo -e "METRICS : \n\n" + cat "${log_dir}/metrics.txt" | grep 'container_name' | sort | uniq +done exit 0 From ad8fdcf7264da4f4172544e4ae8bf490dbf6ff9c Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Fri, 2 May 2025 23:12:52 +0330 Subject: [PATCH 07/13] chore : added healthcheck for docker --- Dockerfile | 11 +++++++++-- container-exporter-local.yml | 2 -- container-exporter.yml | 4 +--- healthcheck.sh | 26 -------------------------- scripts/healthcheck.sh | 21 +++++++++++++++++++++ 5 files changed, 31 insertions(+), 33 deletions(-) delete mode 100644 healthcheck.sh create mode 100644 scripts/healthcheck.sh diff --git a/Dockerfile b/Dockerfile index 6d8718c..f63cb91 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,10 +12,17 @@ WORKDIR /opt/src COPY requirements.txt . -RUN pip install --no-cache-dir -r requirements.txt +RUN pip install --no-cache-dir -r requirements.txt \ + && apt-get update && apt-get install -y --no-install-recommends curl \ + && rm -rf /var/lib/apt/lists/* COPY . . -CMD "./start.sh" $CONTAINER_EXPORTER_PORT +RUN chmod +x /opt/src/healthcheck.sh + +HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=2 \ + CMD /opt/src/scripts/healthcheck.sh + +CMD "./start.sh" diff --git a/container-exporter-local.yml b/container-exporter-local.yml index b61e48d..dfe403f 100644 --- a/container-exporter-local.yml +++ b/container-exporter-local.yml @@ -8,8 +8,6 @@ services: container_name: container-exporter volumes: - /var/run/docker.sock:/var/run/docker.sock - environment: - CONTAINER_EXPORTER_PORT: ${CONTAINER_EXPORTER_PORT} ports: - 8000:8000 restart: always \ No newline at end of file diff --git a/container-exporter.yml b/container-exporter.yml index ab93cb5..21c043e 100644 --- a/container-exporter.yml +++ b/container-exporter.yml @@ -2,12 +2,10 @@ version: !!str 3.7 services: cxp: - image: devopsteen/cxp:latest # use alpine tag for a lighter image + image: shayanghani/container-exporter:latest container_name: container-exporter volumes: - /var/run/docker.sock:/var/run/docker.sock ports: - 8000:8000 - environment: - CONTAINER_EXPORTER_PORT: ${CONTAINER_EXPORTER_PORT} restart: always \ No newline at end of file diff --git a/healthcheck.sh b/healthcheck.sh deleted file mode 100644 index 5cc54a8..0000000 --- a/healthcheck.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -log_dir="/opt/src/logs" -mkdir -p $log_dir - -for i in 1 2 -do - sleep 5 - - if ! curl http://localhost:8000/ > "${log_dir}/index.txt"; then - echo "Port 8000 not responding" - exit 1 - fi - - - if ! curl --max-time 6 --silent --show-error http://localhost:8000/metrics > "${log_dir}/metrics.txt"; then - echo "/metrics endpoint not responding" - exit 1 - fi - - echo -e "METRICS : \n\n" - cat "${log_dir}/metrics.txt" | grep 'container_name' | sort | uniq -done - - -exit 0 diff --git a/scripts/healthcheck.sh b/scripts/healthcheck.sh new file mode 100644 index 0000000..3fe6605 --- /dev/null +++ b/scripts/healthcheck.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +log_dir="/opt/src/logs" +mkdir -p $log_dir + + +if ! curl http://localhost:8000/ > "${log_dir}/index.txt"; then +echo "Port 8000 not responding" +exit 1 +fi + + +if ! curl --max-time 6 --silent --show-error http://localhost:8000/metrics > "${log_dir}/metrics.txt"; then +echo "/metrics endpoint not responding" +exit 1 +fi + +echo -e "METRICS : \n\n" + + +exit 0 From 8619c655bc813a3fb3c23cb2b4105714505c0c49 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Fri, 2 May 2025 23:15:17 +0330 Subject: [PATCH 08/13] chore(ci): added probe for ci --- .github/workflows/ci.yml | 44 ++++++++++++++++++++++++++++++++++----- scripts/healthcheck-ci.sh | 26 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 scripts/healthcheck-ci.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 300d016..062bbb2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,6 +29,8 @@ permissions: env: ghcr_repo_addr: ghcr.io/shayan-ghani/container-exporter:latest + docker_hub_addr: shayanghani/container-exporter:latest + docker_hub_user: shayanghani jobs: @@ -38,10 +40,17 @@ jobs: - name: Checkout uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - - name: Login to GHCR - run: | - echo ${{ secrets.GHCR_TOKEN }} | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ env.docker_hub_user }} + password: ${{ secrets.DOCKER_TOKEN }} + - name: Login to GHCR + uses: docker/login-action@v3 + with: + username: ${{ github.repository_owner }} + password: ${{ secrets.GHCR_TOKEN }} - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -54,8 +63,33 @@ jobs: with: context: . push: true - tags: ${{env.ghcr_repo_addr}} + tags: | + ${{env.ghcr_repo_addr}} + ${{env.docker_repo_addr}} + + + + probe: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: deploy + run: | + docker compose -f ./container-exporter-local.yml up --build -d + + - name: probe metrics api and print out metrics + run: | + bash ./scripts/healthcheck-ci.sh + + # test: # ## pytest to be implemented here ... @@ -63,7 +97,7 @@ jobs: release: if: github.event_name != 'pull_request' runs-on: ubuntu-latest - needs: docker-build + needs: probe steps: - name: Bump version and push tag diff --git a/scripts/healthcheck-ci.sh b/scripts/healthcheck-ci.sh new file mode 100644 index 0000000..5cc54a8 --- /dev/null +++ b/scripts/healthcheck-ci.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +log_dir="/opt/src/logs" +mkdir -p $log_dir + +for i in 1 2 +do + sleep 5 + + if ! curl http://localhost:8000/ > "${log_dir}/index.txt"; then + echo "Port 8000 not responding" + exit 1 + fi + + + if ! curl --max-time 6 --silent --show-error http://localhost:8000/metrics > "${log_dir}/metrics.txt"; then + echo "/metrics endpoint not responding" + exit 1 + fi + + echo -e "METRICS : \n\n" + cat "${log_dir}/metrics.txt" | grep 'container_name' | sort | uniq +done + + +exit 0 From cc8b86232486ff5239434e012bce64f3f9450ce1 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Fri, 2 May 2025 23:20:07 +0330 Subject: [PATCH 09/13] fix(ci): added registry address for ghcr login --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 062bbb2..d7d2feb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,7 @@ jobs: - name: Login to GHCR uses: docker/login-action@v3 with: + registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GHCR_TOKEN }} From ab69093c6cd4800059b5fe1d9eb178e6a3469a31 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Fri, 2 May 2025 23:34:08 +0330 Subject: [PATCH 10/13] fix(ci): healthcheck and docker compose path --- .github/workflows/ci.yml | 3 ++- .github/workflows/dev.yml | 2 +- Dockerfile | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d7d2feb..2e3f353 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,6 +72,7 @@ jobs: probe: runs-on: ubuntu-latest + needs: docker-build steps: - name: Checkout uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 @@ -84,7 +85,7 @@ jobs: - name: deploy run: | - docker compose -f ./container-exporter-local.yml up --build -d + docker compose -f ./container-exporter.yml up --build -d - name: probe metrics api and print out metrics run: | diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 766c885..10c39cd 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -50,4 +50,4 @@ jobs: - name: check for metrics and overall api health run: | - bash ./healthcheck.sh + bash ./scripts/healthcheck-ci.sh diff --git a/Dockerfile b/Dockerfile index f63cb91..ef285d6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ RUN pip install --no-cache-dir -r requirements.txt \ COPY . . -RUN chmod +x /opt/src/healthcheck.sh +RUN chmod +x /opt/src/scripts/healthcheck.sh HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=2 \ CMD /opt/src/scripts/healthcheck.sh From e641c11268b4d2049a6eadb01867eed76eba6eb3 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Sat, 3 May 2025 12:25:25 +0330 Subject: [PATCH 11/13] fix(ci): docker hub repo env typo --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2e3f353..1d02e29 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ permissions: env: ghcr_repo_addr: ghcr.io/shayan-ghani/container-exporter:latest - docker_hub_addr: shayanghani/container-exporter:latest + docker_repo_addr: shayanghani/container-exporter:latest docker_hub_user: shayanghani jobs: From 5860de76fdd74276446973d35f31974f40b87660 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Sat, 3 May 2025 12:50:16 +0330 Subject: [PATCH 12/13] refactor(ci): changed namings and practices --- .github/workflows/cd.yml | 35 +++++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 12 ++++++------ Dockerfile | 4 +--- container-exporter.yml | 2 +- 4 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/cd.yml diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..b4ed529 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,35 @@ +name: CI + +on: + push: + paths-ignore: + - '**.md' + - 'extra/**' + - 'capture/**' + - 'ansible/**' + - 'dashboards/**' + - 'scripts/**' + - '.gitignore' + - 'LICENSE' + branches: + - master + +env: + ghcr_repo_addr: ghcr.io/shayan-ghani/container-exporter:latest + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: deploy from ghcr + run: | + IMAGE=${{env.ghcr_repo_addr}} docker compose -f ./container-exporter.yml up -d \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1d02e29..94f3016 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,7 +70,7 @@ jobs: - probe: + healthcheck: runs-on: ubuntu-latest needs: docker-build steps: @@ -83,11 +83,11 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: deploy + - name: deploy from ghcr run: | - docker compose -f ./container-exporter.yml up --build -d + IMAGE=${{env.ghcr_repo_addr}} docker compose -f ./container-exporter.yml up -d - - name: probe metrics api and print out metrics + - name: check metrics api and print out metrics run: | bash ./scripts/healthcheck-ci.sh @@ -97,9 +97,9 @@ jobs: # ## pytest to be implemented here ... release: - if: github.event_name != 'pull_request' + if: github.event_name != 'pull_request' && github.ref_name == 'master' runs-on: ubuntu-latest - needs: probe + needs: healthcheck steps: - name: Bump version and push tag diff --git a/Dockerfile b/Dockerfile index ef285d6..90791a2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,4 @@ RUN chmod +x /opt/src/scripts/healthcheck.sh HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=2 \ CMD /opt/src/scripts/healthcheck.sh -CMD "./start.sh" - - +CMD ["./start.sh"] diff --git a/container-exporter.yml b/container-exporter.yml index 21c043e..b63b67d 100644 --- a/container-exporter.yml +++ b/container-exporter.yml @@ -2,7 +2,7 @@ version: !!str 3.7 services: cxp: - image: shayanghani/container-exporter:latest + image: "${IMAGE:-shayanghani/container-exporter:latest}" container_name: container-exporter volumes: - /var/run/docker.sock:/var/run/docker.sock From 5236fa0579f4adfee11aea98e6ff0a03fb6983f0 Mon Sep 17 00:00:00 2001 From: Shayan Ghani Date: Sat, 3 May 2025 12:52:49 +0330 Subject: [PATCH 13/13] fix(ci): login to ghcr for healthcheck --- .github/workflows/ci.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 94f3016..14d2591 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -77,6 +77,13 @@ jobs: - name: Checkout uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 + - name: Login to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GHCR_TOKEN }} + - name: Set up QEMU uses: docker/setup-qemu-action@v3