Update 01-config_yaml.sh

This commit is contained in:
Alexandre
2025-07-06 15:17:18 +02:00
committed by GitHub
parent 834830fad2
commit 48e5406ef8

View File

@@ -1,115 +1,208 @@
#!/usr/bin/with-contenv bashio #!/usr/bin/with-contenv bashio
# shellcheck shell=bash # shellcheck shell=bash
set -euo pipefail # shellcheck disable=SC2155,SC1087,SC2163,SC2116,SC2086
set -e
##################
# INITIALIZATION #
##################
# Disable if config not present
if [ ! -d /config ] || ! bashio::supervisor.ping 2>/dev/null; then
echo "..."
exit 0
fi
# Define slug
slug="${HOSTNAME/-/_}" slug="${HOSTNAME/-/_}"
slug="${slug#*_}" slug="${slug#*_}"
# CONFIG FILE # 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
CONFIGLOCATION="/config" # New config location
CONFIGLOCATION="/config"
CONFIGFILEBROWSER="/addon_configs/${HOSTNAME/-/_}/config.yaml"
else else
CONFIGLOCATION="/config/addons_config/${slug}" # Legacy config location
CONFIGLOCATION="/config/addons_config/${slug}"
CONFIGFILEBROWSER="/homeassistant/addons_config/$slug/config.yaml"
fi fi
mkdir -p "$CONFIGLOCATION" # Default location
CONFIGSOURCE="$CONFIGLOCATION/config.yaml" mkdir -p "$CONFIGLOCATION" || true
CONFIGSOURCE="$CONFIGLOCATION"/config.yaml
# 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" == *.* ]] && CONFIGSOURCE="$(dirname "$CONFIGSOURCE")" CONFIGSOURCE=$(bashio::config "CONFIG_LOCATION")
[[ "$CONFIGSOURCE" != *.yaml ]] && CONFIGSOURCE="${CONFIGSOURCE%/}/config.yaml" if [[ "$CONFIGSOURCE" == *.* ]]; then
case "$CONFIGSOURCE" in CONFIGSOURCE=$(dirname "$CONFIGSOURCE")
/share/* | /config/* | /data/*) : ;; fi
*) bashio::log.red "CONFIG_LOCATION must be in /share, /config or /data reverting." && CONFIGSOURCE="$CONFIGLOCATION/config.yaml" ;; # If does not end by config.yaml, remove trailing slash and add config.yaml
esac if [[ "$CONFIGSOURCE" != *".yaml" ]]; then
CONFIGSOURCE="${CONFIGSOURCE%/}"/config.yaml
fi
# Check if config is located in an acceptable location
LOCATIONOK=""
for location in "/share" "/config" "/data"; do
if [[ "$CONFIGSOURCE" == "$location"* ]]; then
LOCATIONOK=true
fi
done
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"
CONFIGSOURCE="$CONFIGLOCATION"/config.yaml
fi
fi fi
if [[ "$CONFIGLOCATION" == "/config" && -f "/homeassistant/addons_config/${slug}/config.yaml" && ! -L "/homeassistant/addons_config/${slug}" ]]; then # Migrate if needed
echo "Migrating config.yaml to $CONFIGLOCATION" if [[ "$CONFIGLOCATION" == "/config" ]]; then
mv "/homeassistant/addons_config/${slug}/config.yaml" "$CONFIGSOURCE" # Migrate file
if [ -f "/homeassistant/addons_config/${slug}/config.yaml" ] && [ ! -L "/homeassistant/addons_config/${slug}" ]; then
echo "Migrating config.yaml to new config location"
mv /homeassistant/addons_config/"${slug}"/config.yaml /config/config.yaml
fi
# Migrate option
if [[ "$(bashio::config "CONFIG_LOCATION")" == "/config/addons_config"* ]] && [ -f /config/config.yaml ]; then
bashio::addon.option "CONFIG_LOCATION" "/config/config.yaml"
CONFIGSOURCE="/config/config.yaml"
fi
fi fi
chmod -R 755 "$(dirname "$CONFIGSOURCE")" if [[ "$CONFIGSOURCE" != *".yaml" ]]; then
bashio::log.error "Something is going wrong in the config location, quitting"
HAS_PYTHON=false
command -v python3 &>/dev/null && HAS_PYTHON=true
HAS_YQ=false
command -v yq &>/dev/null && HAS_YQ=true
$HAS_PYTHON || bashio::log.yellow "python3 not found /env.py export disabled."
$HAS_YQ || bashio::exit.nok "yq not found script not executed."
if [[ ! -f "$CONFIGSOURCE" ]]; then
echo "… no config file, creating one from template."
mkdir -p "$(dirname "$CONFIGSOURCE")"
if [[ -f /templates/config.yaml ]]; then
cp /templates/config.yaml "$CONFIGSOURCE"
else
curl -fsSL "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/config.template" -o "$CONFIGSOURCE"
fi
bashio::log.green "Edit $CONFIGSOURCE then restart the addon."
fi fi
shell_escape() { printf '%q' "$1"; } # Permissions
if [[ "$CONFIGSOURCE" == *".yaml" ]]; then
echo "Setting permissions for the config.yaml directory"
mkdir -p "$(dirname "${CONFIGSOURCE}")"
chmod -R 755 "$(dirname "${CONFIGSOURCE}")" 2>/dev/null
fi
# Prints key=value from YAML, ignoring comments/underscored keys ####################
read_config() { # LOAD CONFIG.YAML #
local file="$1" ####################
yq eval 'to_entries | .[] | select(.key|test("^[#_]")|not) | "\(.key)=\(.value | @sh)"' "$file" 2>/dev/null
echo ""
bashio::log.green "Load environment variables from $CONFIGSOURCE if existing"
if [[ "$CONFIGSOURCE" == "/config"* ]]; then
bashio::log.green "If accessing the file with filebrowser it should be mapped to $CONFIGFILEBROWSER"
else
bashio::log.green "If accessing the file with filebrowser it should be mapped to $CONFIGSOURCE"
fi
bashio::log.green "---------------------------------------------------------"
bashio::log.green "Wiki here on how to use : github.com/alexbelgium/hassio-addons/wiki/Addons-feature-:-add-env-variables"
echo ""
# Check if config file is there, or create one from template
if [ ! -f "$CONFIGSOURCE" ]; then
echo "... no config file, creating one from template. Please customize the file in $CONFIGSOURCE before restarting."
# Create folder
mkdir -p "$(dirname "${CONFIGSOURCE}")"
# Placing template in config
if [ -f /templates/config.yaml ]; then
# Use available template
cp /templates/config.yaml "$(dirname "${CONFIGSOURCE}")"
else
# Download template
TEMPLATESOURCE="https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/config.template"
curl -f -L -s -S "$TEMPLATESOURCE" --output "$CONFIGSOURCE"
fi
fi
# Check if there are lines to read
cp "$CONFIGSOURCE" /tempenv
sed -i '/^#/d' /tempenv
sed -i '/^[[:space:]]*$/d' /tempenv
sed -i '/^$/d' /tempenv
# Exit if empty
if [ ! -s /tempenv ]; then
bashio::log.green "... no env variables found, exiting"
exit 0
fi
rm /tempenv
# Check if yaml is valid
EXIT_CODE=0
yamllint -d relaxed "$CONFIGSOURCE" &>ERROR || EXIT_CODE=$?
if [ "$EXIT_CODE" != 0 ]; then
cat ERROR
bashio::log.yellow "... config file has an invalid yaml format. Please check the file in $CONFIGSOURCE. Errors list above."
fi
# Export all yaml entries as env variables
# Helper function
function parse_yaml {
local prefix=$2 || local prefix=""
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @ | tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
-e "s| #.*$||g" \
-e "s|#.*$||g" \
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
} }
# Get list of parameters in a file
parse_yaml "$CONFIGSOURCE" "" >/tmpfile
# Escape dollars
sed -i 's|$.|\$|g' /tmpfile
# Look where secrets.yaml is located
SECRETSFILE="/config/secrets.yaml" SECRETSFILE="/config/secrets.yaml"
[[ -f "$SECRETSFILE" ]] || SECRETSFILE="/homeassistant/secrets.yaml" if [ -f "$SECRETSFILE" ]; then SECRETSFILE="/homeassistant/secrets.yaml"; fi
get_secret() {
local name="$1"
yq eval ".${name}" "$SECRETSFILE" 2>/dev/null || true
}
# Safe double-quote for .env and /etc/environment (bash and python compatible) while IFS= read -r line; do
dq_escape() { # Clean output
# Escape only embedded double quotes and dollar signs for shell (not for YAML) line="${line//[\"\']/}"
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\$/\\$/g' # Check if secret
} if [[ "${line}" == *'!secret '* ]]; then
echo "secret detected"
while IFS= read -r LINE; do secret=${line#*secret }
[[ -z "$LINE" || "$LINE" != *=* ]] && continue # Check if single match
# Escape special characters not within single quotes secretnum=$(sed -n "/$secret:/=" "$SECRETSFILE")
LINE=$(sed -E "s/([^'])([][\$\`\"\\!&;|<>])/\1\\\\\\2/g" <<<"$LINE") [[ $(echo $secretnum) == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file"
KEY="${LINE%%=*}" # Get text
VALUE="${LINE#*=}" secret=$(sed -n "/$secret:/p" "$SECRETSFILE")
# !secret handling secret=${secret#*: }
if [[ "$VALUE" =~ ^!secret[[:space:]]+(.+) ]]; then line="${line%%=*}='$secret'"
NAME="${BASH_REMATCH[1]}" fi
VALUE="$(get_secret "$NAME")" # Data validation
[[ -z "$VALUE" ]] && bashio::exit.nok "Secret '$NAME' not found in $SECRETSFILE" if [[ "$line" =~ ^.+[=].+$ ]]; then
fi # extract keys and values
VALUE="${VALUE##[[:space:]]}" KEYS="${line%%=*}"
VALUE="${VALUE%%[[:space:]]}" VALUE="${line#*=}"
SAFE_VALUE=$(shell_escape "$VALUE") line="${KEYS}='${VALUE}'"
export "$KEY=$VALUE" export "$line"
if $HAS_PYTHON; then # export to python
python3 - "$KEY" "$VALUE" <<'PY' if command -v "python3" &>/dev/null; then
import json, os, pathlib, sys [ ! -f /env.py ] && echo "import os" > /env.py
k, v = sys.argv[1:3] echo "os.environ['${KEYS}'] = '${VALUE//[\"\']/}'" >> /env.py
p = pathlib.Path('/env.py') python3 /env.py
if not p.exists(): fi
p.write_text('import os\n') # set .env
with p.open('a') as f: if [ -f /.env ]; then echo "$line" >> /.env; fi
f.write(f"os.environ[{json.dumps(k)}] = {json.dumps(v)}\n") mkdir -p /etc
os.environ[k] = v echo "$line" >> /etc/environment
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
env_val=$(dq_escape "$VALUE") if cat /etc/cont-init.d/*run* &>/dev/null; then sed -i "1a export $line" /etc/cont-init.d/*run* 2>/dev/null; fi
printf '%s="%s"\n' "$KEY" "$env_val" >>/.env # For s6
printf '%s="%s"\n' "$KEY" "$env_val" >>/etc/environment if [ -d /var/run/s6/container_environment ]; then printf "%s" "${VALUE}" > /var/run/s6/container_environment/"${KEYS}"; fi
[[ -d /var/run/s6/container_environment ]] && printf '%s' "$VALUE" >"/var/run/s6/container_environment/$KEY" echo "export $line" >> ~/.bashrc
for script in /etc/services.d/*/*run* /etc/cont-init.d/*run*; do # Show in log
[[ -f $script ]] || continue if ! bashio::config.false "verbose"; then bashio::log.blue "$line"; fi
grep -q "^export $KEY=" "$script" || sed -i "1i export $KEY=$SAFE_VALUE" "$script" else
done bashio::log.red "$line does not follow the correct structure. Please check your yaml file."
grep -q "^export $KEY=" ~/.bashrc || echo "export $KEY=$SAFE_VALUE" >>~/.bashrc fi
bashio::log.blue "$KEY='${VALUE:0:60}'${VALUE:60:+…}" done <"/tmpfile"
done < <(read_config "$CONFIGSOURCE")
bashio::log.green "Environment variables successfully loaded."