Files
pikasTech-agentrun/deploy/runtime/boot/agentrun-runner-boot.sh
T
2026-07-20 09:37:55 +02:00

66 lines
3.6 KiB
Bash

#!/bin/sh
set -eu
entrypoint="${1:-src/runner/main.ts}"
repo_url="${AGENTRUN_BOOT_REPO_URL:-http://git-mirror-http.devops-infra.svc.cluster.local/pikasTech/agentrun.git}"
commit="${AGENTRUN_BOOT_COMMIT:-${AGENTRUN_SOURCE_COMMIT:-}}"
app_root="${AGENTRUN_APP_ROOT:-/workspace/agentrun}"
reporter=/opt/agentrun/deploy/runtime/boot/agentrun-boot-report.mjs
if [ -z "$commit" ] || ! printf '%s' "$commit" | grep -Eq '^[0-9a-f]{40}$'; then
printf '{"ok":false,"event":"agentrun-boot","failureKind":"source-commit-invalid","message":"AGENTRUN_BOOT_COMMIT must be a full git SHA"}\n' >&2
exit 64
fi
mkdir -p "$(dirname "$app_root")"
rm -rf "$app_root"
mkdir -p "$app_root"
cd "$app_root"
git init -q
git remote add origin "$repo_url"
attempt=1
fetch_log=/tmp/agentrun-boot-fetch.log
while :; do
start_response=$(node "$reporter" source-fetch-started "$attempt" 2>/tmp/agentrun-boot-report.log || true)
timeout_ms=$(printf '%s' "$start_response" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{try{const n=JSON.parse(s).attemptTimeoutMs;process.stdout.write(Number.isFinite(n)?String(n):"")}catch{}})')
timeout_seconds=""
if [ -n "$timeout_ms" ]; then timeout_seconds=$(( (timeout_ms + 999) / 1000 )); fi
if { [ -n "$timeout_seconds" ] && timeout "${timeout_seconds}s" git fetch --depth=1 origin "$commit" >"$fetch_log" 2>&1; } || { [ -z "$timeout_seconds" ] && git fetch --depth=1 origin "$commit" >"$fetch_log" 2>&1; }; then
node "$reporter" source-fetch-succeeded "$attempt" >/tmp/agentrun-boot-report.log 2>&1 || true
break
fi
message=$(tail -n 20 "$fetch_log" | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g')
failure_kind=git-mirror-fetch-failed
if printf '%s' "$message" | grep -Eiq 'not our ref|unadvertised|Server does not allow request'; then
failure_kind=git-mirror-exact-commit-unavailable
elif printf '%s' "$message" | grep -Eiq 'Could not resolve host|Connection timed out|Failed to connect|timed out'; then
failure_kind=git-mirror-network-failed
elif printf '%s' "$message" | grep -Eiq 'Authentication failed|Permission denied|repository.*not found'; then
failure_kind=git-mirror-auth-failed
fi
failure_response=$(node "$reporter" source-fetch-failed "$attempt" "$failure_kind" 2>/tmp/agentrun-boot-report.log || true)
action=$(printf '%s' "$failure_response" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{try{process.stdout.write(JSON.parse(s).action||"")}catch{}})')
if [ "$action" != "retry" ]; then
printf '{"ok":false,"event":"agentrun-boot","failureKind":"%s","repoUrl":"%s","commit":"%s","message":%s}\n' "$failure_kind" "$repo_url" "$commit" "$(node -e 'console.log(JSON.stringify(process.argv[1] || ""))' "$message")" >&2
exit 65
fi
backoff_ms=$(printf '%s' "$failure_response" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{try{const n=JSON.parse(s).backoffMs;process.stdout.write(Number.isFinite(n)?String(n):"0")}catch{process.stdout.write("0")}})')
sleep_seconds=$(node -e 'process.stdout.write(String(Math.max(0, Number(process.argv[1]) || 0) / 1000))' "$backoff_ms")
sleep "$sleep_seconds"
attempt=$((attempt + 1))
done
git checkout -q --detach "$commit"
actual=$(git rev-parse HEAD)
if [ "$actual" != "$commit" ]; then
printf '{"ok":false,"event":"agentrun-boot","failureKind":"source-commit-mismatch","expected":"%s","actual":"%s"}\n' "$commit" "$actual" >&2
exit 66
fi
ln -sfn /opt/agentrun/node_modules "$app_root/node_modules"
printf '{"ok":true,"event":"agentrun-boot","repoUrl":"%s","commit":"%s","entrypoint":"%s","nodeModules":"/opt/agentrun/node_modules"}\n' "$repo_url" "$commit" "$entrypoint"
exec bun "$entrypoint"