diff --git a/docker/ha-addon-rootfs/etc/cont-init.d/10-requirements.sh b/docker/ha-addon-rootfs/etc/cont-init.d/10-requirements.sh deleted file mode 100755 index 544787d568..0000000000 --- a/docker/ha-addon-rootfs/etc/cont-init.d/10-requirements.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: ESPHome -# This files check if all user configuration requirements are met -# ============================================================================== - -# Check SSL requirements, if enabled -if bashio::config.true 'ssl'; then - if ! bashio::config.has_value 'certfile'; then - bashio::log.fatal 'SSL is enabled, but no certfile was specified.' - bashio::exit.nok - fi - - if ! bashio::config.has_value 'keyfile'; then - bashio::log.fatal 'SSL is enabled, but no keyfile was specified' - bashio::exit.nok - fi - - - certfile="/ssl/$(bashio::config 'certfile')" - keyfile="/ssl/$(bashio::config 'keyfile')" - - if ! bashio::fs.file_exists "${certfile}"; then - if ! bashio::fs.file_exists "${keyfile}"; then - # Both files are missing, let's print a friendlier error message - bashio::log.fatal 'You enabled encrypted connections using the "ssl": true option.' - bashio::log.fatal "However, the SSL files '${certfile}' and '${keyfile}'" - bashio::log.fatal "were not found. If you're using Hass.io on your local network and don't want" - bashio::log.fatal 'to encrypt connections to the ESPHome dashboard, you can manually disable' - bashio::log.fatal 'SSL by setting "ssl" to false."' - bashio::exit.nok - fi - bashio::log.fatal "The configured certfile '${certfile}' was not found." - bashio::exit.nok - fi - - if ! bashio::fs.file_exists "/ssl/$(bashio::config 'keyfile')"; then - bashio::log.fatal "The configured keyfile '${keyfile}' was not found." - bashio::exit.nok - fi -fi diff --git a/docker/ha-addon-rootfs/etc/cont-init.d/20-nginx.sh b/docker/ha-addon-rootfs/etc/cont-init.d/20-nginx.sh deleted file mode 100755 index 107a25c47a..0000000000 --- a/docker/ha-addon-rootfs/etc/cont-init.d/20-nginx.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: ESPHome -# Configures NGINX for use with ESPHome -# ============================================================================== - -declare certfile -declare keyfile -declare direct_port -declare ingress_interface -declare ingress_port - -mkdir -p /var/log/nginx - -direct_port=$(bashio::addon.port 6052) -if bashio::var.has_value "${direct_port}"; then - if bashio::config.true 'ssl'; then - certfile=$(bashio::config 'certfile') - keyfile=$(bashio::config 'keyfile') - - mv /etc/nginx/servers/direct-ssl.disabled /etc/nginx/servers/direct.conf - sed -i "s/%%certfile%%/${certfile}/g" /etc/nginx/servers/direct.conf - sed -i "s/%%keyfile%%/${keyfile}/g" /etc/nginx/servers/direct.conf - else - mv /etc/nginx/servers/direct.disabled /etc/nginx/servers/direct.conf - fi - - sed -i "s/%%port%%/${direct_port}/g" /etc/nginx/servers/direct.conf -fi - -ingress_port=$(bashio::addon.ingress_port) -ingress_interface=$(bashio::addon.ip_address) -sed -i "s/%%port%%/${ingress_port}/g" /etc/nginx/servers/ingress.conf -sed -i "s/%%interface%%/${ingress_interface}/g" /etc/nginx/servers/ingress.conf diff --git a/docker/ha-addon-rootfs/etc/cont-init.d/30-dirs.sh b/docker/ha-addon-rootfs/etc/cont-init.d/30-dirs.sh deleted file mode 100755 index 1073a2fa45..0000000000 --- a/docker/ha-addon-rootfs/etc/cont-init.d/30-dirs.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/with-contenv bashio -# ============================================================================== -# Community Hass.io Add-ons: ESPHome -# This files creates all directories used by esphome -# ============================================================================== - -pio_cache_base=/data/cache/platformio - -mkdir -p "${pio_cache_base}" diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/dependencies.d/base b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/dependencies.d/base new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/finish b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/finish new file mode 100755 index 0000000000..6e0f8fe23a --- /dev/null +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/finish @@ -0,0 +1,26 @@ +#!/command/with-contenv bashio +# shellcheck shell=bash +# ============================================================================== +# Home Assistant Community Add-on: ESPHome +# Take down the S6 supervision tree when ESPHome dashboard fails +# ============================================================================== +declare exit_code +readonly exit_code_container=$( /run/s6-linux-init-container-results/exitcode + fi + [[ "${exit_code_signal}" -eq 15 ]] && exec /run/s6/basedir/bin/halt +elif [[ "${exit_code_service}" -ne 0 ]]; then + if [[ "${exit_code_container}" -eq 0 ]]; then + echo "${exit_code_service}" > /run/s6-linux-init-container-results/exitcode + fi + exec /run/s6/basedir/bin/halt +fi diff --git a/docker/ha-addon-rootfs/etc/services.d/esphome/run b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/run similarity index 91% rename from docker/ha-addon-rootfs/etc/services.d/esphome/run rename to docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/run index 747c64728e..277f26ea49 100755 --- a/docker/ha-addon-rootfs/etc/services.d/esphome/run +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/run @@ -1,10 +1,19 @@ -#!/usr/bin/with-contenv bashio +#!/command/with-contenv bashio +# shellcheck shell=bash # ============================================================================== # Community Hass.io Add-ons: ESPHome # Runs the ESPHome dashboard # ============================================================================== +readonly pio_cache_base=/data/cache/platformio export ESPHOME_IS_HA_ADDON=true +export PLATFORMIO_GLOBALLIB_DIR=/piolibs + +# we can't set core_dir, because the settings file is stored in `core_dir/appstate.json` +# setting `core_dir` would therefore prevent pio from accessing +export PLATFORMIO_PLATFORMS_DIR="${pio_cache_base}/platforms" +export PLATFORMIO_PACKAGES_DIR="${pio_cache_base}/packages" +export PLATFORMIO_CACHE_DIR="${pio_cache_base}/cache" if bashio::config.true 'leave_front_door_open'; then export DISABLE_HA_AUTHENTICATION=true @@ -30,14 +39,7 @@ else fi fi -pio_cache_base=/data/cache/platformio -# we can't set core_dir, because the settings file is stored in `core_dir/appstate.json` -# setting `core_dir` would therefore prevent pio from accessing -export PLATFORMIO_PLATFORMS_DIR="${pio_cache_base}/platforms" -export PLATFORMIO_PACKAGES_DIR="${pio_cache_base}/packages" -export PLATFORMIO_CACHE_DIR="${pio_cache_base}/cache" - -export PLATFORMIO_GLOBALLIB_DIR=/piolibs +mkdir -p "${pio_cache_base}" bashio::log.info "Starting ESPHome dashboard..." exec esphome dashboard /config/esphome --socket /var/run/esphome.sock --ha-addon diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/type b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/type new file mode 100644 index 0000000000..5883cff0cd --- /dev/null +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/type @@ -0,0 +1 @@ +longrun diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/dependencies.d/base b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/dependencies.d/base new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/run b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/run new file mode 100755 index 0000000000..52a211b99f --- /dev/null +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/run @@ -0,0 +1,63 @@ +#!/command/with-contenv bashio +# shellcheck shell=bash +# ============================================================================== +# Community Hass.io Add-ons: ESPHome +# Configures NGINX for use with ESPHome +# ============================================================================== +declare certfile +declare direct_port +declare ingress_interface +declare ingress_port +declare keyfile + +mkdir -p /var/log/nginx + +direct_port=$(bashio::addon.port 6052) +if bashio::var.has_value "${direct_port}"; then + # Check SSL requirements, if enabled + if bashio::config.true 'ssl'; then + if ! bashio::config.has_value 'certfile'; then + bashio::log.fatal 'SSL is enabled, but no certfile was specified.' + bashio::exit.nok + fi + + if ! bashio::config.has_value 'keyfile'; then + bashio::log.fatal 'SSL is enabled, but no keyfile was specified' + bashio::exit.nok + fi + + certfile="/ssl/$(bashio::config 'certfile')" + keyfile="/ssl/$(bashio::config 'keyfile')" + + if ! bashio::fs.file_exists "/ssl/${certfile}"; then + if ! bashio::fs.file_exists "/ssl/${keyfile}"; then + # Both files are missing, let's print a friendlier error message + bashio::log.fatal 'You enabled encrypted connections using the "ssl": true option.' + bashio::log.fatal "However, the SSL files '${certfile}' and '${keyfile}'" + bashio::log.fatal "were not found. If you're using Hass.io on your local network and don't want" + bashio::log.fatal 'to encrypt connections to the ESPHome dashboard, you can manually disable' + bashio::log.fatal 'SSL by setting "ssl" to false."' + bashio::exit.nok + fi + bashio::log.fatal "The configured certfile '/ssl/${certfile}' was not found." + bashio::exit.nok + fi + + if ! bashio::fs.file_exists "/ssl/${keyfile}"; then + bashio::log.fatal "The configured keyfile '/ssl/${keyfile}' was not found." + bashio::exit.nok + fi + mv /etc/nginx/servers/direct-ssl.disabled /etc/nginx/servers/direct.conf + sed -i "s/%%certfile%%/${certfile}/g" /etc/nginx/servers/direct.conf + sed -i "s/%%keyfile%%/${keyfile}/g" /etc/nginx/servers/direct.conf + else + mv /etc/nginx/servers/direct.disabled /etc/nginx/servers/direct.conf + fi + + sed -i "s/%%port%%/${direct_port}/g" /etc/nginx/servers/direct.conf +fi + +ingress_port=$(bashio::addon.ingress_port) +ingress_interface=$(bashio::addon.ip_address) +sed -i "s/%%port%%/${ingress_port}/g" /etc/nginx/servers/ingress.conf +sed -i "s/%%interface%%/${ingress_interface}/g" /etc/nginx/servers/ingress.conf diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/type b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/type new file mode 100644 index 0000000000..bdd22a1850 --- /dev/null +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/type @@ -0,0 +1 @@ +oneshot diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/up b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/up new file mode 100644 index 0000000000..b3b5b494b5 --- /dev/null +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/init-nginx/up @@ -0,0 +1 @@ +/etc/s6-overlay/s6-rc.d/init-nginx/run diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/dependencies.d/esphome b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/dependencies.d/esphome new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/dependencies.d/init-nginx b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/dependencies.d/init-nginx new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/finish b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/finish new file mode 100755 index 0000000000..bbd6d8fecf --- /dev/null +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/finish @@ -0,0 +1,25 @@ +#!/command/with-contenv bashio +# ============================================================================== +# Community Hass.io Add-ons: ESPHome +# Take down the S6 supervision tree when NGINX fails +# ============================================================================== +declare exit_code +readonly exit_code_container=$( /run/s6-linux-init-container-results/exitcode + fi + [[ "${exit_code_signal}" -eq 15 ]] && exec /run/s6/basedir/bin/halt +elif [[ "${exit_code_service}" -ne 0 ]]; then + if [[ "${exit_code_container}" -eq 0 ]]; then + echo "${exit_code_service}" > /run/s6-linux-init-container-results/exitcode + fi + exec /run/s6/basedir/bin/halt +fi diff --git a/docker/ha-addon-rootfs/etc/services.d/nginx/run b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/run similarity index 73% rename from docker/ha-addon-rootfs/etc/services.d/nginx/run rename to docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/run index 8582167b96..e96991cdd1 100755 --- a/docker/ha-addon-rootfs/etc/services.d/nginx/run +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/run @@ -1,10 +1,11 @@ -#!/usr/bin/with-contenv bashio +#!/command/with-contenv bashio +# shellcheck shell=bash # ============================================================================== # Community Hass.io Add-ons: ESPHome # Runs the NGINX proxy # ============================================================================== -bashio::log.info "Waiting for dashboard to come up..." +bashio::log.info "Waiting for ESPHome dashboard to come up..." while [[ ! -S /var/run/esphome.sock ]]; do sleep 0.5 diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/type b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/type new file mode 100644 index 0000000000..5883cff0cd --- /dev/null +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/nginx/type @@ -0,0 +1 @@ +longrun diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/esphome b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/esphome new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/init-nginx b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/init-nginx new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/nginx b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/nginx new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docker/ha-addon-rootfs/etc/services.d/esphome/finish b/docker/ha-addon-rootfs/etc/services.d/esphome/finish deleted file mode 100755 index fed449ce61..0000000000 --- a/docker/ha-addon-rootfs/etc/services.d/esphome/finish +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/execlineb -S0 -# ============================================================================== -# Community Hass.io Add-ons: ESPHome -# Take down the S6 supervision tree when ESPHome fails -# ============================================================================== - -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/docker/ha-addon-rootfs/etc/services.d/nginx/finish b/docker/ha-addon-rootfs/etc/services.d/nginx/finish deleted file mode 100755 index 8030841ec8..0000000000 --- a/docker/ha-addon-rootfs/etc/services.d/nginx/finish +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/execlineb -S0 -# ============================================================================== -# Community Hass.io Add-ons: ESPHome -# Take down the S6 supervision tree when NGINX fails -# ============================================================================== - -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"