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
+18
View File
@@ -595,6 +595,24 @@ server identity, current image/profile, and current IP before fetching a fresh
destination-repository token and bootstrapping a new runner name. Old runner destination-repository token and bootstrapping a new runner name. Old runner
registration and filesystem are never reused. registration and filesystem are never reused.
Runner allocation and Gitea task assignment are separate identities. Bootstrap
installs and registers with the daemon stopped, resolves the exact repository
runner registration, persists its numeric `runner_id`, and only then starts the
daemon over the pinned SSH channel. Webhooks and unfiltered paginated polling
bind lifecycle by exact repository plus `runner_id`; terminal `runner_name` is
not authoritative because Gitea may already have deleted the ephemeral runner
row. If a runner consumes a different queued job, controller first persists a
`transfer_pending` intent, moves VM lifecycle to the actual job, and restores
the allocation-origin job as VM-less deferred work. Reconciliation replays this
ordered transfer before TTL, orphan, capacity, or deferred sweeps, and duplicate
crash-intermediate records count as one VM.
Pre-upgrade records without `runner_id` use exact live runner-name lookup once.
If registration is authoritatively absent and complete repository job polling
proves no job is active, VM moves to durable synthetic cleanup ownership while
origin is restored to deferred. Any malformed, ambiguous, repository-mismatched,
or unavailable identity data leaves state and VM untouched for later retry.
For every creation and reuse rebuild, the controller generates a unique SSH For every creation and reuse rebuild, the controller generates a unique SSH
host key, injects it through authenticated Hetzner `user_data`, and pins that host key, injects it through authenticated Hetzner `user_data`, and pins that
exact public key before sending a registration token. Shared image host keys exact public key before sending a registration token. Shared image host keys
+170 -12
View File
@@ -62,7 +62,7 @@ gcr_sweep_ttl() {
fi fi
case "$status" in case "$status" in
create_pending|pending_vm|vm_active|retiring_vm|rebuild_pending|rebuild_requesting|rebuild_running) ;; create_pending|pending_vm|vm_active|transfer_pending|retiring_vm|rebuild_pending|rebuild_requesting|rebuild_running) ;;
*) continue ;; *) continue ;;
esac esac
ttl_min="$(gcr_record_field "$rec" ttl_min)" ttl_min="$(gcr_record_field "$rec" ttl_min)"
@@ -75,7 +75,7 @@ gcr_sweep_ttl() {
gcr_lock_acquire "$key" || continue gcr_lock_acquire "$key" || continue
rec="$(gcr_record_get "$job_id" "$attempt")" rec="$(gcr_record_get "$job_id" "$attempt")"
case "$(gcr_record_field "$rec" status)" in case "$(gcr_record_field "$rec" status)" in
create_pending|pending_vm|vm_active|retiring_vm|rebuild_pending|rebuild_requesting|rebuild_running) ;; create_pending|pending_vm|vm_active|transfer_pending|retiring_vm|rebuild_pending|rebuild_requesting|rebuild_running) ;;
*) gcr_lock_release "$key"; continue ;; *) gcr_lock_release "$key"; continue ;;
esac esac
ttl_min="$(gcr_record_field "$rec" ttl_min)" ttl_min="$(gcr_record_field "$rec" ttl_min)"
@@ -563,6 +563,108 @@ EOF
IFS="$oldIFS" IFS="$oldIFS"
} }
# Upgrade recovery for records created before runner_id persistence. Exact live
# name lookup binds safely. Confirmed absence plus complete no-active-job scan
# moves VM to synthetic cleanup ownership and restores origin to deferred.
gcr_recover_legacy_runner_ids() {
for legacy_file in $(gcr_active_records); do
legacy_rec="$(cat "$legacy_file")"
case "$(gcr_record_field "$legacy_rec" status)" in
pending_vm|vm_active) ;;
*) continue ;;
esac
[ "$(gcr_record_field "$legacy_rec" bootstrapped)" = true ] || continue
[ -z "$(gcr_record_field "$legacy_rec" runner_id)" ] || continue
legacy_job="$(gcr_record_field "$legacy_rec" job_id)"
legacy_attempt="$(gcr_record_field "$legacy_rec" run_attempt)"
legacy_repo="$(gcr_record_field "$legacy_rec" repo)"
legacy_runner="$(gcr_record_field "$legacy_rec" runner_name)"
[ -n "$legacy_repo" ] && [ -n "$legacy_runner" ] || continue
gcr_repo_allowed "$legacy_repo" || continue
legacy_lookup=0
gcr_gitea_find_runner_exact "$legacy_repo" "$legacy_runner" \
|| legacy_lookup="$?"
case "$legacy_lookup" in
0)
legacy_key="$(gcr_alloc_key "$legacy_job" "$legacy_attempt")"
gcr_lock_acquire "$legacy_key" || continue
legacy_rec="$(gcr_record_get "$legacy_job" "$legacy_attempt")"
if [ -n "$legacy_rec" ] \
&& [ -z "$(gcr_record_field "$legacy_rec" runner_id)" ] \
&& [ "$(gcr_record_field "$legacy_rec" repo)" = "$legacy_repo" ] \
&& [ "$(gcr_record_field "$legacy_rec" runner_name)" = "$legacy_runner" ]; then
legacy_rec="$(printf '%s' "$legacy_rec" | jq -c \
--arg id "$GCR_RUNNER_ID" \
'.runner_id = ($id | tonumber)')" \
&& gcr_record_put "$legacy_job" "$legacy_attempt" \
"$legacy_rec" || true
fi
gcr_lock_release "$legacy_key"
;;
1)
legacy_active=0
gcr_gitea_repo_has_active_jobs "$legacy_repo" \
|| legacy_active="$?"
[ "$legacy_active" -eq 1 ] || continue
legacy_cleanup_job="legacy-cleanup-$legacy_job"
if gcr_transfer_begin "$legacy_job" "$legacy_attempt" \
"$legacy_cleanup_job" "$legacy_attempt" cleanup_pending \
legacy_cleanup; then
gcr_event "legacy-runner-recovered" "$legacy_job" \
"{\"repo\":\"$legacy_repo\",\"runner\":\"$legacy_runner\"}"
fi
;;
*) ;;
esac
done
}
# Poll repository job history by runner ID. A unique active job takes precedence
# over legacy terminal history; ambiguous active or terminal matches stay put.
gcr_reconcile_runner_assignments() {
for assignment_file in $(gcr_active_records); do
assignment_rec="$(cat "$assignment_file")"
case "$(gcr_record_field "$assignment_rec" status)" in
pending_vm|vm_active) ;;
*) continue ;;
esac
assignment_runner="$(gcr_record_field "$assignment_rec" runner_id)"
case "$assignment_runner" in ''|0|*[!0-9]*) continue ;; esac
assignment_repo="$(gcr_record_field "$assignment_rec" repo)"
gcr_repo_allowed "$assignment_repo" || continue
assignment_jobs="$(gcr_gitea_list_jobs "$assignment_repo")" || continue
assignment_matches="$(printf '%s\n' "$assignment_jobs" | jq -cs \
--arg runner "$assignment_runner" \
'[.[] | select((.runner_id | tostring) == $runner)]
| [.[] | select(.status == "in_progress")] as $active
| if ($active | length) == 1 then $active
elif ($active | length) > 1 then []
else [.[] | select(.status == "completed")]
| if length == 1 then . else [] end
end')" || continue
[ "$(printf '%s' "$assignment_matches" | jq 'length')" -eq 1 ] \
|| continue
assignment_job="$(printf '%s' "$assignment_matches" | jq -r '.[0].id')"
assignment_attempt="$(printf '%s' "$assignment_matches" | jq -r '.[0].run_attempt')"
assignment_status="$(printf '%s' "$assignment_matches" | jq -r '.[0].status')"
assignment_conclusion="$(printf '%s' "$assignment_matches" | jq -r '.[0].conclusion')"
assignment_name="$(printf '%s' "$assignment_matches" | jq -r '.[0].runner_name')"
case "$assignment_status" in
in_progress)
gcr_apply_actual_job "$assignment_repo" "$assignment_runner" \
"$assignment_name" "$assignment_job" "$assignment_attempt" \
vm_active polling || true
;;
completed)
gcr_apply_actual_job "$assignment_repo" "$assignment_runner" \
"$assignment_name" "$assignment_job" "$assignment_attempt" \
"completed:${assignment_conclusion:-unknown}" polling || true
;;
esac
done
}
# Runs SSH-push bootstrap for VMs that were created but not yet provisioned. # Runs SSH-push bootstrap for VMs that were created but not yet provisioned.
# Registration token is fetched fresh per attempt (short-lived usefulness). # Registration token is fetched fresh per attempt (short-lived usefulness).
gcr_bootstrap_pending() { gcr_bootstrap_pending() {
@@ -628,10 +730,6 @@ gcr_bootstrap_pending() {
continue continue
fi fi
reg_token="$(gcr_gitea_registration_token "$repo")" || {
gcr_lock_release "$key"
continue
}
ttl_min="$(gcr_record_field "$rec" ttl_min)" ttl_min="$(gcr_record_field "$rec" ttl_min)"
known_hosts_file="$(gcr_vm_known_hosts_file \ known_hosts_file="$(gcr_vm_known_hosts_file \
"$job_id" "$attempt" "$ip")" || { "$job_id" "$attempt" "$ip")" || {
@@ -641,14 +739,59 @@ gcr_bootstrap_pending() {
continue continue
} }
gcr_log info --ns=alloc "bootstrapping vm=$vm_id ip=$ip job=$job_id" runner_id="$(gcr_record_field "$rec" runner_id)"
if gcr_vm_bootstrap_ssh "$ip" "$label" "$reg_token" "$runner_name" \ if [ -z "$runner_id" ]; then
"$known_hosts_file" "$job_id" "$attempt" "$vm_id"; then lookup_status=0
rec="$(printf '%s' "$rec" | jq -c '.bootstrapped = true | .ip = $ip' --arg ip "$ip")" gcr_gitea_find_runner_exact "$repo" "$runner_name" \
|| lookup_status="$?"
if [ "$lookup_status" -eq 1 ]; then
reg_token="$(gcr_gitea_registration_token "$repo")" || {
gcr_lock_release "$key"
continue
}
gcr_log info --ns=alloc \
"registering vm=$vm_id ip=$ip job=$job_id"
if ! gcr_vm_bootstrap_ssh "$ip" "$label" "$reg_token" \
"$runner_name" "$known_hosts_file" "$job_id" "$attempt" \
"$vm_id"; then
gcr_log warn --ns=alloc \
"bootstrap registration failed vm=$vm_id (retry next tick)"
gcr_lock_release "$key"
continue
fi
lookup_status=0
gcr_gitea_find_runner_exact "$repo" "$runner_name" \
|| lookup_status="$?"
fi
if [ "$lookup_status" -ne 0 ] \
|| [ "$GCR_RUNNER_DISABLED" != false ]; then
gcr_lock_release "$key"
continue
fi
runner_id="$GCR_RUNNER_ID"
rec="$(printf '%s' "$rec" | jq -c --arg id "$runner_id" \
--arg ip "$ip" \
'.runner_id = ($id | tonumber) | .ip = $ip')" || {
gcr_lock_release "$key"
continue
}
gcr_record_put "$job_id" "$attempt" "$rec" || {
gcr_lock_release "$key"
continue
}
gcr_event "runner-id-persisted" "$job_id" \
"{\"vm_id\":$vm_id,\"runner_id\":$runner_id}"
fi
if gcr_vm_runner_service_pinned "$ip" "$known_hosts_file" start; then
rec="$(printf '%s' "$rec" | jq -c \
'.bootstrapped = true | .ip = $ip' --arg ip "$ip")"
gcr_record_put "$job_id" "$attempt" "$rec" gcr_record_put "$job_id" "$attempt" "$rec"
gcr_event "vm-bootstrapped" "$job_id" "{\"vm_id\":$vm_id,\"ip\":\"$ip\"}" gcr_event "vm-bootstrapped" "$job_id" \
"{\"vm_id\":$vm_id,\"ip\":\"$ip\",\"runner_id\":$runner_id}"
else else
gcr_log warn --ns=alloc "bootstrap failed vm=$vm_id (retry next tick)" gcr_log warn --ns=alloc \
"runner activation failed vm=$vm_id (retry next tick)"
fi fi
gcr_lock_release "$key" gcr_lock_release "$key"
done done
@@ -682,6 +825,18 @@ gcr_reap_finished_jobs() {
pending_vm|vm_active) ;; pending_vm|vm_active) ;;
*) gcr_lock_release "$key"; continue ;; *) gcr_lock_release "$key"; continue ;;
esac esac
runner_id="$(gcr_record_field "$rec" runner_id)"
if [ -n "$runner_id" ]; then
authoritative_state="$(gcr_gitea_confirm_runner_job \
"$repo" "$runner_id" "$job_id" "$attempt")" || {
gcr_lock_release "$key"
continue
}
case "$authoritative_state" in
completed:*) state="$authoritative_state" ;;
*) gcr_lock_release "$key"; continue ;;
esac
fi
finish_status=0 finish_status=0
gcr_vm_finish_terminal "$job_id" "$attempt" "$rec" "$state" reconcile \ gcr_vm_finish_terminal "$job_id" "$attempt" "$rec" "$state" reconcile \
|| finish_status="$?" || finish_status="$?"
@@ -696,6 +851,9 @@ gcr_reap_finished_jobs() {
} }
gcr_tick() { gcr_tick() {
gcr_reconcile_transfers
gcr_recover_legacy_runner_ids
gcr_reconcile_runner_assignments
gcr_sweep_create_ambiguous gcr_sweep_create_ambiguous
gcr_sweep_cleanup_pending gcr_sweep_cleanup_pending
gcr_sweep_retiring_vms gcr_sweep_retiring_vms
+24 -4
View File
@@ -159,10 +159,20 @@ gcr_decide() {
gcr_count_active() { gcr_count_active() {
count=0 count=0
seen_vms=" "
for f in $(gcr_active_records); do for f in $(gcr_active_records); do
status="$(gcr_record_field "$(cat "$f")" status)" rec="$(cat "$f")"
status="$(gcr_record_field "$rec" status)"
case "$status" in case "$status" in
create_pending|create_ambiguous|pending_vm|vm_active|retiring_vm|rebuild_pending|rebuild_requesting|rebuild_running|cleanup_pending) create_pending|create_ambiguous|pending_vm|vm_active|transfer_pending|retiring_vm|rebuild_pending|rebuild_requesting|rebuild_running|cleanup_pending)
vm_id="$(gcr_record_field "$rec" vm_id)"
case "$vm_id" in
''|0|null) vm_identity="record:${f##*/}" ;;
*[!0-9]*) vm_identity="record:${f##*/}" ;;
*) vm_identity="vm:$vm_id" ;;
esac
case "$seen_vms" in *" $vm_identity "*) continue ;; esac
seen_vms="$seen_vms$vm_identity "
count=$((count + 1)) count=$((count + 1))
;; ;;
esac esac
@@ -173,13 +183,23 @@ gcr_count_active() {
gcr_count_active_repo() { gcr_count_active_repo() {
repo="$1" repo="$1"
count=0 count=0
seen_vms=" "
for f in $(gcr_active_records); do for f in $(gcr_active_records); do
rec="$(cat "$f")" rec="$(cat "$f")"
case "$(gcr_record_field "$rec" status)" in case "$(gcr_record_field "$rec" status)" in
create_pending|create_ambiguous|pending_vm|vm_active|retiring_vm|rebuild_pending|rebuild_requesting|rebuild_running|cleanup_pending) ;; create_pending|create_ambiguous|pending_vm|vm_active|transfer_pending|retiring_vm|rebuild_pending|rebuild_requesting|rebuild_running|cleanup_pending) ;;
*) continue ;; *) continue ;;
esac esac
[ "$(gcr_record_field "$rec" repo)" = "$repo" ] && count=$((count + 1)) [ "$(gcr_record_field "$rec" repo)" = "$repo" ] || continue
vm_id="$(gcr_record_field "$rec" vm_id)"
case "$vm_id" in
''|0|null) vm_identity="record:${f##*/}" ;;
*[!0-9]*) vm_identity="record:${f##*/}" ;;
*) vm_identity="vm:$vm_id" ;;
esac
case "$seen_vms" in *" $vm_identity "*) continue ;; esac
seen_vms="$seen_vms$vm_identity "
count=$((count + 1))
done done
printf '%s' "$count" printf '%s' "$count"
} }
+83
View File
@@ -150,6 +150,89 @@ gcr_gitea_job_state() {
| jq -r '.status + ":" + (.conclusion // "")' | jq -r '.status + ":" + (.conclusion // "")'
} }
# List every repository job without a status filter. Each line is compact JSON;
# complete pagination and strict schema validation make absence authoritative.
gcr_gitea_list_jobs() {
jobs_repo="$1"
gcr_gitea_admin_token >/dev/null || return 1
jobs_owner="${jobs_repo%%/*}"
jobs_name="${jobs_repo#*/}"
jobs_page=1
while :; do
jobs_json="$(gcr_gitea_curl -fsS \
"$GCR_GITEA_URL/api/v1/repos/$jobs_owner/$jobs_name/actions/jobs?page=$jobs_page&limit=50")" \
|| return 1
printf '%s' "$jobs_json" | jq -e \
'.jobs | select(type == "array")
| all(.[];
(.id | type) == "number" and .id > 0
and (.run_attempt | type) == "number" and .run_attempt >= 0
and (.status | type) == "string" and (.status | length) > 0
and ((.conclusion | type) == "string" or
(.conclusion | type) == "null")
and ((.runner_id | type) == "number" or
(.runner_id | type) == "null")
and ((.runner_name | type) == "string" or
(.runner_name | type) == "null"))' \
>/dev/null || return 1
printf '%s' "$jobs_json" | jq -c \
'.jobs[] | {id, run_attempt, status,
conclusion:(.conclusion // ""),
runner_id:(.runner_id // null),
runner_name:(.runner_name // "")}' || return 1
jobs_count="$(printf '%s' "$jobs_json" | jq -er '.jobs | length')" \
|| return 1
[ "$jobs_count" -lt 50 ] && return 0
jobs_page=$((jobs_page + 1))
done
}
# Exit 0 only when complete unfiltered history proves no active repository job.
gcr_gitea_repo_has_active_jobs() {
active_jobs="$(gcr_gitea_list_jobs "$1")" || return 2
if printf '%s\n' "$active_jobs" \
| jq -s -e 'any(.[]; .status == "in_progress")' >/dev/null; then
return 0
fi
return 1
}
# Confirm the current job belonging to repository-scoped RUNNER_ID and require
# that it is the requested job/attempt. One active match takes precedence over
# terminal history; without an active match, exactly one terminal match is
# required. Prints current authoritative state; event state is not trusted.
gcr_gitea_confirm_runner_job() {
confirm_repo="$1"; confirm_runner="$2"
confirm_job="$3"; confirm_attempt="$4"
case "$confirm_runner:$confirm_job:$confirm_attempt" in
*[!0-9:]*|0:*|*:0:*|:*|*::*|*:) return 1 ;;
esac
confirm_jobs="$(gcr_gitea_list_jobs "$confirm_repo")" || return 1
confirm_matches="$(printf '%s\n' "$confirm_jobs" | jq -cs \
--arg runner "$confirm_runner" \
'[.[] | select((.runner_id | tostring) == $runner)]')" || return 1
confirm_selected="$(printf '%s' "$confirm_matches" | jq -ce '
[.[] | select(.status == "in_progress")] as $active
| if ($active | length) == 1 then $active[0]
elif ($active | length) > 1 then empty
else [.[] | select(.status == "completed")] as $terminal
| if ($terminal | length) == 1 then $terminal[0] else empty end
end')" || return 1
printf '%s' "$confirm_selected" | jq -e --arg job "$confirm_job" \
--arg attempt "$confirm_attempt" \
'select((.id | tostring) == $job
and (.run_attempt | tostring) == $attempt)' \
>/dev/null || return 1
confirm_status="$(printf '%s' "$confirm_selected" | jq -r '.status')"
confirm_conclusion="$(printf '%s' "$confirm_selected" | jq -r \
'.conclusion')"
case "$confirm_status" in
in_progress) printf 'vm_active' ;;
completed) printf 'completed:%s' "${confirm_conclusion:-unknown}" ;;
*) return 1 ;;
esac
}
# Exit 0 when an in-progress repository job is assigned to RUNNER_ID, 1 when # Exit 0 when an in-progress repository job is assigned to RUNNER_ID, 1 when
# complete pagination proves none, and 2 on API or schema uncertainty. # complete pagination proves none, and 2 on API or schema uncertainty.
gcr_gitea_runner_has_active_job() { gcr_gitea_runner_has_active_job() {
+13 -7
View File
@@ -652,21 +652,26 @@ gcr_vm_verify_rebuilt() {
# The controller starts the service only after the claim record is written. # The controller starts the service only after the claim record is written.
gcr_vm_runner_service() { gcr_vm_runner_service() {
vm_id="$1"; action="$2" vm_id="$1"; action="$2"
ip="$(gcr_vm_public_ip "$vm_id")" || return 1
[ -n "$ip" ] || return 1
gcr_vm_runner_service_pinned "$ip" \
"${GCR_SSH_KNOWN_HOSTS_FILE:-}" "$action"
}
gcr_vm_runner_service_pinned() {
ip="$1"; known_hosts_file="$2"; action="$3"
case "$action" in case "$action" in
start|stop) service_command="systemctl $action gitea-runner.service" ;; start|stop) service_command="systemctl $action gitea-runner.service" ;;
health) service_command="systemctl is-active --quiet gitea-runner.service" ;; health) service_command="systemctl is-active --quiet gitea-runner.service" ;;
*) return 1 ;; *) return 1 ;;
esac esac
ip="$(gcr_vm_public_ip "$vm_id")" || return 1
[ -n "$ip" ] || return 1
test -n "${GCR_SSH_PRIVKEY_FILE:-}" && test -r "$GCR_SSH_PRIVKEY_FILE" || return 1 test -n "${GCR_SSH_PRIVKEY_FILE:-}" && test -r "$GCR_SSH_PRIVKEY_FILE" || return 1
test -n "${GCR_SSH_KNOWN_HOSTS_FILE:-}" \ test -n "$known_hosts_file" && test -r "$known_hosts_file" || return 1
&& test -r "$GCR_SSH_KNOWN_HOSTS_FILE" || return 1
key_tmp="$(mktemp "${TMPDIR:-/tmp}/gcr-runner-sshkey.XXXXXX")" key_tmp="$(mktemp "${TMPDIR:-/tmp}/gcr-runner-sshkey.XXXXXX")"
cat "$GCR_SSH_PRIVKEY_FILE" > "$key_tmp" cat "$GCR_SSH_PRIVKEY_FILE" > "$key_tmp"
printf '\n' >> "$key_tmp" printf '\n' >> "$key_tmp"
chmod 0600 "$key_tmp" chmod 0600 "$key_tmp"
ssh_opts="-i $key_tmp -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=$GCR_SSH_KNOWN_HOSTS_FILE -o ConnectTimeout=5 -o BatchMode=yes" ssh_opts="-i $key_tmp -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=$known_hosts_file -o ConnectTimeout=5 -o BatchMode=yes"
if timeout 30 ssh $ssh_opts "root@$ip" "$service_command"; then if timeout 30 ssh $ssh_opts "root@$ip" "$service_command"; then
rm -f "$key_tmp" rm -f "$key_tmp"
return 0 return 0
@@ -1015,6 +1020,7 @@ token_file=/run/gcr-registration-token
umask 077 umask 077
printf '%s' '$reg_token_b64' | base64 -d > "\$token_file" printf '%s' '$reg_token_b64' | base64 -d > "\$token_file"
trap 'rm -f "\$token_file"' EXIT HUP INT TERM trap 'rm -f "\$token_file"' EXIT HUP INT TERM
systemctl disable --now gitea-runner.service 2>/dev/null || true
if [ ! -s /var/lib/gitea-runner/.runner ]; then if [ ! -s /var/lib/gitea-runner/.runner ]; then
/usr/local/bin/gitea-runner register --no-interactive --ephemeral \ /usr/local/bin/gitea-runner register --no-interactive --ephemeral \
--instance '$GCR_GITEA_URL' --token "\$(cat "\$token_file")" \ --instance '$GCR_GITEA_URL' --token "\$(cat "\$token_file")" \
@@ -1024,12 +1030,12 @@ fi
rm -f "\$token_file" rm -f "\$token_file"
trap - EXIT HUP INT TERM trap - EXIT HUP INT TERM
systemctl daemon-reload systemctl daemon-reload
systemctl enable --now gitea-runner.service
BSEOF BSEOF
} }
# gcr_vm_bootstrap_ssh IP LABEL REG_TOKEN RUNNER_NAME KNOWN_HOSTS JOB ATTEMPT VM # gcr_vm_bootstrap_ssh IP LABEL REG_TOKEN RUNNER_NAME KNOWN_HOSTS JOB ATTEMPT VM
# Waits for strict-pinned SSH readiness, then pushes the bootstrap script. # Waits for strict-pinned SSH readiness, then installs and registers without
# starting the daemon. Controller persists exact runner ID before activation.
gcr_vm_bootstrap_ssh() ( gcr_vm_bootstrap_ssh() (
ip="$1"; label="$2"; reg_token="$3"; runner_name="$4" ip="$1"; label="$2"; reg_token="$3"; runner_name="$4"
known_hosts_file="${5:-${GCR_SSH_KNOWN_HOSTS_FILE:-}}" known_hosts_file="${5:-${GCR_SSH_KNOWN_HOSTS_FILE:-}}"
+423 -7
View File
@@ -119,6 +119,421 @@ gcr_record_del() {
"$GCR_STATE_DIR/ssh-host-keys/$(gcr_alloc_key "$1" "$2").known_hosts" "$GCR_STATE_DIR/ssh-host-keys/$(gcr_alloc_key "$1" "$2").known_hosts"
} }
# Acquire two allocation locks in lexical order. A failed second acquisition
# releases the first, so webhook and reconcile paths cannot deadlock.
gcr_pair_lock_acquire() {
gcr_pair_a="$1"; gcr_pair_b="$2"
if [ "$gcr_pair_a" = "$gcr_pair_b" ]; then
GCR_PAIR_FIRST="$gcr_pair_a"
GCR_PAIR_SECOND=""
else
GCR_PAIR_FIRST="$(printf '%s\n%s\n' "$gcr_pair_a" "$gcr_pair_b" \
| LC_ALL=C sort | sed -n '1p')"
if [ "$GCR_PAIR_FIRST" = "$gcr_pair_a" ]; then
GCR_PAIR_SECOND="$gcr_pair_b"
else
GCR_PAIR_SECOND="$gcr_pair_a"
fi
fi
gcr_lock_acquire "$GCR_PAIR_FIRST" || return 1
if [ -n "$GCR_PAIR_SECOND" ] \
&& ! gcr_lock_acquire "$GCR_PAIR_SECOND"; then
gcr_lock_release "$GCR_PAIR_FIRST"
return 1
fi
}
gcr_pair_lock_release() {
[ -z "${GCR_PAIR_SECOND:-}" ] || gcr_lock_release "$GCR_PAIR_SECOND"
[ -z "${GCR_PAIR_FIRST:-}" ] || gcr_lock_release "$GCR_PAIR_FIRST"
}
gcr_transfer_key_base() {
printf '%s/ssh-host-keys/%s' "$GCR_STATE_DIR" "$(gcr_alloc_key "$1" "$2")"
}
# New transfer intent is written only when target key namespace is empty.
# Source private/public pair must be complete when present; known_hosts is
# optional because it is derived after IP discovery.
gcr_transfer_keys_preflight() {
gcr_keys_source="$(gcr_transfer_key_base "$1" "$2")"
gcr_keys_target="$(gcr_transfer_key_base "$3" "$4")"
[ "$gcr_keys_source" != "$gcr_keys_target" ] || return 0
for gcr_keys_suffix in '' .pub .known_hosts; do
[ ! -e "$gcr_keys_target$gcr_keys_suffix" ] || return 1
done
if [ -e "$gcr_keys_source" ] || [ -e "$gcr_keys_source.pub" ]; then
[ -s "$gcr_keys_source" ] && [ -s "$gcr_keys_source.pub" ] || return 1
fi
}
# Caller holds both allocation locks. Per-file rename is replayable: intent
# distinguishes an interrupted move from a preexisting target-key conflict.
gcr_transfer_keys_locked() {
gcr_keys_source_job="$1"; gcr_keys_source_attempt="$2"
gcr_keys_target_job="$3"; gcr_keys_target_attempt="$4"
gcr_keys_rec="$5"
gcr_keys_phase="$(gcr_record_field "$gcr_keys_rec" transfer_key_state)"
case "$gcr_keys_phase" in source|target) ;; *) return 1 ;; esac
gcr_keys_source="$(gcr_transfer_key_base \
"$gcr_keys_source_job" "$gcr_keys_source_attempt")"
gcr_keys_target="$(gcr_transfer_key_base \
"$gcr_keys_target_job" "$gcr_keys_target_attempt")"
if [ "$gcr_keys_phase" = target ]; then
for gcr_keys_suffix in '' .pub .known_hosts; do
[ ! -e "$gcr_keys_source$gcr_keys_suffix" ] || return 1
done
printf '%s' "$gcr_keys_rec"
return 0
fi
for gcr_keys_suffix in '' .pub .known_hosts; do
gcr_keys_src="$gcr_keys_source$gcr_keys_suffix"
gcr_keys_dst="$gcr_keys_target$gcr_keys_suffix"
if [ -e "$gcr_keys_src" ]; then
if [ -e "$gcr_keys_dst" ]; then
cmp -s "$gcr_keys_src" "$gcr_keys_dst" || return 1
rm -f "$gcr_keys_src" || return 1
else
mv "$gcr_keys_src" "$gcr_keys_dst" || return 1
fi
fi
done
for gcr_keys_suffix in '' .pub .known_hosts; do
[ ! -e "$gcr_keys_source$gcr_keys_suffix" ] || return 1
done
gcr_keys_rec="$(printf '%s' "$gcr_keys_rec" \
| jq -c '.transfer_key_state = "target"')" || return 1
gcr_record_put "$gcr_keys_source_job" "$gcr_keys_source_attempt" \
"$gcr_keys_rec" || return 1
printf '%s' "$gcr_keys_rec"
}
# Print the unique active record key bound to repository-scoped runner ID.
# Duplicate identities are corruption and fail closed.
gcr_record_find_runner_id() {
gcr_find_repo="$1"; gcr_find_runner_id="$2"
case "$gcr_find_runner_id" in ''|0|*[!0-9]*) return 1 ;; esac
gcr_find_count=0
gcr_find_result=""
for gcr_find_file in $(gcr_active_records); do
gcr_find_rec="$(cat "$gcr_find_file")"
[ "$(gcr_record_field "$gcr_find_rec" repo)" = "$gcr_find_repo" ] \
|| continue
[ "$(gcr_record_field "$gcr_find_rec" runner_id)" = "$gcr_find_runner_id" ] \
|| continue
gcr_find_count=$((gcr_find_count + 1))
gcr_find_result="$(gcr_record_field "$gcr_find_rec" job_id) $(gcr_record_field "$gcr_find_rec" run_attempt)"
done
[ "$gcr_find_count" -eq 1 ] || return 1
printf '%s' "$gcr_find_result"
}
gcr_record_find_legacy_runner_name() {
gcr_find_repo="$1"; gcr_find_runner_name="$2"
[ -n "$gcr_find_runner_name" ] || return 1
gcr_find_count=0
gcr_find_result=""
for gcr_find_file in $(gcr_active_records); do
gcr_find_rec="$(cat "$gcr_find_file")"
[ "$(gcr_record_field "$gcr_find_rec" repo)" = "$gcr_find_repo" ] \
|| continue
[ -z "$(gcr_record_field "$gcr_find_rec" runner_id)" ] || continue
[ "$(gcr_record_field "$gcr_find_rec" runner_name)" \
= "$gcr_find_runner_name" ] || continue
case "$(gcr_record_field "$gcr_find_rec" status)" in
pending_vm|vm_active) ;;
*) continue ;;
esac
gcr_find_count=$((gcr_find_count + 1))
gcr_find_result="$(gcr_record_field "$gcr_find_rec" job_id) $(gcr_record_field "$gcr_find_rec" run_attempt)"
done
[ "$gcr_find_count" -eq 1 ] || return 1
printf '%s' "$gcr_find_result"
}
# Resolve repository-scoped runner identity. Legacy name fallback is accepted
# only while the live exact registration maps that name to the supplied ID.
gcr_record_resolve_runner() {
gcr_resolve_repo="$1"; gcr_resolve_id="$2"; gcr_resolve_name="${3:-}"
gcr_repo_allowed "$gcr_resolve_repo" || return 1
case "$gcr_resolve_id" in ''|0|*[!0-9]*) return 1 ;; esac
gcr_resolved="$(gcr_record_find_runner_id \
"$gcr_resolve_repo" "$gcr_resolve_id")" && {
printf '%s' "$gcr_resolved"
return 0
}
[ -n "$gcr_resolve_name" ] || return 1
gcr_resolve_lookup=0
gcr_gitea_find_runner_exact "$gcr_resolve_repo" "$gcr_resolve_name" \
|| gcr_resolve_lookup="$?"
[ "$gcr_resolve_lookup" -eq 0 ] \
&& [ "$GCR_RUNNER_ID" = "$gcr_resolve_id" ] || return 1
gcr_resolved="$(gcr_record_find_legacy_runner_name \
"$gcr_resolve_repo" "$gcr_resolve_name")" || return 1
set -- $gcr_resolved
gcr_resolve_job="$1"; gcr_resolve_attempt="$2"
gcr_resolve_key="$(gcr_alloc_key "$gcr_resolve_job" "$gcr_resolve_attempt")"
gcr_lock_acquire "$gcr_resolve_key" || return 1
gcr_resolve_rec="$(gcr_record_get \
"$gcr_resolve_job" "$gcr_resolve_attempt")"
if [ "$(gcr_record_field "$gcr_resolve_rec" repo)" \
!= "$gcr_resolve_repo" ] \
|| [ "$(gcr_record_field "$gcr_resolve_rec" runner_name)" \
!= "$gcr_resolve_name" ] \
|| [ -n "$(gcr_record_field "$gcr_resolve_rec" runner_id)" ]; then
gcr_lock_release "$gcr_resolve_key"
return 1
fi
gcr_resolve_rec="$(printf '%s' "$gcr_resolve_rec" | jq -c \
--arg id "$gcr_resolve_id" '.runner_id = ($id | tonumber)')" || {
gcr_lock_release "$gcr_resolve_key"
return 1
}
gcr_record_put "$gcr_resolve_job" "$gcr_resolve_attempt" \
"$gcr_resolve_rec" || {
gcr_lock_release "$gcr_resolve_key"
return 1
}
gcr_lock_release "$gcr_resolve_key"
printf '%s %s' "$gcr_resolve_job" "$gcr_resolve_attempt"
}
# Bind lifecycle only after current API history proves one exact runner/job
# assignment. Event state is syntax-checked but authoritative status wins.
gcr_apply_actual_job() {
gcr_actual_repo="$1"; gcr_actual_runner_id="$2"
gcr_actual_runner_name="$3"; gcr_actual_job="$4"
gcr_actual_attempt="$5"; gcr_actual_state="$6"
gcr_actual_via="${7:-webhook}"
case "$gcr_actual_job:$gcr_actual_attempt" in
*[!0-9:]*|0:*|:*|*::*|*:) return 1 ;;
esac
case "$gcr_actual_state" in vm_active|completed:*) ;; *) return 1 ;; esac
gcr_actual_state="$(gcr_gitea_confirm_runner_job "$gcr_actual_repo" \
"$gcr_actual_runner_id" "$gcr_actual_job" "$gcr_actual_attempt")" \
|| return 1
gcr_actual_source="$(gcr_record_resolve_runner "$gcr_actual_repo" \
"$gcr_actual_runner_id" "$gcr_actual_runner_name")" || return 1
set -- $gcr_actual_source
gcr_actual_source_job="$1"; gcr_actual_source_attempt="$2"
if [ "$gcr_actual_source_job" != "$gcr_actual_job" ] \
|| [ "$gcr_actual_source_attempt" != "$gcr_actual_attempt" ]; then
gcr_transfer_begin "$gcr_actual_source_job" \
"$gcr_actual_source_attempt" "$gcr_actual_job" \
"$gcr_actual_attempt" vm_active assignment || return 1
fi
gcr_actual_key="$(gcr_alloc_key "$gcr_actual_job" "$gcr_actual_attempt")"
gcr_lock_acquire "$gcr_actual_key" || return 1
gcr_actual_rec="$(gcr_record_get "$gcr_actual_job" "$gcr_actual_attempt")"
if [ "$(gcr_record_field "$gcr_actual_rec" repo)" != "$gcr_actual_repo" ] \
|| [ "$(gcr_record_field "$gcr_actual_rec" runner_id)" \
!= "$gcr_actual_runner_id" ]; then
gcr_lock_release "$gcr_actual_key"
return 1
fi
case "$gcr_actual_state" in
vm_active)
case "$(gcr_record_field "$gcr_actual_rec" status)" in
pending_vm|vm_active)
gcr_actual_rec="$(printf '%s' "$gcr_actual_rec" \
| jq -c '.status = "vm_active"')"
gcr_record_put "$gcr_actual_job" "$gcr_actual_attempt" \
"$gcr_actual_rec" || {
gcr_lock_release "$gcr_actual_key"
return 1
}
;;
esac
;;
completed:*)
case "$(gcr_record_field "$gcr_actual_rec" status)" in
pending_vm|vm_active)
gcr_vm_finish_terminal "$gcr_actual_job" \
"$gcr_actual_attempt" "$gcr_actual_rec" \
"$gcr_actual_state" "$gcr_actual_via"
gcr_actual_finish="$?"
gcr_lock_release "$gcr_actual_key"
case "$gcr_actual_finish" in 0|2) return 0 ;; esac
return "$gcr_actual_finish"
;;
esac
;;
esac
gcr_lock_release "$gcr_actual_key"
}
# Persist transfer intent before publishing a second owner. MODE=assignment
# moves lifecycle to the actual job; MODE=legacy_cleanup creates a durable
# synthetic cleanup owner before restoring the original queued job.
gcr_transfer_begin() {
gcr_transfer_source_job="$1"; gcr_transfer_source_attempt="$2"
gcr_transfer_target_job="$3"; gcr_transfer_target_attempt="$4"
gcr_transfer_target_status="$5"; gcr_transfer_mode="${6:-assignment}"
gcr_transfer_source_key="$(gcr_alloc_key \
"$gcr_transfer_source_job" "$gcr_transfer_source_attempt")"
gcr_transfer_target_key="$(gcr_alloc_key \
"$gcr_transfer_target_job" "$gcr_transfer_target_attempt")"
gcr_pair_lock_acquire "$gcr_transfer_source_key" \
"$gcr_transfer_target_key" || return 1
gcr_transfer_source="$(gcr_record_get \
"$gcr_transfer_source_job" "$gcr_transfer_source_attempt")"
case "$(gcr_record_field "$gcr_transfer_source" status)" in
pending_vm|vm_active|transfer_pending) ;;
*) gcr_pair_lock_release; return 1 ;;
esac
if [ "$(gcr_record_field "$gcr_transfer_source" status)" != transfer_pending ]; then
gcr_transfer_keys_preflight "$gcr_transfer_source_job" \
"$gcr_transfer_source_attempt" "$gcr_transfer_target_job" \
"$gcr_transfer_target_attempt" || {
gcr_pair_lock_release
return 1
}
gcr_transfer_source="$(printf '%s' "$gcr_transfer_source" | jq -c \
--arg target_job "$gcr_transfer_target_job" \
--arg target_attempt "$gcr_transfer_target_attempt" \
--arg target_status "$gcr_transfer_target_status" \
--arg mode "$gcr_transfer_mode" \
'.status = "transfer_pending"
| .transfer_target_job_id = $target_job
| .transfer_target_run_attempt = $target_attempt
| .transfer_target_status = $target_status
| .transfer_mode = $mode
| .transfer_key_state = "source"')" || {
gcr_pair_lock_release
return 1
}
gcr_record_put "$gcr_transfer_source_job" \
"$gcr_transfer_source_attempt" "$gcr_transfer_source" || {
gcr_pair_lock_release
return 1
}
fi
gcr_transfer_complete_locked "$gcr_transfer_source_job" \
"$gcr_transfer_source_attempt" "$gcr_transfer_source"
gcr_transfer_result="$?"
gcr_pair_lock_release
return "$gcr_transfer_result"
}
# Caller holds source and target locks.
gcr_transfer_complete_locked() {
gcr_transfer_source_job="$1"; gcr_transfer_source_attempt="$2"
gcr_transfer_source="$3"
gcr_transfer_target_job="$(gcr_record_field \
"$gcr_transfer_source" transfer_target_job_id)"
gcr_transfer_target_attempt="$(gcr_record_field \
"$gcr_transfer_source" transfer_target_run_attempt)"
gcr_transfer_target_status="$(gcr_record_field \
"$gcr_transfer_source" transfer_target_status)"
gcr_transfer_mode="$(gcr_record_field "$gcr_transfer_source" transfer_mode)"
[ -n "$gcr_transfer_target_job" ] \
&& [ -n "$gcr_transfer_target_attempt" ] || return 1
gcr_transfer_target="$(gcr_record_get \
"$gcr_transfer_target_job" "$gcr_transfer_target_attempt")"
if [ -n "$gcr_transfer_target" ]; then
gcr_transfer_existing_source="$(gcr_record_field \
"$gcr_transfer_target" transfer_source_job_id)"
if [ "$gcr_transfer_existing_source" != "$gcr_transfer_source_job" ] \
|| [ "$(gcr_record_field "$gcr_transfer_target" transfer_source_run_attempt)" \
!= "$gcr_transfer_source_attempt" ] \
|| [ "$(gcr_record_field "$gcr_transfer_target" vm_id)" \
!= "$(gcr_record_field "$gcr_transfer_source" vm_id)" ]; then
case "$(gcr_record_field "$gcr_transfer_target" status)" in
deferred)
[ "$(gcr_record_field "$gcr_transfer_target" repo)" \
= "$(gcr_record_field "$gcr_transfer_source" repo)" ] \
|| return 1
[ "$(gcr_record_field "$gcr_transfer_target" label)" \
= "$(gcr_record_field "$gcr_transfer_source" label)" ] \
|| return 1
;;
*) return 1 ;;
esac
gcr_transfer_target=""
fi
fi
if [ -z "$gcr_transfer_target" ]; then
if [ "$gcr_transfer_mode" = legacy_cleanup ]; then
gcr_transfer_target="$(printf '%s' "$gcr_transfer_source" | jq -c \
--arg job "$gcr_transfer_target_job" \
--arg attempt "$gcr_transfer_target_attempt" \
--arg source_job "$gcr_transfer_source_job" \
--arg source_attempt "$gcr_transfer_source_attempt" \
'.job_id = $job | .run_attempt = $attempt
| .status = "cleanup_pending"
| .cleanup_reason = "legacy-runner-absent"
| .cleanup_refund_budget = false
| .cleanup_vm_destroyed = false
| .cleanup_budget_released = false
| .transfer_source_job_id = $source_job
| .transfer_source_run_attempt = $source_attempt
| del(.transfer_target_job_id, .transfer_target_run_attempt,
.transfer_target_status, .transfer_mode,
.transfer_key_state)')" || return 1
else
gcr_transfer_target="$(printf '%s' "$gcr_transfer_source" | jq -c \
--arg job "$gcr_transfer_target_job" \
--arg attempt "$gcr_transfer_target_attempt" \
--arg status "$gcr_transfer_target_status" \
--arg source_job "$gcr_transfer_source_job" \
--arg source_attempt "$gcr_transfer_source_attempt" \
'.job_id = $job | .run_attempt = $attempt | .status = $status
| .allocation_job_id = $source_job
| .allocation_run_attempt = $source_attempt
| .allocation_repo = .repo | .allocation_label = .label
| .transfer_source_job_id = $source_job
| .transfer_source_run_attempt = $source_attempt
| del(.transfer_target_job_id, .transfer_target_run_attempt,
.transfer_target_status, .transfer_mode,
.transfer_key_state)')" || return 1
fi
gcr_record_put "$gcr_transfer_target_job" \
"$gcr_transfer_target_attempt" "$gcr_transfer_target" || return 1
fi
gcr_transfer_source="$(gcr_transfer_keys_locked \
"$gcr_transfer_source_job" "$gcr_transfer_source_attempt" \
"$gcr_transfer_target_job" "$gcr_transfer_target_attempt" \
"$gcr_transfer_source")" || return 1
gcr_transfer_deferred="$(printf '%s' "$gcr_transfer_source" | jq -c \
--arg job "$gcr_transfer_source_job" \
--arg attempt "$gcr_transfer_source_attempt" \
'.job_id = $job | .run_attempt = $attempt | .status = "deferred"
| .ttl_min = null | .vm_id = "" | .vm_name = ""
| .runner_name = "" | .runner_id = "" | .bootstrapped = false
| del(.server_type, .budget_rate, .location, .arch, .image_id,
.ip, .transfer_target_job_id, .transfer_target_run_attempt,
.transfer_target_status, .transfer_mode,
.transfer_key_state,
.transfer_source_job_id, .transfer_source_run_attempt)')" \
|| return 1
gcr_record_put "$gcr_transfer_source_job" \
"$gcr_transfer_source_attempt" "$gcr_transfer_deferred"
}
gcr_reconcile_transfers() {
for gcr_transfer_file in $(gcr_active_records); do
gcr_transfer_rec="$(cat "$gcr_transfer_file")"
[ "$(gcr_record_field "$gcr_transfer_rec" status)" = transfer_pending ] \
|| continue
gcr_transfer_job="$(gcr_record_field "$gcr_transfer_rec" job_id)"
gcr_transfer_attempt="$(gcr_record_field "$gcr_transfer_rec" run_attempt)"
gcr_transfer_begin "$gcr_transfer_job" "$gcr_transfer_attempt" \
"$(gcr_record_field "$gcr_transfer_rec" transfer_target_job_id)" \
"$(gcr_record_field "$gcr_transfer_rec" transfer_target_run_attempt)" \
"$(gcr_record_field "$gcr_transfer_rec" transfer_target_status)" \
"$(gcr_record_field "$gcr_transfer_rec" transfer_mode)" || true
done
}
gcr_record_field() { gcr_record_field() {
printf '%s' "$1" | jq -r --arg f "$2" 'if has($f) then .[$f] else "" end' printf '%s' "$1" | jq -r --arg f "$2" 'if has($f) then .[$f] else "" end'
} }
@@ -271,9 +686,10 @@ gcr_claim_idle() {
--arg runner "gcr-${gcr_claim_job}-${gcr_claim_attempt}" \ --arg runner "gcr-${gcr_claim_job}-${gcr_claim_attempt}" \
--arg image "$gcr_claim_image" \ --arg image "$gcr_claim_image" \
--arg now "$gcr_claim_now" \ --arg now "$gcr_claim_now" \
'.job_id = $job | .run_attempt = $attempt '.job_id = $job | .run_attempt = $attempt
| .repo = $repo | .label = $label | .runner_name = $runner | .repo = $repo | .label = $label | .runner_name = $runner
| .status = "rebuild_pending" | .runner_id = ""
| .status = "rebuild_pending"
| .bootstrapped = false | .bootstrapped = false
| .rebuild_image_id = $image | .rebuild_image_id = $image
| .rebuild_action_id = "" | .rebuild_action_id = ""
@@ -314,8 +730,8 @@ gcr_record_exists_for_vm_owner() {
[ -n "$gcr_owner_rec" ] || return 1 [ -n "$gcr_owner_rec" ] || return 1
[ "$(gcr_record_field "$gcr_owner_rec" vm_name)" = "$gcr_owner_vm_name" ] \ [ "$(gcr_record_field "$gcr_owner_rec" vm_name)" = "$gcr_owner_vm_name" ] \
|| return 1 || return 1
case "$(gcr_record_field "$gcr_owner_rec" status)" in case "$(gcr_record_field "$gcr_owner_rec" status)" in
create_pending|create_ambiguous|pending_vm|vm_active|retiring_vm|idle_vm|rebuild_pending|rebuild_requesting|rebuild_running|cleanup_pending) create_pending|create_ambiguous|pending_vm|vm_active|transfer_pending|retiring_vm|idle_vm|rebuild_pending|rebuild_requesting|rebuild_running|cleanup_pending)
return 0 return 0
;; ;;
esac esac
@@ -331,7 +747,7 @@ gcr_record_exists_for_runner() {
gcr_lookup_owner_repo="$(gcr_record_field "$gcr_lookup_rec" retirement_repo)" gcr_lookup_owner_repo="$(gcr_record_field "$gcr_lookup_rec" retirement_repo)"
gcr_lookup_owner_runner="$(gcr_record_field "$gcr_lookup_rec" retirement_runner_name)" gcr_lookup_owner_runner="$(gcr_record_field "$gcr_lookup_rec" retirement_runner_name)"
;; ;;
create_pending|pending_vm|vm_active|rebuild_pending|rebuild_requesting|rebuild_running) create_pending|pending_vm|vm_active|transfer_pending|rebuild_pending|rebuild_requesting|rebuild_running)
gcr_lookup_owner_repo="$(gcr_record_field "$gcr_lookup_rec" repo)" gcr_lookup_owner_repo="$(gcr_record_field "$gcr_lookup_rec" repo)"
gcr_lookup_owner_runner="$(gcr_record_field "$gcr_lookup_rec" runner_name)" gcr_lookup_owner_runner="$(gcr_record_field "$gcr_lookup_rec" runner_name)"
;; ;;
@@ -531,5 +947,5 @@ gcr_budget_refund_once() {
} }
gcr_active_records() { gcr_active_records() {
grep -El '"status"[[:space:]]*:[[:space:]]*"(create_pending|create_ambiguous|pending_vm|vm_active|retiring_vm|idle_vm|rebuild_pending|rebuild_requesting|rebuild_running|deferred|cleanup_pending)"' "$GCR_STATE_DIR"/jobs/*.json 2>/dev/null || true grep -El '"status"[[:space:]]*:[[:space:]]*"(create_pending|create_ambiguous|pending_vm|vm_active|transfer_pending|retiring_vm|idle_vm|rebuild_pending|rebuild_requesting|rebuild_running|deferred|cleanup_pending)"' "$GCR_STATE_DIR"/jobs/*.json 2>/dev/null || true
} }
+18 -3
View File
@@ -321,6 +321,8 @@ gcr_handle_webhook() {
conclusion="$(printf '%s' "$gcr_body" | jq -r '.workflow_job.conclusion // ""')" conclusion="$(printf '%s' "$gcr_body" | jq -r '.workflow_job.conclusion // ""')"
repo="$(printf '%s' "$gcr_body" | jq -r '.repository.full_name // ""')" repo="$(printf '%s' "$gcr_body" | jq -r '.repository.full_name // ""')"
labels_json="$(printf '%s' "$gcr_body" | jq -c '.workflow_job.labels // []')" labels_json="$(printf '%s' "$gcr_body" | jq -c '.workflow_job.labels // []')"
runner_id="$(printf '%s' "$gcr_body" | jq -r '.workflow_job.runner_id // ""')"
runner_name="$(printf '%s' "$gcr_body" | jq -r '.workflow_job.runner_name // ""')"
[ -n "$action" ] && [ -n "$repo" ] \ [ -n "$action" ] && [ -n "$repo" ] \
|| { gcr_respond 400 "malformed payload"; exit 0; } || { gcr_respond 400 "malformed payload"; exit 0; }
@@ -336,12 +338,25 @@ gcr_handle_webhook() {
gcr_log info --ns=alloc "queued job=$job_id repo=$repo code=$RESPONSE_CODE $RESPONSE_BODY" gcr_log info --ns=alloc "queued job=$job_id repo=$repo code=$RESPONSE_CODE $RESPONSE_BODY"
;; ;;
in_progress) in_progress)
gcr_mark_in_progress "$job_id" "$attempt" "$repo" case "$runner_id" in
''|0|*[!0-9]*) gcr_respond 400 "malformed runner identity"; exit 0 ;;
esac
if ! gcr_apply_actual_job "$repo" "$runner_id" "$runner_name" \
"$job_id" "$attempt" vm_active webhook; then
gcr_log warn --ns=alloc \
"unresolved in-progress assignment job=$job_id repo=$repo runner_id=$runner_id"
fi
RESPONSE_CODE=204 RESPONSE_CODE=204
;; ;;
completed) completed)
gcr_deallocate "$job_id" "$attempt" \ case "$runner_id" in
"completed:${conclusion:-unknown}" "$repo" ''|0|*[!0-9]*) gcr_respond 400 "malformed runner identity"; exit 0 ;;
esac
if ! gcr_apply_actual_job "$repo" "$runner_id" "$runner_name" \
"$job_id" "$attempt" "completed:${conclusion:-unknown}" webhook; then
gcr_log warn --ns=alloc \
"unresolved terminal assignment job=$job_id repo=$repo runner_id=$runner_id"
fi
RESPONSE_CODE=204 RESPONSE_CODE=204
;; ;;
*) *)
@@ -140,7 +140,7 @@ gcr_gitea_set_runner_disabled() {
"$1" "$2" "$3" >> "$calls" "$1" "$2" "$3" >> "$calls"
runner_disabled="$3" 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_record_put 301 1 "$original"
gcr_deallocate 301 1 completed:success gcr_deallocate 301 1 completed:success
test "$(gcr_record_field "$(gcr_record_get 301 1)" status)" = retiring_vm 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 jq -e 'select(.job_id == "303" and .repo == "hectic-lab/util.nix" and
.label == "gross-nix-x86-highmem" and .vm_id == 71 and .label == "gross-nix-x86-highmem" and .vm_id == 71 and
.vm_name == "gcr-301-1" and .runner_name == "gcr-303-1" and .vm_name == "gcr-301-1" and .runner_name == "gcr-303-1" and
.runner_id == "" and
.status == "rebuild_pending" and .bootstrapped == false and .status == "rebuild_pending" and .bootstrapped == false and
.rebuild_image_id == "424242" and .rebuild_action_id == "" and .rebuild_image_id == "424242" and .rebuild_action_id == "" and
.created_at == 1000 and .ttl_min == 480 and .assigned_at == 2800)' \ .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 "$(grep -Ec '^(budget|token|create)$' "$calls" || true)" = 0
test "$(gcr_count_active)" = 1 test "$(gcr_count_active)" = 1
test "$(gcr_count_active_repo hectic-lab/util.nix)" = 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. # Rebuild intent and action id are durable around provider request and polling.
gcr_gitea_job_state() { printf 'queued:'; } gcr_gitea_job_state() { printf 'queued:'; }
@@ -258,15 +265,29 @@ if grep -q '^token$' "$calls"; then
fi fi
gcr_vm_public_ip() { printf '192.0.2.71'; } 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() { gcr_vm_bootstrap_ssh() {
test -r "$5" test -r "$5"
grep -Eq "^$1 ssh-ed25519 [A-Za-z0-9+/=]+$" "$5" grep -Eq "^$1 ssh-ed25519 [A-Za-z0-9+/=]+$" "$5"
printf 'bootstrap ip=%s label=%s token=%s runner=%s\n' \ printf 'bootstrap ip=%s label=%s token=%s runner=%s\n' \
"$1" "$2" "$3" "$4" >> "$calls" "$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 gcr_bootstrap_pending
bootstrapped="$(gcr_record_get 303 1)" bootstrapped="$(gcr_record_get 303 1)"
test "$(gcr_record_field "$bootstrapped" bootstrapped)" = true test "$(gcr_record_field "$bootstrapped" bootstrapped)" = true
test "$(gcr_record_field "$bootstrapped" runner_id)" = 18
test "$(grep -c '^token$' "$calls")" = 1 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" 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 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"')"