Files
gitea-migration/teardown_all.sh
S dc08375ad0 fix: address multiple bugs from code review
- teardown_all.sh: replace `yes |` pipeline with `< <(yes)` process
  substitution to avoid SIGPIPE (exit 141) false failures under pipefail
- phase6_teardown.sh: extract push mirror `.id` instead of `.remote_name`
  to match the DELETE /push_mirrors/{id} API contract
- phase5_migrate_pipelines.sh: expand sed regex from `[a-z_]*` to
  `[a-z_.]*` to handle nested GitHub contexts like
  `github.event.pull_request.number`
- lib/common.sh: render_template now requires explicit variable list to
  prevent envsubst from eating Nginx variables ($host, $proxy_add_...)
- backup scripts: remove MacBook relay, use direct Unraid↔Fedora SCP;
  fix dump path to write to /data/ (mounted volume) instead of /tmp/
  (container-only); add unzip -t integrity verification
- preflight.sh: add --skip-port-checks flag for resuming with
  --start-from (ports already bound by earlier phases)
- run_all.sh: update run_step to pass extra args; use --skip-port-checks
  when --start-from > 1
- post-checks (phase4/7/9): wrap API calls in helper functions with
  >/dev/null redirection instead of passing -o /dev/null as API data
- phase8: replace GitHub archiving with [MIRROR] description marking
  and disable wiki/projects/Pages (archived repos reject push mirrors)
- restore_to_primary.sh: add require_vars for Fedora SSH variables

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 20:18:35 -05:00

161 lines
5.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
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
# ---------------------------------------------------------------------------
# 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
# Feed unlimited 'y' responses via process substitution.
# A pipeline (yes | script) would break under pipefail: when the script
# finishes and closes stdin, `yes` gets SIGPIPE (exit 141), making the
# pipeline report failure even though the teardown succeeded.
# Process substitution avoids this — only the script's exit code matters.
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