Revert code but start services

This commit is contained in:
Alexandre
2025-07-31 15:13:55 +02:00
committed by GitHub
parent f999420048
commit 997dd934d4

View File

@@ -3,133 +3,86 @@
echo "Starting..." echo "Starting..."
####################
# Starting scripts #
####################
# Detect if this is PID1 (main container process) — do this once at the start # Detect if this is PID1 (main container process) — do this once at the start
PID1=false PID1=false
if [ "$$" -eq 1 ]; then if [ "$$" -eq 1 ]; then
PID1=true PID1=true
fi fi
# Choose a single shebang once and reuse it for all scripts (no re-testing per file) ####################
CHOSEN_SHEBANG_CACHE="/run/chosen_shebang" # Starting scripts #
if [ -r "$CHOSEN_SHEBANG_CACHE" ]; then ####################
CHOSEN_SHEBANG="$(cat "$CHOSEN_SHEBANG_CACHE")"
else
mkdir -p "$(dirname "$CHOSEN_SHEBANG_CACHE")" 2>/dev/null || true
candidate_shebangs=()
if $PID1; then
candidate_shebangs+=("/command/with-contenv bashio" "/usr/bin/with-contenv bashio")
fi
candidate_shebangs+=(
"/usr/bin/env bashio"
"/usr/bin/bashio"
"/usr/bin/bash"
"/usr/bin/sh"
"/bin/bash"
"/bin/sh"
)
CHOSEN_SHEBANG="" # Loop through /etc/cont-init.d/*
for shebang in "${candidate_shebangs[@]}"; do for SCRIPTS in /etc/cont-init.d/*; do
command_path="${shebang%% *}" [ -e "$SCRIPTS" ] || continue
if [ -x "$command_path" ]; then echo "$SCRIPTS: executing"
tmpfile="$(mktemp)"
printf '#!%s\nbashio::addon.version\n' "$shebang" > "$tmpfile"
chmod +x "$tmpfile"
if "$tmpfile" >/dev/null 2>&1; then
CHOSEN_SHEBANG="$shebang"
printf '%s\n' "$CHOSEN_SHEBANG" > "$CHOSEN_SHEBANG_CACHE"
rm -f "$tmpfile"
break
fi
rm -f "$tmpfile"
fi
done
if [ -z "${CHOSEN_SHEBANG:-}" ]; then
echo "WARNING: No valid shebang found that can run bashio::addon.version." >&2
fi
fi
run_script() {
runfile="$1"
script_kind="$2"
echo "$runfile: executing"
# Check if run as root # Check if run as root
if [ "$(id -u)" -eq 0 ]; then if [ "$(id -u)" -eq 0 ]; then
chown "$(id -u)":"$(id -g)" "$runfile" chown "$(id -u)":"$(id -g)" "$SCRIPTS"
chmod a+x "$runfile" chmod a+x "$SCRIPTS"
else 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" 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 # Disable chown and chmod in scripts
sed -i -E 's/^([[:space:]]*)chown /\1true # chown /' "$runfile" sed -i "s/^chown /true # chown /g" "$SCRIPTS"
sed -i -E 's/^([[:space:]]*)chmod /\1true # chmod /' "$runfile" 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 fi
# Replace s6-setuidgid with su-based equivalent # Get current shebang, if not available use another
if ! command -v s6-setuidgid >/dev/null 2>&1; then currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$SCRIPTS")"
sed -i -E 's|s6-setuidgid[[:space:]]+([a-zA-Z0-9._-]+)[[:space:]]+(.*)$|su -s /bin/bash \1 -c "\2"|g' "$runfile" if [ ! -f "${currentshebang%% *}" ]; then
fi 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%% *}"
# Apply the previously chosen shebang to this file (no candidate re-testing) if [ -x "$command_path" ] && "$command_path" echo "yes" >/dev/null 2>&1; then
if [ -n "${CHOSEN_SHEBANG:-}" ]; then echo "Valid shebang: $shebang"
currentshebang="$(sed -n '1{s/^#![[:blank:]]*//p;q}' "$runfile")" break
if [ -n "$currentshebang" ]; then
if [ "$currentshebang" != "$CHOSEN_SHEBANG" ]; then
sed -i "1s|^#!.*$|#!$CHOSEN_SHEBANG|" "$runfile"
fi fi
else done
sed -i "1i #!$CHOSEN_SHEBANG" "$runfile" sed -i "s|$currentshebang|$shebang|g" "$SCRIPTS"
fi
fi fi
# Use source to share env variables when requested # Use source to share env variables when requested
if [[ "$script_kind" == service ]]; then if [ "${ha_entry_source:-null}" = true ] && command -v "source" &>/dev/null; then
(exec "$runfile") & true 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 else
if [ "${ha_entry_source:-null}" = true ]; then "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?"
sed -Ei 's/(^|[[:space:]])exit ([0-9]+)/\1return \2 || exit \2/g' "$runfile"
sed -i "s/bashio::exit.nok/return 1/g" "$runfile"
sed -i "s/bashio::exit.ok/return 0/g" "$runfile"
# shellcheck disable=SC1090
source "$runfile" || echo -e "\033[0;31mError\033[0m : $runfile exiting $?"
else
"$runfile" || echo -e "\033[0;31mError\033[0m : $runfile exiting $?"
fi
fi fi
# Cleanup # Cleanup
if [[ "$script_kind" != service ]]; then rm "$SCRIPTS"
rm "$runfile"
fi
}
# Loop through /etc/cont-init.d/*
[[ -d /etc/cont-init.d ]] && \
for SCRIPTS in /etc/cont-init.d/*; do
[ -e "$SCRIPTS" ] || continue
run_script "$SCRIPTS" script
done done
[[ -d /etc/s6-overlay/s6-rc.d ]] && \
for SCRIPTS in /etc/s6-overlay/s6-rc.d/*/run; do
[ -e "$SCRIPTS" ] || continue
run_script "$SCRIPTS" script
done || true
# Start services.d # Start services.d
if $PID1; then if "$PID11"; then
for service_dir in /etc/services.d/*; do if [ "$(ls -A /etc/services.d/*/run)" ]; then
SCRIPTS="${service_dir}/run" for runfile in /etc/services.d/*/run; do
[ -e "$SCRIPTS" ] || continue if [[ -f "$runfile" ]]; then
run_script "$SCRIPTS" service echo "Starting: $runfile"
done # Replace s6-setuidgid with su-based equivalent
else sed -i -E 's|^s6-setuidgid[[:space:]]+([a-zA-Z0-9._-]+)[[:space:]]+(.*)$|su -s /bin/bash \1 -c "\2"|g' "$runfile"
echo "Not PID 1 — skipping service startup" chmod +x "$runfile"
( exec "$runfile" ) & true
fi
done
fi
if [ "$(ls -A /etc/s6-overlay/s6-rc.d/*/run)" ]; then
for runfile in /etc/s6-overlay/s6-rc.d/*/run; do
if [[ -f "$runfile" ]]; then
echo "Starting: $runfile"
# Replace s6-setuidgid with su-based equivalent
sed -i -E 's|^s6-setuidgid[[:space:]]+([a-zA-Z0-9._-]+)[[:space:]]+(.*)$|su -s /bin/bash \1 -c "\2"|g' "$runfile"
chmod +x "$runfile"
( exec "$runfile" ) & true
fi
done
fi
fi fi
###################### ######################
@@ -137,38 +90,38 @@ fi
###################### ######################
# If PID 1, keep alive and manage sigterm # If PID 1, keep alive and manage sigterm
if $PID1; then if "$PID11"; then
echo " " echo " "
echo -e "\033[0;32mEverything started!\033[0m" echo -e "\033[0;32mEverything started!\033[0m"
terminate() { terminate() {
echo "Termination signal received, forwarding to subprocesses..." echo "Termination signal received, forwarding to subprocesses..."
# Terminate all subprocesses # Terminate all subprocesses
if command -v pgrep &> /dev/null; then if command -v pgrep &>/dev/null; then
for pid in $(pgrep -P $$); do for pid in $(pgrep -P $$); do
echo "Terminating child PID $pid" echo "Terminating child PID $pid"
kill -TERM "$pid" 2> /dev/null || echo "Failed to terminate PID $pid" kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid"
done done
else else
# Fallback to iterating through /proc if pgrep is not available # Fallback to iterating through /proc if pgrep is not available
for pid in /proc/[0-9]*/; do for pid in /proc/[0-9]*/; do
pid=${pid#/proc/} pid=${pid#/proc/}
pid=${pid%/} pid=${pid%/}
if [[ "$pid" -ne 1 ]] && grep -q "^PPid:\s*$$" "/proc/$pid/status" 2> /dev/null; then if [[ "$pid" -ne 1 ]] && grep -q "^PPid:\s*$$" "/proc/$pid/status" 2>/dev/null; then
echo "Terminating child PID $pid" echo "Terminating child PID $pid"
kill -TERM "$pid" 2> /dev/null || echo "Failed to terminate PID $pid" kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid"
fi fi
done done
fi fi
kill -TERM -$$ 2>/dev/null || true
sleep 5
kill -KILL -$$ 2>/dev/null || true
wait wait
echo "All subprocesses terminated. Exiting." echo "All subprocesses terminated. Exiting."
exit 0 exit 0
} }
trap terminate SIGTERM SIGINT trap terminate SIGTERM SIGINT
wait -n while :; do
sleep infinity &
wait $!
done
else else
echo " " echo " "
echo -e "\033[0;32mStarting the upstream container\033[0m" echo -e "\033[0;32mStarting the upstream container\033[0m"