151 lines
4.5 KiB
Bash
Executable File
151 lines
4.5 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
|
|
RUN_CLEANUP=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
|
|
;;
|
|
--cleanup) RUN_CLEANUP=true ;;
|
|
--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)
|
|
--cleanup Also run setup/cleanup.sh to uninstall setup prerequisites
|
|
--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") --cleanup Full teardown + uninstall prerequisites
|
|
$(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
|
|
|
|
# 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
|
|
# Pass --yes through to each teardown script so prompts are skipped.
|
|
if "${SCRIPT_DIR}/${script}" --yes; 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
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Optional: Run setup/cleanup.sh to uninstall prerequisites
|
|
# ---------------------------------------------------------------------------
|
|
if [[ "$RUN_CLEANUP" == "true" ]]; then
|
|
log_info ">>> Running setup cleanup (uninstalling prerequisites)..."
|
|
cleanup_args=()
|
|
if [[ "$AUTO_YES" == "true" ]]; then
|
|
cleanup_args+=(--yes)
|
|
fi
|
|
if "${SCRIPT_DIR}/setup/cleanup.sh" "${cleanup_args[@]}"; then
|
|
PASS=$((PASS + 1))
|
|
else
|
|
log_warn "Setup cleanup had issues (continuing)"
|
|
FAIL=$((FAIL + 1))
|
|
fi
|
|
printf '\n'
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 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
|