Skip to content

Commit 9983b7c

Browse files
committed
letsencrypt: notify of renewals
1 parent a0a31b4 commit 9983b7c

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

etc/ncp-config.d/letsencrypt.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
DOMAIN_=mycloud.ownyourbits.com
1111
EMAIL_=mycloud@ownyourbits.com
12+
NOTIFYUSER_=ncp
1213

1314
NCDIR=/var/www/nextcloud
1415
OCC="$NCDIR/occ"
@@ -53,32 +54,51 @@ configure()
5354
{
5455
local DOMAIN_LOWERCASE="${DOMAIN_,,}"
5556

57+
# Configure Apache
5658
grep -q ServerName $VHOSTCFG && \
5759
sed -i "s|ServerName .*|ServerName $DOMAIN_|" $VHOSTCFG || \
5860
sed -i "/DocumentRoot/aServerName $DOMAIN_" $VHOSTCFG
5961

62+
# Do it
6063
letsencrypt certonly -n --no-self-upgrade --webroot -w $NCDIR --hsts --agree-tos -m $EMAIL_ -d $DOMAIN_ && {
64+
65+
# Set up auto-renewal
6166
cat > /etc/cron.weekly/letsencrypt-ncp <<EOF
6267
#!/bin/bash
63-
/usr/bin/certbot renew --quiet
68+
69+
# renew and notify
70+
/usr/bin/certbot renew --quiet --renew-hook '
71+
sudo -u www-data php $OCC notification:generate \
72+
$NOTIFYUSER_ "SSL renewal" \
73+
-l "Your SSL certificate(s) \$RENEWED_DOMAINS has been renewed for another 90 days"
74+
'
75+
76+
# notify if fails
77+
[[ \$? -ne 0 ]] && sudo -u www-data php $OCC notification:generate \
78+
$NOTIFYUSER_ "SSL renewal error" \
79+
-l "SSL certificate renewal failed. See /var/log/letsencrypt/letsencrypt.log"
80+
81+
# cleanup
6482
rm -rf $NCDIR/.well-known
6583
EOF
6684
chmod +x /etc/cron.weekly/letsencrypt-ncp
6785

86+
# Configure Apache
6887
sed -i "s|SSLCertificateFile.*|SSLCertificateFile /etc/letsencrypt/live/$DOMAIN_LOWERCASE/fullchain.pem|" $VHOSTCFG
6988
sed -i "s|SSLCertificateKeyFile.*|SSLCertificateKeyFile /etc/letsencrypt/live/$DOMAIN_LOWERCASE/privkey.pem|" $VHOSTCFG
7089

7190
sed -i "s|SSLCertificateFile.*|SSLCertificateFile /etc/letsencrypt/live/$DOMAIN_LOWERCASE/fullchain.pem|" $VHOSTCFG2
7291
sed -i "s|SSLCertificateKeyFile.*|SSLCertificateKeyFile /etc/letsencrypt/live/$DOMAIN_LOWERCASE/privkey.pem|" $VHOSTCFG2
7392

93+
# Configure Nextcloud
7494
sudo -u www-data php $OCC config:system:set trusted_domains 4 --value=$DOMAIN_
7595
sudo -u www-data php $OCC config:system:set overwrite.cli.url --value=https://$DOMAIN_
7696

7797
# delayed in bg so it does not kill the connection, and we get AJAX response
7898
bash -c "sleep 2 && service apache2 reload" &>/dev/null &
7999
rm -rf $NCDIR/.well-known
80100

81-
# update configuration
101+
# Update configuration
82102
[[ "$DOCKERBUILD" == 1 ]] && update-rc.d letsencrypt enable
83103

84104
return 0

update.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,32 @@ cd /var/www/nextcloud
252252
sudo -u www-data php occ config:system:set trusted_domains 1 --value=$IP
253253
EOF
254254

255+
# letsencrypt: notify of renewals
256+
[[ -f /etc/cron.weekly/letsencrypt-ncp ]] && ! grep -q SSL /etc/cron.weekly/letsencrypt-ncp && {
257+
NCDIR=/var/www/nextcloud
258+
OCC="$NCDIR"/occ
259+
NOTIFYUSER_=ncp
260+
cat > /etc/cron.weekly/letsencrypt-ncp <<EOF
261+
#!/bin/bash
262+
263+
# renew and notify
264+
/usr/bin/certbot renew --quiet --renew-hook '
265+
sudo -u www-data php $OCC notification:generate \
266+
$NOTIFYUSER_ "SSL renewal" \
267+
-l "Your SSL certificate(s) \$RENEWED_DOMAINS has been renewed for another 90 days"
268+
'
269+
270+
# notify if fails
271+
[[ \$? -ne 0 ]] && sudo -u www-data php $OCC notification:generate \
272+
$NOTIFYUSER_ "SSL renewal error" \
273+
-l "SSL certificate renewal failed. See /var/log/letsencrypt/letsencrypt.log"
274+
275+
# cleanup
276+
rm -rf $NCDIR/.well-known
277+
EOF
278+
chmod +x /etc/cron.weekly/letsencrypt-ncp
279+
}
280+
255281
} # end - only live updates
256282

257283
exit 0

0 commit comments

Comments
 (0)