From c3b9a8c5bbcd2b56e30fbb13144210a5baa022a7 Mon Sep 17 00:00:00 2001 From: S Date: Mon, 2 Mar 2026 10:39:57 -0500 Subject: [PATCH] fix: enhance phase2_teardown.sh with error handling and user prompts for container and network removal --- phase2_teardown.sh | 80 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 9 deletions(-) diff --git a/phase2_teardown.sh b/phase2_teardown.sh index 7633369..2c68a0f 100755 --- a/phase2_teardown.sh +++ b/phase2_teardown.sh @@ -18,6 +18,9 @@ DATA_PATH="$FEDORA_GITEA_DATA_PATH" 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 # --------------------------------------------------------------------------- @@ -27,25 +30,76 @@ if ssh_exec FEDORA "test -f '${DATA_PATH}/docker-compose.yml'" 2>/dev/null; then read -r confirm if [[ "$confirm" =~ ^[Yy]$ ]]; then # Try modern "docker compose" first, fall back to standalone - ssh_exec FEDORA "cd '${DATA_PATH}' && docker compose down 2>/dev/null || docker-compose down" || true - log_success "Gitea container stopped and removed" + if ssh_exec FEDORA "cd '${DATA_PATH}' && 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 - log_info "No docker-compose.yml found — container already removed" + # 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." + printf 'Force-remove gitea container directly? [y/N] ' + read -r confirm + if [[ "$confirm" =~ ^[Yy]$ ]]; 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: Optionally remove all Gitea data +# 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 + printf 'Remove macvlan network gitea_net on Fedora? [y/N] ' + read -r confirm + if [[ "$confirm" =~ ^[Yy]$ ]]; 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. # --------------------------------------------------------------------------- -if ssh_exec FEDORA "test -d '${DATA_PATH}'" 2>/dev/null; then +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 printf 'Remove ALL Gitea data at %s on Fedora? This is IRREVERSIBLE. [y/N] ' "$DATA_PATH" read -r confirm if [[ "$confirm" =~ ^[Yy]$ ]]; then - ssh_exec FEDORA "rm -rf '${DATA_PATH}'" - log_success "All Gitea data removed from Fedora" + if ssh_exec FEDORA "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 @@ -54,12 +108,20 @@ else fi # --------------------------------------------------------------------------- -# Step 3: Clear the auto-populated backup API token from .env +# 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. # --------------------------------------------------------------------------- -if [[ -n "${GITEA_BACKUP_ADMIN_TOKEN:-}" ]]; then +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"