refactor: gitea runners
This commit is contained in:
@@ -33,9 +33,11 @@ let
|
||||
nativeBuildInputs = [
|
||||
pkgs.coreutils
|
||||
pkgs.dash
|
||||
pkgs.gawk
|
||||
pkgs.gnugrep
|
||||
pkgs.gnused
|
||||
pkgs.jq
|
||||
pkgs.openssh
|
||||
];
|
||||
LOG_SH = ../../../package/gitea-runner-controller/log.sh;
|
||||
STATE_SH = ../../../package/gitea-runner-controller/state.sh;
|
||||
|
||||
@@ -88,15 +88,23 @@ gcr_hcloud_req() {
|
||||
}
|
||||
|
||||
created="$(gcr_vm_create gcr-9001-1 gross-nix-x86-highmem ccx53 480 reg-token 9001 1 hectic-lab/util.nix)"
|
||||
test "$created" = '9001 ccx53 0.8550'
|
||||
test "$created" = '9001 ccx53 0.8550 hel1 amd64 424242'
|
||||
gcr_record_del 9001 1
|
||||
test "$(wc -l < "$calls" | tr -d ' ')" = '3'
|
||||
jq -e 'select(.server_type == "ccx53" and .location == "nbg1" and .labels["gcr.arch"] == "amd64" and .labels["gcr.label"] == "gross-nix-x86-highmem" and .labels["gcr.ttl-min"] == "480")' "$calls" >/dev/null
|
||||
jq -e 'select(.server_type == "ccx53" and .location == "nbg1" and
|
||||
(.user_data | startswith("#cloud-config\n")) and
|
||||
(.user_data | contains("/etc/ssh/ssh_host_ed25519_key")) and
|
||||
.labels["gcr.arch"] == "amd64" and
|
||||
.labels["gcr.image-id"] == "424242" and
|
||||
.labels["gcr.label"] == "gross-nix-x86-highmem" and
|
||||
.labels["gcr.ttl-min"] == "480")' "$calls" >/dev/null
|
||||
jq -e 'select(.server_type == "ccx53" and .location == "fsn1" and .labels["gcr.arch"] == "amd64")' "$calls" >/dev/null
|
||||
jq -e 'select(.server_type == "ccx53" and .location == "hel1" and .labels["gcr.arch"] == "amd64")' "$calls" >/dev/null
|
||||
if jq -e 'select(.server_type != "ccx53" or .labels["gcr.arch"] != "amd64")' "$calls" >/dev/null; then
|
||||
printf 'highmem VM creation attempted non-ccx53 or non-amd64 candidate\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
gcr_record_del 9001 1
|
||||
|
||||
# Standard allocation charges actual fallback type after cx23 capacity failures.
|
||||
rm -f "$calls" "$GCR_STATE_DIR/budget/$(date -u '+%Y-%m')"
|
||||
@@ -129,6 +137,9 @@ test "$(wc -l < "$GCR_STATE_DIR/lookups" | tr -d ' ')" = '3'
|
||||
jq -e -s 'map(.server_type) == ["cx23", "cx23", "cx23", "cx33"]' "$calls" >/dev/null
|
||||
test "$(gcr_server_hourly_rate cx33)" = '0.008'
|
||||
test "$(cat "$GCR_STATE_DIR/budget/$(date -u '+%Y-%m')")" = '0.0640'
|
||||
jq -e 'select(.server_type == "cx33" and .location == "nbg1" and
|
||||
.arch == "amd64" and .image_id == "424242")' \
|
||||
"$(gcr_record_path 9002 1)" >/dev/null
|
||||
|
||||
# Ambiguous 5xx response adopts only exact deterministic managed identity.
|
||||
gcr_record_del 9002 1
|
||||
@@ -142,7 +153,7 @@ gcr_hcloud_req() {
|
||||
return 1
|
||||
fi
|
||||
GCR_LAST_HTTP=200
|
||||
printf '%s\n' '{"servers":[{"id":9010,"name":"gcr-9010-1","server_type":{"name":"cx23"},"labels":{"gitea-runner-controller":"managed","gcr.job-id":"9010","gcr.run-attempt":"1","gcr.label":"nix","gcr.location":"nbg1","gcr.arch":"amd64"}}]}' > "$GCR_LAST_BODY"
|
||||
printf '%s\n' '{"servers":[{"id":9010,"name":"gcr-9010-1","server_type":{"name":"cx23"},"labels":{"gitea-runner-controller":"managed","gcr.job-id":"9010","gcr.run-attempt":"1","gcr.label":"nix","gcr.location":"nbg1","gcr.arch":"amd64","gcr.image-id":"424242"}}]}' > "$GCR_LAST_BODY"
|
||||
}
|
||||
gcr_alloc 9010 1 hectic-lab/util.nix '["nix"]'
|
||||
test "$RESPONSE_BODY" = 'allocated gcr-9010-1'
|
||||
@@ -183,7 +194,7 @@ gcr_hcloud_req() {
|
||||
test "$1" = GET
|
||||
GCR_LAST_BODY="$GCR_STATE_DIR/last-body.json"
|
||||
GCR_LAST_HTTP=200
|
||||
printf '%s\n' '{"servers":[{"id":9012,"name":"gcr-9012-1","server_type":{"name":"cx23"},"labels":{"gitea-runner-controller":"managed","gcr.job-id":"9012","gcr.run-attempt":"1","gcr.label":"nix","gcr.location":"nbg1","gcr.arch":"amd64"}}]}' > "$GCR_LAST_BODY"
|
||||
printf '%s\n' '{"servers":[{"id":9012,"name":"gcr-9012-1","server_type":{"name":"cx23"},"labels":{"gitea-runner-controller":"managed","gcr.job-id":"9012","gcr.run-attempt":"1","gcr.label":"nix","gcr.location":"nbg1","gcr.arch":"amd64","gcr.image-id":"424242"}}]}' > "$GCR_LAST_BODY"
|
||||
}
|
||||
gcr_vm_destroy() { printf 'unexpected-destroy %s\n' "$1" >> "$GCR_STATE_DIR/ambiguity-destroys"; return 1; }
|
||||
gcr_sweep_create_ambiguous
|
||||
@@ -191,6 +202,9 @@ recovered_rec="$(gcr_record_get 9012 1)"
|
||||
test "$(gcr_record_field "$recovered_rec" status)" = pending_vm
|
||||
test "$(gcr_record_field "$recovered_rec" vm_id)" = 9012
|
||||
test "$(gcr_record_field "$recovered_rec" server_type)" = cx23
|
||||
test "$(gcr_record_field "$recovered_rec" location)" = nbg1
|
||||
test "$(gcr_record_field "$recovered_rec" arch)" = amd64
|
||||
test "$(gcr_record_field "$recovered_rec" image_id)" = 424242
|
||||
test "$(gcr_record_field "$recovered_rec" budget_rate)" = 0.004
|
||||
test "$(cat "$GCR_STATE_DIR/budget/$(date -u '+%Y-%m')")" = '0.0320'
|
||||
test ! -e "$GCR_STATE_DIR/ambiguity-destroys"
|
||||
@@ -219,6 +233,25 @@ test "$(wc -l < "$calls" | tr -d ' ')" = '3'
|
||||
jq -e -s 'all(.server_type == "cx23")' "$calls" >/dev/null
|
||||
test "$(cat "$GCR_STATE_DIR/budget/$(date -u '+%Y-%m')")" = '0.0000'
|
||||
|
||||
# Crash recovery completes a transaction exactly once when the aggregate is at
|
||||
# either durable endpoint, and discards metadata never followed by a write.
|
||||
month="$(date -u '+%Y-%m')"
|
||||
txn_root="$GCR_STATE_DIR/budget/transactions/$month"
|
||||
mkdir -p "$txn_root/recover-after"
|
||||
printf '0.0000\n' > "$txn_root/recover-after/before"
|
||||
printf '0.0320\n' > "$txn_root/recover-after/after"
|
||||
printf '0.0000\n' > "$GCR_STATE_DIR/budget/$month"
|
||||
gcr_budget_recover_pending
|
||||
test "$(cat "$GCR_STATE_DIR/budget/$month")" = 0.0320
|
||||
test "$(cat "$txn_root/recover-after/status")" = committed
|
||||
gcr_budget_recover_pending
|
||||
test "$(cat "$GCR_STATE_DIR/budget/$month")" = 0.0320
|
||||
|
||||
mkdir -p "$txn_root/incomplete"
|
||||
printf '0.0320\n' > "$txn_root/incomplete/before"
|
||||
gcr_budget_recover_pending
|
||||
test ! -e "$txn_root/incomplete"
|
||||
|
||||
# Deferred allocation keeps cleanup ownership and reservation across failed
|
||||
# DELETE, failed post-DELETE state update, and cleanup-owned 404 retry.
|
||||
rm -f "$calls" "$GCR_STATE_DIR/budget/$(date -u '+%Y-%m')"
|
||||
@@ -231,7 +264,7 @@ gcr_record_put() {
|
||||
count="$(cat "$record_put_count" 2>/dev/null || echo 0)"
|
||||
count=$((count + 1))
|
||||
printf '%s\n' "$count" > "$record_put_count"
|
||||
case "$count" in 1|3|5|8) return 1 ;; esac
|
||||
case "$count" in 4|6|8|11) return 1 ;; esac
|
||||
tmp="$(mktemp "$(dirname "$(gcr_record_path "$1" "$2")")/.tmp.XXXXXX")"
|
||||
printf '%s\n' "$3" > "$tmp"
|
||||
mv -f "$tmp" "$(gcr_record_path "$1" "$2")"
|
||||
|
||||
@@ -10,310 +10,363 @@ set -eu
|
||||
. "$WEBHOOK_SH"
|
||||
|
||||
gcr_state_init
|
||||
export GCR_ALLOWED_REPOS='hinterland/hearth'
|
||||
export GCR_CONCURRENCY_CAP=2
|
||||
export GCR_PER_REPO_CAP=1
|
||||
export GCR_ALLOWED_REPOS='hinterland/hearth,hectic-lab/util.nix'
|
||||
export GCR_CONCURRENCY_CAP=3
|
||||
export GCR_PER_REPO_CAP=2
|
||||
export GCR_RECONCILE_INTERVAL_SEC=60
|
||||
export GCR_NIX_IMAGE_ID=424242
|
||||
export GCR_IMAGE_ID=313131
|
||||
NOW=2800
|
||||
gcr_now_epoch() { printf '%s' "$NOW"; }
|
||||
|
||||
# Gitea API hardening: strict token schema, complete pagination, malformed-list
|
||||
# rejection, and disable-before-delete retirement.
|
||||
export GCR_GITEA_URL=https://gitea.example.test
|
||||
export GITEA_ADMIN_TOKEN_FILE="$GCR_STATE_DIR/admin-token"
|
||||
printf 'admin-token\n' > "$GITEA_ADMIN_TOKEN_FILE"
|
||||
hardening_calls="$GCR_STATE_DIR/hardening-calls"
|
||||
curl_mode=malformed-token
|
||||
curl() {
|
||||
for arg do url="$arg"; done
|
||||
printf '%s\n' "$url" >> "$hardening_calls"
|
||||
case "$curl_mode" in
|
||||
malformed-token) printf '{"token":42}\n' ;;
|
||||
hostile-token) printf '%s\n' '{"token":"odd $ token"}' ;;
|
||||
paginated)
|
||||
case "$url" in
|
||||
*page=1*) jq -cn '{runners:[range(1;51) | {id:.,name:("runner-" + tostring)}]}' ;;
|
||||
*page=2*) printf '{"runners":[{"id":51,"name":"runner-51"}]}\n' ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
;;
|
||||
malformed-page) printf '{"runners":[{"id":"bad","name":"runner"}]}\n' ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
if gcr_gitea_registration_token hinterland/hearth >/dev/null 2>&1; then
|
||||
printf 'malformed registration token must fail closed\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
curl_mode=hostile-token
|
||||
test "$(gcr_gitea_registration_token hinterland/hearth)" = "odd \$ token"
|
||||
rm -f "$hardening_calls"
|
||||
curl_mode=paginated
|
||||
runners="$(gcr_gitea_list_runners hinterland/hearth)"
|
||||
test "$(printf '%s\n' "$runners" | wc -l | tr -d ' ')" = 51
|
||||
printf '%s\n' "$runners" | grep -q '^1 runner-1$'
|
||||
printf '%s\n' "$runners" | grep -q '^51 runner-51$'
|
||||
grep -q 'page=1&limit=50$' "$hardening_calls"
|
||||
grep -q 'page=2&limit=50$' "$hardening_calls"
|
||||
curl_mode=malformed-page
|
||||
if gcr_gitea_list_runners hinterland/hearth >/dev/null 2>&1; then
|
||||
printf 'malformed runner list must fail closed\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
retire_calls="$GCR_STATE_DIR/retire-calls"
|
||||
gcr_gitea_list_runners() { printf '17 gcr-77-1\n'; }
|
||||
gcr_gitea_set_runner_disabled() {
|
||||
printf 'disable %s %s %s\n' "$1" "$2" "$3" >> "$retire_calls"
|
||||
return 1
|
||||
}
|
||||
gcr_gitea_delete_runner() {
|
||||
printf 'delete %s %s\n' "$1" "$2" >> "$retire_calls"
|
||||
}
|
||||
if gcr_gitea_deregister_runner_exact hinterland/hearth gcr-77-1; then
|
||||
printf 'disable failure must retain runner identity\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
test "$(cat "$retire_calls")" = 'disable hinterland/hearth 17 true'
|
||||
|
||||
export GCR_NIX_VERSION=2.24.11
|
||||
export GCR_NIX_TARBALL_SHA256=sha256
|
||||
export GCR_ARM_NIX_TARBALL_SHA256=sha256
|
||||
export GCR_ACT_RUNNER_VERSION=0.2.11
|
||||
export GCR_ACT_RUNNER_SHA256=sha256
|
||||
bootstrap="$(gcr_bootstrap_script nix "odd \$ token" 480 gcr-77-1)"
|
||||
encoded="$(printf '%s' "odd \$ token" | base64 | tr -d '\n')"
|
||||
printf '%s\n' "$bootstrap" | grep -Fq "printf '%s' '$encoded' | base64 -d"
|
||||
printf '%s\n' "$bootstrap" | grep -Fq 'if [ ! -s /var/lib/gitea-runner/.runner ]; then'
|
||||
start_script="$(printf '%s\n' "$bootstrap" | sed -n \
|
||||
'/cat > \/usr\/local\/sbin\/gcr-runner-start/,/STARTEOF/p')"
|
||||
if printf '%s\n' "$start_script" | grep -Fq "odd \$ token"; then
|
||||
printf 'registration token must not persist in runner start script\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
calls="$GCR_STATE_DIR/calls"
|
||||
gcr_budget_add() { printf 'budget\n' >> "$calls"; }
|
||||
gcr_gitea_registration_token() { printf 'token'; printf 'token\n' >> "$calls"; }
|
||||
gcr_gitea_registration_token() {
|
||||
printf 'token\n' >> "$calls"
|
||||
printf 'fresh-token'
|
||||
}
|
||||
gcr_vm_create() {
|
||||
printf 'create\n' >> "$calls"
|
||||
gcr_budget_add 0.032 180
|
||||
printf '99 cx53 0.032'
|
||||
printf '99 cx53 0.032 nbg1 amd64 424242'
|
||||
}
|
||||
gcr_vm_destroy() { printf 'destroy vm=%s\n' "$1" >> "$calls"; }
|
||||
gcr_vm_runner_service() { printf 'runner %s vm=%s\n' "$2" "$1" >> "$calls"; }
|
||||
gcr_gitea_runner_disabled() { printf 'runner-disabled %s %s\n' "$2" "$3" >> "$calls"; }
|
||||
|
||||
original='{"job_id":"301","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":1000,"ttl_min":480,"vm_id":71,"vm_name":"gcr-301-1","bootstrapped":true,"status":"vm_active"}'
|
||||
gcr_record_put 301 1 "$original"
|
||||
gcr_deallocate 301 1 completed:success
|
||||
jq -e 'select(.status == "idle_vm" and .idle_expires_at == 4600)' \
|
||||
"$(gcr_record_path 301 1)" >/dev/null
|
||||
idle_once="$(gcr_record_get 301 1)"
|
||||
NOW=3000
|
||||
gcr_deallocate 301 1 completed:success
|
||||
test "$(gcr_record_get 301 1)" = "$idle_once"
|
||||
NOW=2800
|
||||
|
||||
# Exact elapsed hours keep current boundary instead of extending another hour.
|
||||
exact='{"job_id":"300","run_attempt":"1","repo":"hinterland/hearth","label":"gross-x86","created_at":1000,"ttl_min":180,"vm_id":70,"vm_name":"gcr-300-1","bootstrapped":true,"status":"vm_active"}'
|
||||
gcr_record_put 300 1 "$exact"
|
||||
NOW=4600
|
||||
gcr_deallocate 300 1 completed:success
|
||||
jq -e 'select(.status == "idle_vm" and .idle_expires_at == 4600)' \
|
||||
"$(gcr_record_path 300 1)" >/dev/null
|
||||
gcr_record_del 300 1
|
||||
NOW=2800
|
||||
gcr_deallocate 301 1 completed:success
|
||||
jq -e 'select(.status == "idle_vm" and .idle_expires_at == 4600)' \
|
||||
"$(gcr_record_path 301 1)" >/dev/null
|
||||
|
||||
# Exact billing boundary must expire now, not roll into another paid hour.
|
||||
NOW=4600
|
||||
boundary='{"job_id":"302","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":1000,"ttl_min":480,"vm_id":74,"vm_name":"gcr-302-1","bootstrapped":true,"status":"vm_active"}'
|
||||
test "$(gcr_record_idle_json "$boundary" | jq -r '.idle_expires_at')" = 4600
|
||||
NOW=2800
|
||||
|
||||
gcr_alloc 302 1 hinterland/hearth '["gross-nix-x86-perf"]'
|
||||
test "$RESPONSE_BODY" = 'reused gcr-301-1'
|
||||
test ! -e "$(gcr_record_path 301 1)"
|
||||
jq -e 'select(.job_id == "302" and .vm_id == 71 and
|
||||
.vm_name == "gcr-301-1" and .status == "pending_vm" and
|
||||
.created_at == 1000 and .assigned_at == 2800)' \
|
||||
"$(gcr_record_path 302 1)" >/dev/null
|
||||
test "$(grep -Ec '^(budget|token|create)$' "$calls" || true)" = 0
|
||||
|
||||
# Failed post-start health check keeps runner disabled and record retryable.
|
||||
retry_idle='{"job_id":"315","run_attempt":"1","repo":"hinterland/hearth","label":"gross-arm","created_at":1000,"ttl_min":180,"vm_id":79,"vm_name":"gcr-315-1","bootstrapped":true,"status":"idle_vm","idle_since":2000,"idle_expires_at":4600}'
|
||||
gcr_record_put 315 1 "$retry_idle"
|
||||
export GCR_PER_REPO_CAP=2
|
||||
FAIL_HEALTH=1
|
||||
gcr_vm_runner_service() {
|
||||
printf 'runner %s vm=%s\n' "$2" "$1" >> "$calls"
|
||||
[ "$2" = health ] && [ "$FAIL_HEALTH" = 1 ] && return 1
|
||||
printf 'runner-service vm=%s action=%s\n' "$1" "$2" >> "$calls"
|
||||
return 0
|
||||
}
|
||||
gcr_alloc 316 1 hinterland/hearth '["gross-arm"]'
|
||||
retry_rec="$(gcr_record_get 316 1)"
|
||||
test "$(gcr_record_field "$retry_rec" bootstrapped)" = false
|
||||
test "$(gcr_record_field "$retry_rec" reused_vm)" = true
|
||||
grep -q '^runner start vm=79$' "$calls"
|
||||
grep -q '^runner health vm=79$' "$calls"
|
||||
grep -q '^runner-disabled gcr-315-1 true$' "$calls"
|
||||
if grep -q '^runner-disabled gcr-315-1 false$' "$calls"; then
|
||||
printf 'unhealthy reused runner must never become schedulable\n' >&2
|
||||
gcr_gitea_runner_disabled() {
|
||||
printf 'obsolete-runner-toggle %s %s %s\n' "$1" "$2" "$3" >> "$calls"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Terminal registration is retired exactly before VM becomes generic idle.
|
||||
runner_present=1
|
||||
gcr_gitea_list_runners() {
|
||||
printf 'list-runners repo=%s\n' "$1" >> "$calls"
|
||||
[ "$runner_present" -eq 0 ] || printf '17 gcr-301-1\n'
|
||||
}
|
||||
gcr_gitea_delete_runner() {
|
||||
printf 'delete-runner repo=%s id=%s\n' "$1" "$2" >> "$calls"
|
||||
runner_present=0
|
||||
}
|
||||
gcr_gitea_set_runner_disabled() {
|
||||
printf 'disable-runner repo=%s id=%s value=%s\n' \
|
||||
"$1" "$2" "$3" >> "$calls"
|
||||
}
|
||||
original='{"job_id":"301","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":1000,"ttl_min":480,"vm_id":71,"vm_name":"gcr-301-1","runner_name":"gcr-301-1","server_type":"cx53","budget_rate":"0.032","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
gcr_record_put 301 1 "$original"
|
||||
gcr_deallocate 301 1 completed:success
|
||||
idle="$(gcr_record_get 301 1)"
|
||||
jq -e 'select(.status == "idle_vm" and .idle_expires_at == 4600 and
|
||||
.repo == "" and .label == "" and .runner_name == "" and
|
||||
.bootstrapped == false and .created_at == 1000 and .ttl_min == 480 and
|
||||
.server_type == "cx53" and .location == "nbg1" and .arch == "amd64" and
|
||||
.image_id == "424242")' "$(gcr_record_path 301 1)" >/dev/null
|
||||
test "$(grep -c '^list-runners repo=hinterland/hearth$' "$calls")" = 2
|
||||
grep -q '^disable-runner repo=hinterland/hearth id=17 value=true$' "$calls"
|
||||
grep -q '^delete-runner repo=hinterland/hearth id=17$' "$calls"
|
||||
test "$(grep -E '^(disable-runner|delete-runner)' "$calls")" = 'disable-runner repo=hinterland/hearth id=17 value=true
|
||||
delete-runner repo=hinterland/hearth id=17'
|
||||
test "$(gcr_count_active)" = 0
|
||||
if grep -Eq '^(runner-service .*start|destroy|budget|token|create)' "$calls"; then
|
||||
printf 'retirement must not restart, destroy, charge, or fetch a token\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
FAIL_HEALTH=0
|
||||
gcr_record_del 316 1
|
||||
gcr_record_del 301 1
|
||||
|
||||
# Expired idle capacity is never claimed; normal allocation then charges once.
|
||||
expired='{"job_id":"303","run_attempt":"1","repo":"hinterland/hearth","label":"gross-x86","created_at":1000,"ttl_min":180,"vm_id":72,"vm_name":"gcr-303-1","bootstrapped":true,"status":"idle_vm","idle_since":2000,"idle_expires_at":2800}'
|
||||
gcr_record_put 303 1 "$expired"
|
||||
NOW=2800
|
||||
export GCR_PER_REPO_CAP=2
|
||||
gcr_alloc 304 1 hinterland/hearth '["gross-x86"]'
|
||||
test "$RESPONSE_BODY" = 'allocated gcr-304-1'
|
||||
jq -e 'select(.vm_id == 99 and .status == "pending_vm")' \
|
||||
"$(gcr_record_path 304 1)" >/dev/null
|
||||
test "$(grep -c '^budget$' "$calls")" = 1
|
||||
# Failed deregistration remains durable, active, and unclaimable until retry.
|
||||
retire_list_count="$GCR_STATE_DIR/retire-list-count"
|
||||
printf '0\n' > "$retire_list_count"
|
||||
gcr_gitea_list_runners() {
|
||||
retire_lists="$(cat "$retire_list_count")"
|
||||
retire_lists=$((retire_lists + 1))
|
||||
printf '%s\n' "$retire_lists" > "$retire_list_count"
|
||||
printf 'list-retry %s\n' "$retire_lists" >> "$calls"
|
||||
[ "$retire_lists" -gt 1 ] || return 1
|
||||
}
|
||||
pending_retire='{"job_id":"302","run_attempt":"1","repo":"hinterland/hearth","label":"nix","created_at":1000,"ttl_min":480,"vm_id":72,"vm_name":"gcr-302-1","runner_name":"gcr-302-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
gcr_record_put 302 1 "$pending_retire"
|
||||
gcr_deallocate 302 1 completed:success
|
||||
test "$(gcr_record_field "$(gcr_record_get 302 1)" status)" = retiring_vm
|
||||
test "$(gcr_count_active)" = 1
|
||||
gcr_lock_acquire "$(gcr_alloc_key 399 1)"
|
||||
if gcr_claim_idle 399 1 hectic-lab/util.nix nix; then
|
||||
printf 'retiring VM must not be claimable\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
gcr_lock_release "$(gcr_alloc_key 399 1)"
|
||||
gcr_sweep_retiring_vms
|
||||
test "$(gcr_record_field "$(gcr_record_get 302 1)" status)" = idle_vm
|
||||
test "$(gcr_count_active)" = 0
|
||||
gcr_record_del 302 1
|
||||
gcr_record_put 301 1 "$idle"
|
||||
|
||||
# Compatible job in another repository claims generic idle by persisted profile.
|
||||
gcr_gitea_list_runners() { :; }
|
||||
gcr_alloc 303 1 hectic-lab/util.nix '["gross-nix-x86-perf"]'
|
||||
test "$RESPONSE_BODY" = 'rebuilding gcr-301-1'
|
||||
test ! -e "$(gcr_record_path 301 1)"
|
||||
claimed="$(gcr_record_get 303 1)"
|
||||
jq -e 'select(.job_id == "303" and .repo == "hectic-lab/util.nix" and
|
||||
.label == "gross-nix-x86-perf" and .vm_id == 71 and
|
||||
.vm_name == "gcr-301-1" and .runner_name == "gcr-303-1" 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)' \
|
||||
"$(gcr_record_path 303 1)" >/dev/null
|
||||
test "$(grep -Ec '^(budget|token|create)$' "$calls" || true)" = 0
|
||||
test "$(gcr_count_active)" = 1
|
||||
test "$(gcr_count_active_repo hectic-lab/util.nix)" = 1
|
||||
|
||||
# Rebuild intent and action id are durable around provider request and polling.
|
||||
gcr_gitea_job_state() { printf 'queued:'; }
|
||||
gcr_hcloud_req() {
|
||||
method="$1"; path="$2"; body="${3:-}"
|
||||
test ! -e "$GCR_STATE_DIR/jobs/.lock.admission"
|
||||
test ! -e "$GCR_STATE_DIR/jobs/.lock.idle-pool"
|
||||
GCR_LAST_BODY="$GCR_STATE_DIR/last-body.json"
|
||||
case "$method:$path" in
|
||||
POST:/servers/71/actions/rebuild)
|
||||
jq -e 'select(.image == "424242" and
|
||||
(.user_data | startswith("#cloud-config\n")) and
|
||||
(.user_data | contains("/etc/ssh/ssh_host_ed25519_key")) and
|
||||
keys == ["image", "user_data"])' <<EOF >/dev/null
|
||||
$body
|
||||
EOF
|
||||
request_rec="$(gcr_record_get 303 1)"
|
||||
test "$(gcr_record_field "$request_rec" status)" = rebuild_requesting
|
||||
test -z "$(gcr_record_field "$request_rec" rebuild_action_id)"
|
||||
printf '%s\n' '{"action":{"id":501,"status":"running","command":"rebuild","resources":[{"id":71,"type":"server"}]}}' > "$GCR_LAST_BODY"
|
||||
;;
|
||||
GET:/actions/501)
|
||||
poll_rec="$(gcr_record_get 303 1)"
|
||||
test "$(gcr_record_field "$poll_rec" status)" = rebuild_running
|
||||
test "$(gcr_record_field "$poll_rec" rebuild_action_id)" = 501
|
||||
printf '%s\n' '{"action":{"id":501,"status":"success","command":"rebuild","resources":[{"id":71,"type":"server"}]}}' > "$GCR_LAST_BODY"
|
||||
;;
|
||||
GET:/servers/71)
|
||||
printf '%s\n' '{"server":{"id":71,"status":"running","server_type":{"name":"cx53","architecture":"amd64"},"datacenter":{"location":{"name":"nbg1"}},"image":{"id":424242},"public_net":{"ipv4":{"ip":"192.0.2.71"}}}}' > "$GCR_LAST_BODY"
|
||||
;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
gcr_sweep_rebuilds
|
||||
ready="$(gcr_record_get 303 1)"
|
||||
jq -e 'select(.status == "pending_vm" and .bootstrapped == false and
|
||||
.last_rebuild_action_id == 501 and .ip == "192.0.2.71" and
|
||||
.created_at == 1000 and .ttl_min == 480 and .budget_rate == "0.032")' \
|
||||
"$(gcr_record_path 303 1)" >/dev/null
|
||||
if grep -q '^token$' "$calls"; then
|
||||
printf 'registration token must not be fetched before rebuild succeeds\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
gcr_vm_public_ip() { printf '192.0.2.71'; }
|
||||
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"
|
||||
}
|
||||
gcr_bootstrap_pending
|
||||
bootstrapped="$(gcr_record_get 303 1)"
|
||||
test "$(gcr_record_field "$bootstrapped" bootstrapped)" = true
|
||||
test "$(grep -c '^token$' "$calls")" = 1
|
||||
test "$(grep -c '^create$' "$calls")" = 1
|
||||
|
||||
gcr_sweep_ttl
|
||||
grep -q 'destroy vm=72' "$calls"
|
||||
test ! -e "$(gcr_record_path 303 1)"
|
||||
|
||||
# Busy pool lock defers instead of racing into paid allocation.
|
||||
export GCR_CONCURRENCY_CAP=3
|
||||
export GCR_PER_REPO_CAP=3
|
||||
gcr_lock_acquire idle-pool
|
||||
gcr_alloc 305 1 hinterland/hearth '["gross-arm"]'
|
||||
test "$RESPONSE_BODY" = 'deferred: idle pool busy'
|
||||
test "$(gcr_record_field "$(gcr_record_get 305 1)" status)" = deferred
|
||||
test "$(grep -c '^create$' "$calls")" = 1
|
||||
gcr_lock_release idle-pool
|
||||
|
||||
# Claim needs at least one reconcile interval before slot and hard expiry.
|
||||
export GCR_RECONCILE_INTERVAL_SEC=60
|
||||
near_expiry='{"job_id":"313","run_attempt":"1","repo":"hinterland/hearth","label":"gross-mixed-econ","created_at":1000,"ttl_min":180,"vm_id":78,"vm_name":"gcr-313-1","bootstrapped":true,"status":"idle_vm","idle_since":2000,"idle_expires_at":2860}'
|
||||
gcr_record_put 313 1 "$near_expiry"
|
||||
NOW=2801
|
||||
gcr_lock_acquire "$(gcr_alloc_key 314 1)"
|
||||
if gcr_claim_idle 314 1 hinterland/hearth gross-mixed-econ; then
|
||||
printf 'near-expiry VM must not be reused\n' >&2
|
||||
grep -q '^bootstrap ip=192.0.2.71 label=gross-nix-x86-perf token=fresh-token runner=gcr-303-1$' "$calls"
|
||||
if grep -q '^bootstrap .*runner=gcr-301-1$' "$calls"; then
|
||||
printf 'old runner identity must never be bootstrapped\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
gcr_lock_release "$(gcr_alloc_key 314 1)"
|
||||
test -e "$(gcr_record_path 313 1)"
|
||||
test ! -e "$(gcr_record_path 314 1)"
|
||||
gcr_record_del 313 1
|
||||
NOW=2800
|
||||
|
||||
# Two concurrent claims transfer one VM once; original labels/name stay safe.
|
||||
race_idle='{"job_id":"306","run_attempt":"1","repo":"hinterland/hearth","label":"gross-arm","created_at":1000,"ttl_min":180,"vm_id":73,"vm_name":"gcr-306-1","bootstrapped":true,"status":"idle_vm","idle_since":2000,"idle_expires_at":4600}'
|
||||
gcr_record_put 306 1 "$race_idle"
|
||||
# Incompatible image/profile remains idle and normal allocation creates anew.
|
||||
incompatible='{"job_id":"304","run_attempt":"1","repo":"","label":"","created_at":1000,"ttl_min":180,"vm_id":73,"vm_name":"gcr-304-1","runner_name":"","server_type":"cx53","budget_rate":"0.032","location":"nbg1","arch":"amd64","image_id":"313131","bootstrapped":false,"status":"idle_vm","idle_since":2000,"idle_expires_at":4600}'
|
||||
gcr_record_put 304 1 "$incompatible"
|
||||
gcr_alloc 305 1 hectic-lab/util.nix '["nix"]'
|
||||
test "$RESPONSE_BODY" = 'allocated gcr-305-1'
|
||||
test -e "$(gcr_record_path 304 1)"
|
||||
jq -e 'select(.status == "pending_vm" and .location == "nbg1" and
|
||||
.arch == "amd64" and .image_id == "424242")' \
|
||||
"$(gcr_record_path 305 1)" >/dev/null
|
||||
test "$(grep -c '^create$' "$calls")" = 1
|
||||
gcr_record_del 304 1
|
||||
gcr_record_del 305 1
|
||||
|
||||
# Uncertain rebuild POST destroys VM and never fetches target repository token.
|
||||
uncertain='{"job_id":"306","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":1000,"ttl_min":480,"vm_id":74,"vm_name":"gcr-306-old","runner_name":"gcr-306-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":false,"status":"rebuild_pending","rebuild_image_id":"424242","rebuild_action_id":"","rebuild_request_state":"pending"}'
|
||||
gcr_record_put 306 1 "$uncertain"
|
||||
token_count_before="$(grep -c '^token$' "$calls")"
|
||||
gcr_hcloud_req() {
|
||||
test "$1:$2" = 'POST:/servers/74/actions/rebuild'
|
||||
test "$(gcr_record_field "$(gcr_record_get 306 1)" status)" = rebuild_requesting
|
||||
GCR_LAST_HTTP=500
|
||||
return 1
|
||||
}
|
||||
gcr_sweep_rebuilds
|
||||
test ! -e "$(gcr_record_path 306 1)"
|
||||
grep -q '^destroy vm=74$' "$calls"
|
||||
test "$(grep -c '^token$' "$calls")" = "$token_count_before"
|
||||
|
||||
# Crash after durable request intent is unrecoverably ambiguous: destroy
|
||||
# without issuing another rebuild request.
|
||||
requesting='{"job_id":"307","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":1000,"ttl_min":480,"vm_id":75,"vm_name":"gcr-307-old","runner_name":"gcr-307-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":false,"status":"rebuild_requesting","rebuild_image_id":"424242","rebuild_action_id":"","rebuild_request_state":"requesting"}'
|
||||
gcr_record_put 307 1 "$requesting"
|
||||
gcr_hcloud_req() { printf 'unexpected-provider-request\n' >> "$calls"; return 1; }
|
||||
gcr_sweep_rebuilds
|
||||
test ! -e "$(gcr_record_path 307 1)"
|
||||
grep -q '^destroy vm=75$' "$calls"
|
||||
if grep -q '^unexpected-provider-request$' "$calls"; then
|
||||
printf 'ambiguous rebuild intent must not be replayed\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Pool lock contention defers instead of racing into paid allocation.
|
||||
gcr_lock_acquire idle-pool
|
||||
gcr_alloc 308 1 hectic-lab/util.nix '["nix"]'
|
||||
test "$RESPONSE_BODY" = 'deferred: idle pool busy'
|
||||
test "$(gcr_record_field "$(gcr_record_get 308 1)" status)" = deferred
|
||||
gcr_lock_release idle-pool
|
||||
gcr_record_del 308 1
|
||||
|
||||
# Transitional states count toward caps; generic idle does not.
|
||||
for status in retiring_vm rebuild_pending rebuild_requesting rebuild_running; do
|
||||
rec="$(printf '%s' "$uncertain" | jq -c --arg s "$status" '.status = $s')"
|
||||
gcr_record_put 309 1 "$rec"
|
||||
test "$(gcr_count_active)" = 2
|
||||
gcr_record_del 309 1
|
||||
done
|
||||
test "$(gcr_count_active)" = 1
|
||||
gcr_record_del 303 1
|
||||
test "$(gcr_count_active)" = 0
|
||||
|
||||
# Two concurrent compatible claims transfer one VM once.
|
||||
race_idle='{"job_id":"310","run_attempt":"1","repo":"","label":"","created_at":1000,"ttl_min":480,"vm_id":76,"vm_name":"gcr-310-1","runner_name":"","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":false,"status":"idle_vm","idle_since":2000,"idle_expires_at":4600}'
|
||||
gcr_record_put 310 1 "$race_idle"
|
||||
claim_script="$GCR_STATE_DIR/claim.sh"
|
||||
cat > "$claim_script" <<'EOF'
|
||||
#!/bin/dash
|
||||
set -eu
|
||||
. "$STATE_SH"
|
||||
. "$DECIDE_SH"
|
||||
. "$HCLOUD_SH"
|
||||
gcr_now_epoch() { printf '2800'; }
|
||||
key="$(gcr_alloc_key "$1" 1)"
|
||||
gcr_lock_acquire "$key"
|
||||
if gcr_claim_idle "$1" 1 hinterland/hearth gross-arm; then
|
||||
printf 'reused\n' > "$GCR_STATE_DIR/result-$1"
|
||||
if gcr_claim_idle "$1" 1 hectic-lab/util.nix nix; then
|
||||
printf 'claimed\n' > "$GCR_STATE_DIR/result-$1"
|
||||
else
|
||||
printf 'missed\n' > "$GCR_STATE_DIR/result-$1"
|
||||
fi
|
||||
gcr_lock_release "$key"
|
||||
EOF
|
||||
dash "$claim_script" 307 & first=$!
|
||||
dash "$claim_script" 308 & second=$!
|
||||
GCR_NIX_IMAGE_ID=424242 dash "$claim_script" 311 & first=$!
|
||||
GCR_NIX_IMAGE_ID=424242 dash "$claim_script" 312 & second=$!
|
||||
wait "$first"
|
||||
wait "$second"
|
||||
reused_count=0
|
||||
for result in "$GCR_STATE_DIR"/result-*; do
|
||||
[ "$(cat "$result")" = reused ] && reused_count=$((reused_count + 1))
|
||||
done
|
||||
test "$reused_count" = 1
|
||||
test ! -e "$(gcr_record_path 306 1)"
|
||||
|
||||
owner=""
|
||||
for job in 307 308; do
|
||||
test "$(grep -lc '^claimed$' "$GCR_STATE_DIR"/result-* | wc -l | tr -d ' ')" = 1
|
||||
test ! -e "$(gcr_record_path 310 1)"
|
||||
owner_count=0
|
||||
for job in 311 312; do
|
||||
rec="$(gcr_record_get "$job" 1)"
|
||||
if [ -n "$rec" ]; then
|
||||
test "$(gcr_record_field "$rec" vm_id)" = 73
|
||||
test "$(gcr_record_field "$rec" vm_name)" = gcr-306-1
|
||||
owner="$job"
|
||||
fi
|
||||
done
|
||||
test -n "$owner"
|
||||
|
||||
gcr_vm_list_managed() {
|
||||
printf 'list\n' >> "$GCR_STATE_DIR/list-calls"
|
||||
printf '[{"id":73,"labels":{"gcr.job-id":"306","gcr.run-attempt":"1"}}]'
|
||||
}
|
||||
gcr_gitea_list_runners() { printf '17 gcr-306-1\n'; }
|
||||
gcr_gitea_delete_runner() { printf 'delete repo=%s runner=%s\n' "$1" "$2" >> "$calls"; }
|
||||
before_destroy="$(grep -c '^destroy vm=73$' "$calls" || true)"
|
||||
gcr_lock_acquire admission
|
||||
gcr_sweep_orphan_vms
|
||||
test ! -e "$GCR_STATE_DIR/list-calls"
|
||||
gcr_lock_release admission
|
||||
gcr_sweep_orphan_vms
|
||||
test "$(wc -l < "$GCR_STATE_DIR/list-calls" | tr -d ' ')" = 1
|
||||
gcr_sweep_stale_runners
|
||||
after_destroy="$(grep -c '^destroy vm=73$' "$calls" || true)"
|
||||
test "$before_destroy" = "$after_destroy"
|
||||
if grep -q '^delete repo=.* runner=17$' "$calls"; then
|
||||
printf 'reused VM runner registration must not be swept as stale\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Interrupted destination-first transfer leaves duplicate state, never a VM
|
||||
# deletion: sweep drops superseded idle source and keeps active destination.
|
||||
duplicate_idle='{"job_id":"309","run_attempt":"1","repo":"hinterland/hearth","label":"gross-arm","created_at":1000,"ttl_min":180,"vm_id":75,"vm_name":"gcr-309-1","bootstrapped":true,"status":"idle_vm","idle_since":2000,"idle_expires_at":2800}'
|
||||
duplicate_active='{"job_id":"310","run_attempt":"1","repo":"hinterland/hearth","label":"gross-arm","created_at":1000,"ttl_min":180,"vm_id":75,"vm_name":"gcr-309-1","bootstrapped":true,"status":"pending_vm"}'
|
||||
gcr_record_put 309 1 "$duplicate_idle"
|
||||
gcr_record_put 310 1 "$duplicate_active"
|
||||
gcr_sweep_ttl
|
||||
test ! -e "$(gcr_record_path 309 1)"
|
||||
test -e "$(gcr_record_path 310 1)"
|
||||
if grep -q '^destroy vm=75$' "$calls"; then
|
||||
printf 'superseded idle record must not destroy reassigned VM\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Delayed old-job in_progress delivery cannot reactivate idle ownership.
|
||||
late_idle='{"job_id":"311","run_attempt":"1","repo":"hinterland/hearth","label":"gross-arm","created_at":1000,"ttl_min":180,"vm_id":76,"vm_name":"gcr-311-1","bootstrapped":true,"status":"idle_vm","idle_since":2000,"idle_expires_at":4600}'
|
||||
gcr_record_put 311 1 "$late_idle"
|
||||
gcr_mark_in_progress 311 1
|
||||
test "$(gcr_record_field "$(gcr_record_get 311 1)" status)" = idle_vm
|
||||
|
||||
# Crash-stranded locks are reclaimed by dead owner PID; live contention remains.
|
||||
mkdir "$GCR_STATE_DIR/jobs/.lock.stale-test"
|
||||
if gcr_lock_acquire stale-test; then
|
||||
printf 'pre-existing lock must not be reclaimed\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
rm -rf "$GCR_STATE_DIR/jobs/.lock.stale-test"
|
||||
mkdir "$GCR_STATE_DIR/jobs/.lock.crashed-test"
|
||||
printf '999999 1\n' > "$GCR_STATE_DIR/jobs/.lock.crashed-test/owner"
|
||||
gcr_lock_acquire crashed-test
|
||||
gcr_lock_release crashed-test
|
||||
|
||||
# Concurrent recovery of one dead lock admits exactly one owner.
|
||||
mkdir "$GCR_STATE_DIR/jobs/.lock.crashed-race"
|
||||
printf '999999 1\n' > "$GCR_STATE_DIR/jobs/.lock.crashed-race/owner"
|
||||
reclaim_script="$GCR_STATE_DIR/reclaim.sh"
|
||||
cat > "$reclaim_script" <<'EOF'
|
||||
#!/bin/dash
|
||||
set -eu
|
||||
. "$STATE_SH"
|
||||
if gcr_lock_acquire crashed-race; then
|
||||
printf 'acquired\n' > "$GCR_STATE_DIR/reclaim-$1"
|
||||
sleep 1
|
||||
gcr_lock_release crashed-race
|
||||
else
|
||||
printf 'busy\n' > "$GCR_STATE_DIR/reclaim-$1"
|
||||
fi
|
||||
EOF
|
||||
dash "$reclaim_script" first & first=$!
|
||||
dash "$reclaim_script" second & second=$!
|
||||
wait "$first"
|
||||
wait "$second"
|
||||
test "$(grep -lc '^acquired$' "$GCR_STATE_DIR"/reclaim-* | wc -l | tr -d ' ')" = 1
|
||||
mkdir "$GCR_STATE_DIR/jobs/.lock.crashed-test"
|
||||
printf '999999 1\n' > "$GCR_STATE_DIR/jobs/.lock.crashed-test/owner"
|
||||
gcr_lock_acquire crashed-test
|
||||
gcr_lock_release crashed-test
|
||||
|
||||
for job in 307 308 310 311; do
|
||||
[ -z "$rec" ] || owner_count=$((owner_count + 1))
|
||||
[ -z "$rec" ] || test "$(gcr_record_field "$rec" status)" = rebuild_pending
|
||||
gcr_record_del "$job" 1
|
||||
done
|
||||
ttl_record='{"job_id":"312","run_attempt":"1","repo":"hinterland/hearth","label":"gross-x86","created_at":1000,"ttl_min":180,"vm_id":77,"vm_name":"gcr-312-1","bootstrapped":true,"status":"vm_active"}'
|
||||
gcr_record_put 312 1 "$ttl_record"
|
||||
gcr_vm_public_ip() { printf '192.0.2.%s' "$1"; }
|
||||
gcr_vm_collect_diagnostics() {
|
||||
printf 'diag vm=%s job=%s reason=%s\n' "$1" "$3" "$4" >> "$calls"
|
||||
}
|
||||
NOW=11799
|
||||
gcr_sweep_ttl
|
||||
test -e "$(gcr_record_path 312 1)"
|
||||
NOW=11800
|
||||
gcr_sweep_ttl
|
||||
test ! -e "$(gcr_record_path 312 1)"
|
||||
grep -q '^diag vm=77 job=312 reason=ttl$' "$calls"
|
||||
grep -q '^destroy vm=77$' "$calls"
|
||||
test "$owner_count" = 1
|
||||
|
||||
# Admission lock serializes cap check and creation across webhook processes.
|
||||
ADMISSION_STATE="$GCR_STATE_DIR/admission-state"
|
||||
mkdir "$ADMISSION_STATE"
|
||||
admission_script="$ADMISSION_STATE/allocate.sh"
|
||||
cat > "$admission_script" <<'EOF'
|
||||
#!/bin/dash
|
||||
set -eu
|
||||
. "$LOG_SH"
|
||||
. "$STATE_SH"
|
||||
. "$DECIDE_SH"
|
||||
. "$HCLOUD_SH"
|
||||
. "$WEBHOOK_SH"
|
||||
gcr_now_epoch() { printf '2000'; }
|
||||
gcr_budget_add() { printf 'budget %s\n' "$1" >> "$GCR_STATE_DIR/admission-calls"; }
|
||||
gcr_gitea_registration_token() { printf token; }
|
||||
gcr_vm_create() {
|
||||
sleep 1
|
||||
printf 'create %s\n' "$6" >> "$GCR_STATE_DIR/admission-calls"
|
||||
gcr_budget_add 0.032 180
|
||||
printf '%s cx53 0.032' "$6"
|
||||
}
|
||||
gcr_vm_destroy() { :; }
|
||||
gcr_state_init
|
||||
gcr_alloc "$1" 1 hinterland/hearth '["gross-x86"]'
|
||||
EOF
|
||||
old_state="$GCR_STATE_DIR"
|
||||
GCR_STATE_DIR="$ADMISSION_STATE" \
|
||||
GCR_ALLOWED_REPOS=hinterland/hearth \
|
||||
GCR_CONCURRENCY_CAP=1 \
|
||||
GCR_PER_REPO_CAP=1 \
|
||||
dash "$admission_script" 401 & first=$!
|
||||
GCR_STATE_DIR="$ADMISSION_STATE" \
|
||||
GCR_ALLOWED_REPOS=hinterland/hearth \
|
||||
GCR_CONCURRENCY_CAP=1 \
|
||||
GCR_PER_REPO_CAP=1 \
|
||||
dash "$admission_script" 402 & second=$!
|
||||
wait "$first"
|
||||
wait "$second"
|
||||
GCR_STATE_DIR="$ADMISSION_STATE"
|
||||
test "$(grep -c '^create ' "$GCR_STATE_DIR/admission-calls")" = 1
|
||||
active_count="$(gcr_count_active)"
|
||||
test "$active_count" = 1
|
||||
deferred_count="$(grep -El '"status"[[:space:]]*:[[:space:]]*"deferred"' \
|
||||
"$GCR_STATE_DIR"/jobs/*.json | wc -l | tr -d ' ')"
|
||||
test "$deferred_count" = 1
|
||||
GCR_STATE_DIR="$old_state"
|
||||
# Crash-stranded locks are reclaimed; live/ownerless locks remain closed.
|
||||
mkdir "$GCR_STATE_DIR/jobs/.lock.ownerless"
|
||||
if gcr_lock_acquire ownerless; then
|
||||
printf 'fresh ownerless lock must not be reclaimed\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
rm -rf "$GCR_STATE_DIR/jobs/.lock.ownerless"
|
||||
mkdir "$GCR_STATE_DIR/jobs/.lock.crashed"
|
||||
printf '999999 1\n' > "$GCR_STATE_DIR/jobs/.lock.crashed/owner"
|
||||
gcr_lock_acquire crashed
|
||||
gcr_lock_release crashed
|
||||
|
||||
# Original hard TTL remains authoritative after cross-repository assignment.
|
||||
ttl_rebuild='{"job_id":"313","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":1000,"ttl_min":480,"vm_id":77,"vm_name":"gcr-313-old","runner_name":"gcr-313-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":false,"status":"rebuild_running","rebuild_image_id":"424242","rebuild_action_id":700,"rebuild_request_state":"accepted"}'
|
||||
gcr_record_put 313 1 "$ttl_rebuild"
|
||||
NOW=29799
|
||||
gcr_sweep_ttl
|
||||
test -e "$(gcr_record_path 313 1)"
|
||||
NOW=29800
|
||||
gcr_sweep_ttl
|
||||
test ! -e "$(gcr_record_path 313 1)"
|
||||
grep -q '^destroy vm=77$' "$calls"
|
||||
|
||||
@@ -11,6 +11,7 @@ set -eu
|
||||
gcr_state_init
|
||||
calls="$GCR_STATE_DIR/calls"
|
||||
gcr_now_epoch() { printf '1800'; }
|
||||
export GCR_NIX_IMAGE_ID=424242
|
||||
|
||||
gcr_gitea_job_state() {
|
||||
case "$2" in
|
||||
@@ -33,29 +34,21 @@ gcr_vm_destroy() {
|
||||
printf 'destroy vm=%s\n' "$1" >> "$calls"
|
||||
}
|
||||
|
||||
gcr_vm_runner_service() {
|
||||
printf 'runner %s vm=%s\n' "$2" "$1" >> "$calls"
|
||||
gcr_gitea_deregister_runner_exact() {
|
||||
printf 'deregister repo=%s runner=%s\n' "$1" "$2" >> "$calls"
|
||||
}
|
||||
|
||||
gcr_gitea_runner_disabled() {
|
||||
printf 'disabled runner=%s value=%s\n' "$2" "$3" >> "$calls"
|
||||
}
|
||||
|
||||
record_success='{"job_id":"101","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"0","ttl_min":480,"vm_id":41,"vm_name":"gcr-101-1","bootstrapped":true,"status":"vm_active"}'
|
||||
record_failure='{"job_id":"102","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"1","ttl_min":480,"vm_id":42,"vm_name":"gcr-102-1","bootstrapped":true,"status":"vm_active"}'
|
||||
record_success='{"job_id":"101","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"0","ttl_min":480,"vm_id":41,"vm_name":"gcr-101-1","runner_name":"gcr-101-1","server_type":"cx53","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
record_failure='{"job_id":"102","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"1","ttl_min":480,"vm_id":42,"vm_name":"gcr-102-1","runner_name":"gcr-102-1","server_type":"cx53","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
|
||||
gcr_record_put 101 1 "$record_success"
|
||||
gcr_record_put 102 1 "$record_failure"
|
||||
gcr_reap_finished_jobs
|
||||
|
||||
grep -q 'diag vm=42 ip=192.0.2.42 job=102 reason=completed:failure' "$calls"
|
||||
grep -q 'runner health vm=42' "$calls"
|
||||
grep -q 'disabled runner=gcr-102-1 value=true' "$calls"
|
||||
grep -q 'runner stop vm=42' "$calls"
|
||||
test "$(grep -E '^(diag vm=42|runner health vm=42|disabled runner=gcr-102-1|runner stop vm=42)' "$calls")" = 'diag vm=42 ip=192.0.2.42 job=102 reason=completed:failure
|
||||
runner health vm=42
|
||||
disabled runner=gcr-102-1 value=true
|
||||
runner stop vm=42'
|
||||
grep -q 'deregister repo=hinterland/hearth runner=gcr-102-1' "$calls"
|
||||
test "$(grep -E '^(diag vm=42|deregister repo=hinterland/hearth runner=gcr-102-1)' "$calls")" = 'diag vm=42 ip=192.0.2.42 job=102 reason=completed:failure
|
||||
deregister repo=hinterland/hearth runner=gcr-102-1'
|
||||
if grep -q 'diag vm=41' "$calls"; then
|
||||
printf 'success job should not collect diagnostics\n' >&2
|
||||
exit 1
|
||||
@@ -79,7 +72,7 @@ gcr_reap_finished_jobs
|
||||
test "$(gcr_record_get 101 1)" = "$idle_once"
|
||||
|
||||
# Skipped terminal jobs follow same healthy retention policy without diagnostics.
|
||||
record_skipped='{"job_id":"105","run_attempt":"1","repo":"hinterland/hearth","label":"nix","created_at":"1","ttl_min":480,"vm_id":45,"vm_name":"gcr-105-1","bootstrapped":true,"status":"vm_active"}'
|
||||
record_skipped='{"job_id":"105","run_attempt":"1","repo":"hinterland/hearth","label":"nix","created_at":"1","ttl_min":480,"vm_id":45,"vm_name":"gcr-105-1","runner_name":"gcr-105-1","server_type":"cx23","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
gcr_record_put 105 1 "$record_skipped"
|
||||
gcr_reap_finished_jobs
|
||||
test "$(gcr_record_field "$(gcr_record_get 105 1)" status)" = idle_vm
|
||||
@@ -101,10 +94,6 @@ gcr_vm_destroy() {
|
||||
printf 'destroy-failed vm=%s\n' "$1" >> "$calls"
|
||||
return 1
|
||||
}
|
||||
gcr_vm_runner_service() {
|
||||
printf 'runner %s vm=%s\n' "$2" "$1" >> "$calls"
|
||||
[ "$1" != 44 ] || [ "$2" != stop ]
|
||||
}
|
||||
gcr_reap_finished_jobs
|
||||
test "$(gcr_record_field "$(gcr_record_get 104 1)" status)" = cleanup_pending
|
||||
test "$(gcr_count_active)" = 1
|
||||
@@ -117,7 +106,7 @@ grep -q '^destroy-retry vm=44$' "$calls"
|
||||
|
||||
calls_ip_fail="$GCR_STATE_DIR/calls-ip-fail"
|
||||
calls="$calls_ip_fail"
|
||||
record_ip_fail='{"job_id":"103","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"1","ttl_min":480,"vm_id":43,"vm_name":"gcr-103-1","bootstrapped":true,"status":"vm_active"}'
|
||||
record_ip_fail='{"job_id":"103","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"1","ttl_min":480,"vm_id":43,"vm_name":"gcr-103-1","runner_name":"gcr-103-1","server_type":"cx53","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
gcr_record_put 103 1 "$record_ip_fail"
|
||||
|
||||
gcr_gitea_job_state() {
|
||||
@@ -130,11 +119,6 @@ gcr_gitea_job_state() {
|
||||
gcr_vm_public_ip() {
|
||||
return 1
|
||||
}
|
||||
gcr_vm_runner_service() {
|
||||
printf 'runner %s vm=%s\n' "$2" "$1" >> "$calls"
|
||||
return 1
|
||||
}
|
||||
|
||||
gcr_vm_destroy() {
|
||||
printf 'destroy vm=%s\n' "$1" >> "$calls"
|
||||
}
|
||||
@@ -142,6 +126,9 @@ gcr_vm_destroy() {
|
||||
gcr_reap_finished_jobs
|
||||
|
||||
grep -q 'diag vm=43 ip= job=103 reason=completed:failure' "$calls_ip_fail"
|
||||
grep -q 'runner health vm=43' "$calls_ip_fail"
|
||||
grep -q 'destroy vm=43' "$calls_ip_fail"
|
||||
test ! -e "$(gcr_record_path 103 1)"
|
||||
grep -q 'deregister repo=hinterland/hearth runner=gcr-103-1' "$calls_ip_fail"
|
||||
test "$(gcr_record_field "$(gcr_record_get 103 1)" status)" = idle_vm
|
||||
if grep -q 'destroy vm=43' "$calls_ip_fail"; then
|
||||
printf 'diagnostic IP lookup failure must not bypass safe deregistration\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -11,6 +11,7 @@ set -eu
|
||||
gcr_state_init
|
||||
calls="$GCR_STATE_DIR/calls"
|
||||
gcr_now_epoch() { printf '1800'; }
|
||||
export GCR_NIX_IMAGE_ID=424242
|
||||
|
||||
gcr_vm_public_ip() {
|
||||
printf '192.0.2.%s' "$1"
|
||||
@@ -24,16 +25,12 @@ gcr_vm_destroy() {
|
||||
printf 'destroy vm=%s\n' "$1" >> "$calls"
|
||||
}
|
||||
|
||||
gcr_vm_runner_service() {
|
||||
printf 'runner %s vm=%s\n' "$2" "$1" >> "$calls"
|
||||
gcr_gitea_deregister_runner_exact() {
|
||||
printf 'deregister repo=%s runner=%s\n' "$1" "$2" >> "$calls"
|
||||
}
|
||||
|
||||
gcr_gitea_runner_disabled() {
|
||||
printf 'disabled runner=%s value=%s\n' "$2" "$3" >> "$calls"
|
||||
}
|
||||
|
||||
record_success='{"job_id":"201","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"0","ttl_min":480,"vm_id":51,"vm_name":"gcr-201-1","bootstrapped":true,"status":"vm_active"}'
|
||||
record_failure='{"job_id":"202","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"1","ttl_min":480,"vm_id":52,"vm_name":"gcr-202-1","bootstrapped":true,"status":"vm_active"}'
|
||||
record_success='{"job_id":"201","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"0","ttl_min":480,"vm_id":51,"vm_name":"gcr-201-1","runner_name":"gcr-201-1","server_type":"cx53","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
record_failure='{"job_id":"202","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"1","ttl_min":480,"vm_id":52,"vm_name":"gcr-202-1","runner_name":"gcr-202-1","server_type":"cx53","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
|
||||
gcr_record_put 201 1 "$record_success"
|
||||
gcr_record_put 202 1 "$record_failure"
|
||||
@@ -41,13 +38,9 @@ gcr_deallocate 201 1 completed:success
|
||||
gcr_deallocate 202 1 completed:failure
|
||||
|
||||
grep -q 'diag vm=52 ip=192.0.2.52 job=202 reason=completed:failure' "$calls"
|
||||
grep -q 'runner health vm=52' "$calls"
|
||||
grep -q 'disabled runner=gcr-202-1 value=true' "$calls"
|
||||
grep -q 'runner stop vm=52' "$calls"
|
||||
test "$(grep -E '^(diag vm=52|runner health vm=52|disabled runner=gcr-202-1|runner stop vm=52)' "$calls")" = 'diag vm=52 ip=192.0.2.52 job=202 reason=completed:failure
|
||||
runner health vm=52
|
||||
disabled runner=gcr-202-1 value=true
|
||||
runner stop vm=52'
|
||||
grep -q 'deregister repo=hinterland/hearth runner=gcr-202-1' "$calls"
|
||||
test "$(grep -E '^(diag vm=52|deregister repo=hinterland/hearth runner=gcr-202-1)' "$calls")" = 'diag vm=52 ip=192.0.2.52 job=202 reason=completed:failure
|
||||
deregister repo=hinterland/hearth runner=gcr-202-1'
|
||||
if grep -q 'diag vm=51' "$calls"; then
|
||||
printf 'success webhook should not collect diagnostics\n' >&2
|
||||
exit 1
|
||||
@@ -71,7 +64,8 @@ gcr_record_del 201 1
|
||||
gcr_lock_acquire "$(gcr_alloc_key 205 1)"
|
||||
gcr_claim_idle 205 1 hinterland/hearth gross-nix-x86-perf
|
||||
gcr_lock_release "$(gcr_alloc_key 205 1)"
|
||||
jq -e 'select(.job_id == "205" and .vm_id == 52 and .status == "pending_vm" and .reused_vm == true)' \
|
||||
jq -e 'select(.job_id == "205" and .vm_id == 52 and
|
||||
.status == "rebuild_pending" and .runner_name == "gcr-205-1")' \
|
||||
"$(gcr_record_path 205 1)" >/dev/null
|
||||
gcr_record_del 205 1
|
||||
gcr_record_put 201 1 "$idle_once"
|
||||
@@ -79,7 +73,7 @@ gcr_deallocate 201 1 completed:success
|
||||
test "$(gcr_record_get 201 1)" = "$idle_once"
|
||||
|
||||
# Cancelled terminal jobs use same retention policy without failure diagnostics.
|
||||
record_cancelled='{"job_id":"207","run_attempt":"1","repo":"hinterland/hearth","label":"nix","created_at":"1","ttl_min":480,"vm_id":57,"vm_name":"gcr-207-1","bootstrapped":true,"status":"vm_active"}'
|
||||
record_cancelled='{"job_id":"207","run_attempt":"1","repo":"hinterland/hearth","label":"nix","created_at":"1","ttl_min":480,"vm_id":57,"vm_name":"gcr-207-1","runner_name":"gcr-207-1","server_type":"cx23","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
gcr_record_put 207 1 "$record_cancelled"
|
||||
gcr_deallocate 207 1 completed:cancelled
|
||||
test "$(gcr_record_field "$(gcr_record_get 207 1)" status)" = idle_vm
|
||||
@@ -90,25 +84,19 @@ fi
|
||||
|
||||
calls_ip_fail="$GCR_STATE_DIR/calls-ip-fail"
|
||||
calls="$calls_ip_fail"
|
||||
record_ip_fail='{"job_id":"203","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"1","ttl_min":480,"vm_id":53,"vm_name":"gcr-203-1","bootstrapped":true,"status":"vm_active"}'
|
||||
record_ip_fail='{"job_id":"203","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":"1","ttl_min":480,"vm_id":53,"vm_name":"gcr-203-1","runner_name":"gcr-203-1","server_type":"cx53","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
|
||||
gcr_record_put 203 1 "$record_ip_fail"
|
||||
|
||||
gcr_vm_public_ip() {
|
||||
return 1
|
||||
}
|
||||
gcr_vm_runner_service() {
|
||||
printf 'runner %s vm=%s\n' "$2" "$1" >> "$calls"
|
||||
return 1
|
||||
}
|
||||
|
||||
gcr_deallocate 203 1 completed:failure
|
||||
|
||||
grep -q 'diag vm=53 ip= job=203 reason=completed:failure' "$calls_ip_fail"
|
||||
grep -q 'runner health vm=53' "$calls_ip_fail"
|
||||
grep -q 'destroy vm=53' "$calls_ip_fail"
|
||||
test ! -e "$(gcr_record_path 203 1)"
|
||||
if grep -q 'disabled runner=gcr-203-1' "$calls_ip_fail"; then
|
||||
printf 'unhealthy terminal runner must not enter idle shutdown path\n' >&2
|
||||
grep -q 'deregister repo=hinterland/hearth runner=gcr-203-1' "$calls_ip_fail"
|
||||
test "$(gcr_record_field "$(gcr_record_get 203 1)" status)" = idle_vm
|
||||
if grep -q 'destroy vm=53' "$calls_ip_fail"; then
|
||||
printf 'diagnostic IP lookup failure must not bypass safe deregistration\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -119,10 +107,6 @@ gcr_vm_destroy() {
|
||||
printf 'destroy-failed vm=%s\n' "$1" >> "$calls_ip_fail"
|
||||
return 1
|
||||
}
|
||||
gcr_vm_runner_service() {
|
||||
printf 'runner %s vm=%s\n' "$2" "$1" >> "$calls"
|
||||
[ "$2" != stop ]
|
||||
}
|
||||
gcr_deallocate 204 1 completed:failure
|
||||
cleanup_rec="$(gcr_record_get 204 1)"
|
||||
test "$(gcr_record_field "$cleanup_rec" status)" = cleanup_pending
|
||||
@@ -153,11 +137,12 @@ if grep -q '^unexpected-runner 56$' "$calls_ip_fail"; then
|
||||
fi
|
||||
|
||||
# Gitea emits zero-based run_attempt values for initial workflow jobs.
|
||||
next_body='{"action":"queued","workflow_job":{"id":331,"run_attempt":0,"labels":["nix"]},"repository":{"full_name":"hinterland/hearth"}}'
|
||||
gcr_read_request() {
|
||||
gcr_hdr_event_type=workflow_job
|
||||
gcr_hdr_delivery=test-delivery
|
||||
gcr_hdr_signature=test-signature
|
||||
gcr_body='{"action":"queued","workflow_job":{"id":331,"run_attempt":0,"labels":["nix"]},"repository":{"full_name":"hinterland/hearth"}}'
|
||||
gcr_body="$next_body"
|
||||
}
|
||||
gcr_verify_signature() { :; }
|
||||
gcr_alloc() {
|
||||
@@ -170,3 +155,18 @@ gcr_respond() { printf '%s:%s\n' "$1" "$2" > "$GCR_STATE_DIR/response"; }
|
||||
gcr_handle_webhook
|
||||
test "$(cat "$GCR_STATE_DIR/allocation")" = '331:0:hinterland/hearth:["nix"]'
|
||||
test "$(cat "$GCR_STATE_DIR/response")" = '202:allocated'
|
||||
|
||||
# Signed lifecycle events from another or missing repository cannot mutate an
|
||||
# existing allocation with the same numeric job identity.
|
||||
bound='{"job_id":"332","run_attempt":"1","repo":"hinterland/hearth","label":"nix","created_at":"1","ttl_min":480,"vm_id":58,"vm_name":"gcr-332-1","runner_name":"gcr-332-1","server_type":"cx23","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"pending_vm"}'
|
||||
gcr_record_put 332 1 "$bound"
|
||||
next_body='{"action":"in_progress","workflow_job":{"id":332,"run_attempt":1,"labels":["nix"]},"repository":{"full_name":"other/repo"}}'
|
||||
gcr_handle_webhook
|
||||
test "$(gcr_record_field "$(gcr_record_get 332 1)" status)" = pending_vm
|
||||
next_body='{"action":"completed","workflow_job":{"id":332,"run_attempt":1,"conclusion":"success","labels":["nix"]},"repository":{"full_name":"other/repo"}}'
|
||||
gcr_handle_webhook
|
||||
test "$(gcr_record_field "$(gcr_record_get 332 1)" status)" = pending_vm
|
||||
next_body='{"action":"completed","workflow_job":{"id":332,"run_attempt":1,"conclusion":"success","labels":["nix"]},"repository":{}}'
|
||||
gcr_handle_webhook
|
||||
test "$(cat "$GCR_STATE_DIR/response")" = '400:malformed payload'
|
||||
test "$(gcr_record_field "$(gcr_record_get 332 1)" status)" = pending_vm
|
||||
|
||||
Reference in New Issue
Block a user