This commit is contained in:
Alexandre
2025-07-07 08:09:56 +02:00
committed by GitHub
parent f99b24eb2d
commit d5854da450

View File

@@ -7,8 +7,8 @@
# Disable if config not present # Disable if config not present
if [ ! -d /config ] || ! bashio::supervisor.ping 2>/dev/null; then if [ ! -d /config ] || ! bashio::supervisor.ping 2>/dev/null; then
echo "..." echo "..."
exit 0 exit 0
fi fi
# Define slug # Define slug
@@ -17,13 +17,13 @@ slug="${slug#*_}"
# Check type of config folder # Check type of config folder
if [ ! -f /config/configuration.yaml ] && [ ! -f /config/configuration.json ]; then if [ ! -f /config/configuration.yaml ] && [ ! -f /config/configuration.json ]; then
# New config location # New config location
CONFIGLOCATION="/config" CONFIGLOCATION="/config"
CONFIGFILEBROWSER="/addon_configs/${HOSTNAME/-/_}/config.yaml" CONFIGFILEBROWSER="/addon_configs/${HOSTNAME/-/_}/config.yaml"
else else
# Legacy config location # Legacy config location
CONFIGLOCATION="/config/addons_config/${slug}" CONFIGLOCATION="/config/addons_config/${slug}"
CONFIGFILEBROWSER="/homeassistant/addons_config/$slug/config.yaml" CONFIGFILEBROWSER="/homeassistant/addons_config/$slug/config.yaml"
fi fi
# Default location # Default location
@@ -32,51 +32,51 @@ CONFIGSOURCE="$CONFIGLOCATION"/config.yaml
# Is there a custom path # Is there a custom path
if bashio::config.has_value 'CONFIG_LOCATION'; then if bashio::config.has_value 'CONFIG_LOCATION'; then
CONFIGSOURCE=$(bashio::config "CONFIG_LOCATION")
CONFIGSOURCE=$(bashio::config "CONFIG_LOCATION") if [[ "$CONFIGSOURCE" == *.* ]]; then
if [[ "$CONFIGSOURCE" == *.* ]]; then CONFIGSOURCE=$(dirname "$CONFIGSOURCE")
CONFIGSOURCE=$(dirname "$CONFIGSOURCE") fi
fi # If does not end by config.yaml, remove trailing slash and add config.yaml
# If does not end by config.yaml, remove trailing slash and add config.yaml if [[ "$CONFIGSOURCE" != *".yaml" ]]; then
if [[ "$CONFIGSOURCE" != *".yaml" ]]; then CONFIGSOURCE="${CONFIGSOURCE%/}"/config.yaml
CONFIGSOURCE="${CONFIGSOURCE%/}"/config.yaml fi
fi # Check if config is located in an acceptable location
# Check if config is located in an acceptable location LOCATIONOK=""
LOCATIONOK="" for location in "/share" "/config" "/data"; do
for location in "/share" "/config" "/data"; do if [[ "$CONFIGSOURCE" == "$location"* ]]; then
if [[ "$CONFIGSOURCE" == "$location"* ]]; then LOCATIONOK=true
LOCATIONOK=true fi
fi done
done if [ -z "$LOCATIONOK" ]; then
if [ -z "$LOCATIONOK" ]; then bashio::log.red "Watch-out: your CONFIG_LOCATION values can only be set in /share, /config or /data (internal to addon). It will be reset to the default location: $CONFIGLOCATION/config.yaml"
bashio::log.red "Watch-out : your CONFIG_LOCATION values can only be set in /share, /config or /data (internal to addon). It will be reset to the default location : $CONFIGLOCATION/config.yaml" CONFIGSOURCE="$CONFIGLOCATION"/config.yaml
CONFIGSOURCE="$CONFIGLOCATION"/config.yaml fi
fi
fi fi
# Migrate if needed # Migrate if needed
if [[ "$CONFIGLOCATION" == "/config" ]]; then if [[ "$CONFIGLOCATION" == "/config" ]]; then
# Migrate file # Migrate file
if [ -f "/homeassistant/addons_config/${slug}/config.yaml" ] && [ ! -L "/homeassistant/addons_config/${slug}" ]; then if [ -f "/homeassistant/addons_config/${slug}/config.yaml" ] && [ ! -L "/homeassistant/addons_config/${slug}" ]; then
echo "Migrating config.yaml to new config location" echo "Migrating config.yaml to new config location"
mv /homeassistant/addons_config/"${slug}"/config.yaml /config/config.yaml mv "/homeassistant/addons_config/${slug}/config.yaml" /config/config.yaml
fi fi
# Migrate option # Migrate option
if [[ "$(bashio::config "CONFIG_LOCATION")" == "/config/addons_config"* ]] && [ -f /config/config.yaml ]; then if [[ "$(bashio::config "CONFIG_LOCATION")" == "/config/addons_config"* ]] && [ -f /config/config.yaml ]; then
bashio::addon.option "CONFIG_LOCATION" "/config/config.yaml" bashio::addon.option "CONFIG_LOCATION" "/config/config.yaml"
CONFIGSOURCE="/config/config.yaml" CONFIGSOURCE="/config/config.yaml"
fi fi
fi fi
if [[ "$CONFIGSOURCE" != *".yaml" ]]; then if [[ "$CONFIGSOURCE" != *".yaml" ]]; then
bashio::log.error "Something is going wrong in the config location, quitting" bashio::log.error "Something is going wrong in the config location, quitting"
exit 1
fi fi
# Permissions # Permissions
if [[ "$CONFIGSOURCE" == *".yaml" ]]; then if [[ "$CONFIGSOURCE" == *".yaml" ]]; then
echo "Setting permissions for the config.yaml directory" echo "Setting permissions for the config.yaml directory"
mkdir -p "$(dirname "${CONFIGSOURCE}")" mkdir -p "$(dirname "${CONFIGSOURCE}")"
chmod -R 755 "$(dirname "${CONFIGSOURCE}")" 2>/dev/null chmod -R 755 "$(dirname "${CONFIGSOURCE}")" 2>/dev/null
fi fi
#################### ####################
@@ -86,28 +86,28 @@ fi
echo "" echo ""
bashio::log.green "Load environment variables from $CONFIGSOURCE if existing" bashio::log.green "Load environment variables from $CONFIGSOURCE if existing"
if [[ "$CONFIGSOURCE" == "/config"* ]]; then if [[ "$CONFIGSOURCE" == "/config"* ]]; then
bashio::log.green "If accessing the file with filebrowser it should be mapped to $CONFIGFILEBROWSER" bashio::log.green "If accessing the file with filebrowser it should be mapped to $CONFIGFILEBROWSER"
else else
bashio::log.green "If accessing the file with filebrowser it should be mapped to $CONFIGSOURCE" bashio::log.green "If accessing the file with filebrowser it should be mapped to $CONFIGSOURCE"
fi fi
bashio::log.green "---------------------------------------------------------" bashio::log.green "---------------------------------------------------------"
bashio::log.green "Wiki here on how to use : github.com/alexbelgium/hassio-addons/wiki/Addons-feature-:-add-env-variables" bashio::log.green "Wiki here on how to use: github.com/alexbelgium/hassio-addons/wiki/Add-ons-feature:-add-env-variables"
echo "" echo ""
# Check if config file is there, or create one from template # Check if config file is there, or create one from template
if [ ! -f "$CONFIGSOURCE" ]; then if [ ! -f "$CONFIGSOURCE" ]; then
echo "... no config file, creating one from template. Please customize the file in $CONFIGSOURCE before restarting." echo "... no config file, creating one from template. Please customize the file in $CONFIGSOURCE before restarting."
# Create folder # Create folder
mkdir -p "$(dirname "${CONFIGSOURCE}")" mkdir -p "$(dirname "${CONFIGSOURCE}")"
# Placing template in config # Placing template in config
if [ -f /templates/config.yaml ]; then if [ -f /templates/config.yaml ]; then
# Use available template # Use available template
cp /templates/config.yaml "$(dirname "${CONFIGSOURCE}")" cp /templates/config.yaml "$(dirname "${CONFIGSOURCE}")"
else else
# Download template # Download template
TEMPLATESOURCE="https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/config.template" TEMPLATESOURCE="https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/config.template"
curl -f -L -s -S "$TEMPLATESOURCE" --output "$CONFIGSOURCE" curl -f -L -s -S "$TEMPLATESOURCE" --output "$CONFIGSOURCE"
fi fi
fi fi
# Check if there are lines to read # Check if there are lines to read
@@ -115,18 +115,19 @@ cp "$CONFIGSOURCE" /tempenv
sed -i '/^#/d' /tempenv sed -i '/^#/d' /tempenv
sed -i '/^[[:space:]]*$/d' /tempenv sed -i '/^[[:space:]]*$/d' /tempenv
sed -i '/^$/d' /tempenv sed -i '/^$/d' /tempenv
# Exit if empty # Exit if empty
if [ ! -s /tempenv ]; then if [ ! -s /tempenv ]; then
bashio::log.green "... no env variables found, exiting" bashio::log.green "... no env variables found, exiting"
exit 0 exit 0
fi fi
# Check if yaml is valid # Check if yaml is valid
EXIT_CODE=0 EXIT_CODE=0
yamllint -d relaxed /tempenv &>ERROR || EXIT_CODE=$? yamllint -d relaxed /tempenv &>ERROR || EXIT_CODE=$?
if [ "$EXIT_CODE" != 0 ]; then if [ "$EXIT_CODE" != 0 ]; then
cat ERROR cat ERROR
bashio::log.yellow "... config file has an invalid yaml format. Please check the file in $CONFIGSOURCE. Errors list above." bashio::log.yellow "... config file has an invalid yaml format. Please check the file in $CONFIGSOURCE. Errors list above."
fi fi
# converts yaml to variables # converts yaml to variables
@@ -137,52 +138,59 @@ SECRETSFILE="/config/secrets.yaml"
if [ -f "$SECRETSFILE" ]; then SECRETSFILE="/homeassistant/secrets.yaml"; fi if [ -f "$SECRETSFILE" ]; then SECRETSFILE="/homeassistant/secrets.yaml"; fi
while IFS= read -r line; do while IFS= read -r line; do
# Check if secret # Skip empty lines
if [[ "${line}" == *'!secret '* ]]; then if [[ -z "$line" ]]; then
echo "secret detected" continue
secret=${line#*secret } fi
# Check if single match
secretnum=$(sed -n "/$secret:/=" "$SECRETSFILE") # Check if secret
[[ $(echo $secretnum) == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file" if [[ "${line}" == *'!secret '* ]]; then
# Get text echo "secret detected"
secret=$(sed -n "/$secret:/p" "$SECRETSFILE") secret=${line#*secret }
secret=${secret#*: } # Check if single match
line="${line%%=*}='$secret'" secretnum=$(sed -n "/$secret:/=" "$SECRETSFILE")
fi [[ $(echo $secretnum) == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file"
# Data validation # Get text
if [[ "$line" =~ ^.+[=].+$ ]]; then secret=$(sed -n "/$secret:/p" "$SECRETSFILE")
# extract keys and values secret=${secret#*: }
KEYS="${line%%=*}" line="${line%%=*}='$secret'"
VALUE="${line#*=}" fi
# Check if VALUE is quoted
if [[ "$VALUE" != \"*\" ]] && [[ "$VALUE" != \'*\' ]]; then # Data validation
VALUE="\"$VALUE\"" if [[ "$line" =~ ^.+[=].+$ ]]; then
fi # extract keys and values
line="${KEYS}"="${VALUE}" KEYS="${line%%=*}"
export "$line" VALUE="${line#*=}"
# export to python # Check if VALUE is quoted
if command -v "python3" &>/dev/null; then if [[ "$VALUE" != \"*\" ]] && [[ "$VALUE" != \'*\' ]]; then
[ ! -f /env.py ] && echo "import os" >/env.py VALUE="\"$VALUE\""
# Escape single quotes in VALUE fi
VALUE_ESCAPED="${VALUE//\'/\'\"\'\"\'}" line="${KEYS}=${VALUE}"
echo "os.environ['${KEYS}'] = '${VALUE_ESCAPED}'" >>/env.py export "$line"
python3 /env.py # export to python
fi if command -v "python3" &>/dev/null; then
# set .env [ ! -f /env.py ] && echo "import os" > /env.py
echo "$line" >>/.env # Escape single quotes in VALUE
# set environement VALUE_ESCAPED="${VALUE//\'/\'\"\'\"\'}"
mkdir -p /etc echo "os.environ['${KEYS}'] = '${VALUE_ESCAPED}'" >> /env.py
echo "$line" >>/etc/environment python3 /env.py
# Export to scripts fi
if cat /etc/services.d/*/*run* &>/dev/null; then sed -i "1a export $line" /etc/services.d/*/*run* 2>/dev/null; fi # set .env
if cat /etc/cont-init.d/*run* &>/dev/null; then sed -i "1a export $line" /etc/cont-init.d/*run* 2>/dev/null; fi echo "$line" >> /.env
# For s6 # set environment
if [ -d /var/run/s6/container_environment ]; then printf "%s" "${VALUE}" >/var/run/s6/container_environment/"${KEYS}"; fi mkdir -p /etc
echo "export $line" >>~/.bashrc echo "$line" >> /etc/environment
# Show in log # Export to scripts
if ! bashio::config.false "verbose"; then bashio::log.blue "$line"; fi if cat /etc/services.d/*/*run* &>/dev/null; then sed -i "1a export $line" /etc/services.d/*/*run* 2>/dev/null; fi
else if cat /etc/cont-init.d/*run* &>/dev/null; then sed -i "1a export $line" /etc/cont-init.d/*run* 2>/dev/null; fi
bashio::log.red "$line does not follow the correct structure. Please check your yaml file." # For s6
fi if [ -d /var/run/s6/container_environment ]; then printf "%s" "${VALUE}" > /var/run/s6/container_environment/"${KEYS}"; fi
done <"/tempenv" echo "export $line" >> ~/.bashrc
# Show in log
if ! bashio::config.false "verbose"; then bashio::log.blue "$line"; fi
else
bashio::log.red "Skipping line that does not follow the correct structure: $line"
fi
done < "/tempenv"
rm /tempenv rm /tempenv