Skip to content

Commit 02b37cd

Browse files
Merge pull request #16 from JonasAlfredsson/renew_checker_fix
Renew checker fix
2 parents 7a2e1a7 + 531f8b7 commit 02b37cd

File tree

6 files changed

+71
-36
lines changed

6 files changed

+71
-36
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ files).
4747
[Docker](https://www.docker.com/) to function.
4848

4949

50+
## Available Environment Variables
51+
52+
### Reuired
53+
- `CERTBOT_EMAIL`: Your e-mail address. Used by Let's Encrypt to contact you in
54+
case of security issues.
55+
56+
### Optional
57+
- `STAGING`: Set to `1` to use Let's Encrypt's
58+
[staging servers](#initial-testing) (default: `0`)
59+
- `DHPARAM_SIZE`: The size of the
60+
[Diffie-Hellman parameters](#diffie-hellman-parameters)
61+
(default: `2048`)
62+
- `RSA_KEY_SIZE`: The size of the RSA encryption keys (default: `2048`)
63+
- `RENEWAL_INTERVAL`: Time interval between certbot's
64+
[renewal checks](#renewal-check-interval) (default: `8d`)
65+
5066
## Run with `docker run`
5167

5268
### Build it yourself
@@ -103,6 +119,7 @@ services:
103119
- STAGING=0
104120
- DHPARAM_SIZE=2048
105121
- RSA_KEY_SIZE=2048
122+
- RENEWAL_INTERVAL=8d
106123
ports:
107124
- 80:80
108125
- 443:443
@@ -193,6 +210,36 @@ certificate request from the above file will then become something like this
193210
certbot ... -d yourdomain.org -d www.yourdomain.org -d sub.yourdomain.org
194211
```
195212

213+
### Renewal check interval
214+
This container will automatically start a certbot certificate renewal check
215+
after the time duration that is defined in the environmental variable
216+
`RENEWAL_INTERVAL` has passed. After certbot has done its stuff, the code will
217+
return and wait the defined time before triggering again.
218+
219+
This process is very simple, and is just a `while [ true ];` loop with a `sleep`
220+
at the end:
221+
222+
```bash
223+
while [ true ]; do
224+
# Run certbot...
225+
sleep "$RENEWAL_INTERVAL"
226+
done
227+
```
228+
229+
So when setting the environmental variable, it is possible to use any string
230+
that is recognized by `sleep`, e.g. `3600` or `60m` or `1h`. Read more about
231+
which values that are allowed in its
232+
[manual](http://man7.org/linux/man-pages/man1/sleep.1.html).
233+
234+
The default is `8d`, since this allows for multiple retries per month, while
235+
keeping the output in the logs at a very low level. If nothing needs to be
236+
renewed certbot won't do anything, so it should be no problem setting it lower
237+
if you want to. The only thing to think about is to not to make it longer than
238+
one month, because then you would
239+
[miss the window](https://community.letsencrypt.org/t/solved-how-often-to-renew/13678)
240+
where certbot would deem it necessary to update the certificates.
241+
242+
196243
### Diffie-Hellman parameters
197244
Regarding the Diffie-Hellman parameter it is recommended that you have one for
198245
your server. However, you can make a config file without it and Nginx will work

example/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
- STAGING
1010
- DHPARAM_SIZE
1111
- RSA_KEY_SIZE
12+
- RENEWAL_INTERVAL
1213
ports:
1314
- 80:80
1415
- 443:443

src/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ [email protected]
55
STAGING=0
66
DHPARAM_SIZE=2048
77
RSA_KEY_SIZE=2048
8+
RENEWAL_INTERVAL=8d

src/scripts/entrypoint.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ echo "Starting the Nginx service"
2525
nginx -g "daemon off;" &
2626
NGINX_PID=$!
2727

28+
# Make sure a renewal interval is set before continuing.
29+
if [ -z "$RENEWAL_INTERVAL" ]; then
30+
echo "RENEWAL_INTERVAL unset, using default of '8d'"
31+
RENEWAL_INTERVAL='8d'
32+
fi
33+
2834
# Instead of trying to run 'cron' or something like that, just sleep and
2935
# execute the 'certbot' script.
3036
(
@@ -33,8 +39,8 @@ while [ true ]; do
3339
echo "Run certbot!"
3440
/scripts/run_certbot.sh
3541

36-
echo "Certbot will now sleep for 8 days..."
37-
sleep 8d
42+
echo "Certbot will now sleep..."
43+
sleep "$RENEWAL_INTERVAL"
3844
done
3945
) &
4046

src/scripts/run_certbot.sh

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@ exit_code=0
1414
# the certificate request.
1515
for conf_file in /etc/nginx/conf.d/*.conf*; do
1616
for primary_domain in $(parse_primary_domains $conf_file); do
17-
if is_renewal_required $primary_domain; then
18-
# Renewal required for this domain!
19-
# The last one happened over a week ago (or never)
20-
21-
# At minimum we will make a request for the primary domain
22-
domain_request="-d $primary_domain"
23-
24-
# Find all 'server_names' in this .conf file
25-
for server_name in $(parse_server_names $conf_file); do
26-
domain_request="$domain_request -d $server_name"
27-
done
28-
29-
if ! get_certificate $primary_domain $CERTBOT_EMAIL "$domain_request"; then
30-
error "Certbot failed for $primary_domain. Check the logs for details."
31-
exit_code=1
32-
fi
33-
else
34-
echo "Not running certbot for $primary_domain; last renewal happened just recently."
17+
# At minimum we will make a request for the primary domain.
18+
domain_request="-d $primary_domain"
19+
20+
# Find all 'server_names' in this .conf file and add them to the same
21+
# request.
22+
for server_name in $(parse_server_names $conf_file); do
23+
domain_request="$domain_request -d $server_name"
24+
done
25+
26+
# Hand over all the info required for the certificate request, and let
27+
# certbot decide if it is necessary to update the certificate.
28+
if ! get_certificate $primary_domain $CERTBOT_EMAIL "$domain_request"; then
29+
error "Certbot failed for $primary_domain. Check the logs for details."
30+
exit_code=1
3531
fi
3632
done
3733
done

src/scripts/util.sh

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,3 @@ get_certificate() {
161161
$3 \
162162
--debug
163163
}
164-
165-
# Given a domain name, return true if a renewal is required (last renewal
166-
# ran over a week ago or never happened yet), otherwise return false.
167-
is_renewal_required() {
168-
# If the file does not exist assume a renewal is required
169-
last_renewal_file="/etc/letsencrypt/live/$1/privkey.pem"
170-
[ ! -e "$last_renewal_file" ] && return;
171-
172-
# If the file exists, check if the last renewal was more than a week ago
173-
one_week_sec=604800
174-
now_sec=$(date -d now +%s)
175-
last_renewal_sec=$(stat -c %Y "$last_renewal_file")
176-
last_renewal_delta_sec=$(( ($now_sec - $last_renewal_sec) ))
177-
is_finshed_week_sec=$(( ($one_week_sec - $last_renewal_delta_sec) ))
178-
[ $is_finshed_week_sec -lt 0 ]
179-
}

0 commit comments

Comments
 (0)