Skip to content

Commit

Permalink
Revert "chmod +x scripts"
Browse files Browse the repository at this point in the history
This reverts commit 3351e5b.
  • Loading branch information
alexbelgium committed Jun 15, 2022
1 parent 27bb72e commit 42bef66
Show file tree
Hide file tree
Showing 218 changed files with 9,448 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .templates/00-aaa_dockerfile_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

# If dockerfile failed install manually

##############################
# Automatic modules download #
##############################
if [ -e "/MODULESFILE" ]; then
MODULES=$(</MODULESFILE)
MODULES="${MODULES:-00-banner.sh}"
echo "Executing modules script : $MODULES"

if ! command -v bash >/dev/null 2>/dev/null; then (apt-get update && apt-get install -yqq --no-install-recommends bash || apk add --no-cache bash) >/dev/null; fi \
&& if ! command -v curl >/dev/null 2>/dev/null; then (apt-get update && apt-get install -yqq --no-install-recommends curl || apk add --no-cache curl) >/dev/null; fi \
&& apt-get update && apt-get install -yqq --no-install-recommends ca-certificates || apk add --no-cache ca-certificates >/dev/null || true \
&& mkdir -p /etc/cont-init.d \
&& for scripts in $MODULES; do echo "$scripts" && curl -f -L -s -S "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/$scripts" -o /etc/cont-init.d/"$scripts" && [ "$(sed -n '/\/bin/p;q' /etc/cont-init.d/"$scripts")" != "" ] || (echo "script failed to install $scripts" && exit 1); done \
&& chmod -R 755 /etc/cont-init.d
fi

#######################
# Automatic installer #
#######################
if [ -e "/ENVFILE" ]; then
PACKAGES=$(</ENVFILE)
echo "Executing dependency script with custom elements : $PACKAGES"

if ! command -v bash >/dev/null 2>/dev/null; then (apt-get update && apt-get install -yqq --no-install-recommends bash || apk add --no-cache bash) >/dev/null; fi \
&& if ! command -v curl >/dev/null 2>/dev/null; then (apt-get update && apt-get install -yqq --no-install-recommends curl || apk add --no-cache curl) >/dev/null; fi \
&& curl -f -L -s -S "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/automatic_packages.sh" --output /automatic_packages.sh \
&& chmod 777 /automatic_packages.sh \
&& eval /./automatic_packages.sh "${PACKAGES:-}" \
&& rm /automatic_packages.sh
fi

if [ -e "/MODULESFILE" ] && [ ! -f /entrypoint.sh ]; then
for scripts in $MODULES; do
echo "$scripts : executing"
chown "$(id -u)":"$(id -g)" /etc/cont-init.d/"$scripts"
chmod a+x /etc/cont-init.d/"$scripts"
/./etc/cont-init.d/"$scripts" || echo "/etc/cont-init.d/$scripts: exiting $?"
rm /etc/cont-init.d/"$scripts"
done | tac
fi

#######################
# Correct permissions #
#######################
[ -d /etc/services.d ] && chmod -R 777 /etc/services.d
[ -d /etc/cont-init.d ] && chmod -R 777 /etc/cont-init.d
46 changes: 46 additions & 0 deletions .templates/00-banner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# Displays a simple add-on banner on startup
# ==============================================================================
if bashio::supervisor.ping; then
bashio::log.blue \
'-----------------------------------------------------------'
bashio::log.blue " Add-on: $(bashio::addon.name)"
bashio::log.blue " $(bashio::addon.description)"
bashio::log.blue \
'-----------------------------------------------------------'

bashio::log.blue " Add-on version: $(bashio::addon.version)"
if bashio::var.true "$(bashio::addon.update_available)"; then
bashio::log.magenta ' There is an update available for this add-on!'
bashio::log.magenta \
" Latest add-on version: $(bashio::addon.version_latest)"
bashio::log.magenta ' Please consider upgrading as soon as possible.'
else
bashio::log.green ' You are running the latest version of this add-on.'
fi

bashio::log.blue " System: $(bashio::info.operating_system)" \
" ($(bashio::info.arch) / $(bashio::info.machine))"
bashio::log.blue " Home Assistant Core: $(bashio::info.homeassistant)"
bashio::log.blue " Home Assistant Supervisor: $(bashio::info.supervisor)"

bashio::log.blue \
'-----------------------------------------------------------'
bashio::log.blue \
' Please, share the above information when looking for help'
bashio::log.blue \
' or support in, e.g., GitHub, forums'
bashio::log.green \
' https://github.com/alexbelgium/hassio-addons'
bashio::log.blue \
'-----------------------------------------------------------'
fi

