Skip to content

Commit 881320f

Browse files
jorianvohamburml
authored andcommitted
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
1 parent c039523 commit 881320f

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

renewAndSendToProxy.sh

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ RED='\033[0;31m'
55
GREEN='\033[0;32m'
66
NC='\033[0m' # No Color
77

8+
#times we tried curl
9+
TRIES=0
10+
11+
#maximum number of retries
12+
MAXRETRIES=5
13+
14+
#timeout
15+
TIMEOUT=5
16+
817
printf "${GREEN}Hello! renewAndSendToProxy runs. Today is $(date)${NC}\n"
918

1019
#full path is needed or it is not started when run as cron
@@ -25,14 +34,29 @@ for d in /etc/letsencrypt/live/*/ ; do
2534
cat cert.pem chain.pem privkey.pem > $folder.combined.pem
2635
printf "${GREEN}generated $folder.combined.pem${NC}\n"
2736

28-
#send to proxy
37+
#send to proxy, retry up to 5 times with a timeout of $TIMEOUT seconds
2938
printf "${GREEN}transmit $folder.combined.pem to $PROXY_ADDRESS${NC}\n"
3039

31-
curl -i -XPUT \
32-
--data-binary @$folder.combined.pem \
33-
"$PROXY_ADDRESS:8080/v1/docker-flow-proxy/cert?certName=$folder.combined.pem&distribute=true" > /var/log/dockeroutput.log
40+
exitcode=0
41+
until [ $TRIES -ge $MAXRETRIES ]
42+
do
43+
TRIES=$[$TRIES+1]
44+
curl --silent --show-error -i -XPUT \
45+
--data-binary @$folder.combined.pem \
46+
"$PROXY_ADDRESS:8080/v1/docker-flow-proxy/cert?certName=$folder.combined.pem&distribute=true" > /var/log/dockeroutput.log && break
47+
exitcode=$?
48+
49+
if [ $TRIES -eq $MAXRETRIES ]; then
50+
printf "${RED}transmit failed after ${TRIES} attempts.${NC}\n"
51+
else
52+
printf "${RED}transmit failed, we try again in ${TIMEOUT} seconds.${NC}\n"
53+
sleep $TIMEOUT
54+
fi
55+
done
3456

35-
printf "proxy received $folder.combined.pem\n"
57+
if [ $exitcode -eq 0 ]; then
58+
printf "proxy received $folder.combined.pem\n"
59+
fi
3660

3761
done
3862

0 commit comments

Comments
 (0)