fix: gitea controller
runner nix smoke / nix label and flake smoke (push) Canceled after 0s
runner ubuntu smoke / ubuntu-latest label smoke (push) Canceled after 0s

This commit is contained in:
2026-11-03 15:17:38 +00:00
parent 2093f17fa5
commit c6a504c951
9 changed files with 1080 additions and 34 deletions
@@ -140,7 +140,7 @@ gcr_gitea_set_runner_disabled() {
"$1" "$2" "$3" >> "$calls"
runner_disabled="$3"
}
original='{"job_id":"301","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-highmem","created_at":1000,"ttl_min":480,"vm_id":71,"vm_name":"gcr-301-1","runner_name":"gcr-301-1","server_type":"ccx53","budget_rate":"0.8550","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
original='{"job_id":"301","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-highmem","created_at":1000,"ttl_min":480,"vm_id":71,"vm_name":"gcr-301-1","runner_name":"gcr-301-1","runner_id":17,"server_type":"ccx53","budget_rate":"0.8550","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
gcr_record_put 301 1 "$original"
gcr_deallocate 301 1 completed:success
test "$(gcr_record_field "$(gcr_record_get 301 1)" status)" = retiring_vm
@@ -201,6 +201,7 @@ claimed="$(gcr_record_get 303 1)"
jq -e 'select(.job_id == "303" and .repo == "hectic-lab/util.nix" and
.label == "gross-nix-x86-highmem" and .vm_id == 71 and
.vm_name == "gcr-301-1" and .runner_name == "gcr-303-1" and
.runner_id == "" and
.status == "rebuild_pending" and .bootstrapped == false and
.rebuild_image_id == "424242" and .rebuild_action_id == "" and
.created_at == 1000 and .ttl_min == 480 and .assigned_at == 2800)' \
@@ -208,6 +209,12 @@ jq -e 'select(.job_id == "303" and .repo == "hectic-lab/util.nix" and
test "$(grep -Ec '^(budget|token|create)$' "$calls" || true)" = 0
test "$(gcr_count_active)" = 1
test "$(gcr_count_active_repo hectic-lab/util.nix)" = 1
gcr_gitea_list_jobs() {
printf '%s\n' '{"id":301,"run_attempt":1,"status":"completed","conclusion":"success","runner_id":17,"runner_name":""}'
}
gcr_reconcile_runner_assignments
test "$(gcr_record_field "$(gcr_record_get 303 1)" status)" = rebuild_pending
test ! -e "$(gcr_record_path 301 1)"
# Rebuild intent and action id are durable around provider request and polling.
gcr_gitea_job_state() { printf 'queued:'; }
@@ -258,15 +265,29 @@ if grep -q '^token$' "$calls"; then
fi
gcr_vm_public_ip() { printf '192.0.2.71'; }
bootstrap_registered=false
gcr_gitea_find_runner_exact() {
[ "$bootstrap_registered" = true ] || return 1
GCR_RUNNER_ID=18
GCR_RUNNER_DISABLED=false
}
gcr_vm_bootstrap_ssh() {
test -r "$5"
grep -Eq "^$1 ssh-ed25519 [A-Za-z0-9+/=]+$" "$5"
printf 'bootstrap ip=%s label=%s token=%s runner=%s\n' \
"$1" "$2" "$3" "$4" >> "$calls"
bootstrap_registered=true
}
gcr_vm_runner_service_pinned() {
test "$1" = 192.0.2.71
test -r "$2"
test "$3" = start
test "$(gcr_record_field "$(gcr_record_get 303 1)" runner_id)" = 18
}
gcr_bootstrap_pending
bootstrapped="$(gcr_record_get 303 1)"
test "$(gcr_record_field "$bootstrapped" bootstrapped)" = true
test "$(gcr_record_field "$bootstrapped" runner_id)" = 18
test "$(grep -c '^token$' "$calls")" = 1
grep -q '^bootstrap ip=192.0.2.71 label=gross-nix-x86-highmem token=fresh-token runner=gcr-303-1$' "$calls"
if grep -q '^bootstrap .*runner=gcr-301-1$' "$calls"; then
@@ -0,0 +1,309 @@
#!/bin/dash
set -eu
. "$LOG_SH"
. "$STATE_SH"
. "$DECIDE_SH"
. "$HCLOUD_SH"
. "$GITEA_SH"
. "$CONTROLLER_SH"
. "$WEBHOOK_SH"
gcr_state_init
export GCR_ALLOWED_REPOS='hinterland/hearth'
export GCR_CONCURRENCY_CAP=1
export GCR_PER_REPO_CAP=1
export GCR_NIX_IMAGE_ID=424242
export GCR_RECONCILE_INTERVAL_SEC=60
NOW=2000
gcr_now_epoch() { printf '%s' "$NOW"; }
calls="$GCR_STATE_DIR/calls"
source_477='{"job_id":"477","run_attempt":"1","repo":"hinterland/hearth","label":"nix","created_at":1000,"ttl_min":480,"vm_id":174316477,"vm_name":"gcr-477-1","runner_name":"gcr-477-1","runner_id":136,"server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"pending_vm"}'
target_471='{"job_id":"471","run_attempt":"1","repo":"hinterland/hearth","label":"nix","created_at":900,"ttl_min":null,"vm_id":"","vm_name":"","status":"deferred"}'
gcr_record_put 477 1 "$source_477"
gcr_record_put 471 1 "$target_471"
authoritative_jobs='{"id":477,"run_attempt":1,"status":"completed","conclusion":"failure","runner_id":136,"runner_name":"gcr-477-1"}
{"id":471,"run_attempt":1,"status":"in_progress","conclusion":"","runner_id":136,"runner_name":"gcr-477-1"}'
gcr_gitea_list_jobs() { printf '%s\n' "$authoritative_jobs"; }
gcr_vm_host_key_prepare 477 1
# Bootstrap-specific override is defined later.
# shellcheck disable=SC2218
gcr_vm_known_hosts_file 477 1 192.0.2.77 >/dev/null
original_public="$(cat "$(gcr_vm_host_key_path 477 1).pub")"
# Allocation 477's runner accepts actual job 471. VM lifecycle follows 471;
# origin 477 returns to deferred without a second VM or budget reservation.
# Polling must recover even if no webhook arrived and legacy completed 477
# remains in the same runner's history. A subsequent webhook is idempotent.
gcr_reconcile_runner_assignments
test "$(gcr_record_field "$(gcr_record_get 471 1)" status)" = vm_active
test "$(gcr_record_field "$(gcr_record_get 477 1)" status)" = deferred
gcr_apply_actual_job hinterland/hearth 136 gcr-477-1 471 1 vm_active webhook
j471="$(gcr_record_get 471 1)"
j477="$(gcr_record_get 477 1)"
jq -e 'select(.job_id == "471" and .status == "vm_active" and
.runner_id == 136 and .vm_id == 174316477 and
.allocation_job_id == "477" and .allocation_run_attempt == "1" and
.created_at == 1000 and .ttl_min == 480 and .server_type == "cx23" and
.budget_rate == "0.004" and .image_id == "424242")' \
"$(gcr_record_path 471 1)" >/dev/null
jq -e 'select(.job_id == "477" and .status == "deferred" and
.ttl_min == null and .vm_id == "" and .runner_id == "")' \
"$(gcr_record_path 477 1)" >/dev/null
test "$(gcr_count_active)" = 1
test "$(gcr_count_active_repo hinterland/hearth)" = 1
test ! -e "$(gcr_vm_host_key_path 477 1)"
gcr_vm_host_key_pair_valid "$(gcr_vm_host_key_path 471 1)"
target_known="$(gcr_vm_known_hosts_file 471 1 192.0.2.77)"
test "$target_known" = "$(gcr_vm_host_key_path 471 1).known_hosts"
# Delayed terminal webhook for allocation origin 477 cannot move actual active
# job 471 back or retire its VM; current API still binds runner 136 to 471.
if gcr_apply_actual_job hinterland/hearth 136 gcr-477-1 477 1 \
completed:failure webhook; then
printf 'stale allocation-origin terminal event must be rejected\n' >&2
exit 1
fi
test "$(gcr_record_field "$(gcr_record_get 471 1)" status)" = vm_active
test "$(gcr_record_field "$(gcr_record_get 477 1)" status)" = deferred
gcr_gitea_job_state() { printf queued:; }
gcr_vm_create() { printf 'unexpected-create\n' >> "$calls"; return 1; }
gcr_alloc_deferred 477 1
test "$(gcr_record_field "$(gcr_record_get 477 1)" status)" = deferred
test ! -e "$calls"
gcr_vm_host_key_prepare 477 1
test "$(cat "$(gcr_vm_host_key_path 477 1).pub")" != "$original_public"
# Terminal event remains resolvable by runner ID after ephemeral runner row
# deletion blanks runner_name.
gcr_vm_finish_terminal() {
printf 'finish job=%s attempt=%s vm=%s state=%s via=%s\n' \
"$1" "$2" "$(gcr_record_field "$3" vm_id)" "$4" "$5" >> "$calls"
gcr_record_del "$1" "$2"
}
authoritative_jobs='{"id":471,"run_attempt":1,"status":"completed","conclusion":"failure","runner_id":136,"runner_name":""}'
gcr_apply_actual_job hinterland/hearth 136 '' 471 1 completed:failure webhook
grep -q '^finish job=471 attempt=1 vm=174316477 state=completed:failure via=webhook$' "$calls"
test ! -e "$(gcr_record_path 471 1)"
test "$(gcr_record_field "$(gcr_record_get 477 1)" status)" = deferred
test ! -e "$(gcr_vm_host_key_path 471 1)"
test -e "$(gcr_vm_host_key_path 477 1)"
# Polling catches short terminal job missed by webhooks, with no status filter
# and no surviving runner name.
poll_source="$(printf '%s' "$source_477" | jq -c \
'.job_id = "480" | .vm_id = 174316480 | .vm_name = "gcr-480-1"
| .runner_name = "gcr-480-1" | .runner_id = 137')"
gcr_record_put 480 1 "$poll_source"
authoritative_jobs='{"id":472,"run_attempt":1,"status":"completed","conclusion":"success","runner_id":137,"runner_name":""}'
gcr_reconcile_runner_assignments
grep -q '^finish job=472 attempt=1 vm=174316480 state=completed:success via=polling$' "$calls"
test "$(gcr_record_field "$(gcr_record_get 480 1)" status)" = deferred
test ! -e "$(gcr_record_path 472 1)"
# Transfer replay covers intent-only and target-written/source-not-replaced
# crash points while duplicate records still count one logical VM.
crash_source="$(printf '%s' "$source_477" | jq -c \
'.job_id = "481" | .vm_id = 174316481 | .vm_name = "gcr-481-1"
| .runner_name = "gcr-481-1" | .runner_id = 139
| .status = "transfer_pending"
| .transfer_target_job_id = "473"
| .transfer_target_run_attempt = "1"
| .transfer_target_status = "vm_active"
| .transfer_mode = "assignment"
| .transfer_key_state = "source"')"
gcr_record_put 481 1 "$crash_source"
gcr_reconcile_transfers
crash_target="$(gcr_record_get 473 1)"
test "$(gcr_record_field "$crash_target" vm_id)" = 174316481
test "$(gcr_record_field "$(gcr_record_get 481 1)" status)" = deferred
gcr_record_put 481 1 "$crash_source"
test "$(gcr_count_active)" = 1
gcr_reconcile_transfers
test "$(gcr_record_field "$(gcr_record_get 481 1)" status)" = deferred
test "$(gcr_record_field "$(gcr_record_get 473 1)" vm_id)" = 174316481
gcr_record_del 473 1
gcr_record_del 481 1
# Interrupted per-file key move resumes from durable source phase after target
# publication. Preexisting target namespace remains a hard conflict.
partial_source="$(printf '%s' "$source_477" | jq -c \
'.job_id = "489" | .vm_id = 174316489 | .vm_name = "gcr-489-1"
| .runner_name = "gcr-489-1" | .runner_id = 143
| .status = "transfer_pending"
| .transfer_target_job_id = "478"
| .transfer_target_run_attempt = "1"
| .transfer_target_status = "vm_active"
| .transfer_mode = "assignment"
| .transfer_key_state = "source"')"
partial_target="$(printf '%s' "$partial_source" | jq -c \
'.job_id = "478" | .run_attempt = "1" | .status = "vm_active"
| .transfer_source_job_id = "489"
| .transfer_source_run_attempt = "1"
| del(.transfer_target_job_id, .transfer_target_run_attempt,
.transfer_target_status, .transfer_mode, .transfer_key_state)')"
gcr_record_put 489 1 "$partial_source"
gcr_record_put 478 1 "$partial_target"
gcr_vm_host_key_prepare 489 1
# Bootstrap-specific override is defined later.
# shellcheck disable=SC2218
gcr_vm_known_hosts_file 489 1 192.0.2.89 >/dev/null
mv "$(gcr_vm_host_key_path 489 1)" "$(gcr_vm_host_key_path 478 1)"
gcr_reconcile_transfers
test "$(gcr_record_field "$(gcr_record_get 489 1)" status)" = deferred
gcr_vm_host_key_pair_valid "$(gcr_vm_host_key_path 478 1)"
test -e "$(gcr_vm_host_key_path 478 1).known_hosts"
test ! -e "$(gcr_vm_host_key_path 489 1).pub"
gcr_record_del 478 1
gcr_record_del 489 1
conflict_source="$(printf '%s' "$source_477" | jq -c \
'.job_id = "490" | .vm_id = 174316490 | .vm_name = "gcr-490-1"
| .runner_name = "gcr-490-1" | .runner_id = 144')"
gcr_record_put 490 1 "$conflict_source"
gcr_vm_host_key_prepare 490 1
gcr_vm_host_key_prepare 479 1
if gcr_transfer_begin 490 1 479 1 vm_active assignment; then
printf 'preexisting target key namespace must fail closed\n' >&2
exit 1
fi
test "$(gcr_record_get 490 1)" = "$conflict_source"
test ! -e "$(gcr_record_path 479 1)"
gcr_record_del 490 1
gcr_record_del 479 1
# Repository mismatch and duplicate runner identity fail closed.
gcr_record_put 482 1 "$(printf '%s' "$source_477" | jq -c \
'.job_id = "482" | .vm_id = 174316482 | .runner_id = 140')"
before="$(gcr_record_get 482 1)"
authoritative_jobs='{"id":474,"run_attempt":1,"status":"in_progress","conclusion":"","runner_id":140,"runner_name":"gcr-482-1"}'
if gcr_apply_actual_job other/repo 140 gcr-482-1 474 1 vm_active webhook; then
printf 'wrong repository must fail closed\n' >&2
exit 1
fi
test "$(gcr_record_get 482 1)" = "$before"
gcr_record_put 483 1 "$(printf '%s' "$before" | jq -c \
'.job_id = "483" | .vm_id = 174316483')"
if gcr_apply_actual_job hinterland/hearth 140 gcr-482-1 474 1 vm_active webhook; then
printf 'ambiguous runner identity must fail closed\n' >&2
exit 1
fi
test ! -e "$(gcr_record_path 474 1)"
gcr_record_del 482 1
gcr_record_del 483 1
# Wrong runner assignment and ambiguous current or terminal history fail closed.
gcr_record_put 488 1 "$(printf '%s' "$source_477" | jq -c \
'.job_id = "488" | .vm_id = 174316488 | .runner_id = 142')"
authoritative_jobs='{"id":475,"run_attempt":1,"status":"in_progress","conclusion":"","runner_id":999,"runner_name":"other"}'
if gcr_apply_actual_job hinterland/hearth 142 gcr-488-1 475 1 vm_active webhook; then
printf 'API runner mismatch must fail closed\n' >&2
exit 1
fi
test ! -e "$(gcr_record_path 475 1)"
authoritative_jobs='{"id":488,"run_attempt":1,"status":"completed","conclusion":"failure","runner_id":142,"runner_name":"gcr-488-1"}
{"id":475,"run_attempt":1,"status":"in_progress","conclusion":"","runner_id":142,"runner_name":"gcr-488-1"}
{"id":476,"run_attempt":1,"status":"in_progress","conclusion":"","runner_id":142,"runner_name":"gcr-488-1"}'
if gcr_apply_actual_job hinterland/hearth 142 gcr-488-1 475 1 vm_active webhook; then
printf 'multiple active runner jobs must fail closed\n' >&2
exit 1
fi
test "$(gcr_record_get 488 1)" = "$(printf '%s' "$source_477" | jq -c \
'.job_id = "488" | .vm_id = 174316488 | .runner_id = 142')"
gcr_gitea_job_state() { printf completed:failure; }
gcr_reap_finished_jobs
test "$(gcr_record_get 488 1)" = "$(printf '%s' "$source_477" | jq -c \
'.job_id = "488" | .vm_id = 174316488 | .runner_id = 142')"
test "$(grep -c '^finish job=488 ' "$calls" || true)" = 0
authoritative_jobs='{"id":475,"run_attempt":1,"status":"completed","conclusion":"success","runner_id":142,"runner_name":""}
{"id":476,"run_attempt":1,"status":"completed","conclusion":"failure","runner_id":142,"runner_name":""}'
if gcr_apply_actual_job hinterland/hearth 142 gcr-488-1 475 1 \
completed:success polling; then
printf 'multiple terminal runner jobs must fail closed\n' >&2
exit 1
fi
gcr_gitea_job_state() { printf queued:; }
gcr_record_del 488 1
# Bootstrap registration is inert until exact runner ID has been persisted.
bootstrap_rec="$(printf '%s' "$source_477" | jq -c \
'.job_id = "484" | .vm_id = 174316484 | .vm_name = "gcr-484-1"
| .runner_name = "gcr-484-1" | .runner_id = ""
| .bootstrapped = false | .status = "pending_vm"')"
gcr_record_put 484 1 "$bootstrap_rec"
gcr_vm_public_ip() { printf '192.0.2.84'; }
gcr_vm_known_hosts_file() {
known="$GCR_STATE_DIR/known-hosts"
printf '192.0.2.84 ssh-ed25519 test\n' > "$known"
printf '%s' "$known"
}
gcr_gitea_job_state() { printf queued:; }
gcr_gitea_registration_token() { printf test-token; }
runner_registered=false
gcr_gitea_find_runner_exact() {
[ "$runner_registered" = true ] || return 1
GCR_RUNNER_ID=138
GCR_RUNNER_DISABLED=false
}
gcr_vm_bootstrap_ssh() {
test -z "$(gcr_record_field "$(gcr_record_get 484 1)" runner_id)"
printf 'register\n' >> "$calls"
runner_registered=true
}
gcr_vm_runner_service_pinned() {
test "$3" = start
test "$(gcr_record_field "$(gcr_record_get 484 1)" runner_id)" = 138
printf 'start\n' >> "$calls"
}
gcr_bootstrap_pending
test "$(grep -E '^(register|start)$' "$calls")" = 'register
start'
j484="$(gcr_record_get 484 1)"
test "$(gcr_record_field "$j484" runner_id)" = 138
test "$(gcr_record_field "$j484" bootstrapped)" = true
gcr_record_del 484 1
# Legacy live name lookup persists ID. Confirmed absent registration and no
# active jobs creates durable cleanup owner plus deferred origin; API uncertainty
# leaves VM untouched.
legacy="$(printf '%s' "$source_477" | jq -c \
'.job_id = "485" | .vm_id = 174316485 | .vm_name = "gcr-485-1"
| .runner_name = "gcr-485-1" | .runner_id = ""')"
gcr_record_put 485 1 "$legacy"
legacy_mode=present
gcr_gitea_find_runner_exact() {
case "$legacy_mode" in
present) GCR_RUNNER_ID=141; GCR_RUNNER_DISABLED=false; return 0 ;;
absent) return 1 ;;
unknown) return 2 ;;
esac
}
gcr_gitea_repo_has_active_jobs() {
[ "$legacy_mode" = absent ] && return 1
return 2
}
gcr_recover_legacy_runner_ids
test "$(gcr_record_field "$(gcr_record_get 485 1)" runner_id)" = 141
gcr_record_del 485 1
legacy_mode=absent
gcr_record_put 486 1 "$(printf '%s' "$legacy" | jq -c \
'.job_id = "486" | .vm_id = 174316486 | .vm_name = "gcr-486-1"
| .runner_name = "gcr-486-1"')"
gcr_recover_legacy_runner_ids
test "$(gcr_record_field "$(gcr_record_get 486 1)" status)" = deferred
legacy_cleanup="$(gcr_record_get legacy-cleanup-486 1)"
test "$(gcr_record_field "$legacy_cleanup" status)" = cleanup_pending
test "$(gcr_record_field "$legacy_cleanup" vm_id)" = 174316486
test "$(gcr_count_active)" = 1
gcr_record_del 486 1
gcr_record_del legacy-cleanup-486 1
legacy_mode=unknown
gcr_record_put 487 1 "$(printf '%s' "$legacy" | jq -c \
'.job_id = "487" | .vm_id = 174316487 | .vm_name = "gcr-487-1"
| .runner_name = "gcr-487-1"')"
gcr_recover_legacy_runner_ids
test "$(gcr_record_get 487 1)" = "$(printf '%s' "$legacy" | jq -c \
'.job_id = "487" | .vm_id = 174316487 | .vm_name = "gcr-487-1"
| .runner_name = "gcr-487-1"')"