Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retry failed ssl renewal with jittered backoff 🐛 #349

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ansible/roles/letsencrypt/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---

dummy_certs: false

vars_ssl_renew_number_of_retries: 10
vars_ssl_renew_retry_delay: 5
52 changes: 42 additions & 10 deletions ansible/roles/letsencrypt/templates/renew-ssl-certs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,47 @@ rm -rf "/etc/ssl/letsencrypt/accounts/localhost_14000/{{ email }}"
server="https://acme-v02.api.letsencrypt.org/directory"
{% endif %}

lego --email "{{ email }}" \
--server "${server}" \
--accept-tos \
--path /etc/ssl/letsencrypt \
--http \
${renew_kind} \
{% for domain in domains -%}
-d "{{ domain }}" \
{% endfor -%}
${action}
retries="{{ vars_ssl_renew_number_of_retries }}"
wait_time="{{ vars_ssl_renew_retry_delay }}"

lego_cmd="lego --email '{{ email }}' \
--server '${server}' \
--accept-tos \
--path /etc/ssl/letsencrypt \
--http \
${renew_kind} \
{% for domain in domains -%}
-d '{{ domain }}' \
{% endfor -%}
${action}"

function run_with_retries {
command=$1
local i=0
while true; do
${command}
exit_code=$?

if [ ${exit_code} -eq 0 ]; then
break
fi

if [ ${i} -ge ${retries} ]; then
exit ${exit_code}
fi

jitter=$(($RANDOM % 10))
wait_time_with_jitter=$((${wait_time} + ${jitter}))

echo "Command failed with exit code ${exit_code}. Retrying in ${wait_time_with_jitter} seconds..."
sleep ${wait_time_with_jitter}

i=$(($i + 1))
done
}

set +e
run_with_retries $lego_cmd
set -e

sudo /etc/ssl/letsencrypt/after-renew