135 lines
4.8 KiB
Bash
Executable File
135 lines
4.8 KiB
Bash
Executable File
#!/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 <<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 UNRAID_IP UNRAID_SSH_USER UNRAID_GITEA_DATA_PATH UNRAID_COMPOSE_DIR
|
|
|
|
DATA_PATH="$UNRAID_GITEA_DATA_PATH"
|
|
COMPOSE_DIR="${UNRAID_COMPOSE_DIR}/gitea"
|
|
|
|
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
|
|
# ---------------------------------------------------------------------------
|
|
# Check if docker-compose file exists (skip if already torn down)
|
|
if ssh_exec UNRAID "test -f '${COMPOSE_DIR}/docker-compose.yml'" 2>/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"
|