|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +domains=(${OSPOS_DOMAIN_NAME}) |
| 4 | +rsa_key_size=4096 |
| 5 | +data_path="./data/certbot" |
| 6 | +email="${OSPOS_CONTACT_EMAIL}" |
| 7 | +staging=${OSPOS_STAGING} |
| 8 | + |
| 9 | +if [ -d "$data_path" ]; then |
| 10 | + read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision |
| 11 | + if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then |
| 12 | + exit |
| 13 | + fi |
| 14 | +fi |
| 15 | + |
| 16 | + |
| 17 | +if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then |
| 18 | + echo "### Downloading recommended TLS parameters ..." |
| 19 | + mkdir -p "$data_path/conf" |
| 20 | + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf" |
| 21 | + curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem" |
| 22 | + echo |
| 23 | +fi |
| 24 | + |
| 25 | +echo "### Creating dummy certificate for $domains ..." |
| 26 | +path="/etc/letsencrypt/live/$domains" |
| 27 | +mkdir -p "$data_path/conf/live/$domains" |
| 28 | +docker-compose run --rm --entrypoint "\ |
| 29 | + openssl req -x509 -nodes -newkey rsa:1024 -days 1\ |
| 30 | + -keyout '$path/privkey.pem' \ |
| 31 | + -out '$path/fullchain.pem' \ |
| 32 | + -subj '/CN=localhost'" certbot |
| 33 | +echo |
| 34 | + |
| 35 | + |
| 36 | +echo "### Starting nginx ..." |
| 37 | +docker-compose up --force-recreate -d nginx |
| 38 | +echo |
| 39 | + |
| 40 | +echo "### Deleting dummy certificate for $domains ..." |
| 41 | +docker-compose run --rm --entrypoint "\ |
| 42 | + rm -Rf /etc/letsencrypt/live/$domains && \ |
| 43 | + rm -Rf /etc/letsencrypt/archive/$domains && \ |
| 44 | + rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot |
| 45 | +echo |
| 46 | + |
| 47 | + |
| 48 | +echo "### Requesting Let's Encrypt certificate for $domains ..." |
| 49 | +#Join $domains to -d args |
| 50 | +domain_args="" |
| 51 | +for domain in "${domains[@]}"; do |
| 52 | + domain_args="$domain_args -d $domain" |
| 53 | +done |
| 54 | + |
| 55 | +# Select appropriate email arg |
| 56 | +case "$email" in |
| 57 | + "") email_arg="--register-unsafely-without-email" ;; |
| 58 | + *) email_arg="--email $email" ;; |
| 59 | +esac |
| 60 | + |
| 61 | +# Enable staging mode if needed |
| 62 | +if [ $staging != "0" ]; then staging_arg="--staging"; fi |
| 63 | + |
| 64 | +docker-compose run --rm --entrypoint "\ |
| 65 | + certbot certonly --webroot -w /var/www/certbot \ |
| 66 | + $staging_arg \ |
| 67 | + $email_arg \ |
| 68 | + $domain_args \ |
| 69 | + --rsa-key-size $rsa_key_size \ |
| 70 | + --agree-tos \ |
| 71 | + --force-renewal" certbot |
| 72 | +echo |
| 73 | + |
| 74 | +echo "### Reloading nginx ..." |
| 75 | +docker-compose exec nginx nginx -s reload |
0 commit comments