Files
gitea-migration/phase3_runners.sh
S f4a6b04d14 feat: rework runner config to INI format with full field support
Replace pipe-delimited runners.conf with INI-style sections supporting
host resolution, container images, repo-scoped tokens, resource limits,
capacity, and SSH key passthrough. All defaults pulled from .env.

- Add INI parsing helpers (ini_list_sections, ini_get, ini_set) to common.sh
- Add SSH key support (UNRAID_SSH_KEY, FEDORA_SSH_KEY) to ssh_exec/scp_to
- Add .env vars: RUNNER_DEFAULT_IMAGE, RUNNER_DEFAULT_CAPACITY,
  RUNNER_DEFAULT_DATA_PATH, LOCAL_RUNNER_DATA_PATH, LOCAL_REGISTRY
- Rewrite manage_runner.sh with host/image/token resolution and resource limits
- Rewrite configure_runners.sh wizard for INI format with all 9 fields
- Update phase3 scripts to use ini_list_sections instead of pipe parsing
- Add runners.conf INI validation to preflight.sh (check 5b)
- Update templates to use resolved labels, capacity, and deploy resources

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 23:14:46 -05:00

98 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# phase3_runners.sh — Deploy Gitea Actions runners from runners.conf
# Depends on: Phase 1 complete (Gitea running on Unraid with admin token)
# Steps:
# 1. Get runner registration token from Gitea admin API
# 2. Save token to .env for reuse by manage_runner.sh
# 3. Deploy each runner defined in runners.conf via manage_runner.sh
# Idempotent: skips already-deployed runners, reuses valid tokens.
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/lib/common.sh"
load_env
require_vars GITEA_INTERNAL_URL GITEA_ADMIN_TOKEN ACT_RUNNER_VERSION
phase_header 3 "Gitea Actions Runners"
RUNNERS_CONF="${SCRIPT_DIR}/runners.conf"
# ---------------------------------------------------------------------------
# Pre-check: runners.conf must exist with at least one entry
# ---------------------------------------------------------------------------
if [[ ! -f "$RUNNERS_CONF" ]]; then
log_error "runners.conf not found — copy runners.conf.example and fill in values"
exit 1
fi
# Count INI sections to verify there are runners to deploy
RUNNER_COUNT=$(ini_list_sections "$RUNNERS_CONF" | wc -l | xargs)
if [[ "$RUNNER_COUNT" -eq 0 ]]; then
log_error "No runners defined in runners.conf"
exit 1
fi
log_info "Found ${RUNNER_COUNT} runner(s) in runners.conf"
# ---------------------------------------------------------------------------
# Step 1: Get or reuse runner registration token
# The registration token is a shared secret that all runners use to register
# themselves with the Gitea instance. It's retrieved from the admin API and
# saved to .env so manage_runner.sh can use it independently.
# ---------------------------------------------------------------------------
log_step 1 "Getting runner registration token..."
if [[ -n "${GITEA_RUNNER_REGISTRATION_TOKEN:-}" ]]; then
log_info "Registration token already in .env — reusing"
else
# Gitea returns the token as a JSON object: {"token": "..."}
TOKEN_RESPONSE=$(gitea_api GET "/admin/runners/registration-token")
GITEA_RUNNER_REGISTRATION_TOKEN=$(printf '%s' "$TOKEN_RESPONSE" | jq -r '.token')
if [[ -z "$GITEA_RUNNER_REGISTRATION_TOKEN" ]] || [[ "$GITEA_RUNNER_REGISTRATION_TOKEN" == "null" ]]; then
log_error "Failed to get runner registration token from Gitea"
exit 1
fi
save_env_var "GITEA_RUNNER_REGISTRATION_TOKEN" "$GITEA_RUNNER_REGISTRATION_TOKEN"
log_success "Registration token saved to .env"
fi
# ---------------------------------------------------------------------------
# Step 2: Deploy each runner via manage_runner.sh
# Iterates over every INI section in runners.conf (each section = one runner).
# manage_runner.sh handles its own idempotency (skips already-running runners).
# ---------------------------------------------------------------------------
log_step 2 "Deploying runners..."
DEPLOYED=0
FAILED=0
while IFS= read -r name; do
[[ -z "$name" ]] && continue
log_info "Processing runner: ${name}"
if "${SCRIPT_DIR}/manage_runner.sh" add --name "$name"; then
DEPLOYED=$((DEPLOYED + 1))
else
log_error "Failed to deploy runner: ${name}"
FAILED=$((FAILED + 1))
fi
done < <(ini_list_sections "$RUNNERS_CONF")
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
printf '\n'
log_info "Results: ${DEPLOYED} deployed, ${FAILED} failed (out of ${RUNNER_COUNT})"
if [[ $FAILED -gt 0 ]]; then
log_error "Some runners failed to deploy — check logs above"
exit 1
fi
log_success "Phase 3 complete — all ${RUNNER_COUNT} runner(s) deployed"