Skip to content

Commit

Permalink
Improve error detection, add manual script, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBrones committed Jan 21, 2022
1 parent a3758d8 commit a7f7da0
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 13 deletions.
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
FROM ubuntu:20.04
FROM ubuntu:latest
#FROM certbot/certbot

# Install cron
# Install cron, certbot, curl and msmtp for sending mail
RUN apt-get update && apt-get install -y certbot cron msmtp curl
RUN rm -rf /var/lib/apt/list

# Add files
# Add scripts
ADD run.sh /run.sh
ADD runmanual.sh /runmanual.sh
ADD entrypoint.sh /entrypoint.sh

RUN chmod +x /run.sh /entrypoint.sh
RUN chmod +x /run.sh /runmanual.sh /entrypoint.sh
RUN mkdir /output/

ENTRYPOINT /entrypoint.sh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Certbot-Runner
Release 1.0
Release 1.1

Certbot in a docker container that runs a http server to use as a reverse-proxy backend.
This container needs to catch the challange from Let's Encrypt so a rule on the reverse-proxy is required.
Expand Down
14 changes: 14 additions & 0 deletions drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ steps:
- latest
when:
branch: [ main ]
event: [ push ]
- name: docker-dev
image: plugins/docker
settings:
username:
from_secret: DOCKERUSERNAME
password:
from_secret: DOCKERPASSWORD
repo: thebrones/certbot-runner
#auto_tag: true Not working
tags:
- dev
when:
branch: [ dev ]
event: [ push ]
19 changes: 11 additions & 8 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
# Probaly the worst script you have ever seen, feel free to improve :-)
# Probably the worst script you have ever seen, feel free to improve :-)
currentDate=`date`
ERROR=0
echo
echo
echo "---------- Job started on $currentDate ----------"
Expand All @@ -25,30 +26,32 @@ do
chown $RIGHTS /output/$SITE.pem
else
echo -e "\e[1;31mError finding certificate!\e[0m"
RESULT=1
ERROR=1
fi

else
echo -e "\e[1;31mError obtaining certificate!\e[0m"
ERROR=1
fi
done
echo '--------'
echo
echo "Finished requesting certificates"
echo "Execute configured action: $ACTION"
$ACTION
echo '--------'

# Determine exit code
if [ $RESULT -eq 0 ]; then
if [ $ERROR -eq 0 ]; then
# Actions
echo "Run looks good"
echo
echo "Execute configured action: $ACTION"
$ACTION
echo "No errors detected"
echo -e "Subject: Certbot-runner succesfuly requested one or more certificate(s)\n\Certbot-runner was succesful requesting certificate(s), see the logs for details." | msmtp $EMAIL
echo '--------'
echo "End of run"
exit 0
else
# This command adds a / in the body of the E-mail, how to solve?
echo "Something went wrong, sending E-mail!"
echo "Something went wrong, sending E-mail to warn!"
echo -e "Subject: Certbot-runner encountered an error while renewing one or more certificate(s)\n\Certbot-runner encountered an error while renewing one or more certificate(s), see the logs for details." | msmtp $EMAIL
echo '--------'
echo "End of run"
Expand Down
57 changes: 57 additions & 0 deletions runmanual.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash
# Probably the worst script you have ever seen, feel free to improve :-)
currentDate=`date`

# Load config
. /settings.conf

echo
echo "Please enter domain to request certificate"
read SITE

echo "---------- Manual job started on $currentDate ----------"

# Re-use code in loop:
echo ------------------- ${SITE} -------------------
certbot certonly --standalone --preferred-challenges http --http-01-port 80 --renew-by-default --non-interactive --email $EMAIL --rsa-key-size 4096 $TOS -d $SITE
# Check for errors
RESULT=$?
if [ $RESULT -eq 0 ]; then
echo Succesfully requested certificate.

# Check if folder and certificate files exist. (Does not mean that the last run was succesful) && [ -d "/etc/letsencrypt/live/$SITE/fullchain.pem" ] && [ -d "/etc/letsencrypt/live/$SITE/privkey.pem" ];
if [ -d "/etc/letsencrypt/live/$SITE/" ]; then
# Cat files to make combined .pem files in the output folder.
cat /etc/letsencrypt/live/$SITE/fullchain.pem /etc/letsencrypt/live/$SITE/privkey.pem > /output/$SITE.pem
chown $RIGHTS /output/$SITE.pem
else
echo -e "\e[1;31mError finding certificate!\e[0m"
fi

else
echo -e "\e[1;31mError obtaining certificate!\e[0m"
fi

echo '--------'
echo
echo "Finished requesting certificate manualy"

echo "Add domain to the domains-file for automatic renewal? y/n"
read RUNADD

if [ $RUNADD == "y" ]; then
echo "Adding '$SITE' to domain file"
echo $SITE >> /domains.conf
fi

echo "Run Actions? y/n"
read RUNACTIONS

if [ $RUNACTIONS == "y" ]; then
echo
echo "Execute configured action: $ACTION"
$ACTION
echo '--------'
echo "End of run"
fi
exit 0

0 comments on commit a7f7da0

Please sign in to comment.