Files
gitea-migration/teardown_all.sh
2026-02-26 15:33:51 -06:00

135 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# teardown_all.sh — Tear down migration in reverse order
# Runs phase teardown scripts from phase 9 → phase 1 (or a subset).
#
# Usage:
# ./teardown_all.sh # Tear down everything (phases 9 → 1)
# ./teardown_all.sh --through=5 # Tear down phases 9 → 5 (leave 1-4)
# ./teardown_all.sh --yes # Skip confirmation prompts
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/lib/common.sh"
# ---------------------------------------------------------------------------
# Parse arguments
# ---------------------------------------------------------------------------
THROUGH=1
AUTO_YES=false
for arg in "$@"; do
case "$arg" in
--through=*)
THROUGH="${arg#*=}"
if ! [[ "$THROUGH" =~ ^[0-9]+$ ]] || [[ "$THROUGH" -lt 1 ]] || [[ "$THROUGH" -gt 9 ]]; then
log_error "--through must be a number between 1 and 9"
exit 1
fi
;;
--yes|-y) AUTO_YES=true ;;
--help|-h)
cat <<EOF
Usage: $(basename "$0") [options]
Options:
--through=N Only tear down phases N through 9 (default: 1 = everything)
--yes, -y Skip all confirmation prompts
--help Show this help
Examples:
$(basename "$0") Tear down everything
$(basename "$0") --through=5 Tear down phases 5-9, leave 1-4
$(basename "$0") --yes Non-interactive teardown
EOF
exit 0 ;;
*) log_error "Unknown argument: $arg"; exit 1 ;;
esac
done
# ---------------------------------------------------------------------------
# Confirmation (unless --yes)
# ---------------------------------------------------------------------------
if [[ "$AUTO_YES" == "false" ]]; then
if [[ "$THROUGH" -eq 1 ]]; then
log_warn "This will tear down ALL phases (9 → 1)."
else
log_warn "This will tear down phases 9 → ${THROUGH}."
fi
printf 'Are you sure? [y/N] '
read -r confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
log_info "Teardown cancelled"
exit 0
fi
fi
# ---------------------------------------------------------------------------
# Export YES=true so individual teardown scripts skip their own prompts
# when --yes is passed. Each teardown script checks for interactive input,
# but when called from here with --yes, we pipe 'y' to them instead.
# ---------------------------------------------------------------------------
# Teardown scripts in reverse order (9 → 1)
# Each entry: phase_num|script_path
TEARDOWNS=(
"9|phase9_teardown.sh"
"8|phase8_teardown.sh"
"7|phase7_teardown.sh"
"6|phase6_teardown.sh"
"5|phase5_teardown.sh"
"4|phase4_teardown.sh"
"3|phase3_teardown.sh"
"2|phase2_teardown.sh"
"1|phase1_teardown.sh"
)
PASS=0
FAIL=0
for entry in "${TEARDOWNS[@]}"; do
IFS='|' read -r phase_num script <<< "$entry"
# Skip phases outside the teardown range
if [[ "$phase_num" -lt "$THROUGH" ]]; then
log_info "Skipping Phase ${phase_num} teardown (--through=${THROUGH})"
continue
fi
log_info ">>> Tearing down Phase ${phase_num}..."
if [[ "$AUTO_YES" == "true" ]]; then
# Pipe 'y' to all prompts in the teardown script
if echo "y" | "${SCRIPT_DIR}/${script}"; then
PASS=$((PASS + 1))
else
log_warn "Phase ${phase_num} teardown had issues (continuing)"
FAIL=$((FAIL + 1))
fi
else
# Run interactively — let user respond to each prompt
if "${SCRIPT_DIR}/${script}"; then
PASS=$((PASS + 1))
else
log_warn "Phase ${phase_num} teardown had issues (continuing)"
FAIL=$((FAIL + 1))
fi
fi
printf '\n'
done
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
printf '\n'
log_info "Teardown summary: ${PASS} succeeded, ${FAIL} had issues"
if [[ $FAIL -gt 0 ]]; then
log_warn "Some teardowns had issues — check logs above"
else
log_success "All teardowns completed successfully"
fi