Always terminate the container if pihole-FTL binary exits. Either naturally or via an error. Don't attempt to restart it, allow the container's restart policy to do this.

Signed-off-by: Adam Warner <me@adamwarner.co.uk>
This commit is contained in:
Adam Warner
2024-10-06 22:39:04 +01:00
parent 89eb7f6b0d
commit 210a1172ff
2 changed files with 56 additions and 25 deletions

View File

@@ -202,25 +202,25 @@ setup_web_password() {
start_ftl() { start_ftl() {
echo " [i] pihole-FTL pre-start checks" echo " [i] pihole-FTL pre-start checks"
echo ""
# Remove possible leftovers from previous pihole-FTL processes # Remove possible leftovers from previous pihole-FTL processes
rm -f /dev/shm/FTL-* 2>/dev/null rm -f /dev/shm/FTL-* 2>/dev/null
rm -f /run/pihole/FTL.sock rm -f /run/pihole/FTL.sock
# Is /var/run/pihole used anymore? Or is this just a hangover from old container version
# /var/log sorted by running pihole-FTL-prestart.sh
# mkdir -p /var/run/pihole /var/log/pihole
# touch /var/log/pihole/FTL.log /var/log/pihole/pihole.log
# chown -R pihole:pihole /var/run/pihole /var/log/pihole /etc/pihole
fix_capabilities fix_capabilities
sh /opt/pihole/pihole-FTL-prestart.sh sh /opt/pihole/pihole-FTL-prestart.sh
echo " [i] Starting pihole-FTL ($FTL_CMD) as ${DNSMASQ_USER}" echo " [i] Starting pihole-FTL ($FTL_CMD) as ${DNSMASQ_USER}"
capsh --user=$DNSMASQ_USER --keep=1 -- -c "/usr/bin/pihole-FTL $FTL_CMD >/dev/null" &
echo "" echo ""
capsh --user=$DNSMASQ_USER --keep=1 -- -c "/usr/bin/pihole-FTL $FTL_CMD >/dev/null"
ftl_exit_code=$?
# Store the exit code so that we can exit the container with the same code from start.sh
echo $ftl_exit_code >/pihole-FTL.exit
# pihole-FTL has exited, so we should stop the rest of the container
killall --signal 15 start.sh
# Notes on above: # Notes on above:
# - DNSMASQ_USER default of pihole is in Dockerfile & can be overwritten by runtime container env # - DNSMASQ_USER default of pihole is in Dockerfile & can be overwritten by runtime container env
# - /var/log/pihole/pihole*.log has FTL's output that no-daemon would normally print in FG too # - /var/log/pihole/pihole*.log has FTL's output that no-daemon would normally print in FG too

View File

@@ -1,4 +1,4 @@
#!/bin/bash -e #!/bin/bash
if [ "${PH_VERBOSE:-0}" -gt 0 ]; then if [ "${PH_VERBOSE:-0}" -gt 0 ]; then
set -x set -x
@@ -53,11 +53,13 @@ start() {
#migrate Gravity Database if needed: #migrate Gravity Database if needed:
migrate_gravity migrate_gravity
# Start pihole-FTL # Start pihole-FTL in the background
start_ftl start_ftl &
# Give FTL a couple of seconds to start up # Wait until the log file exists before continuing
sleep 2 while [ ! -f /var/log/pihole/FTL.log ]; do
sleep 0.5
done
# If we are migrating from v5 to v6, we now need to run the basic configuration step that we deferred earlier # If we are migrating from v5 to v6, we now need to run the basic configuration step that we deferred earlier
# This is because pihole-FTL needs to migrate the config files before we can perform the basic configuration checks # This is because pihole-FTL needs to migrate the config files before we can perform the basic configuration checks
@@ -75,8 +77,8 @@ start() {
# Start tailing the FTL log from the most recent "FTL Started" message # Start tailing the FTL log from the most recent "FTL Started" message
# Get the line number # Get the line number
startFrom=$(grep -n '########## FTL started' /var/log/pihole/FTL.log | tail -1 | cut -d: -f1) startFrom=$(grep -n '########## FTL started' /var/log/pihole/FTL.log | tail -1 | cut -d: -f1)
# Start the tail from the line number # Start the tail from the line number and background it
tail -f -n +${startFrom} /var/log/pihole/FTL.log & tail --follow=name -n +${startFrom} /var/log/pihole/FTL.log &
else else
echo " [i] FTL log output is disabled. Remove the Environment variable TAIL_FTL_LOG, or set it to 1 to enable FTL log output." echo " [i] FTL log output is disabled. Remove the Environment variable TAIL_FTL_LOG, or set it to 1 to enable FTL log output."
fi fi
@@ -86,22 +88,51 @@ start() {
} }
stop() { stop() {
# Only attempt to close pihole-FTL if it is running, it may already have crashed
if pgrep pihole-FTL >/dev/null; then
echo ""
echo " [i] Container stop requested..."
echo " [i] pihole-FTL is running - Attempting to shut it down cleanly"
echo ""
# Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT # Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT
ftl_pid=$(pgrep pihole-FTL) ftl_pid=$(pgrep pihole-FTL)
killall --signal 15 pihole-FTL killall --signal 15 pihole-FTL
# Wait for pihole-FTL to exit # Wait for pihole-FTL to exit
while test -d /proc/"${ftl_pid}"; do while test -d /proc/"${ftl_pid}"; do
sleep 0.5 sleep 0.5
done done
fi
# If we are running pytest, keep the container alive for a little longer # Wait for a few seconds to allow the FTL log tail to catch up before exiting the container
# to allow the tests to complete sleep 2
# read the FTL exit code from the file created in the `start_ftl` function
FTL_EXIT_CODE=$(cat /pihole-FTL.exit)
rm /pihole-FTL.exit
# ensure the exit code is an integer, if not set it to 1
if ! [[ "${FTL_EXIT_CODE}" =~ ^[0-9]+$ ]]; then
FTL_EXIT_CODE=1
fi
echo ""
echo " [i] pihole-FTL exited with status $FTL_EXIT_CODE"
echo ""
echo " [i] Container will now stop or restart depending on your restart policy"
echo " https://docs.docker.com/engine/containers/start-containers-automatically/#use-a-restart-policy"
echo ""
# # If we are running pytest, keep the container alive for a little longer
# # to allow the tests to complete
if [[ ${PYTEST} ]]; then if [[ ${PYTEST} ]]; then
sleep 10 sleep 10
fi fi
exit exit ${FTL_EXIT_CODE}
} }
start start