From 96897f8e37ce062e085d30710c5525bc0e991d96 Mon Sep 17 00:00:00 2001 From: Bruno Zell Date: Fri, 28 Sep 2018 17:07:33 +0200 Subject: [PATCH] move interval management to a util funcion --- src/scripts/entrypoint.sh | 13 ++++--------- src/scripts/util.sh | 13 +++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/scripts/entrypoint.sh b/src/scripts/entrypoint.sh index 8885434..6d9d712 100644 --- a/src/scripts/entrypoint.sh +++ b/src/scripts/entrypoint.sh @@ -24,23 +24,18 @@ done echo "Done with startup" last_sync_file="/etc/letsencrypt/last_sync.txt" -one_week_sec=604800 # Instead of trying to run `cron` or something like that, just sleep and run `certbot`. while [ true ]; do - 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 [ ! -e "$last_sync_file" ] || [ $is_finshed_week_sec -lt 0 ]; then - # recreate the file + if [ is_sync_required $last_sync_file ]; then + # recreate the file to persist the last sync timestamp touch "$last_sync_file" # run certbot to request all the ssl certs we can find echo "Run certbot" /scripts/run_certbot.sh + else + echo "Not run certbot" fi # Sleep for 1 week diff --git a/src/scripts/util.sh b/src/scripts/util.sh index d2446ce..9bc775b 100644 --- a/src/scripts/util.sh +++ b/src/scripts/util.sh @@ -75,3 +75,16 @@ get_certificate() { $letsencrypt_url -d $1 --http-01-port 1337 \ --standalone --preferred-challenges http-01 --debug } + +is_sync_required() { + if [ ! -e "$1" ]; then + return true + fi + + one_week_sec=604800 + last_sync_sec=$(stat -c %Y "$1") + now_sec=$(date -d now +%s) + last_sync_delta_sec=$(( ($now_sec - $last_sync_sec) )) + is_finshed_week_sec=$(( ($one_week_sec - $last_sync_delta_sec) )) + return $is_finshed_week_sec -lt 0 +}