From 73a0bfffe54499760c2c1f4a13a628b9425048c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Bonelle?= Date: Sun, 27 Aug 2023 17:53:19 +0200 Subject: [PATCH] Revert "fix" This reverts commit a7c9ca3f2fd72315ace47d38f2ea763fd6af96ad. --- telegraf/Dockerfile | 5 ++ telegraf/config.yaml | 6 +-- telegraf/entrypoint.sh | 17 +++++++ telegraf/rootfs/etc/conf-init.d/00-banner.sh | 37 +++++++++++++++ .../rootfs/etc/conf-init.d/01-log-level.sh | 46 +++++++++++++++++++ .../rootfs/etc/conf-init.d/02-set-timezone.sh | 11 +++++ .../rootfs/etc/services.d/telegraf/finish | 15 ------ telegraf/rootfs/etc/services.d/telegraf/run | 27 ----------- telegraf/settings.sh | 13 ++++++ 9 files changed, 131 insertions(+), 46 deletions(-) create mode 100755 telegraf/entrypoint.sh create mode 100755 telegraf/rootfs/etc/conf-init.d/00-banner.sh create mode 100755 telegraf/rootfs/etc/conf-init.d/01-log-level.sh create mode 100755 telegraf/rootfs/etc/conf-init.d/02-set-timezone.sh delete mode 100755 telegraf/rootfs/etc/services.d/telegraf/finish delete mode 100755 telegraf/rootfs/etc/services.d/telegraf/run create mode 100755 telegraf/settings.sh diff --git a/telegraf/Dockerfile b/telegraf/Dockerfile index d0371b4..d0c6663 100755 --- a/telegraf/Dockerfile +++ b/telegraf/Dockerfile @@ -44,6 +44,11 @@ RUN set -ex && \ EXPOSE 8125/udp 8092/udp 8094 +COPY entrypoint.sh /entrypoint.sh +COPY settings.sh /settings.sh +ENTRYPOINT ["/entrypoint.sh"] +CMD ["telegraf"] + # Labels LABEL \ io.hass.name="${BUILD_NAME}" \ diff --git a/telegraf/config.yaml b/telegraf/config.yaml index 628ed18..ba3b698 100644 --- a/telegraf/config.yaml +++ b/telegraf/config.yaml @@ -32,10 +32,8 @@ docker_api: true host_pid: true full_access: true options: - configuration: + custom_conf: location: "/config/telegraf.conf" - log_level: info schema: - configuration: + custom_conf: location: str - log_level: list(trace|debug|info|notice|warning|error|fatal)? \ No newline at end of file diff --git a/telegraf/entrypoint.sh b/telegraf/entrypoint.sh new file mode 100755 index 0000000..f2f0b0c --- /dev/null +++ b/telegraf/entrypoint.sh @@ -0,0 +1,17 @@ +#!/bin/bash +set -e + +bashio /settings.sh + +if [ "${1:0:1}" = '-' ]; then + set -- telegraf "$@" +fi + +if [ $EUID -ne 0 ]; then + exec "$@" +else + # Allow telegraf to send ICMP packets and bind to privliged ports + setcap cap_net_raw,cap_net_bind_service+ep /usr/bin/telegraf || echo "Failed to set additional capabilities on /usr/bin/telegraf" + + exec setpriv --reuid root --init-groups "$@" +fi \ No newline at end of file diff --git a/telegraf/rootfs/etc/conf-init.d/00-banner.sh b/telegraf/rootfs/etc/conf-init.d/00-banner.sh new file mode 100755 index 0000000..c031ad8 --- /dev/null +++ b/telegraf/rootfs/etc/conf-init.d/00-banner.sh @@ -0,0 +1,37 @@ +#!/command/with-contenv bashio +# ============================================================================== +# Home Assistant Community Add-on: Base Images +# Displays a simple add-on banner on startup +# ============================================================================== +if bashio::supervisor.ping; then + bashio::log.blue \ + '-----------------------------------------------------------' + bashio::log.blue " Add-on: $(bashio::addon.name)" + bashio::log.blue " $(bashio::addon.description)" + bashio::log.blue \ + '-----------------------------------------------------------' + + bashio::log.blue " Add-on version: $(bashio::addon.version)" + if bashio::var.true "$(bashio::addon.update_available)"; then + bashio::log.magenta ' There is an update available for this add-on!' + bashio::log.magenta \ + " Latest add-on version: $(bashio::addon.version_latest)" + bashio::log.magenta ' Please consider upgrading as soon as possible.' + else + bashio::log.green ' You are running the latest version of this add-on.' + fi + + bashio::log.blue " System: $(bashio::info.operating_system)" \ + " ($(bashio::info.arch) / $(bashio::info.machine))" + bashio::log.blue " Home Assistant Core: $(bashio::info.homeassistant)" + bashio::log.blue " Home Assistant Supervisor: $(bashio::info.supervisor)" + + bashio::log.blue \ + '-----------------------------------------------------------' + bashio::log.blue \ + ' Please, share the above information when looking for help' + bashio::log.blue \ + ' or support in, e.g., GitHub, forums or the Discord chat.' + bashio::log.blue \ + '-----------------------------------------------------------' +fi \ No newline at end of file diff --git a/telegraf/rootfs/etc/conf-init.d/01-log-level.sh b/telegraf/rootfs/etc/conf-init.d/01-log-level.sh new file mode 100755 index 0000000..47b46eb --- /dev/null +++ b/telegraf/rootfs/etc/conf-init.d/01-log-level.sh @@ -0,0 +1,46 @@ +#!/command/with-contenv bashio +# ============================================================================== +# Home Assistant Community Add-on: Base Images +# Sets the log level correctly +# ============================================================================== +declare log_level + +# Check if the log level configuration option exists +if bashio::config.exists log_level; then + + # Find the matching LOG_LEVEL + log_level=$(bashio::string.lower "$(bashio::config log_level)") + case "${log_level}" in + all) + log_level="${__BASHIO_LOG_LEVEL_ALL}" + ;; + trace) + log_level="${__BASHIO_LOG_LEVEL_TRACE}" + ;; + debug) + log_level="${__BASHIO_LOG_LEVEL_DEBUG}" + ;; + info) + log_level="${__BASHIO_LOG_LEVEL_INFO}" + ;; + notice) + log_level="${__BASHIO_LOG_LEVEL_NOTICE}" + ;; + warning) + log_level="${__BASHIO_LOG_LEVEL_WARNING}" + ;; + error) + log_level="${__BASHIO_LOG_LEVEL_ERROR}" + ;; + fatal) + log_level="${__BASHIO_LOG_LEVEL_FATAL}" + ;; + off) + log_level="${__BASHIO_LOG_LEVEL_OFF}" + ;; + *) + bashio::exit.nok "Unknown log_level: ${log_level}" + esac + + bashio::log.blue "Log level is set to ${__BASHIO_LOG_LEVELS[$log_level]}" +fi \ No newline at end of file diff --git a/telegraf/rootfs/etc/conf-init.d/02-set-timezone.sh b/telegraf/rootfs/etc/conf-init.d/02-set-timezone.sh new file mode 100755 index 0000000..35b0336 --- /dev/null +++ b/telegraf/rootfs/etc/conf-init.d/02-set-timezone.sh @@ -0,0 +1,11 @@ +#!/command/with-contenv bashio +# ============================================================================== +# Home Assistant Community Add-on: Base Images +# Configures the timezone +# ============================================================================== +if ! bashio::var.is_empty "${TZ}"; then + bashio::log.info "Configuring timezone" + + ln --symbolic --no-dereference --force "/usr/share/zoneinfo/${TZ}" /etc/localtime + echo "${TZ}" > /etc/timezone +fi \ No newline at end of file diff --git a/telegraf/rootfs/etc/services.d/telegraf/finish b/telegraf/rootfs/etc/services.d/telegraf/finish deleted file mode 100755 index 42da589..0000000 --- a/telegraf/rootfs/etc/services.d/telegraf/finish +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bashio -# ============================================================================== -# Take down the S6 supervision tree when telegraf fails -# s6-overlay docs: https://github.com/just-containers/s6-overlay -# ============================================================================== - -declare APP_EXIT_CODE=${1} - -if [[ "${APP_EXIT_CODE}" -ne 0 ]] && [[ "${APP_EXIT_CODE}" -ne 256 ]]; then - bashio::log.warning "Halt add-on with exit code ${APP_EXIT_CODE}" - echo "${APP_EXIT_CODE}" > /run/s6-linux-init-container-results/exitcode - exec /run/s6/basedir/bin/halt -fi - -bashio::log.info "Service restart after closing" diff --git a/telegraf/rootfs/etc/services.d/telegraf/run b/telegraf/rootfs/etc/services.d/telegraf/run deleted file mode 100755 index 3fd8c30..0000000 --- a/telegraf/rootfs/etc/services.d/telegraf/run +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/with-contenv bashio -# shellcheck shell=bash -# ============================================================================== -# Home Assistant Add-on: telegraf -# Runs telegraf -# ============================================================================== -declare log_level -bashio::log.info 'Starting telegraf...' -bashio::require.unprotected - -CONFIGURATION=$(bashio::config 'configuration.location') -bashio::log.info "Using configuration file ${CONFIGURATION}" -rm /etc/telegraf/telegraf.conf -cp "${CONFIGURATION}" /etc/telegraf/telegraf.conf - -if [ "${1:0:1}" = '-' ]; then - set -- telegraf "$@" -fi - -if [ $EUID -ne 0 ]; then - exec "$@" -else - # Allow telegraf to send ICMP packets and bind to privliged ports - setcap cap_net_raw,cap_net_bind_service+ep /usr/bin/telegraf || echo "Failed to set additional capabilities on /usr/bin/telegraf" - - exec setpriv --reuid root --init-groups "$@" -fi \ No newline at end of file diff --git a/telegraf/settings.sh b/telegraf/settings.sh new file mode 100755 index 0000000..c1c28a9 --- /dev/null +++ b/telegraf/settings.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bashio +declare hostname +bashio::require.unprotected + +readonly CONFIG="/etc/telegraf/telegraf.conf" + +CUSTOM_CONF=$(bashio::config 'custom_conf.location') + +bashio::log.info "Using custom conf file" +rm /etc/telegraf/telegraf.conf +cp "${CUSTOM_CONF}" /etc/telegraf/telegraf.conf + +bashio::log.info "Finished updating config, Starting Telegraf" \ No newline at end of file