#!/usr/bin/env bash set -euo pipefail # ============================================================================= # phase3_post_check.sh — Verify Phase 3 (Runners) succeeded # Checks that every runner defined in runners.conf is registered and online # in the Gitea admin panel. # Exits 0 only if ALL checks pass. # ============================================================================= SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "${SCRIPT_DIR}/lib/common.sh" load_env require_vars GITEA_INTERNAL_URL GITEA_ADMIN_TOKEN log_info "=== Phase 3 Post-Check ===" RUNNERS_CONF="${SCRIPT_DIR}/runners.conf" if [[ ! -f "$RUNNERS_CONF" ]]; then log_error "runners.conf not found" exit 1 fi PASS=0 FAIL=0 # --------------------------------------------------------------------------- # Fetch all registered runners from Gitea admin API (single call) # This avoids making N API calls — one per runner — which would be slow. # --------------------------------------------------------------------------- API_RUNNERS=$(gitea_api GET "/admin/runners" 2>/dev/null || echo "[]") # --------------------------------------------------------------------------- # Check each runner: exists in API response AND status is online/idle/active # A runner that registered but is not running will show as "offline". # --------------------------------------------------------------------------- while IFS= read -r name; do [[ -z "$name" ]] && continue # Look up runner by name in the API response local_status=$(printf '%s' "$API_RUNNERS" | jq -r --arg n "$name" '.[] | select(.name == $n) | .status' 2>/dev/null || true) if [[ -z "$local_status" ]]; then log_error "FAIL: Runner '${name}' not found in Gitea admin" FAIL=$((FAIL + 1)) elif [[ "$local_status" == "offline" ]]; then log_error "FAIL: Runner '${name}' is registered but offline" FAIL=$((FAIL + 1)) else log_success "Runner '${name}' is ${local_status}" PASS=$((PASS + 1)) fi done < <(ini_list_sections "$RUNNERS_CONF") # --------------------------------------------------------------------------- # Check: runner count matches runners.conf # --------------------------------------------------------------------------- EXPECTED_COUNT=$(ini_list_sections "$RUNNERS_CONF" | wc -l | xargs) ACTUAL_COUNT=$(printf '%s' "$API_RUNNERS" | jq 'length' 2>/dev/null || echo 0) if [[ "$ACTUAL_COUNT" -ge "$EXPECTED_COUNT" ]]; then log_success "Runner count OK: ${ACTUAL_COUNT} registered (${EXPECTED_COUNT} expected)" PASS=$((PASS + 1)) else log_error "FAIL: Only ${ACTUAL_COUNT} runners registered (expected ${EXPECTED_COUNT})" FAIL=$((FAIL + 1)) fi # --------------------------------------------------------------------------- # Summary # --------------------------------------------------------------------------- printf '\n' log_info "Results: ${PASS} passed, ${FAIL} failed" if [[ $FAIL -gt 0 ]]; then log_error "Phase 3 post-check FAILED" exit 1 else log_success "Phase 3 post-check PASSED — all runners are online" exit 0 fi