item-atlas/deploy/public/install-api-certificate.sh

37 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
set -eu
umask 077
lineage=/etc/letsencrypt/live/api-atlas.tutudog.com
if [ -n "${RENEWED_LINEAGE:-}" ] && [ "$RENEWED_LINEAGE" != "$lineage" ]; then
exit 0
fi
ssl_root=${ATLAS_EDGE_SSL_ROOT:-/home/dongjf/dongjf-ng/ssl}
nginx=${ATLAS_EDGE_NGINX:-dongjf-nginx}
target="$ssl_root/api-atlas.tutudog.com"
install -d -m 0700 "$target"
cert_key=$(openssl x509 -in "$lineage/fullchain.pem" -pubkey -noout | openssl sha256)
private_key=$(openssl pkey -in "$lineage/privkey.pem" -pubout | openssl sha256)
test "$cert_key" = "$private_key"
openssl x509 -in "$lineage/fullchain.pem" -checkend 86400 -noout
release=$(mktemp -d "$target/cert.XXXXXX")
install -m 0644 "$lineage/fullchain.pem" "$release/fullchain.pem"
install -m 0600 "$lineage/privkey.pem" "$release/privkey.pem"
previous=$(readlink "$target/current" || true)
ln -s "$(basename "$release")" "$target/.current.$$"
mv -Tf "$target/.current.$$" "$target/current"
if docker exec "$nginx" nginx -t && docker exec "$nginx" nginx -s reload; then
exit 0
fi
if [ -n "$previous" ]; then
ln -s "$previous" "$target/.current.$$"
mv -Tf "$target/.current.$$" "$target/current"
else
mv "$target/current" "$target/failed.$$"
fi
exit 1