[fix] proxy and certbot now exit properly with code 0

- also fixed (currently not used) saving log to file
This commit is contained in:
2020-05-23 08:06:05 -04:00
parent 76096a037d
commit 3d778ff698
2 changed files with 51 additions and 9 deletions

View File

@@ -40,11 +40,27 @@ function log()
echo "[$time]:[$prefix]:[$0][$func]: $@" 2>&1
;;
*)
echo "[$time]:[$prefix]:[$0][$func]: $@" >> log_path
echo "[$time]:[$prefix]:[$0][$func]: $@" >> $log_file
;;
esac
}
function sleep_wait()
{
# waiting on sleep to be able to handle SIGTERM
sleep $1 &
wait $!
}
function exit_handler()
{
log exit_handler "container shutting down"
exit
}
trap "exit_handler" SIGINT
trap "exit_handler" SIGTERM
function env_domain_file()
{
log env_domain_file $(echo "ENV DOMAINS: $DOMAINS" | tr '\n' '#' | sed 's|#|\\n |g')
@@ -135,7 +151,7 @@ function get_cert()
while [ ! $webserver_exists -eq 1 ] && [ "$retries" -lt "$server_retries" ]; do
retries=$(( retries+1 ))
log get_cert WARNING "Webserver for [$root_domain] not reachable, trying again in 1s"
sleep 1
sleep_wait 1
verify_local_webserver $root_domain && webserver_exists=1
done
@@ -256,7 +272,7 @@ function main()
log main "Ending intervall"
log main "Next interval in $intervall"
sleep "$intervall"
sleep_wait "$intervall"
done
}

View File

@@ -11,6 +11,7 @@ log_file="" # leave empty for stdout
# variables
domain_file="$temp_path/domains.txt"
certs_updated="$cert_root_path/.updated"
certs_daemon_pid=-1
#functions
function log()
@@ -35,11 +36,36 @@ function log()
echo "[$time]:[$prefix]:[$0][$func]: $@" 2>&1
;;
*)
echo "[$time]:[$prefix]:[$0][$func]: $@" >> log_path
echo "[$time]:[$prefix]:[$0][$func]: $@" >> $log_file
;;
esac
}
function sleep_wait()
{
# waiting on sleep to be able to handle SIGTERM
sleep $1 &
wait $!
}
function exit_handler()
{
# TODO: because nginx is started in foreground and replaces
# the shell via exec, this handler is never called, however as there
# is no cleanup for the certs_daemon it doesn't really matter
if [ $certs_daemon_pid -gt 0 ] ; then
log exit_handler "stopping certs_daemon"
kill -SIGTERM $certs_daemon_pid
fi
log exit_handler "shutting down nginx"
nginx -s quit
log exit_handler "container shutting down"
exit
}
trap "exit_handler" SIGINT
trap "exit_handler" SIGTERM
function var_to_file()
{
local var=$1
@@ -89,13 +115,13 @@ function certs_daemon()
# wait for nginx to start
while [ ! -f "$nginx_pid" ]; do
log certs_daemon "Waiting for nginx to start"
sleep 1
sleep_wait 1
done
# wait for inital cert update
while [ ! -f "$certs_updated" ]; do
log certs_daemon "Waiting for initial cert update"
sleep 1
sleep_wait 1
done
# reload nginx
@@ -107,6 +133,7 @@ function certs_daemon()
nginx -s reload
done
) &
certs_daemon_pid=$!
}
function nginx_handler()
@@ -121,13 +148,12 @@ function nginx_handler()
while ! read_file cert_exists "$domain_file" && [ "$retries" -lt "$server_retries" ]; do
log nginx_handler "Waiting for certs to be created"
retries=$(( retries+1 ))
sleep 1
sleep_wait 1
done
# starting nginx in foreground
log nginx_handler "Starting nginx in foreground"
nginx -g "daemon off;"
exec nginx -g "daemon off;"
}
function main()