my-homelab-configs/infra/ai-stack/bootstrap-omniroute.sh

218 lines
6.2 KiB
Bash

#!/usr/bin/env bash
# Bootstrap OmniRoute management auth and provider connections.
# This script intentionally never prints secret values.
set -euo pipefail
INSTALL_DIR="${LAB_AI_STACK_INSTALL_DIR:-/data/homelab-ai-stack}"
ENV_FILE="${LAB_AI_STACK_ENV_FILE:-${INSTALL_DIR}/.env}"
PORT="${LAB_OMNIROUTE_PORT:-20128}"
BASE_URL="${LAB_AI_STACK_OMNIROUTE_BASE_URL:-http://127.0.0.1:${PORT}}"
OPENROUTER_URL="${LAB_AI_STACK_OPENROUTER_URL:-https://openrouter.ai/api/v1}"
NVIDIA_URL="${LAB_AI_STACK_NVIDIA_URL:-https://integrate.api.nvidia.com/v1}"
OMNIROUTE_BOOTSTRAP_TMP_DIR=""
if [[ ! -s "${ENV_FILE}" ]]; then
echo "Missing ${ENV_FILE}; run seed-secrets.sh first." >&2
exit 1
fi
env_value() {
local key="$1"
awk -F= -v key="${key}" '$1 == key { sub(/^[^=]*=/, ""); print; found = 1 } END { exit found ? 0 : 1 }' "${ENV_FILE}"
}
cleanup() {
if [[ -n "${OMNIROUTE_BOOTSTRAP_TMP_DIR}" && -d "${OMNIROUTE_BOOTSTRAP_TMP_DIR}" ]]; then
rm -rf "${OMNIROUTE_BOOTSTRAP_TMP_DIR}"
fi
}
trap cleanup EXIT
wait_for_omniroute() {
local status
for _ in $(seq 1 30); do
status="$(curl -sS -o /dev/null -w '%{http_code}' --connect-timeout 2 --max-time 5 "${BASE_URL}/api/auth/status" 2>/dev/null || true)"
case "${status}" in
200 | 401 | 403)
return 0
;;
esac
sleep 2
done
echo "OmniRoute did not become reachable at ${BASE_URL}." >&2
exit 1
}
login_omniroute() {
local password="$1"
local cookie_jar="$2"
local body_file="$3"
local headers_file="$4"
local status
status="$(
PASSWORD="${password}" python3 - <<'PY' |
import json
import os
print(json.dumps({"password": os.environ["PASSWORD"]}))
PY
curl -sS \
-D "${headers_file}" \
-c "${cookie_jar}" \
-o "${body_file}" \
-w '%{http_code}' \
-H 'Content-Type: application/json' \
-X POST \
--data-binary @- \
"${BASE_URL}/api/auth/login"
)"
if [[ "${status}" != "200" ]]; then
echo "OmniRoute management login failed with HTTP ${status}." >&2
sed -n '1,3p' "${body_file}" >&2 || true
exit 1
fi
}
extract_auth_header() {
local body_file="$1"
local token
token="$(
python3 - "${body_file}" <<'PY' 2>/dev/null || true
import json
import sys
with open(sys.argv[1], encoding="utf-8") as fh:
data = json.load(fh)
for key in ("token", "accessToken", "jwt", "authToken"):
value = data.get(key)
if isinstance(value, str) and value:
print(value)
break
PY
)"
if [[ -n "${token}" ]]; then
printf 'Authorization: Bearer %s\n' "${token}"
fi
}
provider_id_for() {
local cookie_jar="$1"
local auth_header="$2"
local provider="$3"
local name="$4"
local body_file="$5"
if [[ -n "${auth_header}" ]]; then
curl -fsS -b "${cookie_jar}" -H "${auth_header}" "${BASE_URL}/api/providers" -o "${body_file}"
else
curl -fsS -b "${cookie_jar}" "${BASE_URL}/api/providers" -o "${body_file}"
fi
python3 - "${body_file}" "${provider}" "${name}" <<'PY'
import json
import sys
path, provider, name = sys.argv[1:4]
with open(path, encoding="utf-8") as fh:
data = json.load(fh)
for item in data.get("connections", []) or []:
if item.get("provider") == provider and item.get("name") == name:
print(item.get("id", ""))
sys.exit(0)
sys.exit(1)
PY
}
create_provider() {
local cookie_jar="$1"
local auth_header="$2"
local provider="$3"
local name="$4"
local url="$5"
local api_key="$6"
local body_file="$7"
local status
status="$(
PROVIDER="${provider}" NAME="${name}" URL="${url}" API_KEY="${api_key}" python3 - <<'PY' |
import json
import os
print(json.dumps({
"provider": os.environ["PROVIDER"],
"name": os.environ["NAME"],
"url": os.environ["URL"],
"apiKey": os.environ["API_KEY"],
"isActive": True,
"maxConcurrent": 2,
}))
PY
if [[ -n "${auth_header}" ]]; then
curl -sS -b "${cookie_jar}" -H "${auth_header}" -o "${body_file}" -w '%{http_code}' -H 'Content-Type: application/json' -X POST --data-binary @- "${BASE_URL}/api/providers"
else
curl -sS -b "${cookie_jar}" -o "${body_file}" -w '%{http_code}' -H 'Content-Type: application/json' -X POST --data-binary @- "${BASE_URL}/api/providers"
fi
)"
if [[ "${status}" != "201" && "${status}" != "200" ]]; then
echo "Failed to create OmniRoute provider ${provider} (${name}); HTTP ${status}." >&2
sed -n '1,3p' "${body_file}" >&2 || true
exit 1
fi
}
upsert_provider() {
local cookie_jar="$1"
local auth_header="$2"
local provider="$3"
local name="$4"
local url="$5"
local api_key="$6"
local body_file="$7"
if [[ -z "${api_key}" ]]; then
echo "OmniRoute provider ${provider}: skipped; key missing"
return 0
fi
if provider_id_for "${cookie_jar}" "${auth_header}" "${provider}" "${name}" "${body_file}" >/dev/null 2>&1; then
echo "OmniRoute provider ${provider}: already configured"
return 0
fi
create_provider "${cookie_jar}" "${auth_header}" "${provider}" "${name}" "${url}" "${api_key}" "${body_file}"
echo "OmniRoute provider ${provider}: configured"
}
main() {
local password openrouter_key nvidia_key cookie_jar headers_file body_file auth_header
password="$(env_value INITIAL_PASSWORD)"
openrouter_key="$(env_value OPENROUTER_API_KEY 2>/dev/null || true)"
nvidia_key="$(env_value NVIDIA_API_KEY 2>/dev/null || true)"
wait_for_omniroute
OMNIROUTE_BOOTSTRAP_TMP_DIR="$(mktemp -d)"
cookie_jar="${OMNIROUTE_BOOTSTRAP_TMP_DIR}/cookies.txt"
headers_file="${OMNIROUTE_BOOTSTRAP_TMP_DIR}/headers.txt"
body_file="${OMNIROUTE_BOOTSTRAP_TMP_DIR}/body.json"
login_omniroute "${password}" "${cookie_jar}" "${body_file}" "${headers_file}"
auth_header="$(extract_auth_header "${body_file}")"
upsert_provider "${cookie_jar}" "${auth_header}" "openrouter" "openrouter-seeded" "${OPENROUTER_URL}" "${openrouter_key}" "${body_file}"
upsert_provider "${cookie_jar}" "${auth_header}" "nvidia" "nvidia-seeded" "${NVIDIA_URL}" "${nvidia_key}" "${body_file}"
}
main "$@"