fix: enhance Gitea teardown script with error handling and safety checks
This commit is contained in:
@@ -18,6 +18,9 @@ DATA_PATH="$UNRAID_GITEA_DATA_PATH"
|
|||||||
|
|
||||||
log_warn "=== Phase 1 Teardown: Gitea on Unraid ==="
|
log_warn "=== Phase 1 Teardown: Gitea on Unraid ==="
|
||||||
|
|
||||||
|
# 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,55 @@ if ssh_exec UNRAID "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 UNRAID "cd '${DATA_PATH}' && docker compose down 2>/dev/null || docker-compose down" || true
|
if ssh_exec UNRAID "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 UNRAID "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 UNRAID "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: Optionally remove all Gitea data
|
||||||
# This is irreversible — removes repos, database, config, everything.
|
# This is irreversible — removes repos, database, config, everything.
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
if ssh_exec UNRAID "test -d '${DATA_PATH}'" 2>/dev/null; then
|
running_container=$(ssh_exec UNRAID "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 UNRAID "test -d '${DATA_PATH}'" 2>/dev/null; then
|
||||||
printf 'Remove ALL Gitea data at %s? This is IRREVERSIBLE. [y/N] ' "$DATA_PATH"
|
printf 'Remove ALL Gitea data at %s? This is IRREVERSIBLE. [y/N] ' "$DATA_PATH"
|
||||||
read -r confirm
|
read -r confirm
|
||||||
if [[ "$confirm" =~ ^[Yy]$ ]]; then
|
if [[ "$confirm" =~ ^[Yy]$ ]]; then
|
||||||
ssh_exec UNRAID "rm -rf '${DATA_PATH}'"
|
if ssh_exec UNRAID "rm -rf '${DATA_PATH}'"; then
|
||||||
log_success "All Gitea data removed from Unraid"
|
log_success "All Gitea data removed from Unraid"
|
||||||
|
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
|
||||||
@@ -57,9 +90,17 @@ fi
|
|||||||
# Step 3: Clear the auto-populated API token from .env
|
# Step 3: Clear the auto-populated API token from .env
|
||||||
# The token is useless after teardown — clear it so phase1 generates a new one.
|
# The token is useless after teardown — clear it so phase1 generates a new one.
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
if [[ -n "${GITEA_ADMIN_TOKEN:-}" ]]; then
|
remaining_container=$(ssh_exec UNRAID "docker ps -a --filter 'name=^/gitea$' --format '{{.ID}}'" 2>/dev/null || true)
|
||||||
|
if [[ -z "$remaining_container" ]] && [[ -n "${GITEA_ADMIN_TOKEN:-}" ]]; then
|
||||||
save_env_var "GITEA_ADMIN_TOKEN" ""
|
save_env_var "GITEA_ADMIN_TOKEN" ""
|
||||||
log_success "GITEA_ADMIN_TOKEN cleared from .env"
|
log_success "GITEA_ADMIN_TOKEN cleared from .env"
|
||||||
|
elif [[ -n "$remaining_container" ]]; then
|
||||||
|
log_info "Keeping GITEA_ADMIN_TOKEN in .env because gitea container still exists"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if $TEARDOWN_FAILED; then
|
||||||
|
log_error "Phase 1 teardown completed with errors"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
log_success "Phase 1 teardown complete"
|
log_success "Phase 1 teardown complete"
|
||||||
|
|||||||
Reference in New Issue
Block a user