@@ -15,18 +15,19 @@ trap cleanup EXIT
1515IFS=' ' read -r -a domains <<< " ${DOMAINS}"
1616LEGOPATH=/data
1717export HOME=/root
18- SERVER=${SERVER:- https:// acme-v01.api.letsencrypt.org/ directory}
18+ ACME_SERVER=${ACME_SERVER:- https:// acme-v01.api.letsencrypt.org/ directory}
19+ ACME_DAYS=${ACME_DAYS:- 30}
1920
2021verify_preconditions () {
21- [ ! -z " ${SERVER } " ] && [ ! -z " ${EMAIL } " ] && [ ! -z " ${DNS_PROVIDER} " ] && verify_domains_in_platformsh
22+ [ ! -z " ${ACME_SERVER } " ] && [ ! -z " ${ACME_EMAIL } " ] && [ ! -z " ${DNS_PROVIDER} " ] && verify_domains_in_platformsh
2223}
2324
2425verify_domains_in_platformsh () {
2526 local status=0
2627
2728 for i in " ${! domains[@]} "
2829 do
29- platform domain:get --yes --project=" ${PLATFORMSH_PROJECT_ID} " " ${domains[i]} "
30+ platform domain:get --no --project=" ${PLATFORMSH_PROJECT_ID} " " ${domains[i]} "
3031 local err=$?
3132 status=$(( ${err} | ${status} ))
3233 done
@@ -53,12 +54,12 @@ domain_exists () {
5354
5455create_domain () {
5556 local domain=$1
56- lego --domains=${domain} --server=${SERVER } --email=${EMAIL } --accept-tos --path=${LEGOPATH} --dns=${DNS_PROVIDER} run
57+ lego --domains=${domain} --server=${ACME_SERVER } --email=${ACME_EMAIL } --accept-tos --path=${LEGOPATH} --dns=${DNS_PROVIDER} run
5758}
5859
5960renew_domain () {
6061 local domain=$1
61- lego --domains=${domain} --server=${SERVER } --email=${EMAIL } --accept-tos --path=${LEGOPATH} --dns=${DNS_PROVIDER} renew --days=60
62+ lego --domains=${domain} --server=${ACME_SERVER } --email=${ACME_EMAIL } --accept-tos --path=${LEGOPATH} --dns=${DNS_PROVIDER} renew --days=${ACME_DAYS}
6263}
6364
6465upload_certificates () {
@@ -76,7 +77,19 @@ upload_certificate () {
7677 local cert=${TMPDIR} /cert-01
7778 local key=${LEGOPATH} /certificates/${domain} .key
7879 local chain=${TMPDIR} /cert-02
79- platform domain:update --yes --cert=${cert} --key=${key} --chain=${chain} --project=" ${PLATFORMSH_PROJECT_ID} " " ${domain} "
80+ local current=${TMPDIR} /current
81+
82+ # Compare certificate to current certificate at platform.sh. Only upload if they are different.
83+
84+ # We allow the following commands to fail because there might not be a current certificate.
85+ set -x
86+ platform domain:get --no --project=" ${PLATFORMSH_PROJECT_ID} " --property=ssl " ${domain} " | shyaml get-value certificate > " ${current} "
87+
88+ if [ " $( openssl x509 -in " ${cert} " -noout -fingerprint) " != " $( openssl x509 -in " ${current} " -noout -fingerprint) " ]; then
89+ platform domain:update --no --cert=${cert} --key=${key} --chain=${chain} --project=" ${PLATFORMSH_PROJECT_ID} " " ${domain} "
90+ fi
91+
92+ set +x
8093}
8194
8295verify_preconditions && create_or_renew_domains && upload_certificates
0 commit comments