disable config_yaml

This commit is contained in:
Bruvv
2022-01-01 13:18:35 +01:00
parent 4e52bfda7b
commit 032f2be8cc

View File

@@ -1,102 +1,102 @@
#!/usr/bin/with-contenv bashio # #!/usr/bin/with-contenv bashio
################## # ##################
# INITIALIZATION # # # INITIALIZATION #
################## # ##################
# Where is the config # # Where is the config
CONFIGSOURCE=$(bashio::config "CONFIG_LOCATION") # CONFIGSOURCE=$(bashio::config "CONFIG_LOCATION")
# 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 "Using config file found in $CONFIGSOURCE" # echo "Using config file found in $CONFIGSOURCE"
else # else
echo "No config file, creating one from template" # echo "No config file, creating one from template"
# 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/zzz_templates/config.template" # TEMPLATESOURCE="https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/zzz_templates/config.template"
curl -L -f -s $TEMPLATESOURCE --output "$CONFIGSOURCE" # curl -L -f -s $TEMPLATESOURCE --output "$CONFIGSOURCE"
fi # fi
# Need to restart # # Need to restart
bashio::log.fatal "Config file not found, creating a new one. Please customize the file in $CONFIGSOURCE before restarting." # bashio::log.fatal "Config file not found, creating a new one. Please customize the file in $CONFIGSOURCE before restarting."
# bashio::exit.nok # # bashio::exit.nok
fi # fi
# Check if yaml is valid # # Check if yaml is valid
EXIT_CODE=0 # EXIT_CODE=0
yamllint -d relaxed "$CONFIGSOURCE" &>ERROR || EXIT_CODE=$? # yamllint -d relaxed "$CONFIGSOURCE" &>ERROR || EXIT_CODE=$?
if [ $EXIT_CODE = 0 ]; then # if [ $EXIT_CODE = 0 ]; then
echo "Config file is a valid yaml" # echo "Config file is a valid yaml"
else # else
cat ERROR # cat ERROR
bashio::log.fatal "Config file has an invalid yaml format. Please check the file in $CONFIGSOURCE. Errors list above." # bashio::log.fatal "Config file has an invalid yaml format. Please check the file in $CONFIGSOURCE. Errors list above."
# bashio::exit.nok # # bashio::exit.nok
fi # fi
# Export all yaml entries as env variables # # Export all yaml entries as env variables
# Helper function # # Helper function
function parse_yaml { # function parse_yaml {
local prefix=$2 || local prefix="" # local prefix=$2 || local prefix=""
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @ | tr @ '\034') # local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @ | tr @ '\034')
sed -ne "s|^\($s\):|\1|" \ # sed -ne "s|^\($s\):|\1|" \
-e "s| #.*$||g" \ # -e "s| #.*$||g" \
-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" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | # -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{ # awk -F$fs '{
indent = length($1)/2; # indent = length($1)/2;
vname[indent] = $2; # vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}} # for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) { # if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")} # vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'"$prefix"'",vn, $2, $3); # printf("%s%s%s=\"%s\"\n", "'"$prefix"'",vn, $2, $3);
} # }
}' # }'
} # }
# Get variables and export # # Get variables and export
bashio::log.info "Starting the app with the variables in in $CONFIGSOURCE" # bashio::log.info "Starting the app with the variables in in $CONFIGSOURCE"
# Get list of parameters in a file # # Get list of parameters in a file
parse_yaml "$CONFIGSOURCE" "" >/tmpfile # parse_yaml "$CONFIGSOURCE" "" >/tmpfile
while IFS= read -r line; do # while IFS= read -r line; do
# Clean output # # Clean output
line=${line//[\"\']/} # line=${line//[\"\']/}
# Check if secret # # Check if secret
if [[ "${line}" == *'!secret '* ]]; then # if [[ "${line}" == *'!secret '* ]]; then
echo "secret detected" # echo "secret detected"
secret=${line#*secret } # secret=${line#*secret }
# Check if single match # # Check if single match
secretnum=$(sed -n "/$secret:/=" /config/secrets.yaml) # secretnum=$(sed -n "/$secret:/=" /config/secrets.yaml)
[[ $("$secretnum") == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file" # [[ $("$secretnum") == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file"
# Get text # # Get text
secret=$(sed -n "/$secret:/p" /config/secrets.yaml) # secret=$(sed -n "/$secret:/p" /config/secrets.yaml)
secret=${secret#*: } # secret=${secret#*: }
line="${line%%=*}=$secret" # line="${line%%=*}=$secret"
fi # fi
# Data validation # # Data validation
if [[ $line =~ ^.+[=].+$ ]]; then # if [[ $line =~ ^.+[=].+$ ]]; then
export "{$line}" # export "{$line}"
# Export the variable # # Export the variable
sed -i "1a export $line" /etc/services.d/*/*run* 2>/dev/null || true # sed -i "1a export $line" /etc/services.d/*/*run* 2>/dev/null || true
sed -i "1a export $line" /scripts/*run* 2>/dev/null || true # sed -i "1a export $line" /scripts/*run* 2>/dev/null || true
# Show in log # # Show in log
if ! bashio::config.false "verbose"; then bashio::log.blue "$line"; fi # if ! bashio::config.false "verbose"; then bashio::log.blue "$line"; fi
else # else
bashio::exit.nok "$line does not follow the correct structure. Please check your yaml file." # bashio::exit.nok "$line does not follow the correct structure. Please check your yaml file."
fi # fi
done <"/tmpfile" # done <"/tmpfile"
# Test mode # # Test mode
TZ=$(bashio::config "TZ") # TZ=$(bashio::config "TZ")
if [ "$TZ" = "test" ]; then # if [ "$TZ" = "test" ]; then
echo "secret mode found, launching script in /config/test.sh" # echo "secret mode found, launching script in /config/test.sh"
cd /config || exit # cd /config || exit
chmod 777 test.sh # chmod 777 test.sh
./test.sh # ./test.sh
fi # fi