diff --git a/src/Dockerfile b/src/Dockerfile index a8b431f..93126d9 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -1,5 +1,5 @@ FROM nginx -MAINTAINER Elliot Saba +LABEL maintainer="Elliot Saba , Valder Gallo " VOLUME /etc/letsencrypt EXPOSE 80 diff --git a/src/scripts/entrypoint.sh b/src/scripts/entrypoint.sh index b6445bf..ba6f2c1 100644 --- a/src/scripts/entrypoint.sh +++ b/src/scripts/entrypoint.sh @@ -14,26 +14,47 @@ auto_enable_configs nginx -g "daemon off;" & export NGINX_PID=$! -# Next, run certbot to request all the ssl certs we can find -/scripts/run_certbot.sh - # Lastly, run startup scripts for f in /scripts/startup/*.sh; do - if [[ -x "$f" ]]; then + if [ -x "$f" ]; then echo "Running startup script $f" $f fi done echo "Done with startup" +last_sync_file="/etc/letsencrypt/last_sync.txt" + +if [ ! -e "$last_sync_file" ]; then + touch "$last_sync_file" + + # run certbot to request all the ssl certs we can find + echo "Run first time certbot" + /scripts/run_certbot.sh +fi + +one_week_sec=604800 + # Instead of trying to run `cron` or something like that, just leep and run `certbot`. while [ true ]; do # Sleep for 1 week - sleep 604800 & + sleep 604810 & SLEEP_PID=$! - # re-run certbot - /scripts/run_certbot.sh + last_sync_sec=$(stat -c %Y "$last_sync_file") + now_sec=$(date -d now +%s) + runned_sec=$(( ($now_sec - $last_sync_sec) )) + is_finshed_week_sec=$(( ($one_week_sec - $runned_sec) )) + + echo "Not run_certbot.sh" + if [ $is_finshed_week_sec -lt 0 ]; then + # recreate the file + touch "$last_sync_file" + + # re-run certbot + echo "Run certbot" + /scripts/run_certbot.sh + fi # Wait on sleep so that when we get ctrl-c'ed it kills everything due to our trap wait "$SLEEP_PID" diff --git a/src/scripts/register.sh b/src/scripts/register.sh new file mode 100644 index 0000000..61ab856 --- /dev/null +++ b/src/scripts/register.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +PRODUCTION_URL='https://acme-v01.api.letsencrypt.org/directory' +STAGING_URL='https://acme-staging.api.letsencrypt.org/directory' + +if [ "${IS_STAGING}" = "1" ]; then + letsencrypt_url=$STAGING_URL + echo "Staging ..." +else + letsencrypt_url=$PRODUCTION_URL + echo "Production ..." +fi + +echo "running certbot ... $letsencrypt_url $1 $2" +certbot certonly --agree-tos --keep -n --text --email $2 --server \ + $letsencrypt_url -d $1 --http-01-port 1337 \ + --standalone --preferred-challenges http-01 --debug diff --git a/src/scripts/util.sh b/src/scripts/util.sh index afe376e..d2446ce 100644 --- a/src/scripts/util.sh +++ b/src/scripts/util.sh @@ -59,7 +59,19 @@ auto_enable_configs() { # EMAIL environment variable, to register the proper support email address. get_certificate() { echo "Getting certificate for domain $1 on behalf of user $2" + PRODUCTION_URL='https://acme-v01.api.letsencrypt.org/directory' + STAGING_URL='https://acme-staging.api.letsencrypt.org/directory' + + if [ "${IS_STAGING}" = "1" ]; then + letsencrypt_url=$STAGING_URL + echo "Staging ..." + else + letsencrypt_url=$PRODUCTION_URL + echo "Production ..." + fi + + echo "running certbot ... $letsencrypt_url $1 $2" certbot certonly --agree-tos --keep -n --text --email $2 --server \ - https://acme-v02.api.letsencrypt.org/directory -d $1 --http-01-port 1337 \ - --standalone --standalone-supported-challenges http-01 --debug + $letsencrypt_url -d $1 --http-01-port 1337 \ + --standalone --preferred-challenges http-01 --debug }