Files

45 lines
2.1 KiB
Bash

#!/bin/sh
set -eu
entrypoint="${1:-src/mgr/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}"
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"
fetch_log=/tmp/agentrun-boot-fetch.log
if ! git fetch --depth=1 origin "$commit" >"$fetch_log" 2>&1; then
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'; 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
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
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"