From 50587cbd1c9a7f9e5cea717533a9e731ed735218 Mon Sep 17 00:00:00 2001 From: Nemo Date: Fri, 24 Jun 2022 12:51:36 +0530 Subject: [PATCH] container stuff --- .github/workflows/ci.yml | 79 +++++++++++++++++++++++++++++++++++++ .github/workflows/tests.yml | 18 --------- .gitignore | 3 +- Dockerfile | 36 +++++++++++++++-- README.md | 34 +++++----------- index.js | 6 +-- package-lock.json | 11 ------ package.json | 5 +-- server.js | 2 +- 9 files changed, 126 insertions(+), 68 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..56a8dc0 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,79 @@ +on: push +name: CI +jobs: + npm-tests: + env: + CHROME_BIN: /usr/bin/chromium + runs-on: [self-hosted] + strategy: + matrix: + node: ['16'] + name: Node Tests + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v2 + with: + node-version: ${{matrix.node}} + - run: npm install + - name: test + continue-on-error: true + id: test + run: npm test + - name: retry + continue-on-error: true + id: retry1 + if: steps.test.outcome=='failure' + run: npm test + - name: set the status + if: always() + run: | + if ${{ steps.test.outcome=='success' || steps.retry1.outcome=='success' }}; then + echo "Tests Passed" + else + exit 1 + fi + docker-tests: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Checkout Code + uses: actions/checkout@v3 + - name: Docker test + run: | + wget https://github.com/tkp1n/chromium-ci/raw/41510dc154c4184f7e09461ba76f86f61c460070/seccomp/chromium.json + docker build -t prom-act-test . + docker run --security-opt seccomp=chromium.json --detach --name=prom-act-test prom-act-test + until [ "`docker inspect -f {{.State.Health.Status}} prom-act-test`"=="healthy" ]; do + sleep 1; + echo "Waiting for container to be healthy" + done; + # Show usage + docker exec prom-act-test wget -q -O- localhost:3000/metrics | grep act_fup + # Check logs + docker logs prom-act-test + # Stop and kill the test container + docker stop prom-act-test && docker rm prom-act-test && docker rmi prom-act-test + publish: + needs: [npm-tests, docker-tests] + runs-on: [self-hosted] + steps: + - name: Checkout Code + uses: actions/checkout@v3 + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Push to GitHub Container Registry + uses: docker/build-push-action@v2 + if: ${{github.ref_name != 'master'}} + with: + push: true + tags: ghcr.io/${{ github.repository }}:${{github.ref_name}} + - name: Push to GitHub Container Registry (latest) + uses: docker/build-push-action@v2 + if: ${{github.ref_name == 'master'}} + with: + push: true + tags: ghcr.io/${{ github.repository }}:latest diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml deleted file mode 100644 index a811abc..0000000 --- a/.github/workflows/tests.yml +++ /dev/null @@ -1,18 +0,0 @@ -on: push -name: Tests -jobs: - tests: - runs-on: [self-hosted] - strategy: - matrix: - node: ['16'] - name: Node Tests - steps: - - uses: actions/checkout@v1 - - uses: actions/setup-node@v2 - with: - node-version: ${{matrix.node}} - - run: npm install - - run: npm test - env: - CHROME_BIN: /usr/bin/chromium diff --git a/.gitignore b/.gitignore index e47366d..f289bad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -screenshots/ \ No newline at end of file +screenshots/ +chromium.json diff --git a/Dockerfile b/Dockerfile index 1a6e3c6..8889a66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,43 @@ -FROM schliflo/docker-puppeteer:14.4.1 +FROM alpine LABEL maintainer "Nemo " +RUN apk add --no-cache \ + chromium \ + nss \ + freetype \ + harfbuzz \ + ca-certificates \ + ttf-freefont \ + nodejs \ + npm + +# Tell Puppeteer to skip installing Chrome. We'll be using the installed package. +ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \ + PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser \ + CHROME_BIN=/usr/bin/chromium-browser + +# Add user so we don't need --no-sandbox. +RUN addgroup -S pptruser && adduser -S -G pptruser pptruser \ + && addgroup pptruser audio \ + && addgroup pptruser video \ + && mkdir -p /home/pptruser/Downloads /app \ + && chown -R pptruser:pptruser /home/pptruser \ + && chown -R pptruser:pptruser /app + +USER pptruser + WORKDIR /app -COPY package.json package-lock.json /app/ +COPY --chown=pptruser package.json package-lock.json /app/ RUN npm install -COPY index.js server.js prom.js *.md /app/ +COPY --chown=pptruser index.js server.js prom.js *.md /app/ -ENTRYPOINT ["/usr/local/bin/node", "server.js"] +ENTRYPOINT ["/usr/bin/node", "server.js"] EXPOSE 3000 + +HEALTHCHECK --interval=1m --timeout=5s \ + CMD wget -q http://localhost:3000/ || exit 1 diff --git a/README.md b/README.md index ad95247..acc1cb5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # prometheus-act-exporter -![Docker Image Version (latest semver)](https://img.shields.io/docker/v/captn3m0/prometheus-act-exporter) ![Docker Image Size (latest semver)](https://img.shields.io/docker/image-size/captn3m0/prometheus-act-exporter) [![npm version](https://badge.fury.io/js/prometheus-act-exporter.svg)](https://badge.fury.io/js/prometheus-act-exporter) [![License: WTFPL](https://img.shields.io/badge/License-WTFPL-blue.svg)](http://www.wtfpl.net/) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) +![Docker Image Version (latest semver)](https://img.shields.io/docker/v/captn3m0/prometheus-act-exporter) ![Docker Image Size (latest semver)](https://img.shields.io/docker/image-size/captn3m0/prometheus-act-exporter) [![npm version](https://badge.fury.io/js/prometheus-act-exporter.svg)](https://badge.fury.io/js/prometheus-act-exporter) [![License: WTFPL](https://img.shields.io/badge/License-WTFPL-blue.svg)](http://www.wtfpl.net/) [![CI](https://github.com/captn3m0/prometheus-act-exporter/actions/workflows/ci.yml/badge.svg)](https://github.com/captn3m0/prometheus-act-exporter/actions/workflows/ci.yml) Exposes your current ACT FUP usage as prometheus metrics. Scrapes the data from the ACT Portal website by using [puppeteer](https://developers.google.com/web/tools/puppeteer/). This only supports [ACT Fibernet](https://www.actcorp.in/) in India. @@ -42,17 +42,7 @@ act_fup_aggregate_total_bytes 900000000 Install it with `npm i prometheus-act-exporter`. -```js -const act = require("prometheus-act-exporter"); -let m = await act.getUsage(); -// Returns -// { -// live: { usedBytes: 0, totalBytes: 800000000 }, -// flexibytes: { usedBytes: 102580000, totalBytes: 100000000 }, -// aggregate: { usedBytes: 102580000, totalBytes: 900000000 } -// } -// calculations made assuming ACT is using SI GB (exactly 1 billion bytes) -``` +See `test.js` for sample usage. # Configuration @@ -68,20 +58,14 @@ You can pass the following environment variables: If running via Docker, here are some simple cookbook configurations: -`docker run -it -p 3000:3000 -e captn3m0/prometheus-act-exporter` - -Run a simple test server locally in debug mode and test it on `http://localhost:3000/metrics` - -## Node - -```sh -export DISABLE_HEADLESS=1 -# Change to the correct invocation -export CHROME_BIN=$(which chromium) -npm install -node server.js -curl localhost:3000/metrics ``` +wget https://github.com/tkp1n/chromium-ci/raw/41510dc154c4184f7e09461ba76f86f61c460070/seccomp/chromium.json +docker run --security-opt seccomp=chromium.json -it -p 3000:3000 -e captn3m0/prometheus-act-exporter +``` + +Note that the above uses a [minimal secure seccomp profile for running Chromium inside Docker](https://github.com/docker/for-linux/issues/496#issuecomment-441149510). + +**Warning**: You should [not be running](https://ndportmann.com/chrome-in-docker/) this using `--privileged` or `--cap-add=SYS_ADMIN`, or `--no-sandbox`. # LICENSE diff --git a/index.js b/index.js index 0e9be64..70bf392 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ const puppeteer = require("puppeteer-core"); -const containerized = require("containerized"); const MY_PACKAGE_SELECTOR_ID = 'table[style="margin-top:-10px;"] tr:first-child+tr'; @@ -82,10 +81,7 @@ async function getUsage() { } function chromeLaunchConfig() { - let defaultArgs = []; - if (containerized()) { - defaultArgs = ["--no-sandbox", "--disable-setuid-sandbox"]; - } + let defaultArgs = ['--disable-dev-shm-usage']; var options = { // These are set for Docker usage // https://github.com/alekzonder/docker-puppeteer#before-usage diff --git a/package-lock.json b/package-lock.json index 4f91ad3..b7fa717 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "3.1.2", "license": "WTFPL", "dependencies": { - "containerized": "^1.0.2", "prom-client": "^14.0.1", "puppeteer-core": "^15.1.1" } @@ -129,11 +128,6 @@ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, - "node_modules/containerized": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/containerized/-/containerized-1.0.2.tgz", - "integrity": "sha512-iSeP1Le1eRhwYIeBYHBw4rB9lYIwR1f9PsPweoItSDaB7B/6aLKgeU9Fomio/53nSni6YdSrGBjE+xwqrbzM3w==" - }, "node_modules/cross-fetch": { "version": "3.1.5", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz", @@ -720,11 +714,6 @@ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, - "containerized": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/containerized/-/containerized-1.0.2.tgz", - "integrity": "sha512-iSeP1Le1eRhwYIeBYHBw4rB9lYIwR1f9PsPweoItSDaB7B/6aLKgeU9Fomio/53nSni6YdSrGBjE+xwqrbzM3w==" - }, "cross-fetch": { "version": "3.1.5", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz", diff --git a/package.json b/package.json index 224ca44..bffa351 100644 --- a/package.json +++ b/package.json @@ -14,15 +14,14 @@ ], "repository": { "type": "git", - "url": "git+ssh://git@git.captnemo.in/nemo/prometheus-act-exporter.git" + "url": "git+ssh://github.com/captn3m0/prometheus-act-exporter.git" }, "bugs": { - "url": "https://git.captnemo.in/nemo/prometheus-act-exporter/issues" + "url": "https://github.com/captn3m0/prometheus-act-exporter/issues" }, "author": "Nemo ", "license": "WTFPL", "dependencies": { - "containerized": "^1.0.2", "prom-client": "^14.0.1", "puppeteer-core": "^15.1.1" } diff --git a/server.js b/server.js index 39e8535..33c7659 100644 --- a/server.js +++ b/server.js @@ -3,7 +3,7 @@ const port = 3000; const metrics = require("./index"); const promFormatter = require("./prom"); -CACHE = {}; +CACHE = null; const requestHandler = async (req, res) => { switch (req.url) {