|
| 1 | +#!/bin/sh |
| 2 | +# shellcheck disable=SC2039,SC2113 |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +print_usage() { |
| 7 | +cat << EOF |
| 8 | +usage: ${0} options |
| 9 | +
|
| 10 | +This script wraps certbot to obtain and renew certificates. |
| 11 | +
|
| 12 | +OPTIONS: |
| 13 | + Parameters: |
| 14 | + -d Domain for certificate. |
| 15 | + -e Administrator email used when obtaining certificates. |
| 16 | + -r Webroot location to place challenge auth files. |
| 17 | +
|
| 18 | +Version: ${VERSION} |
| 19 | +EOF |
| 20 | +} |
| 21 | + |
| 22 | +function check_for_cert { |
| 23 | + if [ -z "${1}" ] ; then |
| 24 | + echo "Missing parameter 1: Domain name" |
| 25 | + return 1 |
| 26 | + fi |
| 27 | + local cert_domain="${1}" |
| 28 | + |
| 29 | + local cert_dir="/etc/letsencrypt/live/${cert_domain}" |
| 30 | + |
| 31 | + echo "Check if certificate directory exists" |
| 32 | + [ -d "${cert_dir}" ] && return 0 || return 1 |
| 33 | +} |
| 34 | + |
| 35 | +function copy_certificates { |
| 36 | + if [ -z "${1}" ] ; then |
| 37 | + echo "Missing parameter 1: Domain name" |
| 38 | + return 1 |
| 39 | + fi |
| 40 | + local cert_domain="${1}" |
| 41 | + |
| 42 | + echo "Copy certificate files to /certs/ docker volume" |
| 43 | + cp -v /etc/letsencrypt/live/"${cert_domain}"/cert.pem /certs/"${cert_domain}".pem |
| 44 | + cp -v /etc/letsencrypt/live/"${cert_domain}"/privkey.pem /certs/"${cert_domain}".key.pem |
| 45 | + cp -v /etc/letsencrypt/live/"${cert_domain}"/chain.pem /certs/"${cert_domain}".chain.pem |
| 46 | + cp -v /etc/letsencrypt/live/"${cert_domain}"/fullchain.pem /certs/"${cert_domain}".fullchain.pem |
| 47 | +} |
| 48 | + |
| 49 | +function obtain_certificate { |
| 50 | + # Obtain certificate for provided domain |
| 51 | + if [ -z "${1}" ] ; then |
| 52 | + echo "Missing parameter 1: Admin email" |
| 53 | + return 1 |
| 54 | + fi |
| 55 | + local admin_email="${1}" |
| 56 | + |
| 57 | + if [ -z "${2}" ] ; then |
| 58 | + echo "Missing parameter 2: Certificate domain name" |
| 59 | + return 1 |
| 60 | + fi |
| 61 | + local cert_domain="${2}" |
| 62 | + |
| 63 | + if [ -z "${3}" ] ; then |
| 64 | + echo "Missing parameter 3: Webroot location" |
| 65 | + return 1 |
| 66 | + fi |
| 67 | + local cert_webroot="${3}" |
| 68 | + |
| 69 | + echo "Obtaining certificate for ${cert_domain}" |
| 70 | + certbot certonly \ |
| 71 | + --agree-tos \ |
| 72 | + --domain "${cert_domain}" \ |
| 73 | + --email "${admin_email}" \ |
| 74 | + --keep-until-expiring \ |
| 75 | + --max-log-backups 100 \ |
| 76 | + --non-interactive \ |
| 77 | + --renew-by-default \ |
| 78 | + --webroot -w "${cert_webroot}" |
| 79 | +} |
| 80 | + |
| 81 | +function renew_certificate { |
| 82 | + if [ -z "${1}" ] ; then |
| 83 | + echo "Missing parameter 1: Webroot location" |
| 84 | + return 1 |
| 85 | + fi |
| 86 | + local cert_webroot="${1}" |
| 87 | + |
| 88 | + echo "Renewing certificates registered on system." |
| 89 | + certbot renew \ |
| 90 | + --non-interactive \ |
| 91 | + --webroot -w "${cert_webroot}" |
| 92 | +} |
| 93 | + |
| 94 | +function process_certificates { |
| 95 | + if [ -z "${1}" ] ; then |
| 96 | + echo "Missing required parameter 1: Admin email" |
| 97 | + return 1 |
| 98 | + fi |
| 99 | + local admin_email="${1}" |
| 100 | + |
| 101 | + if [ -z "${2}" ] ; then |
| 102 | + echo "Missing required parameter 2: Certificate domain name" |
| 103 | + return 1 |
| 104 | + fi |
| 105 | + local cert_domain="${2}" |
| 106 | + |
| 107 | + if [ -z "${3}" ] ; then |
| 108 | + echo "Missing required parameter 3: Webroot location" |
| 109 | + return 1 |
| 110 | + fi |
| 111 | + local cert_webroot="${3}" |
| 112 | + |
| 113 | + if check_for_cert "${cert_domain}"; then |
| 114 | + renew_certificate "${cert_webroot}" |
| 115 | + else |
| 116 | + obtain_certificate "${admin_email}" "${cert_domain}" "${cert_webroot}" |
| 117 | + fi |
| 118 | + |
| 119 | + copy_certificates "${cert_domain}" |
| 120 | +} |
| 121 | + |
| 122 | +admin_email="${admin_email:-}" |
| 123 | +certificate_domain="${certificate_domain:-}" |
| 124 | +certificate_webroot="${certificate_webroot:-}" |
| 125 | +while getopts ":d:e:r:A:SD" opt; do |
| 126 | + case ${opt} in |
| 127 | + 'd') certificate_domain="${OPTARG}" ;; |
| 128 | + 'e') admin_email="${OPTARG}" ;; |
| 129 | + 'r') certificate_webroot="${OPTARG}" ;; |
| 130 | + '?') |
| 131 | + echo "Invalid option: -${OPTARG}" |
| 132 | + print_usage |
| 133 | + exit 0 |
| 134 | + ;; |
| 135 | + ':') |
| 136 | + echo "Missing option argument for -${OPTARG}" |
| 137 | + print_usage |
| 138 | + exit 0 |
| 139 | + ;; |
| 140 | + '*') # Anything else |
| 141 | + echo "Unknown error while processing options" |
| 142 | + print_usage |
| 143 | + exit 1 |
| 144 | + ;; |
| 145 | + esac |
| 146 | +done |
| 147 | + |
| 148 | +process_certificates "${admin_email}" "${certificate_domain}" "${certificate_webroot}" |
0 commit comments