# ==============================================================================
# Global actions for all addons
# ==============================================================================

[ -d /etc/cont-init.d ] && chmod -R 777 /etc/cont-init.d
[ -d /etc/services.d ] && chmod -R 777 /etc/services.d
8 changes: 8 additions & 0 deletions .templates/00-bettercomments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# Color comments
#! Red
#? Question
#// Done
#todo To do
#* Green
41 changes: 41 additions & 0 deletions .templates/00-global_var.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash

###################################
# Export all addon options as env #
###################################

# For all keys in options.json
JSONSOURCE="/data/options.json"

# Export keys as env variables
# echo "All addon options were exported as variables"
mapfile -t arr < <(jq -r 'keys[]' "${JSONSOURCE}")

for KEYS in "${arr[@]}"; do
# export key
VALUE=$(jq ."$KEYS" "${JSONSOURCE}")
line="${KEYS}='${VALUE//[\"\']/}'"
# text
if bashio::config.false "verbose" || [[ "${KEYS}" == *"PASS"* ]]; then
bashio::log.blue "${KEYS}=******"
else
bashio::log.blue "$line"
fi
# Use locally
export "${KEYS}='${VALUE//[\"\']/}'"
# Export the variable to run scripts
if cat /etc/services.d/*/*run* &>/dev/null; then sed -i "1a export $line" /etc/services.d/*/*run* 2>/dev/null; fi
if cat /etc/cont-init.d/*run* &>/dev/null; then sed -i "1a export $line" /etc/cont-init.d/*run* 2>/dev/null; fi
done

################
# Set timezone #
################
set +e
if [ -n "$TZ" ] && [ -f /etc/localtime ]; then
if [ -f /usr/share/zoneinfo/"$TZ" ]; then
echo "Timezone set from $(cat /etc/timezone) to $TZ"
ln -snf /usr/share/zoneinfo/"$TZ" /etc/localtime && echo "$TZ" >/etc/timezone
fi
fi
45 changes: 45 additions & 0 deletions .templates/19-json_repair.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# shellchek disable=SC2015

JSONTOCHECK='/config/transmission/settings.json'
JSONSOURCE='/defaults/settings.json'

# If json already exists
if [ -f "${JSONTOCHECK}" ]; then
# Variables
echo "Checking settings.json format"

# Check if json file valid or not
jq . -S "${JSONTOCHECK}" &>/dev/null && ERROR=false || ERROR=true
if [ $ERROR = true ]; then
bashio::log.fatal "Settings.json structure is abnormal, restoring options from scratch. Your old file is renamed as settings.json_old"
mv "${JSONSOURCE}" "${JSONSOURCE}"_old
cp "${JSONSOURCE}" "${JSONTOCHECK}"
exit 0
fi

# Get the default keys from the original file
mapfile -t arr < <(jq -r 'keys[]' "${JSONSOURCE}")

# Check if all keys are still there, or add them
# spellcheck disable=SC2068
for KEYS in "${arr[@]}"; do
# Check if key exists
KEYSTHERE=$(jq "has(\"${KEYS}\")" "${JSONTOCHECK}")
if [ "$KEYSTHERE" != "true" ]; then
#Fetch initial value
JSONSOURCEVALUE=$(jq -r ".\"$KEYS\"" "${JSONSOURCE}")
#Add key
sed -i "3 i\"${KEYS}\": \"${JSONSOURCEVALUE}\"," "${JSONTOCHECK}"
# Message
bashio::log.warning "${KEYS} was missing from your settings.json, it was added with the default value ${JSONSOURCEVALUE}"
fi
done

# Show structure in a nice way
jq . -S "${JSONTOCHECK}" | cat >temp.json && mv temp.json "${JSONTOCHECK}"

# Message
bashio::log.info "Your settings.json was checked and seems perfectly normal!"
fi
126 changes: 126 additions & 0 deletions .templates/90-config_yaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# shellcheck disable=SC2155,SC1087,SC2163,SC2116,SC2086

##################
# INITIALIZATION #
##################

# Where is the config
CONFIGSOURCE=$(bashio::config "CONFIG_LOCATION")

# 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
CONFIGSOURCE=/config/addons_config/${HOSTNAME#*-}
bashio::log.fatal "Your CONFIG_LOCATION values can only be set in /share, /config or /data (internal to addon). It will be reset to the default location : $CONFIGSOURCE"
fi

# Check if config file is there, or create one from template
if [ -f "$CONFIGSOURCE" ]; then
echo "Using config file found in $CONFIGSOURCE"
else
echo "No config file, creating one from template"
# 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
# Need to restart
bashio::log.fatal "Config file not found, creating a new one. Please customize the file in $CONFIGSOURCE before restarting."
# bashio::exit.nok
fi

# Permissions
chmod -R 755 "$(dirname "${CONFIGSOURCE}")"

# Check if yaml is valid
EXIT_CODE=0
yamllint -d relaxed "$CONFIGSOURCE" &>ERROR || EXIT_CODE=$?
if [ "$EXIT_CODE" = 0 ]; then
echo "Config file is a valid yaml"
else
cat ERROR
bashio::log.warning "Config file has an invalid yaml format. Please check the file in $CONFIGSOURCE. Errors list above."
# bashio::exit.nok
fi

# Check if = instead of :
if [[ "$(grep -c "=" "$CONFIGSOURCE")" -gt 2 ]]; then
bashio::log.warning 'Are you sure you did not use "KEY=VALUE" ? yaml nomenclature requires "KEY:VALUE"'
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 variables and export
bashio::log.info "Starting the app with the variables in in $CONFIGSOURCE"
# Get list of parameters in a file
parse_yaml "$CONFIGSOURCE" "" >/tmpfile
while IFS= read -r line; do
# Clean output
line="${line//[\"\']/}"
# Check if secret
if [[ "${line}" == *'!secret '* ]]; then
echo "secret detected"
secret=${line#*secret }
# Check if single match
secretnum=$(sed -n "/$secret:/=" /config/secrets.yaml)
[[ $(echo $secretnum) == *' '* ]] && bashio::exit.nok "There are multiple matches for your password name. Please check your secrets.yaml file"
# Get text
secret=$(sed -n "/$secret:/p" /config/secrets.yaml)
secret=${secret#*: }
line="${line%%=*}='$secret'"
fi
# Data validation
if [[ "$line" =~ ^.+[=].+$ ]]; then
export "$line"
# Export the variable
sed -i "1a export $line" /etc/services.d/*/*run* 2>/dev/null || true
sed -i "1a export $line" /etc/cont-init.d/*run* 2>/dev/null || true
sed -i "1a export $line" /scripts/*run* 2>/dev/null || true
# Show in log
if ! bashio::config.false "verbose"; then bashio::log.blue "$line"; fi
else
bashio::exit.nok "$line does not follow the correct structure. Please check your yaml file."
fi
done <"/tmpfile"

# Test mode
TZ=$(bashio::config "TZ")
if [ "$TZ" = "test" ]; then
echo "secret mode found, launching script in /config/test.sh"
cd /config || exit
chmod 777 test.sh
./test.sh
fi
43 changes: 43 additions & 0 deletions .templates/90-dns_set.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash

###############
# DNS SETTING #
###############

# Avoid usage of local dns such as adguard home or pihole\n"

if bashio::config.has_value 'DNS_server'; then
# Define variables
DNSSERVER=$(bashio::config 'DNS_server')
DNS=""
DNSLIST=""

# Get DNS servers
# shellcheck disable=SC2086
for server in ${DNSSERVER//,/ }; do # Separate comma separated values
# Only add DNS if successful
if ping -c 1 "$server" &> /dev/null
then
DNS="${DNS}nameserver $server\n"
DNSLIST="$server $DNSLIST"
else
bashio::log.warning "DNS $server was requested but can't be pinged. It won't be used"
fi
done

# Only add DNS if there are DNS set
# shellcheck disable=SC2236
if [[ ! -z "$DNS" ]]; then
# Write resolv.conf
# shellcheck disable=SC2059
printf "${DNS}" >/etc/resolv.conf
chmod 644 /etc/resolv.conf
bashio::log.info "DNS SERVERS set to $DNSLIST"
else
bashio::log.warning "No valid DNS were found. Using default router (or HA) dns servers."
fi

else
bashio::log.info "DNS Servers option empty. Using default router (or HA) dns servers."
fi
13 changes: 13 additions & 0 deletions .templates/91-silent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash

###############
# SILENT MODE #
###############

if bashio::config.true 'silent'; then
APPEND=' > /dev/null'
sed -i '$s|$|'"$APPEND"'|' /etc/services.d/*/run &>/dev/null || true
sed -i '$s|$|'"$APPEND"'|' /etc/cont-init.d/*/*run* &>/dev/null || true
bashio::log.info 'Silent mode activated, all logs from emby server are hidden. Disable this option if you need to troubleshoot the addon.'
fi
Loading

0 comments on commit 42bef66

Please sign in to comment.