diff --git a/.templates/ha_entrypoint.sh b/.templates/ha_entrypoint.sh index e68542fd7..57bf9773e 100755 --- a/.templates/ha_entrypoint.sh +++ b/.templates/ha_entrypoint.sh @@ -7,50 +7,96 @@ echo "Starting..." # Starting scripts # #################### +# Loop through /etc/cont-init.d/* for SCRIPTS in /etc/cont-init.d/*; do - [ -e "$SCRIPTS" ] || continue - echo "$SCRIPTS: executing" + [ -e "$SCRIPTS" ] || continue + echo "$SCRIPTS: executing" - # Check if run as root - if [ "$(id -u)" -eq 0 ]; then - chown "$(id -u)":"$(id -g)" "$SCRIPTS" - chmod a+x "$SCRIPTS" - else - echo -e "\e[38;5;214m$(date) WARNING: Script executed with user $(id -u):$(id -g), things can break and chown won't work\e[0m" - # Disable chown and chmod in scripts - sed -i "s/^chown /true # chown /g" "$SCRIPTS" - sed -i "s/ chown / true # chown /g" "$SCRIPTS" - sed -i "s/^chmod /true # chmod /g" "$SCRIPTS" - sed -i "s/ chmod / true # chmod /g" "$SCRIPTS" - fi + # Check if run as root + if [ "$(id -u)" -eq 0 ]; then + chown "$(id -u)":"$(id -g)" "$SCRIPTS" + chmod a+x "$SCRIPTS" + else + echo -e "\e[38;5;214m$(date) WARNING: Script executed with user $(id -u):$(id -g), things can break and chown won't work\e[0m" + # Disable chown and chmod in scripts + sed -i "s/^chown /true # chown /g" "$SCRIPTS" + sed -i "s/ chown / true # chown /g" "$SCRIPTS" + sed -i "s/^chmod /true # chmod /g" "$SCRIPTS" + sed -i "s/ chmod / true # chmod /g" "$SCRIPTS" + fi - # Get current shebang, if not available use another - currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SCRIPTS")" - if [ ! -f "${currentshebang%% *}" ]; then - for shebang in "/command/with-contenv bashio" "/usr/bin/with-contenv bashio" "/usr/bin/env bashio" "/usr/bin/bashio" "/usr/bin/bash" "/usr/bin/sh" "/bin/bash" "/bin/sh"; do - command_path="${shebang%% *}" - if [ -x "$command_path" ] && "$command_path" echo "yes" >/dev/null 2>&1; then - echo "Valid shebang: $shebang" - break - fi - done - sed -i "s|$currentshebang|$shebang|g" "$SCRIPTS" - fi + # Get current shebang, if not available use another + currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SCRIPTS")" + if [ ! -f "${currentshebang%% *}" ]; then + for shebang in "/command/with-contenv bashio" "/usr/bin/with-contenv bashio" "/usr/bin/env bashio" "/usr/bin/bashio" "/usr/bin/bash" "/usr/bin/sh" "/bin/bash" "/bin/sh"; do + command_path="${shebang%% *}" + if [ -x "$command_path" ] && "$command_path" echo "yes" >/dev/null 2>&1; then + echo "Valid shebang: $shebang" + break + fi + done + sed -i "s|$currentshebang|$shebang|g" "$SCRIPTS" + fi - # Use source to share env variables when requested - if [ "${ha_entry_source:-null}" = true ] && command -v "source" &>/dev/null; then - sed -i "s/(.*\s|^)exit ([0-9]+)/\1 return \2 || exit \2/g" "$SCRIPTS" - sed -i "s/bashio::exit.nok/return 1/g" "$SCRIPTS" - sed -i "s/bashio::exit.ok/return 0/g" "$SCRIPTS" - # shellcheck disable=SC1090 - source "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?" - else - "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?" - fi + # Use source to share env variables when requested + if [ "${ha_entry_source:-null}" = true ] && command -v "source" &>/dev/null; then + sed -i "s/(.*\s|^)exit \([0-9]\+\)/ \1 return \2 || exit \2/g" "$SCRIPTS" + sed -i "s/bashio::exit.nok/return 1/g" "$SCRIPTS" + sed -i "s/bashio::exit.ok/return 0/g" "$SCRIPTS" + # shellcheck disable=SC1090 + source "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?" + else + "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?" + fi - # Cleanup - rm "$SCRIPTS" + # Cleanup + rm "$SCRIPTS" +done +# Loop through /etc/services.d/*/run +[[ -d /etc/services.d ]] && for SERVICE in /etc/services.d/*/run; do + [ -e "$SERVICE" ] || continue + echo "$SERVICE: executing" + + # Check if run as root + if [ "$(id -u)" -eq 0 ]; then + chown "$(id -u)":"$(id -g)" "$SERVICE" + chmod a+x "$SERVICE" + else + echo -e "\e[38;5;214m$(date) WARNING: Script executed with user $(id -u):$(id -g), things can break and chown won't work\e[0m" + # Disable chown and chmod in scripts + sed -i "s/^chown /true # chown /g" "$SERVICE" + sed -i "s/ chown / true # chown /g" "$SERVICE" + sed -i "s/^chmod /true # chmod /g" "$SERVICE" + sed -i "s/ chmod / true # chmod /g" "$SERVICE" + fi + + # Get current shebang, if not available use another + currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SERVICE")" + if [ ! -f "${currentshebang%% *}" ]; then + for shebang in "/command/with-contenv bashio" "/usr/bin/with-contenv bashio" "/usr/bin/env bashio" "/usr/bin/bashio" "/usr/bin/bash" "/usr/bin/sh" "/bin/bash" "/bin/sh"; do + command_path="${shebang%% *}" + if [ -x "$command_path" ] && "$command_path" echo "yes" >/dev/null 2>&1; then + echo "Valid shebang: $shebang" + break + fi + done + sed -i "s|$currentshebang|$shebang|g" "$SERVICE" + fi + + # Use source to share env variables when requested + if [ "${ha_entry_source:-null}" = true ] && command -v "source" &>/dev/null; then + sed -i "s/(.*\s|^)exit \([0-9]\+\)/ \1 return \2 || exit \2/g" "$SERVICE" + sed -i "s/bashio::exit.nok/return 1/g" "$SERVICE" + sed -i "s/bashio::exit.ok/return 0/g" "$SERVICE" + # shellcheck disable=SC1090 + source "$SERVICE" || echo -e "\033[0;31mError\033[0m : $SERVICE exiting $?" + else + "$SERVICE" || echo -e "\033[0;31mError\033[0m : $SERVICE exiting $?" + fi + + # Cleanup + rm "$SERVICE" done ###################### @@ -59,41 +105,41 @@ done # If PID 1, keep alive and manage sigterm if [ "$$" -eq 1 ]; then - echo " " - echo -e "\033[0;32mEverything started!\033[0m" - terminate() { - echo "Termination signal received, forwarding to subprocesses..." - # Terminate all subprocesses - if command -v pgrep &>/dev/null; then - for pid in $(pgrep -P $$); do - echo "Terminating child PID $pid" - kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid" - done - else - # Fallback to iterating through /proc if pgrep is not available - for pid in /proc/[0-9]*/; do - pid=${pid#/proc/} - pid=${pid%/} - if [[ "$pid" -ne 1 ]] && grep -q "^PPid:\s*$$" "/proc/$pid/status" 2>/dev/null; then - echo "Terminating child PID $pid" - kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid" - fi - done - fi + echo " " + echo -e "\033[0;32mEverything started!\033[0m" + terminate() { + echo "Termination signal received, forwarding to subprocesses..." + # Terminate all subprocesses + if command -v pgrep &>/dev/null; then + for pid in $(pgrep -P $$); do + echo "Terminating child PID $pid" + kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid" + done + else + # Fallback to iterating through /proc if pgrep is not available + for pid in /proc/[0-9]*/; do + pid=${pid#/proc/} + pid=${pid%/} + if [[ "$pid" -ne 1 ]] && grep -q "^PPid:\s*$$" "/proc/$pid/status" 2>/dev/null; then + echo "Terminating child PID $pid" + kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid" + fi + done + fi - wait - echo "All subprocesses terminated. Exiting." - exit 0 - } - trap terminate SIGTERM SIGINT - while :; do - sleep infinity & - wait $! - done + wait + echo "All subprocesses terminated. Exiting." + exit 0 + } + trap terminate SIGTERM SIGINT + while :; do + sleep infinity & + wait $! + done else - echo " " - echo -e "\033[0;32mStarting the upstream container\033[0m" - echo " " - # Launch lsio mods - if [ -f /docker-mods ]; then exec /docker-mods; fi + echo " " + echo -e "\033[0;32mStarting the upstream container\033[0m" + echo " " + # Launch lsio mods + if [ -f /docker-mods ]; then exec /docker-mods; fi fi