adferrand / dnsrobocert

Orchestrate Certbot and Lexicon together to provide Let's Encrypt TLS certificates validated by DNS challenges
https://dnsrobocert.readthedocs.io
MIT License
553 stars 90 forks source link

Running deploy hook before restarting the containers? #1182

Closed lriley2020 closed 7 months ago

lriley2020 commented 7 months ago

Hiya! I have a deploy hook written which copies the certificates from the /etc/letsencrypt/live/certname/... directory into a directory which my mail server (mailcow) uses. I then want to restart the affected mailcow docker containers. I've just been looking through the source code and it seems like the container restart gets run before the deploy hook - this would be a bit of in issue for me: https://github.com/adferrand/dnsrobocert/blob/a0ebdbb6e5ebdb9525f91c4e4ffd272f5143f6ae/src/dnsrobocert/core/hooks.py#L126-L137

Is there any way I can work around this, as I really need the certificates to copy before the containers restart! Can I write the docker container restart bit myself as part of the deploy hook file? Thanks in advance!

lriley2020 commented 7 months ago

Just tried restarting the containers from the deploy hook script - was very easy actually and everything seems to be working perfectly! Thanks so much for making this brilliant project :) Just posting my deploy hook here in case its useful for someone else:


### This script is only intended to be run as a post renewal hook by dnsrobocert! ###
### Running it directly will not work, as the necessary env vars will not be present for the cert copy to succeed! ###

LETSENCRYPT_BASE="/etc/letsencrypt/live"

latest_fullchain="$LETSENCRYPT_BASE/$DNSROBOCERT_CERTIFICATE_NAME/fullchain.pem"
latest_privkey="$LETSENCRYPT_BASE/$DNSROBOCERT_CERTIFICATE_NAME/privkey.pem"

echo "Latest fullchain found: " $latest_fullchain
echo "Latest privkey found: " $latest_privkey

echo "Mailcow certificate copy started..."

cp $latest_fullchain /mailcow-ssl/cert.pem

echo "Copying privkey to Mailcow..."
cp $latest_privkey /mailcow-ssl/key.pem

echo "Mailcow Certificate Copy finished..."

echo "Exiting, affected containers should now be restarted..."

postfix_c=$(docker ps -qaf name=postfix-mailcow)
dovecot_c=$(docker ps -qaf name=dovecot-mailcow)
nginx_c=$(docker ps -qaf name=nginx-mailcow)
docker restart ${postfix_c} ${dovecot_c} ${nginx_c}