#!/usr/bin/env bash set -euo pipefail # ============================================================================= # phase11_teardown.sh — Remove custom runner infrastructure deployed by phase 11 # Reverses: # 1. Repository variables # 2. Docker runners (per-repo + shared emulator) # 3. Shared macOS runner → restore original per-repo macOS runners # 4. Toolchain images on Unraid # ============================================================================= SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "${SCRIPT_DIR}/lib/common.sh" load_env require_vars GITEA_ADMIN_TOKEN GITEA_INTERNAL_URL GITEA_ORG_NAME phase_header 11 "Custom Runners — Teardown" REPO_VARS_CONF="${SCRIPT_DIR}/repo_variables.conf" AUTO_YES=false for arg in "$@"; do case "$arg" in --yes|-y) AUTO_YES=true ;; *) ;; esac done if [[ "$AUTO_YES" != "true" ]]; then log_warn "This will remove all phase 11 custom runners and repo variables." printf 'Continue? [y/N] ' >&2 read -r confirm if [[ "$confirm" != "y" ]] && [[ "$confirm" != "Y" ]]; then log_info "Aborted" exit 0 fi fi REMOVED=0 FAILED=0 # ========================================================================= # Step 1: Delete repository variables # ========================================================================= log_step 1 "Removing repository variables" if [[ -f "$REPO_VARS_CONF" ]]; then while IFS= read -r repo; do [[ -z "$repo" ]] && continue log_info "--- Removing variables for repo: ${repo} ---" # Parse keys from section in_section=false while IFS= read -r line; do line="${line#"${line%%[![:space:]]*}"}" line="${line%"${line##*[![:space:]]}"}" [[ -z "$line" ]] && continue [[ "$line" == \#* ]] && continue if [[ "$line" =~ ^\[([^]]+)\] ]]; then if [[ "${BASH_REMATCH[1]}" == "$repo" ]]; then in_section=true elif $in_section; then break fi continue fi if $in_section && [[ "$line" =~ ^([^=]+)= ]]; then k="${BASH_REMATCH[1]}" k="${k#"${k%%[![:space:]]*}"}" k="${k%"${k##*[![:space:]]}"}" if gitea_api DELETE "/repos/${GITEA_ORG_NAME}/${repo}/actions/variables/${k}" >/dev/null 2>&1; then log_success " Deleted ${k} from ${repo}" REMOVED=$((REMOVED + 1)) else log_warn " Could not delete ${k} from ${repo} (may not exist)" fi fi done < "$REPO_VARS_CONF" done < <(ini_list_sections "$REPO_VARS_CONF") else log_info "repo_variables.conf not found — skipping" fi # ========================================================================= # Step 2: Remove Docker runners # ========================================================================= log_step 2 "Removing Docker runners" PHASE11_DOCKER_RUNNERS=( unraid-go-node-1 unraid-go-node-2 unraid-go-node-3 unraid-jvm-slim-1 unraid-jvm-slim-2 unraid-android-emulator ) for runner_name in "${PHASE11_DOCKER_RUNNERS[@]}"; do log_info "Removing runner '${runner_name}'..." if "${SCRIPT_DIR}/manage_runner.sh" remove --name "$runner_name" 2>/dev/null; then log_success "Removed ${runner_name}" REMOVED=$((REMOVED + 1)) else log_warn "Could not remove ${runner_name} (may not exist)" fi done # ========================================================================= # Step 3: Remove shared macOS runner, restore original per-repo runners # ========================================================================= log_step 3 "Restoring original macOS runner configuration" # Remove shared runner if launchctl list 2>/dev/null | grep -q "com.gitea.runner.macbook-runner"; then log_info "Removing shared macOS runner 'macbook-runner'..." "${SCRIPT_DIR}/manage_runner.sh" remove --name macbook-runner 2>/dev/null || true REMOVED=$((REMOVED + 1)) fi # Note: original per-repo macOS runner sections were replaced in runners.conf # during phase 11. They need to be re-added manually or by re-running # configure_runners.sh. This teardown only cleans up deployed resources. log_info "Note: original macOS runner sections (macbook-runner-periodvault," log_info " macbook-runner-intermittent-fasting-tracker) must be restored in" log_info " runners.conf manually or via git checkout." # ========================================================================= # Step 4: Remove toolchain images from Unraid # ========================================================================= log_step 4 "Removing toolchain images from Unraid" IMAGES_TO_REMOVE=( "go-node-runner:latest" "jvm-android-runner:slim" "jvm-android-runner:full" ) for img in "${IMAGES_TO_REMOVE[@]}"; do if ssh_exec "UNRAID" "docker rmi '${img}' 2>/dev/null"; then log_success "Removed image ${img}" REMOVED=$((REMOVED + 1)) else log_warn "Could not remove image ${img} (may not exist or in use)" fi done # ========================================================================= # Step 5: Remove phase 11 runner sections from runners.conf # ========================================================================= log_step 5 "Cleaning runners.conf" RUNNERS_CONF="${SCRIPT_DIR}/runners.conf" PHASE11_SECTIONS=( unraid-go-node-1 unraid-go-node-2 unraid-go-node-3 unraid-jvm-slim-1 unraid-jvm-slim-2 unraid-android-emulator macbook-runner ) for section in "${PHASE11_SECTIONS[@]}"; do if ini_list_sections "$RUNNERS_CONF" | grep -qx "$section" 2>/dev/null; then ini_remove_section "$RUNNERS_CONF" "$section" log_success "Removed [${section}] from runners.conf" REMOVED=$((REMOVED + 1)) fi done # --------------------------------------------------------------------------- # Summary # --------------------------------------------------------------------------- printf '\n' log_info "Results: ${REMOVED} items removed, ${FAILED} failures" if [[ $FAILED -gt 0 ]]; then log_error "Some removals failed — check logs above" exit 1 fi log_success "Phase 11 teardown complete"