fix: gitea runners
This commit is contained in:
@@ -32,35 +32,51 @@ gcr_vm_host_key_path() {
|
||||
printf '%s/ssh-host-keys/%s' "$GCR_STATE_DIR" "$(gcr_alloc_key "$1" "$2")"
|
||||
}
|
||||
|
||||
gcr_vm_host_key_pair_valid() {
|
||||
host_key_path="$1"
|
||||
[ -s "$host_key_path" ] && [ -s "$host_key_path.pub" ] || return 1
|
||||
derived_public="$(ssh-keygen -y -f "$host_key_path" 2>/dev/null)" || return 1
|
||||
derived_public="$(printf '%s\n' "$derived_public" | awk 'NR == 1 {print $1 " " $2}')"
|
||||
expected_public="$(awk 'NR == 1 {print $1 " " $2}' "$host_key_path.pub")"
|
||||
[ -n "$derived_public" ] && [ "$derived_public" = "$expected_public" ]
|
||||
}
|
||||
|
||||
gcr_vm_host_key_prepare() {
|
||||
host_key_path="$(gcr_vm_host_key_path "$1" "$2")"
|
||||
if [ -s "$host_key_path" ] && [ -s "$host_key_path.pub" ]; then
|
||||
if ! gcr_vm_host_key_pair_valid "$host_key_path"; then
|
||||
gcr_log error --ns=hcloud "existing SSH host-key pair is invalid job=$1 attempt=$2"
|
||||
return 1
|
||||
fi
|
||||
chmod 0600 "$host_key_path"
|
||||
chmod 0644 "$host_key_path.pub"
|
||||
return 0
|
||||
fi
|
||||
rm -f "$host_key_path" "$host_key_path.pub"
|
||||
ssh-keygen -q -t ed25519 -N '' -C '' -f "$host_key_path" || return 1
|
||||
chmod 0600 "$host_key_path"
|
||||
chmod 0644 "$host_key_path.pub"
|
||||
gcr_vm_host_key_pair_valid "$host_key_path"
|
||||
}
|
||||
|
||||
gcr_vm_host_key_userdata() {
|
||||
host_key_path="$(gcr_vm_host_key_path "$1" "$2")"
|
||||
[ -s "$host_key_path" ] && [ -s "$host_key_path.pub" ] || return 1
|
||||
host_private_b64="$(base64 < "$host_key_path" | tr -d '\n')" || return 1
|
||||
host_public_b64="$(base64 < "$host_key_path.pub" | tr -d '\n')" || return 1
|
||||
gcr_vm_host_key_pair_valid "$host_key_path" || {
|
||||
gcr_log error --ns=hcloud "SSH host-key pair is invalid job=$1 attempt=$2"
|
||||
return 1
|
||||
}
|
||||
host_public_key="$(awk 'NR == 1 {print $1 " " $2}' "$host_key_path.pub")" \
|
||||
|| return 1
|
||||
[ -n "$host_public_key" ] || return 1
|
||||
cat <<EOF
|
||||
#cloud-config
|
||||
write_files:
|
||||
- path: /etc/ssh/ssh_host_ed25519_key
|
||||
owner: root:root
|
||||
permissions: '0600'
|
||||
encoding: b64
|
||||
content: $host_private_b64
|
||||
- path: /etc/ssh/ssh_host_ed25519_key.pub
|
||||
owner: root:root
|
||||
permissions: '0644'
|
||||
encoding: b64
|
||||
content: $host_public_b64
|
||||
# Replace image keys with this VM's unique controller-generated key.
|
||||
ssh_deletekeys: true
|
||||
ssh_genkeytypes: [ed25519]
|
||||
ssh_keys:
|
||||
ed25519_private: |
|
||||
$(sed 's/^/ /' "$host_key_path")
|
||||
ed25519_public: $host_public_key
|
||||
runcmd:
|
||||
- [ sh, -c, 'systemctl restart sshd.service 2>/dev/null || systemctl restart ssh.service' ]
|
||||
EOF
|
||||
@@ -69,7 +85,10 @@ EOF
|
||||
gcr_vm_known_hosts_file() {
|
||||
known_job="$1"; known_attempt="$2"; known_ip="$3"
|
||||
known_key_path="$(gcr_vm_host_key_path "$known_job" "$known_attempt")"
|
||||
[ -s "$known_key_path.pub" ] || return 1
|
||||
gcr_vm_host_key_pair_valid "$known_key_path" || {
|
||||
gcr_log error --ns=hcloud "cannot pin invalid SSH host-key pair job=$known_job attempt=$known_attempt"
|
||||
return 1
|
||||
}
|
||||
known_type="$(cut -d' ' -f1 < "$known_key_path.pub")"
|
||||
known_key="$(cut -d' ' -f2 < "$known_key_path.pub")"
|
||||
[ -n "$known_type" ] && [ -n "$known_key" ] || return 1
|
||||
@@ -681,6 +700,7 @@ gcr_vm_collect_diagnostics() {
|
||||
printf "== kernel failure signals ==\n"; dmesg -T 2>/dev/null | grep -Ei "out of memory|oom-kill|killed process|no space|I/O error|EXT4-fs error|xfs.*error|nvme.*error" | tail -n 80
|
||||
printf "== runner service ==\n"; systemctl show gitea-runner.service -p ActiveState -p SubState -p Result -p ExecMainStatus -p ExecMainCode -p NRestarts 2>/dev/null
|
||||
printf "== bootstrap service ==\n"; systemctl show gcr-bootstrap.service -p ActiveState -p SubState -p Result -p ExecMainStatus -p ExecMainCode 2>/dev/null
|
||||
printf "== cloud-init ==\n"; if command -v cloud-init >/dev/null 2>&1; then cloud-init status --long 2>/dev/null | head -n 20; else printf "cloud-init unavailable\\n"; fi
|
||||
printf "== process sample ==\n"; ps -eo pid,ppid,stat,etime,comm 2>/dev/null | head -n 80' \
|
||||
> "$diag_out" 2>&1; then
|
||||
gcr_redact < "$diag_out" >&2
|
||||
@@ -693,6 +713,44 @@ gcr_vm_collect_diagnostics() {
|
||||
return 0
|
||||
}
|
||||
|
||||
gcr_ssh_error_category() {
|
||||
error_text="$(cat "$1")"
|
||||
case "$error_text" in
|
||||
*"REMOTE HOST IDENTIFICATION HAS CHANGED"*|*"Host key verification failed"*)
|
||||
printf 'host-key-verification'
|
||||
;;
|
||||
*"Permission denied"*|*"Too many authentication failures"*)
|
||||
printf 'authentication'
|
||||
;;
|
||||
*"Connection refused"*|*"Connection timed out"*|*"Operation timed out"*|\
|
||||
*"No route to host"*|*"Network is unreachable"*|*"Connection reset"*|\
|
||||
*"Could not resolve hostname"*|*"Name or service not known"*)
|
||||
printf 'connectivity-or-sshd'
|
||||
;;
|
||||
*"Bad configuration option"*|*"Could not open"*|*"Load key"*)
|
||||
printf 'ssh-client-error'
|
||||
;;
|
||||
*)
|
||||
printf 'unknown'
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
gcr_ssh_run_captured() {
|
||||
captured_error_file="$1"; shift
|
||||
(
|
||||
if ! ulimit -f 8; then
|
||||
exit 125
|
||||
fi
|
||||
"$@"
|
||||
) 2>"$captured_error_file"
|
||||
}
|
||||
|
||||
gcr_ssh_temp_cleanup() {
|
||||
[ -z "${1:-}" ] || rm -f "$1"
|
||||
[ -z "${2:-}" ] || rm -f "$2"
|
||||
}
|
||||
|
||||
# Retry durable repository-runner retirement. Generic idle is written only
|
||||
# after exact-name absence is confirmed and the persisted VM profile is valid.
|
||||
gcr_vm_retire_pending() {
|
||||
@@ -919,10 +977,16 @@ systemctl enable --now gitea-runner.service
|
||||
BSEOF
|
||||
}
|
||||
|
||||
# gcr_vm_bootstrap_ssh IP LABEL REG_TOKEN — blocking; returns ssh exit status.
|
||||
gcr_vm_bootstrap_ssh() {
|
||||
# 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.
|
||||
gcr_vm_bootstrap_ssh() (
|
||||
ip="$1"; label="$2"; reg_token="$3"; runner_name="$4"
|
||||
known_hosts_file="${5:-${GCR_SSH_KNOWN_HOSTS_FILE:-}}"
|
||||
job_id="${6:-}"; attempt="${7:-}"; vm_id="${8:-}"
|
||||
key_tmp=""
|
||||
ssh_error_file=""
|
||||
trap 'gcr_ssh_temp_cleanup "${key_tmp:-}" "${ssh_error_file:-}"' EXIT
|
||||
trap 'exit 1' HUP INT TERM
|
||||
test -n "${GCR_SSH_PRIVKEY_FILE:-}" && test -r "$GCR_SSH_PRIVKEY_FILE" || {
|
||||
gcr_log error --ns=hcloud "GCR_SSH_PRIVKEY_FILE missing or unreadable"
|
||||
return 1
|
||||
@@ -937,12 +1001,22 @@ gcr_vm_bootstrap_ssh() {
|
||||
chmod 0600 "$key_tmp"
|
||||
SSH_OPTS="-i $key_tmp -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=$known_hosts_file -o ConnectTimeout=5 -o BatchMode=yes"
|
||||
|
||||
ssh_error_file="$(mktemp "${TMPDIR:-/tmp}/gcr-ssh-error.XXXXXX")"
|
||||
chmod 0600 "$ssh_error_file"
|
||||
waited=0
|
||||
until ssh $SSH_OPTS "root@$ip" true 2>/dev/null; do
|
||||
ssh_status=0
|
||||
while :; do
|
||||
if gcr_ssh_run_captured "$ssh_error_file" \
|
||||
ssh $SSH_OPTS "root@$ip" true; then
|
||||
break
|
||||
else
|
||||
ssh_status="$?"
|
||||
fi
|
||||
waited=$((waited + 5))
|
||||
[ "$waited" -ge 900 ] && {
|
||||
gcr_log warn --ns=hcloud "sshd never came up on $ip"
|
||||
rm -f "$key_tmp"
|
||||
ssh_category="$(gcr_ssh_error_category "$ssh_error_file")"
|
||||
gcr_log warn --ns=hcloud \
|
||||
"SSH readiness failed stage=ssh-readiness category=$ssh_category exit=$ssh_status after=${waited}s ip=$ip"
|
||||
return 1
|
||||
}
|
||||
sleep 5
|
||||
@@ -950,10 +1024,28 @@ gcr_vm_bootstrap_ssh() {
|
||||
|
||||
ttl_min="$(printf '%s' "$(gcr_label_profile "$label")" | awk '{print $2}')"
|
||||
script="$(gcr_bootstrap_script "$label" "$reg_token" "$ttl_min" "$runner_name")"
|
||||
if printf '%s' "$script" | ssh $SSH_OPTS "root@$ip" sh -s; then
|
||||
rm -f "$key_tmp"
|
||||
if printf '%s' "$script" | gcr_ssh_run_captured "$ssh_error_file" \
|
||||
ssh $SSH_OPTS "root@$ip" sh -s; then
|
||||
return 0
|
||||
else
|
||||
ssh_status="$?"
|
||||
fi
|
||||
ssh_category="$(gcr_ssh_error_category "$ssh_error_file")"
|
||||
if [ "$ssh_status" -eq 255 ]; then
|
||||
diagnostics_reason=bootstrap-transport-failed
|
||||
if [ "$ssh_category" = unknown ]; then
|
||||
ssh_category=ssh-client-error
|
||||
fi
|
||||
gcr_log warn --ns=hcloud \
|
||||
"SSH bootstrap failed stage=ssh-transport category=$ssh_category exit=$ssh_status ip=$ip"
|
||||
else
|
||||
diagnostics_reason=bootstrap-command-failed
|
||||
gcr_log warn --ns=hcloud \
|
||||
"SSH bootstrap failed stage=remote-bootstrap-command category=remote-bootstrap-command-failed exit=$ssh_status ip=$ip"
|
||||
fi
|
||||
if [ -n "$job_id" ] && [ -n "$attempt" ] && [ -n "$vm_id" ]; then
|
||||
gcr_vm_collect_diagnostics "$vm_id" "$ip" "$job_id" \
|
||||
"$diagnostics_reason" "$attempt" || true
|
||||
fi
|
||||
rm -f "$key_tmp"
|
||||
return 1
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user