From 33677abbe2de20d055afe62db6f1b8172e200fbb Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 18 Apr 2019 22:24:04 +0200 Subject: [PATCH] :hammer: Refactor of init scripts --- .../{50-compiled-symlink.sh => appdaemon.sh} | 0 .../etc/cont-init.d/20-init-configuration.sh | 9 -- .../rootfs/etc/cont-init.d/21-compiled-dir.sh | 31 ------ .../rootfs/etc/cont-init.d/30-auto-token.sh | 28 ------ appdaemon/rootfs/etc/cont-init.d/31-ha-url.sh | 13 --- .../etc/cont-init.d/50-compiled-symlink.sh | 12 --- .../etc/cont-init.d/80-system-packages.sh | 14 --- .../etc/cont-init.d/81-python-packages.sh | 11 --- appdaemon/rootfs/etc/cont-init.d/appdaemon.sh | 96 +++++++++++++++++++ 9 files changed, 96 insertions(+), 118 deletions(-) rename appdaemon/rootfs/etc/cont-finish.d/{50-compiled-symlink.sh => appdaemon.sh} (100%) delete mode 100644 appdaemon/rootfs/etc/cont-init.d/20-init-configuration.sh delete mode 100644 appdaemon/rootfs/etc/cont-init.d/21-compiled-dir.sh delete mode 100644 appdaemon/rootfs/etc/cont-init.d/30-auto-token.sh delete mode 100644 appdaemon/rootfs/etc/cont-init.d/31-ha-url.sh delete mode 100644 appdaemon/rootfs/etc/cont-init.d/50-compiled-symlink.sh delete mode 100644 appdaemon/rootfs/etc/cont-init.d/80-system-packages.sh delete mode 100644 appdaemon/rootfs/etc/cont-init.d/81-python-packages.sh create mode 100644 appdaemon/rootfs/etc/cont-init.d/appdaemon.sh diff --git a/appdaemon/rootfs/etc/cont-finish.d/50-compiled-symlink.sh b/appdaemon/rootfs/etc/cont-finish.d/appdaemon.sh similarity index 100% rename from appdaemon/rootfs/etc/cont-finish.d/50-compiled-symlink.sh rename to appdaemon/rootfs/etc/cont-finish.d/appdaemon.sh diff --git a/appdaemon/rootfs/etc/cont-init.d/20-init-configuration.sh b/appdaemon/rootfs/etc/cont-init.d/20-init-configuration.sh deleted file mode 100644 index 1cd9021..0000000 --- a/appdaemon/rootfs/etc/cont-init.d/20-init-configuration.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: AppDaemon -# Creates initial AppDaemon configuration in case it is non-existing -# ============================================================================== -if ! bashio::fs.directory_exists '/config/appdaemon'; then - cp -R /root/appdaemon /config/appdaemon \ - || bashio::exit.nok 'Failed to create initial AppDaemon configuration' -fi diff --git a/appdaemon/rootfs/etc/cont-init.d/21-compiled-dir.sh b/appdaemon/rootfs/etc/cont-init.d/21-compiled-dir.sh deleted file mode 100644 index 5600686..0000000 --- a/appdaemon/rootfs/etc/cont-init.d/21-compiled-dir.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: AppDaemon -# Ensures directories for pre-compiled dashboard scripts exists -# ============================================================================== -if bashio::fs.directory_exists '/data/compiled'; then - - # Deleted compiled cache, in case we don't know the version - if ! bashio::fs.file_exists '/data/version'; then - bashio::log.info "Detected version upgrade, clearing compiled cache..." - rm -fr /data/compiled - fi - - # Given version does not match - if bashio::fs.file_exists '/data/version' \ - && [[ "$( /data/version -fi diff --git a/appdaemon/rootfs/etc/cont-init.d/30-auto-token.sh b/appdaemon/rootfs/etc/cont-init.d/30-auto-token.sh deleted file mode 100644 index 8fd2c23..0000000 --- a/appdaemon/rootfs/etc/cont-init.d/30-auto-token.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: AppDaemon -# Ensure the correct API key is in the AppDaemon is used -# ============================================================================== -readonly CONFIG_FILE="/config/appdaemon/appdaemon.yaml" - -# Do not run when auto token has been disabled -if bashio::config.true 'disable_auto_token'; then - bashio::log.info 'Automatic update of Home Assistant token is disabled.' - exit 0 -fi - -# Ensure older key is deleted -yq delete --inplace "${CONFIG_FILE}" 'appdaemon.plugins.HASS.ha_key' - -# Add token -if [[ "$(yq read ${CONFIG_FILE} 'appdaemon.plugins.HASS.ha_url')" = "http://hassio/homeassistant" - && "$(yq read ${CONFIG_FILE} 'appdaemon.plugins.HASS.token')" != "${HASSIO_TOKEN}" - && "$(yq read ${CONFIG_FILE} 'appdaemon.plugins.HASS.token')" != '!secret '* ]]; -then - bashio::log.info \ - 'Updating Hass.io API token in AppDaemon with the current one...' - - yq write --inplace "${CONFIG_FILE}" \ - 'appdaemon.plugins.HASS.token' "${HASSIO_TOKEN}" \ - || bashio::exit.nok 'Failed to set Hass.io API token into the AppDaemon config' -fi diff --git a/appdaemon/rootfs/etc/cont-init.d/31-ha-url.sh b/appdaemon/rootfs/etc/cont-init.d/31-ha-url.sh deleted file mode 100644 index 7c45d3e..0000000 --- a/appdaemon/rootfs/etc/cont-init.d/31-ha-url.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: AppDaemon -# Checks the currently used HA URL and warns if the Hass.io proxy isn't used -# ============================================================================== -readonly CONFIG_FILE="/config/appdaemon/appdaemon.yaml" -readonly HA_URL=$(yq read "${CONFIG_FILE}" 'appdaemon.plugins.HASS.ha_url') - -if [[ "${HA_URL}" != "http://hassio/homeassistant" ]]; then - bashio::log.warning 'You are using an non-recommended Home Assistant URL!' - bashio::log.warning 'Setting the "ha_url" option in your AppDaemon config to' - bashio::log.warning '"http://hassio/homeassistant" is recommended!' -fi diff --git a/appdaemon/rootfs/etc/cont-init.d/50-compiled-symlink.sh b/appdaemon/rootfs/etc/cont-init.d/50-compiled-symlink.sh deleted file mode 100644 index f8bce2b..0000000 --- a/appdaemon/rootfs/etc/cont-init.d/50-compiled-symlink.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: AppDaemon -# Symlinks the compiled directory into the users AppDaemon directory -# ============================================================================== -if bashio::fs.directory_exists '/config/appdaemon/compiled'; then - rm -f -r /config/appdaemon/compiled \ - || bashio::exit.nok 'Failed to remove old compiled symlink' -fi - -ln -s /data/compiled /config/appdaemon/compiled \ - || bashio::exit.nok 'Failed to symlink to compiled directory' diff --git a/appdaemon/rootfs/etc/cont-init.d/80-system-packages.sh b/appdaemon/rootfs/etc/cont-init.d/80-system-packages.sh deleted file mode 100644 index f8d44b5..0000000 --- a/appdaemon/rootfs/etc/cont-init.d/80-system-packages.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: AppDaemon -# Install user configured/requested packages -# ============================================================================== -if bashio::config.has_value 'system_packages'; then - apk update \ - || bashio::exit.nok 'Failed updating Alpine packages repository indexes' - - for package in $(bashio::config 'system_packages'); do - apk add "$package" \ - || bashio::exit.nok "Failed installing package ${package}" - done -fi diff --git a/appdaemon/rootfs/etc/cont-init.d/81-python-packages.sh b/appdaemon/rootfs/etc/cont-init.d/81-python-packages.sh deleted file mode 100644 index 5cc1e6b..0000000 --- a/appdaemon/rootfs/etc/cont-init.d/81-python-packages.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: AppDaemon -# Install user configured/requested Python packages -# ============================================================================== -if bashio::config.has_value 'python_packages'; then - for package in $(bashio::config 'python_packages'); do - pip3 install "$package" \ - || bashio::exit.nok "Failed installing package ${package}" - done -fi diff --git a/appdaemon/rootfs/etc/cont-init.d/appdaemon.sh b/appdaemon/rootfs/etc/cont-init.d/appdaemon.sh new file mode 100644 index 0000000..b63162a --- /dev/null +++ b/appdaemon/rootfs/etc/cont-init.d/appdaemon.sh @@ -0,0 +1,96 @@ +#!/usr/bin/with-contenv bashio +# ============================================================================== +# Community Hass.io Add-ons: AppDaemon +# Configures AppDaemon +# ============================================================================== +readonly CONFIG_FILE="/config/appdaemon/appdaemon.yaml" +declare ha_url + +# Creates initial AppDaemon configuration in case it is non-existing +if ! bashio::fs.directory_exists '/config/appdaemon'; then + cp -R /root/appdaemon /config/appdaemon \ + || bashio::exit.nok 'Failed to create initial AppDaemon configuration' +fi + +# Ensures directories for pre-compiled dashboard scripts exists +if bashio::fs.directory_exists '/data/compiled'; then + + # Deleted compiled cache, in case we don't know the version + if ! bashio::fs.file_exists '/data/version'; then + bashio::log.info "Detected version upgrade, clearing compiled cache..." + rm -fr /data/compiled + fi + + # Given version does not match + if bashio::fs.file_exists '/data/version' \ + && [[ "$( /data/version +fi + +# Ensure the correct API key is in the AppDaemon is used +if bashio::config.false 'disable_auto_token'; then + # Ensure older key is deleted + yq delete --inplace "${CONFIG_FILE}" 'appdaemon.plugins.HASS.ha_key' + + # Add token + if [[ "$(yq read ${CONFIG_FILE} 'appdaemon.plugins.HASS.ha_url')" = "http://hassio/homeassistant" + && "$(yq read ${CONFIG_FILE} 'appdaemon.plugins.HASS.token')" != "${HASSIO_TOKEN}" + && "$(yq read ${CONFIG_FILE} 'appdaemon.plugins.HASS.token')" != '!secret '* ]]; + then + bashio::log.info \ + 'Updating Hass.io API token in AppDaemon with the current one...' + + yq write --inplace "${CONFIG_FILE}" \ + 'appdaemon.plugins.HASS.token' "${HASSIO_TOKEN}" \ + || bashio::exit.nok 'Failed to set Hass.io API token into the AppDaemon config' + fi +fi + +# Checks the currently used HA URL and warns if the Hass.io proxy isn't used +ha_url=$(yq read "${CONFIG_FILE}" 'appdaemon.plugins.HASS.ha_url') +if [[ "${ha_url}" != "http://hassio/homeassistant" ]]; then + bashio::log.warning 'You are using an non-recommended Home Assistant URL!' + bashio::log.warning 'Setting the "ha_url" option in your AppDaemon config to' + bashio::log.warning '"http://hassio/homeassistant" is recommended!' +fi + +# Symlinks the compiled directory into the users AppDaemon directory +if bashio::fs.directory_exists '/config/appdaemon/compiled'; then + rm -f -r /config/appdaemon/compiled \ + || bashio::exit.nok 'Failed to remove old compiled symlink' +fi + +ln -s /data/compiled /config/appdaemon/compiled \ + || bashio::exit.nok 'Failed to symlink to compiled directory' + +# Install user configured/requested packages +if bashio::config.has_value 'system_packages'; then + apk update \ + || bashio::exit.nok 'Failed updating Alpine packages repository indexes' + + for package in $(bashio::config 'system_packages'); do + apk add "$package" \ + || bashio::exit.nok "Failed installing package ${package}" + done +fi + +# Install user configured/requested Python packages +if bashio::config.has_value 'python_packages'; then + for package in $(bashio::config 'python_packages'); do + pip3 install "$package" \ + || bashio::exit.nok "Failed installing package ${package}" + done +fi