fix: enhance phase2_teardown.sh with error handling and user prompts for container and network removal

This commit is contained in:
S
2026-03-02 10:39:57 -05:00
parent a3342d6612
commit c3b9a8c5bb

View File

@@ -18,6 +18,9 @@ DATA_PATH="$FEDORA_GITEA_DATA_PATH"
log_warn "=== Phase 2 Teardown: Gitea on Fedora ===" 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 # 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 read -r confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then if [[ "$confirm" =~ ^[Yy]$ ]]; then
# Try modern "docker compose" first, fall back to standalone # 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 if ssh_exec FEDORA "cd '${DATA_PATH}' && docker compose down 2>/dev/null || docker-compose down"; then
log_success "Gitea container stopped and removed" log_success "Gitea container stopped and removed"
else
log_error "Failed to stop/remove Gitea container via docker-compose"
TEARDOWN_FAILED=true
fi
else else
log_info "Skipped container shutdown" log_info "Skipped container shutdown"
fi fi
else 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."
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" log_info "No docker-compose.yml found — container already removed"
fi
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. # 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" printf 'Remove ALL Gitea data at %s on Fedora? This is IRREVERSIBLE. [y/N] ' "$DATA_PATH"
read -r confirm read -r confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then if [[ "$confirm" =~ ^[Yy]$ ]]; then
ssh_exec FEDORA "rm -rf '${DATA_PATH}'" if ssh_exec FEDORA "rm -rf '${DATA_PATH}'"; then
log_success "All Gitea data removed from Fedora" log_success "All Gitea data removed from Fedora"
else
log_error "Failed to remove data directory ${DATA_PATH}"
TEARDOWN_FAILED=true
fi
else else
log_info "Data preserved at ${DATA_PATH}" log_info "Data preserved at ${DATA_PATH}"
fi fi
@@ -54,12 +108,20 @@ else
fi 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. # 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" "" save_env_var "GITEA_BACKUP_ADMIN_TOKEN" ""
log_success "GITEA_BACKUP_ADMIN_TOKEN cleared from .env" 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 fi
log_success "Phase 2 teardown complete" log_success "Phase 2 teardown complete"