From 3d778ff6987b2108369cf51cfde99401371e408b Mon Sep 17 00:00:00 2001 From: Pascal Date: Sat, 23 May 2020 08:06:05 -0400 Subject: [PATCH] [fix] proxy and certbot now exit properly with code 0 - also fixed (currently not used) saving log to file --- certbot/files/entrypoint.sh | 22 ++++++++++++++++++--- proxy/files/entrypoint.sh | 38 +++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/certbot/files/entrypoint.sh b/certbot/files/entrypoint.sh index b0c0e69..b2e9809 100644 --- a/certbot/files/entrypoint.sh +++ b/certbot/files/entrypoint.sh @@ -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 } diff --git a/proxy/files/entrypoint.sh b/proxy/files/entrypoint.sh index b629cc0..26d5df3 100644 --- a/proxy/files/entrypoint.sh +++ b/proxy/files/entrypoint.sh @@ -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()