From 881320f87c414797595fd466d582db1d0710623d Mon Sep 17 00:00:00 2001 From: jorianvo Date: Mon, 27 Mar 2017 18:59:15 +0200 Subject: [PATCH] Add retry option to curl when uploading certificates (#17) * Update renewAndSendToProxy.sh Add a retry option in case curl fails * Update renewAndSendToProxy.sh * Update renewAndSendToProxy.sh Fix updating the wrong variable * Update renewAndSendToProxy.sh Move counter update to the end of the loop, so our counter logic is valid * Update renewAndSendToProxy.sh Add options to curl to hide the progress bar but show errors which can be helpful * Update renewAndSendToProxy.sh Move counter back to the beginning as this avoids any logic in the print statement * Update renewAndSendToProxy.sh Move sleep statement to avoid unnecessary sleep --- renewAndSendToProxy.sh | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/renewAndSendToProxy.sh b/renewAndSendToProxy.sh index 30288ba..e20aec0 100644 --- a/renewAndSendToProxy.sh +++ b/renewAndSendToProxy.sh @@ -5,6 +5,15 @@ RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' # No Color +#times we tried curl +TRIES=0 + +#maximum number of retries +MAXRETRIES=5 + +#timeout +TIMEOUT=5 + printf "${GREEN}Hello! renewAndSendToProxy runs. Today is $(date)${NC}\n" #full path is needed or it is not started when run as cron @@ -25,14 +34,29 @@ for d in /etc/letsencrypt/live/*/ ; do cat cert.pem chain.pem privkey.pem > $folder.combined.pem printf "${GREEN}generated $folder.combined.pem${NC}\n" - #send to proxy + #send to proxy, retry up to 5 times with a timeout of $TIMEOUT seconds printf "${GREEN}transmit $folder.combined.pem to $PROXY_ADDRESS${NC}\n" - curl -i -XPUT \ - --data-binary @$folder.combined.pem \ - "$PROXY_ADDRESS:8080/v1/docker-flow-proxy/cert?certName=$folder.combined.pem&distribute=true" > /var/log/dockeroutput.log + exitcode=0 + until [ $TRIES -ge $MAXRETRIES ] + do + TRIES=$[$TRIES+1] + curl --silent --show-error -i -XPUT \ + --data-binary @$folder.combined.pem \ + "$PROXY_ADDRESS:8080/v1/docker-flow-proxy/cert?certName=$folder.combined.pem&distribute=true" > /var/log/dockeroutput.log && break + exitcode=$? + + if [ $TRIES -eq $MAXRETRIES ]; then + printf "${RED}transmit failed after ${TRIES} attempts.${NC}\n" + else + printf "${RED}transmit failed, we try again in ${TIMEOUT} seconds.${NC}\n" + sleep $TIMEOUT + fi + done - printf "proxy received $folder.combined.pem\n" + if [ $exitcode -eq 0 ]; then + printf "proxy received $folder.combined.pem\n" + fi done