353 lines
14 KiB
Bash
353 lines
14 KiB
Bash
#!/bin/dash
|
|
# Gitea API wrappers for gitea-runner-controller.
|
|
# Requires: GCR_GITEA_URL, GITEA_REGISTRATION_TOKEN_FILE, GITEA_ADMIN_TOKEN_FILE
|
|
|
|
gcr_gitea_curl() {
|
|
gcr_gitea_admin_token >/dev/null || return 1
|
|
gitea_auth_header="$(mktemp "${TMPDIR:-/tmp}/gcr-gitea-auth.XXXXXX")" \
|
|
|| return 1
|
|
chmod 0600 "$gitea_auth_header"
|
|
if ! printf 'Authorization: token ' > "$gitea_auth_header" \
|
|
|| ! tr -d '\n' < "$GITEA_ADMIN_TOKEN_FILE" >> "$gitea_auth_header" \
|
|
|| ! printf '\n' >> "$gitea_auth_header"; then
|
|
rm -f "$gitea_auth_header"
|
|
return 1
|
|
fi
|
|
gitea_curl_status=0
|
|
curl -H "@$gitea_auth_header" "$@" || gitea_curl_status="$?"
|
|
rm -f "$gitea_auth_header"
|
|
return "$gitea_curl_status"
|
|
}
|
|
|
|
gcr_gitea_registration_token() {
|
|
repo="$1"
|
|
gcr_gitea_admin_token >/dev/null || return 1
|
|
owner="${repo%%/*}"
|
|
name="${repo#*/}"
|
|
gcr_gitea_curl -fsS -X POST \
|
|
"$GCR_GITEA_URL/api/v1/repos/$owner/$name/actions/runners/registration-token" \
|
|
| jq -er '.token | select(type == "string" and length > 0)'
|
|
}
|
|
|
|
gcr_gitea_admin_token() {
|
|
test -r "${GITEA_ADMIN_TOKEN_FILE:-}" || {
|
|
gcr_log error --ns=gitea "GITEA_ADMIN_TOKEN_FILE missing"
|
|
return 1
|
|
}
|
|
tr -d '\n' < "$GITEA_ADMIN_TOKEN_FILE"
|
|
}
|
|
|
|
# gcr_gitea_list_runners REPO — prints "id busy disabled name" lines.
|
|
gcr_gitea_list_runners() {
|
|
repo="$1"
|
|
gcr_gitea_admin_token >/dev/null || return 1
|
|
owner="${repo%%/*}"
|
|
name="${repo#*/}"
|
|
page=1
|
|
while :; do
|
|
runner_page="$(gcr_gitea_curl -fsS \
|
|
"$GCR_GITEA_URL/api/v1/repos/$owner/$name/actions/runners?page=$page&limit=50")" \
|
|
|| return 1
|
|
printf '%s' "$runner_page" | jq -e \
|
|
'.runners | select(type == "array")
|
|
| all(.[]; (.id | type) == "number"
|
|
and (.busy | type) == "boolean"
|
|
and (.disabled | type) == "boolean"
|
|
and (.name | type) == "string")' \
|
|
>/dev/null || return 1
|
|
printf '%s' "$runner_page" \
|
|
| jq -r '.runners[] | "\(.id) \(.busy) \(.disabled) \(.name)"' \
|
|
|| return 1
|
|
runner_count="$(printf '%s' "$runner_page" | jq -er '.runners | length')" \
|
|
|| return 1
|
|
[ "$runner_count" -lt 50 ] && return 0
|
|
page=$((page + 1))
|
|
done
|
|
}
|
|
|
|
# Find one exact runner after a fresh list. Globals avoid command-substitution
|
|
# subshell state loss. Exit 0 = found, 1 = absent, 2 = ambiguous/list failure.
|
|
gcr_gitea_find_runner_exact() {
|
|
find_runner_repo="$1"; find_runner_name="$2"
|
|
GCR_RUNNER_ID=""
|
|
GCR_RUNNER_DISABLED=""
|
|
find_runner_list="$(gcr_gitea_list_runners "$find_runner_repo")" || return 2
|
|
find_runner_count=0
|
|
while read -r find_runner_id find_runner_busy find_runner_disabled \
|
|
find_runner_candidate_name; do
|
|
[ "${find_runner_candidate_name:-}" = "$find_runner_name" ] || continue
|
|
case "$find_runner_id" in ''|*[!0-9]*) return 2 ;; esac
|
|
case "$find_runner_busy" in true|false) ;; *) return 2 ;; esac
|
|
case "$find_runner_disabled" in true|false) ;; *) return 2 ;; esac
|
|
find_runner_count=$((find_runner_count + 1))
|
|
GCR_RUNNER_ID="$find_runner_id"
|
|
GCR_RUNNER_DISABLED="$find_runner_disabled"
|
|
done <<EOF
|
|
$find_runner_list
|
|
EOF
|
|
case "$find_runner_count" in
|
|
0) return 1 ;;
|
|
1) return 0 ;;
|
|
*)
|
|
gcr_log error --ns=gitea \
|
|
"duplicate exact runner registrations repo=$find_runner_repo name=$find_runner_name"
|
|
return 2
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# gcr_gitea_list_owner_repos OWNER — prints fully-qualified repository names.
|
|
# Allowed owner globs may refer to either organizations or users. Probe the
|
|
# organization route first, then use the user route on a confirmed 404.
|
|
gcr_gitea_list_owner_repos() {
|
|
owner="$1"
|
|
gcr_gitea_admin_token >/dev/null || return 1
|
|
owner_kind=orgs
|
|
page=1
|
|
while :; do
|
|
owner_page="$(mktemp "${TMPDIR:-/tmp}/gcr-owner-repos.XXXXXX")" \
|
|
|| return 1
|
|
owner_status="$(gcr_gitea_curl -sS -o "$owner_page" -w '%{http_code}' \
|
|
"$GCR_GITEA_URL/api/v1/$owner_kind/$owner/repos?page=$page&limit=50")" \
|
|
|| { rm -f "$owner_page"; return 1; }
|
|
if [ "$owner_status" = 404 ] && [ "$owner_kind" = orgs ] && [ "$page" = 1 ]; then
|
|
owner_kind=users
|
|
rm -f "$owner_page"
|
|
continue
|
|
fi
|
|
[ "$owner_status" = 200 ] || {
|
|
rm -f "$owner_page"
|
|
return 1
|
|
}
|
|
jq -e 'type == "array"' "$owner_page" >/dev/null || {
|
|
rm -f "$owner_page"
|
|
return 1
|
|
}
|
|
jq -r '.[] | .full_name' "$owner_page"
|
|
count="$(jq 'length' "$owner_page")" || {
|
|
rm -f "$owner_page"
|
|
return 1
|
|
}
|
|
rm -f "$owner_page"
|
|
[ "$count" -lt 50 ] && return 0
|
|
page=$((page + 1))
|
|
done
|
|
}
|
|
|
|
# Compatibility name for callers outside active controller path.
|
|
gcr_gitea_list_org_repos() {
|
|
gcr_gitea_list_owner_repos "$1"
|
|
}
|
|
|
|
# gcr_gitea_job_state REPO JOB_ID — prints "<status>:<conclusion>".
|
|
gcr_gitea_job_state() {
|
|
repo="$1"; job_id="$2"
|
|
gcr_gitea_admin_token >/dev/null || return 1
|
|
owner="${repo%%/*}"
|
|
name="${repo#*/}"
|
|
gcr_gitea_curl -fsS \
|
|
"$GCR_GITEA_URL/api/v1/repos/$owner/$name/actions/jobs/$job_id" \
|
|
| 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
|
|
# complete pagination proves none, and 2 on API or schema uncertainty.
|
|
gcr_gitea_runner_has_active_job() {
|
|
active_repo="$1"; active_runner_id="$2"
|
|
case "$active_runner_id" in ''|*[!0-9]*) return 2 ;; esac
|
|
active_owner="${active_repo%%/*}"
|
|
active_name="${active_repo#*/}"
|
|
active_page=1
|
|
while :; do
|
|
active_jobs="$(gcr_gitea_curl -fsS \
|
|
"$GCR_GITEA_URL/api/v1/repos/$active_owner/$active_name/actions/jobs?status=in_progress&page=$active_page&limit=50")" \
|
|
|| return 2
|
|
printf '%s' "$active_jobs" | jq -e \
|
|
'.jobs | select(type == "array")
|
|
| all(.[]; (.id | type) == "number"
|
|
and (.status | type) == "string"
|
|
and ((.runner_id | type) == "number" or
|
|
(.runner_id | type) == "null"))' \
|
|
>/dev/null || return 2
|
|
if printf '%s' "$active_jobs" | jq -e --arg runner "$active_runner_id" \
|
|
'.jobs | any(.[];
|
|
.status == "in_progress" and (.runner_id | tostring) == $runner)' \
|
|
>/dev/null; then
|
|
return 0
|
|
fi
|
|
active_count="$(printf '%s' "$active_jobs" | jq -er '.jobs | length')" \
|
|
|| return 2
|
|
[ "$active_count" -lt 50 ] && return 1
|
|
active_page=$((active_page + 1))
|
|
done
|
|
}
|
|
|
|
gcr_gitea_delete_runner() {
|
|
repo="$1"; id="$2"
|
|
gcr_gitea_admin_token >/dev/null || return 1
|
|
owner="${repo%%/*}"
|
|
name="${repo#*/}"
|
|
gcr_gitea_curl -fsS -X DELETE \
|
|
"$GCR_GITEA_URL/api/v1/repos/$owner/$name/actions/runners/$id"
|
|
}
|
|
|
|
# Remove exactly one repository-scoped runner identity and confirm absence.
|
|
# A failed/ambiguous DELETE is safe only when a fresh list proves the exact
|
|
# name disappeared. Duplicate exact names fail closed for durable retry.
|
|
gcr_gitea_deregister_runner_exact() {
|
|
dereg_repo="$1"; dereg_name="$2"
|
|
# Stale registrations have no allocation record for a durable barrier.
|
|
# Their sweep already retries disable on a later pass; VM retirement
|
|
# explicitly supplies an empty third argument to request a new barrier.
|
|
dereg_delete_not_before="${3-0}"
|
|
[ -n "$dereg_repo" ] && [ -n "$dereg_name" ] || return 1
|
|
case "$dereg_delete_not_before" in *[!0-9]*) return 1 ;; esac
|
|
|
|
dereg_lookup=0
|
|
gcr_gitea_find_runner_exact "$dereg_repo" "$dereg_name" \
|
|
|| dereg_lookup="$?"
|
|
case "$dereg_lookup" in
|
|
0) dereg_id="$GCR_RUNNER_ID" ;;
|
|
1) return 0 ;;
|
|
*) return 1 ;;
|
|
esac
|
|
|
|
if [ "$GCR_RUNNER_DISABLED" = false ]; then
|
|
if ! gcr_gitea_set_runner_disabled "$dereg_repo" "$dereg_id" true; then
|
|
gcr_log warn --ns=gitea \
|
|
"failed disabling runner repo=$dereg_repo name=$dereg_name"
|
|
return 1
|
|
fi
|
|
# Caller starts the deletion barrier only after this PATCH succeeds.
|
|
return 3
|
|
fi
|
|
# Legacy records without a barrier must establish one after disabled state
|
|
# has been confirmed, never delete immediately.
|
|
[ -n "$dereg_delete_not_before" ] || return 3
|
|
[ "$(gcr_now_epoch)" -ge "$dereg_delete_not_before" ] || return 2
|
|
dereg_active_status=0
|
|
gcr_gitea_runner_has_active_job "$dereg_repo" "$dereg_id" \
|
|
|| dereg_active_status="$?"
|
|
case "$dereg_active_status" in
|
|
0) return 2 ;;
|
|
1) ;;
|
|
*) return 1 ;;
|
|
esac
|
|
if ! gcr_gitea_delete_runner "$dereg_repo" "$dereg_id"; then
|
|
gcr_log warn --ns=gitea \
|
|
"runner DELETE uncertain repo=$dereg_repo name=$dereg_name; confirming"
|
|
fi
|
|
dereg_lookup=0
|
|
gcr_gitea_find_runner_exact "$dereg_repo" "$dereg_name" \
|
|
|| dereg_lookup="$?"
|
|
[ "$dereg_lookup" -eq 1 ]
|
|
}
|
|
|
|
gcr_gitea_set_runner_disabled() {
|
|
repo="$1"; id="$2"; disabled="$3"
|
|
case "$disabled" in true|false) ;; *) return 1 ;; esac
|
|
gcr_gitea_admin_token >/dev/null || return 1
|
|
owner="${repo%%/*}"
|
|
name="${repo#*/}"
|
|
gcr_gitea_curl -fsS -X PATCH \
|
|
-H 'Content-Type: application/json' --data "{\"disabled\":$disabled}" \
|
|
"$GCR_GITEA_URL/api/v1/repos/$owner/$name/actions/runners/$id" >/dev/null
|
|
}
|
|
|
|
# gcr_gitea_runner_disabled REPO RUNNER_NAME true|false
|
|
gcr_gitea_runner_disabled() {
|
|
repo="$1"; runner_name="$2"; disabled="$3"
|
|
runners="$(gcr_gitea_list_runners "$repo")" || return 1
|
|
while read -r id _busy _disabled name; do
|
|
[ "$name" = "$runner_name" ] || continue
|
|
gcr_gitea_set_runner_disabled "$repo" "$id" "$disabled"
|
|
return "$?"
|
|
done <<EOF
|
|
$runners
|
|
EOF
|
|
return 1
|
|
}
|