Files
gitea-migration/phase2_teardown.sh

154 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# phase2_teardown.sh — Tear down Gitea on Fedora
# Destructive: stops container, optionally removes all data.
# Prompts for confirmation before each destructive action.
# Safe to run against an already-torn-down instance (no errors).
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/lib/common.sh"
# Parse arguments
AUTO_YES=false
for arg in "$@"; do
case "$arg" in
--yes|-y) AUTO_YES=true ;;
--help|-h)
cat <<EOF
Usage: $(basename "$0") [options]
Options:
--yes, -y Skip all confirmation prompts
--help, -h Show this help
EOF
exit 0
;;
*)
log_error "Unknown argument: $arg"
exit 1
;;
esac
done
confirm_action() {
local prompt="$1"
if [[ "$AUTO_YES" == "true" ]]; then
log_info "Auto-confirmed (--yes): ${prompt}"
return 0
fi
printf '%s' "$prompt"
read -r confirm
[[ "$confirm" =~ ^[Yy]$ ]]
}
load_env
require_vars FEDORA_IP FEDORA_SSH_USER FEDORA_GITEA_DATA_PATH FEDORA_COMPOSE_DIR
DATA_PATH="$FEDORA_GITEA_DATA_PATH"
COMPOSE_DIR="${FEDORA_COMPOSE_DIR}/gitea"
log_warn "=== Phase 2 Teardown: Gitea on Fedora ==="
# Track whether teardown encountered operational errors (not user skips).
TEARDOWN_FAILED=false
# ---------------------------------------------------------------------------
# Step 1: Stop and remove the Gitea container
# ---------------------------------------------------------------------------
# Check if docker-compose file exists (skip if already torn down)
if ssh_exec FEDORA "test -f '${COMPOSE_DIR}/docker-compose.yml'" 2>/dev/null; then
if confirm_action 'This will stop Gitea on Fedora. Continue? [y/N] '; then
# Try modern "docker compose" first, fall back to standalone
if ssh_exec FEDORA "cd '${COMPOSE_DIR}' && docker compose down 2>/dev/null || docker-compose down"; then
log_success "Gitea container stopped and removed"
else
log_error "Failed to stop/remove Gitea container via docker-compose"
TEARDOWN_FAILED=true
fi
else
log_info "Skipped container shutdown"
fi
else
# Safety fallback: compose file may be missing while container still exists.
existing_container=$(ssh_exec FEDORA "docker ps -a --filter 'name=^/gitea$' --format '{{.ID}}'" 2>/dev/null || true)
if [[ -n "$existing_container" ]]; then
log_warn "docker-compose.yml not found, but gitea container still exists."
if confirm_action 'Force-remove gitea container directly? [y/N] '; then
if ssh_exec FEDORA "docker rm -f gitea" >/dev/null 2>&1; then
log_success "Gitea container force-removed"
else
log_error "Failed to force-remove gitea container"
TEARDOWN_FAILED=true
fi
else
log_info "Skipped direct container removal"
fi
else
log_info "No docker-compose.yml found — container already removed"
fi
fi
# ---------------------------------------------------------------------------
# Step 2: Remove macvlan Docker network
# Only removed if no containers are still attached.
# ---------------------------------------------------------------------------
if ssh_exec FEDORA "docker network inspect gitea_net" &>/dev/null; then
if confirm_action 'Remove macvlan network gitea_net on Fedora? [y/N] '; then
if ssh_exec FEDORA "docker network rm gitea_net" >/dev/null 2>&1; then
log_success "macvlan network gitea_net removed"
else
log_error "Failed to remove gitea_net (containers may still be attached)"
TEARDOWN_FAILED=true
fi
else
log_info "Kept gitea_net network"
fi
else
log_info "gitea_net network already removed"
fi
# ---------------------------------------------------------------------------
# Step 3: Optionally remove all Gitea data
# This is irreversible — removes repos, database, config, everything.
# ---------------------------------------------------------------------------
running_container=$(ssh_exec FEDORA "docker ps --filter 'name=^/gitea$' --format '{{.ID}}'" 2>/dev/null || true)
if [[ -n "$running_container" ]]; then
log_warn "Gitea container is still running — refusing to remove data directory."
log_warn "Stop the container first, then re-run teardown to remove data."
elif ssh_exec FEDORA "test -d '${DATA_PATH}'" 2>/dev/null; then
if confirm_action "$(printf 'Remove ALL Gitea data at %s on Fedora? This is IRREVERSIBLE. [y/N] ' "$DATA_PATH")"; then
if ssh_exec FEDORA "sudo rm -rf '${DATA_PATH}'"; then
log_success "All Gitea data removed from Fedora"
else
log_error "Failed to remove data directory ${DATA_PATH}"
TEARDOWN_FAILED=true
fi
else
log_info "Data preserved at ${DATA_PATH}"
fi
else
log_info "Data directory already removed"
fi
# ---------------------------------------------------------------------------
# Step 4: Clear the auto-populated backup API token from .env
# The token is useless after teardown — clear it so phase2 generates a new one.
# ---------------------------------------------------------------------------
remaining_container=$(ssh_exec FEDORA "docker ps -a --filter 'name=^/gitea$' --format '{{.ID}}'" 2>/dev/null || true)
if [[ -z "$remaining_container" ]] && [[ -n "${GITEA_BACKUP_ADMIN_TOKEN:-}" ]]; then
save_env_var "GITEA_BACKUP_ADMIN_TOKEN" ""
log_success "GITEA_BACKUP_ADMIN_TOKEN cleared from .env"
elif [[ -n "$remaining_container" ]]; then
log_info "Keeping GITEA_BACKUP_ADMIN_TOKEN in .env because gitea container still exists"
fi
if $TEARDOWN_FAILED; then
log_error "Phase 2 teardown completed with errors"
exit 1
fi
log_success "Phase 2 teardown complete"