From 51588498719be7ec4be4238a2795e9ad7a2b4734 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sun, 26 Jul 2020 20:47:36 +0200 Subject: [PATCH] Bunch of tweaks (#1) * :ambulance: Fix GitLab CI to have the right base images * :pencil2: Docs cleanup * :shirt: Dockerfile tweaks * :fire: Remove unused variable from proemetheus init * :shirt: Prettier on JSON & YAML files * :shirt: Other minor styling * :fire: Removal of disabled shellcheck rule * :shirt: Fix shellcheck linter warning * :pencil2: Small tweak to Python code * :hammer: Reduce resulting image with 68Mb --- .gitlab-ci.yml | 6 ++--- README.md | 2 -- prometheus/DOCS.md | 12 ++++----- prometheus/Dockerfile | 26 ++++++++++++++----- prometheus/config.json | 6 ++--- .../rootfs/etc/cont-init.d/prometheus.sh | 3 --- .../rootfs/etc/prometheus/prometheus.yml | 10 +++---- .../etc/services.d/prometheus-configgen/run | 5 ++-- .../rootfs/opt/prometheus-configgen/combiner | 3 +-- .../prometheus-configgen/prometheus.template | 2 +- .../opt/prometheus-configgen/requirements.txt | 7 +++-- 11 files changed, 43 insertions(+), 39 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3a90474..780b407 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,6 +9,6 @@ variables: ADDON_ARMHF: "false" ADDON_I386: "false" - ADDON_AARCH64_BASE: "hassioaddons/base-aarch64:3.2.1" - ADDON_AMD64_BASE: "hassioaddons/base-amd64:3.2.1" - ADDON_ARMV7_BASE: "hassioaddons/base-armv7:3.2.1" + ADDON_AARCH64_BASE: "hassioaddons/base-aarch64:8.0.1" + ADDON_AMD64_BASE: "hassioaddons/base-amd64:8.0.1" + ADDON_ARMV7_BASE: "hassioaddons/base-armv7:8.0.1" diff --git a/README.md b/README.md index 862af93..688e180 100644 --- a/README.md +++ b/README.md @@ -104,8 +104,6 @@ SOFTWARE. [forum-shield]: https://img.shields.io/badge/community-forum-brightgreen.svg [forum]: https://example.net [mjrider]: https://github.com/mjrider -[github-sponsors-shield]: https://frenck.dev/wp-content/uploads/2019/12/github_sponsor.png -[github-sponsors]: https://github.com/sponsors/frenck [gitlabci-shield]: https://gitlab.com/hassio-addons/addon-prometheus/badges/master/pipeline.svg [gitlabci]: https://gitlab.com/hassio-addons/addon-prometheus/pipelines [i386-shield]: https://img.shields.io/badge/i386-no-red.svg diff --git a/prometheus/DOCS.md b/prometheus/DOCS.md index a240f9e..b229cf2 100644 --- a/prometheus/DOCS.md +++ b/prometheus/DOCS.md @@ -36,17 +36,17 @@ static_configs: **Note**: _This is just an example, don't copy and paste it! Create your own!_ - The job names `home-assistant` and `prometheus` are already defined by default. -Rules can be created under /share/prometheus/rules/ +Rules can be created under `/share/prometheus/rules/` -The addon will reload the configuration if a valid configuration is available. If not it will log errors in the addon log +The addon will reload the configuration if a valid configuration is available. +If not it will log errors in the addon log ## Known issues and limitations -* Job name must be unique, but this has to be enforced by the user. -* no alert manager yet +- Job name must be unique, but this has to be enforced by the user. +- no alert manager yet ## Changelog & Releases @@ -88,7 +88,7 @@ check [the contributor's page][contributors]. MIT License -Copyright (c) 2018-2020 Franck Nijhof +Copyright (c) 2020-2020 Robbert Müller Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/prometheus/Dockerfile b/prometheus/Dockerfile index 0b44073..aca893e 100755 --- a/prometheus/Dockerfile +++ b/prometheus/Dockerfile @@ -16,12 +16,19 @@ RUN \ ARCH="${BUILD_ARCH}" \ && if [ "${BUILD_ARCH}" = "aarch64" ]; then ARCH="arm64"; fi \ \ - && apk update \ - && apk --no-cache add python3 py3-idna py3-certifi py3-chardet py3-yaml py3-urllib3 py3-requests \ - && apk --no-cache add --virtual builddeps py-pip \ + && apk --no-cache add \ + python3 \ + py3-idna \ + py3-certifi \ + py3-chardet \ + py3-yaml \ + py3-urllib3 \ + py3-requests \ + && apk --no-cache add --virtual .builddeps \ + py-pip \ \ && curl -J -L -o /tmp/prometheus.tar.gz \ - https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-${ARCH}.tar.gz \ + "https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-${ARCH}.tar.gz" \ && adduser -s /bin/false -D -H prometheus \ && cd /tmp \ && tar -xvf /tmp/prometheus.tar.gz \ @@ -32,8 +39,15 @@ RUN \ && cp -R prometheus-${PROMETHEUS_VERSION}.linux-${ARCH}/consoles/ /etc/prometheus/ \ && rm -r prometheus-${PROMETHEUS_VERSION}.linux-${ARCH} \ && chown -R prometheus:prometheus /etc/prometheus \ - && pip3 install -r /opt/prometheus-configgen/requirements.txt \ - && apk del builddeps + && pip3 install \ + --no-cache-dir \ + --prefer-binary \ + --find-links "https://wheels.home-assistant.io/alpine-3.12/${BUILD_ARCH}/" \ + -r /opt/prometheus-configgen/requirements.txt \ + \ + && apk --no-cache del .builddeps \ + && rm -f -r \ + /tmp/* # Build arguments ARG BUILD_DATE diff --git a/prometheus/config.json b/prometheus/config.json index 7d36c3a..230b989 100755 --- a/prometheus/config.json +++ b/prometheus/config.json @@ -16,14 +16,12 @@ "homeassistant_api": true, "hassio_role": "default", "map": ["share:rw"], - "options": { - }, + "options": {}, "ports": { "9090/tcp": null }, "ports_description": { "9090/tcp": "Not required for Ingress" }, - "schema": { - } + "schema": {} } diff --git a/prometheus/rootfs/etc/cont-init.d/prometheus.sh b/prometheus/rootfs/etc/cont-init.d/prometheus.sh index 227a639..447c78e 100644 --- a/prometheus/rootfs/etc/cont-init.d/prometheus.sh +++ b/prometheus/rootfs/etc/cont-init.d/prometheus.sh @@ -3,7 +3,4 @@ # Home Assistant Community Add-on: Prometheus # Configures Prometheus # ============================================================================== -readonly CONFIG="/etc/prometheus/prometheus.yml" - echo "${HASSIO_TOKEN}" > '/run/home-assistant.token' - diff --git a/prometheus/rootfs/etc/prometheus/prometheus.yml b/prometheus/rootfs/etc/prometheus/prometheus.yml index 8b6d1d1..9d8e327 100644 --- a/prometheus/rootfs/etc/prometheus/prometheus.yml +++ b/prometheus/rootfs/etc/prometheus/prometheus.yml @@ -21,20 +21,20 @@ rule_files: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job=` to any timeseries scraped from this config. - - job_name: 'prometheus' + - job_name: "prometheus" # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - - targets: ['localhost:9090'] - - job_name: 'home-assistant' + - targets: ["localhost:9090"] + - job_name: "home-assistant" scrape_interval: 60s metrics_path: /core/api/prometheus # Long-Lived Access Token - bearer_token_file: '/run/home-assistant.token' + bearer_token_file: "/run/home-assistant.token" scheme: http static_configs: - - targets: ['supervisor:80'] + - targets: ["supervisor:80"] diff --git a/prometheus/rootfs/etc/services.d/prometheus-configgen/run b/prometheus/rootfs/etc/services.d/prometheus-configgen/run index 2de1b0b..2016867 100755 --- a/prometheus/rootfs/etc/services.d/prometheus-configgen/run +++ b/prometheus/rootfs/etc/services.d/prometheus-configgen/run @@ -1,6 +1,4 @@ #!/usr/bin/with-contenv bashio -# shellcheck disable=SC2191 - bashio::log.info 'Starting prometheus config generator...' if [[ ! -d /share/promethus/targets ]] ; then @@ -8,7 +6,8 @@ if [[ ! -d /share/promethus/targets ]] ; then chown -R prometheus:prometheus /share/prometheus/targets fi -cd /opt/prometheus-configgen +cd /opt/prometheus-configgen || bashio::exit.nok \ + "Could not change working directory for prometheus-configgen." # Run Prometheus exec s6-setuidgid prometheus python3 combiner diff --git a/prometheus/rootfs/opt/prometheus-configgen/combiner b/prometheus/rootfs/opt/prometheus-configgen/combiner index 94c69f4..ccc9e3a 100644 --- a/prometheus/rootfs/opt/prometheus-configgen/combiner +++ b/prometheus/rootfs/opt/prometheus-configgen/combiner @@ -39,7 +39,7 @@ def testConfig(config): except: print("Failed to validate") raise - if result == False: + if not result: raise Exception("validation error") return result @@ -51,7 +51,6 @@ def writeConfig(config, file): r = requests.post(url="http://localhost:9090/-/reload", data={}) except: print("Exception") - pass loop = asyncio.get_event_loop() diff --git a/prometheus/rootfs/opt/prometheus-configgen/prometheus.template b/prometheus/rootfs/opt/prometheus-configgen/prometheus.template index c9aa2b2..7589935 100644 --- a/prometheus/rootfs/opt/prometheus-configgen/prometheus.template +++ b/prometheus/rootfs/opt/prometheus-configgen/prometheus.template @@ -35,4 +35,4 @@ rule_files: # scheme defaults to 'http'. static_configs: - - targets: ['localhost:9090'] + - targets: ['localhost:9090'] diff --git a/prometheus/rootfs/opt/prometheus-configgen/requirements.txt b/prometheus/rootfs/opt/prometheus-configgen/requirements.txt index 15b5dd2..ea01d9e 100644 --- a/prometheus/rootfs/opt/prometheus-configgen/requirements.txt +++ b/prometheus/rootfs/opt/prometheus-configgen/requirements.txt @@ -1,5 +1,4 @@ -PyYAML>=5.3.1 -pyyaml-include>=1.2 -requests>=2.23.0 aionotify - +pyyaml-include>=1.2 +PyYAML>=5.3.1 +requests>=2.23.0