105 lines
3.1 KiB
Bash
Executable File
105 lines
3.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# =============================================================================
|
|
# phase3_teardown.sh — Tear down all runners defined in runners.conf
|
|
# For each runner: calls manage_runner.sh remove to stop and clean up.
|
|
# Clears GITEA_RUNNER_REGISTRATION_TOKEN from .env since it becomes useless
|
|
# after all runners are removed.
|
|
# Safe to run against already-torn-down runners (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
|
|
|
|
log_warn "=== Phase 3 Teardown: Runners ==="
|
|
|
|
RUNNERS_CONF="${SCRIPT_DIR}/runners.conf"
|
|
|
|
if [[ ! -f "$RUNNERS_CONF" ]]; then
|
|
log_info "runners.conf not found — nothing to tear down"
|
|
exit 0
|
|
fi
|
|
|
|
# Track whether teardown encountered operational errors (not user skips).
|
|
TEARDOWN_FAILED=false
|
|
REMOVED=0
|
|
FAILED=0
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 1: Remove each runner via manage_runner.sh
|
|
# manage_runner.sh handles its own safety (skips already-removed runners).
|
|
# ---------------------------------------------------------------------------
|
|
if confirm_action 'This will stop and remove all runners. Continue? [y/N] '; then
|
|
while IFS= read -r name; do
|
|
[[ -z "$name" ]] && continue
|
|
log_info "Removing runner: ${name}"
|
|
if "${SCRIPT_DIR}/manage_runner.sh" remove --name "$name"; then
|
|
REMOVED=$((REMOVED + 1))
|
|
else
|
|
log_error "Failed to remove runner: ${name}"
|
|
FAILED=$((FAILED + 1))
|
|
TEARDOWN_FAILED=true
|
|
fi
|
|
done < <(ini_list_sections "$RUNNERS_CONF")
|
|
|
|
TOTAL=$((REMOVED + FAILED))
|
|
log_info "Results: ${REMOVED} removed, ${FAILED} failed (out of ${TOTAL})"
|
|
else
|
|
log_info "Skipped runner removal"
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step 2: Clear the registration token from .env
|
|
# Only clear if no runners failed — a partial teardown should preserve the
|
|
# token so the user can retry without needing to regenerate it.
|
|
# ---------------------------------------------------------------------------
|
|
if $TEARDOWN_FAILED; then
|
|
log_info "Keeping GITEA_RUNNER_REGISTRATION_TOKEN in .env because some runners failed to remove"
|
|
elif [[ -n "${GITEA_RUNNER_REGISTRATION_TOKEN:-}" ]]; then
|
|
save_env_var "GITEA_RUNNER_REGISTRATION_TOKEN" ""
|
|
log_success "GITEA_RUNNER_REGISTRATION_TOKEN cleared from .env"
|
|
fi
|
|
|
|
if $TEARDOWN_FAILED; then
|
|
log_error "Phase 3 teardown completed with errors"
|
|
exit 1
|
|
fi
|
|
|
|
log_success "Phase 3 teardown complete"
|