From e8f4c77ed713a0c727c0702ac649a6b1ec2aa642 Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Thu, 19 Jan 2017 18:17:56 -0800 Subject: [PATCH] More better error handling! --- Dockerfile | 2 +- scripts/entrypoint.sh | 7 +++++-- scripts/run_certbot.sh | 34 ++++++++++++++++++++++++++-------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index f47ab79..ba66392 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ COPY ./crontab /etc/cron.d/certbot RUN crontab /etc/cron.d/certbot COPY ./scripts/ /scripts -RUN chmod +x /scripts/run_certbot.sh +RUN chmod +x /scripts/*.sh ENTRYPOINT [] CMD ["/bin/bash", "/scripts/entrypoint.sh"] diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 6b45867..be8dbb4 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -1,3 +1,6 @@ #!/bin/bash -/bin/bash /scripts/run_certbot.sh -exec cron -f + +trap "exit" INT TERM +trap "kill 0" EXIT +/scripts/run_certbot.sh && cron -f & +wait diff --git a/scripts/run_certbot.sh b/scripts/run_certbot.sh index b933427..27d497b 100755 --- a/scripts/run_certbot.sh +++ b/scripts/run_certbot.sh @@ -1,4 +1,19 @@ -echo "Running certbot for domains $DOMAINS" +error() { + (set +x; tput -Tscreen bold + tput -Tscreen setaf 1 + echo $* + tput -Tscreen sgr0) >&2 +} + +if [ -z "$DOMAINS" ]; then + error "DOMAINS environment variable undefined; certbot will do nothing" + exit 1 +fi +if [ -z "$EMAIL" ]; then + error "EMAIL environment variable undefined; certbot will do nothing" + exit 1 +fi +echo "Running certbot for domains $DOMAINS for user $EMAIL..." get_certificate() { # Gets the certificate for the domain(s) CERT_DOMAINS (a comma separated list) @@ -15,16 +30,19 @@ get_certificate() { ec=$? echo "certbot exit code $ec" if [ $ec -eq 0 ]; then - echo "Certificates for $CERT_DOMAINS can be found in /etc/letsencrypt/live/$d" + error "Certificates for $CERT_DOMAINS can be found in /etc/letsencrypt/live/$d" else - echo "Cerbot failed for $CERT_DOMAINS. Check the logs for details." + error "Cerbot failed for $CERT_DOMAINS. Check the logs for details." + exit 1 fi } +exit_code=0 set -x -for d in $DOMAINS -do - CERT_DOMAINS=$d - get_certificate +for d in $DOMAINS; do + CERT_DOMAINS=$d + if ! get_certificate; then + exit_code=1 + fi done - +exit $exit_code