From 836fa1fbdf253fd489bcda288a2e8457d219942f Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 1 Apr 2019 12:35:17 +0200 Subject: [PATCH] :hammer: Rewrite add-on onto Bashio --- .../rootfs/etc/cont-init.d/10-requirements.sh | 25 ++------------ glances/rootfs/etc/cont-init.d/11-docker.sh | 33 ------------------- glances/rootfs/etc/cont-init.d/20-config.sh | 11 +++---- glances/rootfs/etc/cont-init.d/30-influxdb.sh | 19 +++++------ glances/rootfs/etc/cont-init.d/70-nginx.sh | 11 +++---- glances/rootfs/etc/services.d/glances/run | 13 +++----- glances/rootfs/etc/services.d/influxdb/run | 11 +++---- glances/rootfs/etc/services.d/nginx/run | 11 +++---- 8 files changed, 32 insertions(+), 102 deletions(-) delete mode 100644 glances/rootfs/etc/cont-init.d/11-docker.sh diff --git a/glances/rootfs/etc/cont-init.d/10-requirements.sh b/glances/rootfs/etc/cont-init.d/10-requirements.sh index a5a4a07..0da4996 100644 --- a/glances/rootfs/etc/cont-init.d/10-requirements.sh +++ b/glances/rootfs/etc/cont-init.d/10-requirements.sh @@ -1,26 +1,7 @@ -#!/usr/bin/with-contenv bash +#!/usr/bin/with-contenv bashio # ============================================================================== # Community Hass.io Add-ons: Glances # This files check if all user configuration requirements are met # ============================================================================== -# shellcheck disable=SC1091 -source /usr/lib/hassio-addons/base.sh - -# Check SSL requirements, if enabled -if hass.config.true 'ssl'; then - if ! hass.config.has_value 'certfile'; then - hass.die 'SSL is enabled, but no certfile was specified' - fi - - if ! hass.config.has_value 'keyfile'; then - hass.die 'SSL is enabled, but no keyfile was specified' - fi - - if ! hass.file_exists "/ssl/$(hass.config.get 'certfile')"; then - hass.die 'The configured certfile is not found' - fi - - if ! hass.file_exists "/ssl/$(hass.config.get 'keyfile')"; then - hass.die 'The configured keyfile is not found' - fi -fi +bashio::config.require.ssl +bashio::require.unprotected diff --git a/glances/rootfs/etc/cont-init.d/11-docker.sh b/glances/rootfs/etc/cont-init.d/11-docker.sh deleted file mode 100644 index 394ce5d..0000000 --- a/glances/rootfs/etc/cont-init.d/11-docker.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/with-contenv bash -# ============================================================================== -# Community Hass.io Add-ons: Glances -# Unprotects this add-on in order to gain access to the Docker socket -# ============================================================================== -# shellcheck disable=SC1091 -source /usr/lib/hassio-addons/base.sh - -# Break in case the API is unreachable -if ! hass.api.supervisor.ping; then - hass.die "Could you contact the HassIO API" -fi - -# Docker support is enabled! -if [[ -S "/var/run/docker.sock" ]]; then - hass.log.info 'Docker support has been enabled.' - exit "${EX_OK}" -fi - -hass.log.fatal "PROTECTION MODE ENABLED!" -hass.log.fatal "" -hass.log.fatal "To be able to use this add-on, you'll need to disable" -hass.log.fatal "protection mode on this add-on. Without it, the add-on" -hass.log.fatal "is unable to access Docker." -hass.log.fatal "" -hass.log.fatal "Steps:" -hass.log.fatal " - Go to the Hass.io Panel." -hass.log.fatal " - Click on the 'Glances' add-on." -hass.log.fatal " - Set the 'Protection mode' switch to off." -hass.log.fatal " - Restart the add-on." -hass.log.fatal "" - -exit "${EX_NOK}" diff --git a/glances/rootfs/etc/cont-init.d/20-config.sh b/glances/rootfs/etc/cont-init.d/20-config.sh index f66525c..0796965 100644 --- a/glances/rootfs/etc/cont-init.d/20-config.sh +++ b/glances/rootfs/etc/cont-init.d/20-config.sh @@ -1,19 +1,16 @@ -#!/usr/bin/with-contenv bash +#!/usr/bin/with-contenv bashio # ============================================================================== # Community Hass.io Add-ons: Glances # Ensure the configuration exists # ============================================================================== -# shellcheck disable=SC1091 -source /usr/lib/hassio-addons/base.sh - -if hass.file_exists '/config/glances/glances.conf'; then +if bashio::fs.file_exists '/config/glances/glances.conf'; then cp -f /config/glances/glances.conf /etc/glances.conf - exit "${EX_OK}" + exit 0 fi # Ensure configuration exists mkdir -p /config/glances \ - || hass.die "Failed to create the Glances configuration directory" + || bashio::exit.nok "Failed to create the Glances configuration directory" # Copy in template file cp /etc/glances.conf /config/glances/ diff --git a/glances/rootfs/etc/cont-init.d/30-influxdb.sh b/glances/rootfs/etc/cont-init.d/30-influxdb.sh index 471f496..f7114b6 100644 --- a/glances/rootfs/etc/cont-init.d/30-influxdb.sh +++ b/glances/rootfs/etc/cont-init.d/30-influxdb.sh @@ -1,22 +1,19 @@ -#!/usr/bin/with-contenv bash +#!/usr/bin/with-contenv bashio # ============================================================================== # Community Hass.io Add-ons: Glances # Export Glances data to InfluxDB # ============================================================================== -# shellcheck disable=SC1091 -source /usr/lib/hassio-addons/base.sh - -if hass.config.false 'influxdb.enabled'; then +if bashio::config.false 'influxdb.enabled'; then exit "${EX_OK}" fi # Modify the configuration { echo "[influxdb]" - echo "host=$(hass.config.get 'influxdb.host')" - echo "port=$(hass.config.get 'influxdb.port')" - echo "user=$(hass.config.get 'influxdb.username')" - echo "password=$(hass.config.get 'influxdb.password')" - echo "db=$(hass.config.get 'influxdb.database')" - echo "prefix=$(hass.config.get 'influxdb.prefix')" + echo "host=$(bashio::config 'influxdb.host')" + echo "port=$(bashio::config 'influxdb.port')" + echo "user=$(bashio::config 'influxdb.username')" + echo "password=$(bashio::config 'influxdb.password')" + echo "db=$(bashio::config 'influxdb.database')" + echo "prefix=$(bashio::config 'influxdb.prefix')" } >> /etc/glances.conf diff --git a/glances/rootfs/etc/cont-init.d/70-nginx.sh b/glances/rootfs/etc/cont-init.d/70-nginx.sh index 96b814e..770b62f 100644 --- a/glances/rootfs/etc/cont-init.d/70-nginx.sh +++ b/glances/rootfs/etc/cont-init.d/70-nginx.sh @@ -1,14 +1,11 @@ -#!/usr/bin/with-contenv bash +#!/usr/bin/with-contenv bashio # ============================================================================== # Community Hass.io Add-ons: Glances # Configure the use of SSL in NGINX # ============================================================================== -# shellcheck disable=SC1091 -source /usr/lib/hassio-addons/base.sh - -if hass.config.true 'ssl'; then - certfile=$(hass.config.get 'certfile') - keyfile=$(hass.config.get 'keyfile') +if bashio::config.true 'ssl'; then + certfile=$(bashio::config 'certfile') + keyfile=$(bashio::config 'keyfile') sed -i "s/%%certfile%%/${certfile}/g" /etc/nginx/nginx-ssl.conf sed -i "s/%%keyfile%%/${keyfile}/g" /etc/nginx/nginx-ssl.conf diff --git a/glances/rootfs/etc/services.d/glances/run b/glances/rootfs/etc/services.d/glances/run index fe233e4..c6906d3 100644 --- a/glances/rootfs/etc/services.d/glances/run +++ b/glances/rootfs/etc/services.d/glances/run @@ -1,26 +1,23 @@ -#!/usr/bin/with-contenv bash +#!/usr/bin/with-contenv bashio # ============================================================================== # Community Hass.io Add-ons: Glances # Runs Glances # ============================================================================== -# shellcheck disable=SC1091 -source /usr/lib/hassio-addons/base.sh - declare -a options -hass.log.info 'Starting Glances' +bashio::log.info 'Starting Glances...' options+=(-C /etc/glances.conf) options+=(-w) options+=(--port 61209) options+=(--bind 127.0.0.1) -options+=(--time "$(hass.config.get 'refresh_time')") +options+=(--time "$(bashio::config 'refresh_time')") -if hass.config.false 'process_info'; then +if bashio::config.false 'process_info'; then options+=(--disable-process) fi -if hass.debug; then +if bashio::debug; then options+=(--debug) fi diff --git a/glances/rootfs/etc/services.d/influxdb/run b/glances/rootfs/etc/services.d/influxdb/run index d0cfd6a..72f3b75 100644 --- a/glances/rootfs/etc/services.d/influxdb/run +++ b/glances/rootfs/etc/services.d/influxdb/run @@ -1,14 +1,11 @@ -#!/usr/bin/with-contenv bash +#!/usr/bin/with-contenv bashio # ============================================================================== # Community Hass.io Add-ons: Glances # Runs Glances InfluxDB Export # ============================================================================== -# shellcheck disable=SC1091 -source /usr/lib/hassio-addons/base.sh - declare -a options -if hass.config.false 'influxdb.enabled'; then +if bashio::config.false 'influxdb.enabled'; then exec sleep 86400 fi @@ -16,12 +13,12 @@ options+=(-C /etc/glances.conf) options+=(--export influxdb) options+=(--quiet) -if hass.debug; then +if bashio::debug; then options+=(--debug) fi # Interval -sleep "$(hass.config.get 'influxdb.interval')" +sleep "$(bashio::config 'influxdb.interval')" # Run Glances exec glances "${options[@]}" diff --git a/glances/rootfs/etc/services.d/nginx/run b/glances/rootfs/etc/services.d/nginx/run index e2a7907..deef0ab 100644 --- a/glances/rootfs/etc/services.d/nginx/run +++ b/glances/rootfs/etc/services.d/nginx/run @@ -1,22 +1,19 @@ -#!/usr/bin/with-contenv bash +#!/usr/bin/with-contenv bashio # ============================================================================== # Community Hass.io Add-ons: Glances # Runs the nginx daemon # ============================================================================== -# shellcheck disable=SC1091 -source /usr/lib/hassio-addons/base.sh - -hass.log.info 'Starting the NGINX daemon' +bashio::log.info 'Starting the NGINX daemon...' # Wait for Glances to become available s6-svwait -u -t 5000 /var/run/s6/services/glances # Disable HA Authentication if front door is open -if hass.config.true 'leave_front_door_open'; then +if bashio::config.true 'leave_front_door_open'; then export DISABLE_HA_AUTHENTICATION=true fi -if hass.config.true 'ssl'; then +if bashio::config.true 'ssl'; then exec nginx -c /etc/nginx/nginx-ssl.conf else exec nginx -c /etc/nginx/nginx.conf