mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-05-30 12:24:04 +02:00
Update 01-config_yaml.sh
This commit is contained in:
@@ -1,155 +1,100 @@
|
|||||||
#!/usr/bin/with-contenv bashio
|
#!/usr/bin/with-contenv bashio
|
||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
# shellcheck disable=SC2155,SC1087,SC2163,SC2116,SC2086
|
|
||||||
# -----------------------------------------------------------------------------
|
|
||||||
# Robust environment‑variable loader for Home‑Assistant add‑ons
|
|
||||||
# • Parses YAML with embedded Python, no external yq dependency
|
|
||||||
# • Supports !secret look‑ups
|
|
||||||
# • Correctly escapes values containing quotes, $, \\ , newlines …
|
|
||||||
# • Exports to: shell, env.py, .env, /etc/environment, s6, service scripts
|
|
||||||
# -----------------------------------------------------------------------------
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
##################
|
slug="${HOSTNAME/-/_}" ; slug="${slug#*_}"
|
||||||
# INITIALIZATION #
|
|
||||||
##################
|
|
||||||
|
|
||||||
# Run outside HA? then do nothing
|
# CONFIG FILE
|
||||||
if [[ ! -d /config ]] || ! bashio::supervisor.ping &>/dev/null; then
|
|
||||||
echo "..."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
slug="${HOSTNAME/-/_}"
|
|
||||||
slug="${slug#*_}"
|
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
|
||||||
# Resolve CONFIGSOURCE #
|
|
||||||
# -----------------------------------------------------------------------------
|
|
||||||
if [[ ! -f /config/configuration.yaml && ! -f /config/configuration.json ]]; then
|
if [[ ! -f /config/configuration.yaml && ! -f /config/configuration.json ]]; then
|
||||||
CONFIGLOCATION="/config" # New architecture
|
CONFIGLOCATION="/config"
|
||||||
CONFIGFILEBROWSER="/addon_configs/${HOSTNAME/-/_}/config.yaml"
|
|
||||||
else
|
else
|
||||||
CONFIGLOCATION="/config/addons_config/${slug}" # Legacy architecture
|
CONFIGLOCATION="/config/addons_config/${slug}"
|
||||||
CONFIGFILEBROWSER="/homeassistant/addons_config/${slug}/config.yaml"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mkdir -p "$CONFIGLOCATION"
|
mkdir -p "$CONFIGLOCATION"
|
||||||
CONFIGSOURCE="$CONFIGLOCATION/config.yaml"
|
CONFIGSOURCE="$CONFIGLOCATION/config.yaml"
|
||||||
|
|
||||||
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")"
|
||||||
[[ "$CONFIGSOURCE" == *.* ]] && CONFIGSOURCE="$(dirname "$CONFIGSOURCE")"
|
[[ "$CONFIGSOURCE" == *.* ]] && CONFIGSOURCE="$(dirname "$CONFIGSOURCE")"
|
||||||
[[ "$CONFIGSOURCE" != *.yaml ]] && CONFIGSOURCE="${CONFIGSOURCE%/}/config.yaml"
|
[[ "$CONFIGSOURCE" != *.yaml ]] && CONFIGSOURCE="${CONFIGSOURCE%/}/config.yaml"
|
||||||
case "$CONFIGSOURCE" in
|
case "$CONFIGSOURCE" in
|
||||||
/share/* | /config/* | /data/*) : ;;
|
/share/*|/config/*|/data/*) :;;
|
||||||
*) bashio::log.red "CONFIG_LOCATION must be in /share, /config or /data – defaulting." && CONFIGSOURCE="$CONFIGLOCATION/config.yaml" ;;
|
*) bashio::log.red "CONFIG_LOCATION must be in /share, /config or /data – reverting." && CONFIGSOURCE="$CONFIGLOCATION/config.yaml";;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$CONFIGLOCATION" == "/config" && -f "/homeassistant/addons_config/${slug}/config.yaml" && ! -L "/homeassistant/addons_config/${slug}" ]]; then
|
if [[ "$CONFIGLOCATION" == "/config" && -f "/homeassistant/addons_config/${slug}/config.yaml" && ! -L "/homeassistant/addons_config/${slug}" ]]; then
|
||||||
echo "Migrating config.yaml to $CONFIGLOCATION"
|
echo "Migrating config.yaml to $CONFIGLOCATION"
|
||||||
mv "/homeassistant/addons_config/${slug}/config.yaml" "$CONFIGSOURCE"
|
mv "/homeassistant/addons_config/${slug}/config.yaml" "$CONFIGSOURCE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
chmod -R 755 "$(dirname "$CONFIGSOURCE")"
|
chmod -R 755 "$(dirname "$CONFIGSOURCE")"
|
||||||
|
|
||||||
####################
|
HAS_PYTHON=false; command -v python3 &>/dev/null && HAS_PYTHON=true
|
||||||
# CONFIG TEMPLATE #
|
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::log.yellow "yq not found – YAML parsing will use fallback parser."
|
||||||
|
|
||||||
if [[ ! -f "$CONFIGSOURCE" ]]; then
|
if [[ ! -f "$CONFIGSOURCE" ]]; then
|
||||||
echo "... no config file, creating one from template."
|
echo "… no config file, creating one from template."
|
||||||
mkdir -p "$(dirname "$CONFIGSOURCE")"
|
mkdir -p "$(dirname "$CONFIGSOURCE")"
|
||||||
if [[ -f /templates/config.yaml ]]; then
|
if [[ -f /templates/config.yaml ]]; then
|
||||||
cp /templates/config.yaml "$CONFIGSOURCE"
|
cp /templates/config.yaml "$CONFIGSOURCE"
|
||||||
else
|
else
|
||||||
curl -fsSL "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/config.template" -o "$CONFIGSOURCE"
|
curl -fsSL "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/config.template" -o "$CONFIGSOURCE"
|
||||||
fi
|
fi
|
||||||
bashio::log.green "Edit $CONFIGSOURCE then restart the add‑on."
|
bashio::log.green "Edit $CONFIGSOURCE then restart the add‑on."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! grep -qE '^[[:space:]]*[A-Za-z0-9_]+:' "$CONFIGSOURCE"; then
|
shell_escape() { printf '%q' "$1"; }
|
||||||
bashio::log.green "... no env variables found, exiting"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
############################################
|
read_config() {
|
||||||
# HELPER: read_yaml() (Python) #
|
local file="$1"
|
||||||
############################################
|
if $HAS_YQ; then
|
||||||
# Prints flattened KEY=value lines for scalar leaves.
|
yq eval 'to_entries | .[] | select(.key|test("^[#_]")|not) | "\(.key)=\(.value)"' "$file" 2>/dev/null
|
||||||
read_yaml() {
|
return
|
||||||
python3 - "$1" <<'PY'
|
fi
|
||||||
import sys, yaml, json, pathlib
|
awk '
|
||||||
from collections.abc import Mapping, Sequence
|
BEGIN{FS=":"}
|
||||||
|
/^[ \t]*#/ {next} # skip comment lines
|
||||||
def walk(node, prefix=""):
|
/^[ \t]*$/ {next} # skip blank lines
|
||||||
if isinstance(node, Mapping):
|
/^[ \t]*[_#]/ {next} # skip keys starting with _ or #
|
||||||
for k, v in node.items():
|
match($0, /^[[:space:]]*([A-Za-z0-9][A-Za-z0-9_]*)[[:space:]]*:[[:space:]]*(.*)$/, m) {
|
||||||
yield from walk(v, f"{prefix}{k}_")
|
key=m[1]; val=m[2]
|
||||||
elif isinstance(node, Sequence) and not isinstance(node, (str, bytes)):
|
gsub(/^['\"]|['\"]$/,"",val) # remove surrounding quotes
|
||||||
for i, v in enumerate(node):
|
print key "=" val
|
||||||
yield from walk(v, f"{prefix}{i}_")
|
}
|
||||||
else:
|
' "$file"
|
||||||
yield prefix[:-1], node
|
|
||||||
|
|
||||||
fname = sys.argv[1]
|
|
||||||
with open(fname, 'r') as f:
|
|
||||||
data = yaml.safe_load(f) or {}
|
|
||||||
for k, v in walk(data):
|
|
||||||
if isinstance(v, (str, int, float, bool)):
|
|
||||||
print(f"{k}={v}")
|
|
||||||
PY
|
|
||||||
}
|
}
|
||||||
|
|
||||||
############################################
|
|
||||||
# HELPER: shell_escape() #
|
|
||||||
############################################
|
|
||||||
# Uses printf %q – POSIX‑sh safe quoting.
|
|
||||||
shell_escape() {
|
|
||||||
printf '%q' "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
############################################
|
|
||||||
# Locate secrets.yaml #
|
|
||||||
############################################
|
|
||||||
SECRETSFILE="/config/secrets.yaml"
|
SECRETSFILE="/config/secrets.yaml"
|
||||||
[[ -f "$SECRETSFILE" ]] || SECRETSFILE="/homeassistant/secrets.yaml"
|
[[ -f "$SECRETSFILE" ]] || SECRETSFILE="/homeassistant/secrets.yaml"
|
||||||
|
|
||||||
get_secret() {
|
get_secret() {
|
||||||
python3 - "$SECRETSFILE" "$1" <<'PY'
|
local name="$1"
|
||||||
import sys, yaml, pathlib
|
if $HAS_YQ; then
|
||||||
sec, key = sys.argv[1:3]
|
yq eval ".${name}" "$SECRETSFILE" 2>/dev/null || true
|
||||||
try:
|
else
|
||||||
with open(sec) as f:
|
grep -m1 "^${name}:" "$SECRETSFILE" 2>/dev/null | sed 's/.*:[[:space:]]*//'
|
||||||
data = yaml.safe_load(f) or {}
|
fi
|
||||||
print(data.get(key, ""))
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass
|
|
||||||
PY
|
|
||||||
}
|
}
|
||||||
|
|
||||||
############################################
|
while IFS= read -r LINE; do
|
||||||
# MAIN LOOP #
|
[[ -z "$LINE" || "$LINE" != *=* ]] && continue
|
||||||
############################################
|
KEY="${LINE%%=*}"
|
||||||
while IFS= read -r PAIR; do
|
VALUE="${LINE#*=}"
|
||||||
KEY="${PAIR%%=*}"
|
# !secret handling
|
||||||
VALUE="${PAIR#*=}"
|
if [[ "$VALUE" =~ ^!secret[[:space:]]+(.+) ]]; then
|
||||||
|
NAME="${BASH_REMATCH[1]}"
|
||||||
# !secret support
|
VALUE="$(get_secret "$NAME")"
|
||||||
if [[ "$VALUE" =~ ^!secret[[:space:]]+(.+) ]]; then
|
[[ -z "$VALUE" ]] && bashio::exit.nok "Secret '$NAME' not found in $SECRETSFILE"
|
||||||
NAME="${BASH_REMATCH[1]}"
|
fi
|
||||||
VALUE="$(get_secret "$NAME")"
|
VALUE="${VALUE##[[:space:]]}" ; VALUE="${VALUE%%[[:space:]]}"
|
||||||
[[ -z "$VALUE" ]] && bashio::exit.nok "Secret '$NAME' not found in $SECRETSFILE"
|
SAFE_VALUE=$(shell_escape "$VALUE")
|
||||||
fi
|
export "$KEY=$VALUE"
|
||||||
|
if $HAS_PYTHON; then
|
||||||
SAFE_VALUE=$(shell_escape "$VALUE")
|
python3 - "$KEY" "$VALUE" <<'PY'
|
||||||
|
|
||||||
# 1) Export to current shell
|
|
||||||
export "$KEY=$VALUE"
|
|
||||||
|
|
||||||
# 2) env.py (idempotent)
|
|
||||||
python3 - "$KEY" "$VALUE" <<'PY'
|
|
||||||
import json, os, pathlib, sys
|
import json, os, pathlib, sys
|
||||||
k, v = sys.argv[1:3]
|
k, v = sys.argv[1:3]
|
||||||
p = pathlib.Path('/env.py')
|
p = pathlib.Path('/env.py')
|
||||||
@@ -159,28 +104,17 @@ with p.open('a') as f:
|
|||||||
f.write(f"os.environ[{json.dumps(k)}] = {json.dumps(v)}\n")
|
f.write(f"os.environ[{json.dumps(k)}] = {json.dumps(v)}\n")
|
||||||
os.environ[k] = v
|
os.environ[k] = v
|
||||||
PY
|
PY
|
||||||
|
fi
|
||||||
# 3) .env & /etc/environment (double‑quoted, internal " escaped)
|
env_val="${VALUE//"/\"}"
|
||||||
env_val="${VALUE//\"/\"}"
|
printf '%s="%s"\n' "$KEY" "$env_val" >> /.env
|
||||||
printf '%s="%s"\n' "$KEY" "$env_val" >>/.env
|
printf '%s="%s"\n' "$KEY" "$env_val" >> /etc/environment
|
||||||
printf '%s="%s"\n' "$KEY" "$env_val" >>/etc/environment
|
[[ -d /var/run/s6/container_environment ]] && printf '%s' "$VALUE" > "/var/run/s6/container_environment/$KEY"
|
||||||
|
for script in /etc/services.d/*/*run* /etc/cont-init.d/*run*; do
|
||||||
# 4) s6 container_environment (raw value)
|
[[ -f $script ]] || continue
|
||||||
if [[ -d /var/run/s6/container_environment ]]; then
|
grep -q "^export $KEY=" "$script" || sed -i "1i export $KEY=$SAFE_VALUE" "$script"
|
||||||
printf '%s' "$VALUE" >"/var/run/s6/container_environment/$KEY"
|
done
|
||||||
fi
|
grep -q "^export $KEY=" ~/.bashrc || echo "export $KEY=$SAFE_VALUE" >> ~/.bashrc
|
||||||
|
bashio::log.blue "$KEY='${VALUE:0:60}'${VALUE:60:+…}"
|
||||||
# 5) Prepend export to service scripts
|
done < <(read_config "$CONFIGSOURCE")
|
||||||
for script in /etc/services.d/*/*run* /etc/cont-init.d/*run*; do
|
|
||||||
[[ -f $script ]] || continue
|
|
||||||
grep -q "^export $KEY=" "$script" || sed -i "1i export $KEY=$SAFE_VALUE" "$script"
|
|
||||||
done
|
|
||||||
|
|
||||||
# 6) Persist for interactive shells
|
|
||||||
grep -q "^export $KEY=" ~/.bashrc || echo "export $KEY=$SAFE_VALUE" >>~/.bashrc
|
|
||||||
|
|
||||||
# 7) Log (truncate long values)
|
|
||||||
bashio::log.blue "$KEY='${VALUE:0:60}'${VALUE:60:+…}"
|
|
||||||
done < <(read_yaml "$CONFIGSOURCE")
|
|
||||||
|
|
||||||
bashio::log.green "Environment variables successfully loaded."
|
bashio::log.green "Environment variables successfully loaded."
|
||||||
|
|||||||
Reference in New Issue
Block a user