interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / hub / forge-edge-rollback.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Undo forge-edge-apply.sh on the hub: restore the three edited files from the backup dir, |
| 3 | # remove the files the apply created, `nginx -t`, reload, check the static sites. |
| 4 | # Usage: sudo bash forge-edge-rollback.sh [/etc/nginx/forge-backups/<UTC>] (default: newest) |
| 5 | set -u |
| 6 | N=/etc/nginx |
| 7 | BK=${1:-$(ls -1d $N/forge-backups/*/ 2>/dev/null | sort | tail -1)} |
| 8 | BK=${BK%/} |
| 9 | [ "$(id -u)" = 0 ] || { echo "run with sudo" >&2; exit 1; } |
| 10 | [ -n "$BK" ] && [ -s "$BK/sites-enabled_interplane.io" ] && [ -s "$BK/sites-enabled_aienos.com" ] && [ -s "$BK/stream.d_sni-router.conf" ] \ |
| 11 | || { echo "ROLLBACK: no usable backup in '$BK'" >&2; exit 1; } |
| 12 | echo "rolling back from $BK" |
| 13 | cp -a "$BK/sites-enabled_interplane.io" $N/sites-enabled/interplane.io |
| 14 | cp -a "$BK/sites-enabled_aienos.com" $N/sites-enabled/aienos.com |
| 15 | cp -a "$BK/stream.d_sni-router.conf" $N/stream.d/sni-router.conf |
| 16 | while read -r f; do [ -n "$f" ] && rm -f -- "$f"; done < "$BK/NEW_FILES" |
| 17 | for f in conf.d/forge.conf snippets/forge-proxy.conf snippets/forge-headers.conf snippets/forge-archive.conf snippets/aienos-forge.conf; do |
| 18 | b="$BK/$(echo "$f" | tr / _)"; [ -e "$b" ] && cp -a "$b" "$N/$f" |
| 19 | done |
| 20 | nginx -t || { echo "ROLLBACK: nginx -t fails on restored files; nginx NOT reloaded (old config still running)" >&2; exit 1; } |
| 21 | ${FORGE_RELOAD:-systemctl reload nginx} # FORGE_RELOAD: only for the container rehearsal |
| 22 | sleep 2 |
| 23 | rc=0 |
| 24 | for u in https://interplane.io/ https://www.aienos.com/ https://hs.aienos.com/health; do |
| 25 | h=${u#https://}; h=${h%%/*} |
| 26 | c=$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 --resolve "$h:443:127.0.0.1" "$u") |
| 27 | echo "$c $u"; [ "$c" = 200 ] || rc=1 |
| 28 | done |
| 29 | [ $rc = 0 ] && echo "ROLLBACK OK" || echo "ROLLBACK: a site check failed (see above)" >&2 |
| 30 | exit $rc |