#!/usr/bin/env bash set -euo pipefail # ============================================================================= # phase1_teardown.sh — Tear down Gitea on Unraid # 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 </dev/null; then if confirm_action 'This will stop Gitea on Unraid. Continue? [y/N] '; then # Try modern "docker compose" first, fall back to standalone if ssh_exec UNRAID "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 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." if confirm_action 'Force-remove gitea container directly? [y/N] '; 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" fi fi # --------------------------------------------------------------------------- # Step 2: Optionally remove all Gitea data # This is irreversible — removes repos, database, config, everything. # --------------------------------------------------------------------------- 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 if confirm_action "$(printf 'Remove ALL Gitea data at %s? This is IRREVERSIBLE. [y/N] ' "$DATA_PATH")"; then if ssh_exec UNRAID "sudo rm -rf '${DATA_PATH}'"; then log_success "All Gitea data removed from Unraid" 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 3: Clear the auto-populated API token from .env # The token is useless after teardown — clear it so phase1 generates a new one. # --------------------------------------------------------------------------- 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" "" 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 log_success "Phase 1 teardown complete"