Skip to content
This repository has been archived by the owner on Feb 28, 2020. It is now read-only.

Commit

Permalink
🔨 Refactor of init scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck committed Apr 18, 2019
1 parent 79f91fd commit 33677ab
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 118 deletions.
9 changes: 0 additions & 9 deletions appdaemon/rootfs/etc/cont-init.d/20-init-configuration.sh

This file was deleted.

31 changes: 0 additions & 31 deletions appdaemon/rootfs/etc/cont-init.d/21-compiled-dir.sh

This file was deleted.

28 changes: 0 additions & 28 deletions appdaemon/rootfs/etc/cont-init.d/30-auto-token.sh

This file was deleted.

13 changes: 0 additions & 13 deletions appdaemon/rootfs/etc/cont-init.d/31-ha-url.sh

This file was deleted.

12 changes: 0 additions & 12 deletions appdaemon/rootfs/etc/cont-init.d/50-compiled-symlink.sh

This file was deleted.

14 changes: 0 additions & 14 deletions appdaemon/rootfs/etc/cont-init.d/80-system-packages.sh

This file was deleted.

11 changes: 0 additions & 11 deletions appdaemon/rootfs/etc/cont-init.d/81-python-packages.sh

This file was deleted.

96 changes: 96 additions & 0 deletions appdaemon/rootfs/etc/cont-init.d/appdaemon.sh
Original file line number Diff line number Diff line change
@@ -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)" != "$(bashio::addon.version)" ]];
then
bashio::log.info "Detected version upgrade, clearing compiled cache..."
rm -fr /data/compiled
fi
fi

if ! bashio::fs.directory_exists '/data/compiled'; then
mkdir -p \
/data/compiled/css \
/data/compiled/html \
/data/compiled/javascript \
|| bashio::exit.nok 'Failed to created compiled directory'

echo -e "$(bashio::addon.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

0 comments on commit 33677ab

Please sign in to comment.