interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / spark / edge-http-rollback.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Undo spark/edge-http-apply.sh: restore the firewall file from the backup, load it, remove the files |
| 3 | # the apply added (NEW_FILES), `nginx -t`, reload nginx. Run as root on the Spark: |
| 4 | # sudo bash infra/spark/edge-http-rollback.sh /etc/nginx/forge-backups/http80-<UTC> |
| 5 | # With no argument it uses the newest http80-* backup. |
| 6 | set -uo pipefail |
| 7 | NFT=${EDGE_NFT:-/etc/atlas/ingress.nft} |
| 8 | BKROOT=${EDGE_BKROOT:-/etc/nginx/forge-backups} |
| 9 | BK=${1:-$(ls -1d "$BKROOT"/http80-* 2>/dev/null | sort | tail -n 1)} |
| 10 | die() { echo "edge-http-rollback: FAIL: $*" >&2; exit 1; } |
| 11 | [ "$(id -u)" = 0 ] || die "run as root (sudo)" |
| 12 | [ -n "$BK" ] && [ -f "$BK/ingress.nft" ] && [ -f "$BK/NEW_FILES" ] || die "no usable backup '$BK'" |
| 13 | nft -c -f "$BK/ingress.nft" || die "backup firewall file fails nft -c; nothing changed" |
| 14 | install -m 600 "$BK/ingress.nft" "$NFT" |
| 15 | nft -f "$NFT" || die "nft -f of the restored file failed" |
| 16 | while IFS= read -r f; do |
| 17 | [ -n "$f" ] || continue |
| 18 | case "$f" in /etc/nginx/sites-enabled/http-redirect|"${EDGE_SITE_DST:-/etc/nginx/sites-enabled/http-redirect}") rm -f -- "$f" ;; |
| 19 | *) echo "edge-http-rollback: not removing unexpected path $f" >&2 ;; esac |
| 20 | done < "$BK/NEW_FILES" |
| 21 | nginx -t -q 2>/dev/null || die "nginx -t fails after removing the redirect site" |
| 22 | # A failed reload must not be reported as success: the site file is already gone, but the |
| 23 | # running nginx may still be serving the redirect. |
| 24 | ${EDGE_RELOAD:-systemctl reload nginx} || die "nginx reload failed after the redirect site was removed; running nginx may not match disk" |
| 25 | cmp -s "$NFT" "$BK/ingress.nft" || die "firewall file differs from the backup after restore" |
| 26 | if nft list chain inet atlas_ingress http80_redirect > /dev/null 2>&1; then die "chain http80_redirect still live"; fi |
| 27 | echo "edge-http-rollback: restored $NFT from $BK (byte-identical), redirect site removed, nginx reloaded" |