fix: bootstrap OmniRoute providers in AI stack
This commit is contained in:
parent
d7d9e4822f
commit
fe6a5bf141
|
|
@ -24,6 +24,9 @@ Secrets:
|
|||
- `~/open-router_key` seeds `OPENROUTER_API_KEY`.
|
||||
- `~/nvidia_key` seeds `NVIDIA_API_KEY`.
|
||||
- The generated env file is `/data/homelab-ai-stack/.env`, mode `0600`.
|
||||
- Jeannie also generates persistent local-only OmniRoute management secrets in
|
||||
that env file, logs in against `127.0.0.1`, and creates provider connections
|
||||
through OmniRoute's management API. Secret values are never printed.
|
||||
- Key files and generated env files are ignored by git.
|
||||
|
||||
The container name defaults to `omniroute` so this command owns the same gateway
|
||||
|
|
|
|||
|
|
@ -0,0 +1,236 @@
|
|||
#!/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}"
|
||||
|
||||
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}"
|
||||
}
|
||||
|
||||
json_value() {
|
||||
local expr="$1"
|
||||
python3 - "${expr}" <<'PY'
|
||||
import json
|
||||
import sys
|
||||
|
||||
expr = sys.argv[1]
|
||||
try:
|
||||
data = json.load(sys.stdin)
|
||||
except Exception:
|
||||
sys.exit(1)
|
||||
|
||||
for key in expr.split("."):
|
||||
if isinstance(data, dict):
|
||||
data = data.get(key)
|
||||
else:
|
||||
data = None
|
||||
if data is None:
|
||||
sys.exit(1)
|
||||
|
||||
if isinstance(data, (dict, list)):
|
||||
print(json.dumps(data))
|
||||
else:
|
||||
print(data)
|
||||
PY
|
||||
}
|
||||
|
||||
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 tmp_dir 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
|
||||
|
||||
tmp_dir="$(mktemp -d)"
|
||||
trap 'rm -rf "${tmp_dir}"' EXIT
|
||||
cookie_jar="${tmp_dir}/cookies.txt"
|
||||
headers_file="${tmp_dir}/headers.txt"
|
||||
body_file="${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 "$@"
|
||||
|
|
@ -19,8 +19,13 @@ services:
|
|||
OPEN_ROUTER_API_KEY: ${OPENROUTER_API_KEY:-}
|
||||
NVIDIA_API_KEY: ${NVIDIA_API_KEY:-}
|
||||
NVIDIA_NIM_API_KEY: ${NVIDIA_API_KEY:-}
|
||||
REQUIRE_API_KEY: ${REQUIRE_API_KEY:-false}
|
||||
INITIAL_PASSWORD: ${INITIAL_PASSWORD:?INITIAL_PASSWORD must be seeded by seed-secrets.sh}
|
||||
API_KEY_SECRET: ${API_KEY_SECRET:?API_KEY_SECRET must be seeded by seed-secrets.sh}
|
||||
AUTH_SECRET: ${AUTH_SECRET:?AUTH_SECRET must be seeded by seed-secrets.sh}
|
||||
JWT_SECRET: ${JWT_SECRET:?JWT_SECRET must be seeded by seed-secrets.sh}
|
||||
ports:
|
||||
- "${OMNIROUTE_PORT:-20128}:20128"
|
||||
- "127.0.0.1:${OMNIROUTE_PORT:-20128}:20128"
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
volumes:
|
||||
|
|
|
|||
|
|
@ -27,6 +27,19 @@ existing_env_value() {
|
|||
awk -F= -v key="${key}" '$1 == key { sub(/^[^=]*=/, ""); print; found = 1 } END { exit found ? 0 : 1 }' "${file}"
|
||||
}
|
||||
|
||||
random_secret() {
|
||||
if command -v openssl >/dev/null 2>&1; then
|
||||
openssl rand -hex 32
|
||||
return 0
|
||||
fi
|
||||
|
||||
python3 - <<'PY'
|
||||
import secrets
|
||||
|
||||
print(secrets.token_hex(32))
|
||||
PY
|
||||
}
|
||||
|
||||
write_kv() {
|
||||
local key="$1"
|
||||
local value="$2"
|
||||
|
|
@ -43,6 +56,10 @@ chmod 0750 "${INSTALL_DIR}"
|
|||
|
||||
openrouter_key="$(read_first_line "${OPENROUTER_KEY_FILE}" 2>/dev/null || existing_env_value OPENROUTER_API_KEY "${ENV_FILE}" 2>/dev/null || true)"
|
||||
nvidia_key="$(read_first_line "${NVIDIA_KEY_FILE}" 2>/dev/null || existing_env_value NVIDIA_API_KEY "${ENV_FILE}" 2>/dev/null || true)"
|
||||
initial_password="$(existing_env_value INITIAL_PASSWORD "${ENV_FILE}" 2>/dev/null || random_secret)"
|
||||
api_key_secret="$(existing_env_value API_KEY_SECRET "${ENV_FILE}" 2>/dev/null || random_secret)"
|
||||
auth_secret="$(existing_env_value AUTH_SECRET "${ENV_FILE}" 2>/dev/null || random_secret)"
|
||||
jwt_secret="$(existing_env_value JWT_SECRET "${ENV_FILE}" 2>/dev/null || random_secret)"
|
||||
|
||||
tmp_file="$(mktemp)"
|
||||
trap 'rm -f "${tmp_file}"' EXIT
|
||||
|
|
@ -54,6 +71,11 @@ umask 077
|
|||
write_kv OMNIROUTE_PORT "${LAB_OMNIROUTE_PORT:-20128}"
|
||||
write_kv OLLAMA_BASE_URL "${LAB_AI_STACK_OLLAMA_BASE_URL:-http://host.docker.internal:11434}"
|
||||
write_kv OLLAMA_HOST "${LAB_AI_STACK_OLLAMA_HOST:-http://host.docker.internal:11434}"
|
||||
write_kv REQUIRE_API_KEY "${LAB_AI_STACK_REQUIRE_API_KEY:-false}"
|
||||
write_kv INITIAL_PASSWORD "${initial_password}"
|
||||
write_kv API_KEY_SECRET "${api_key_secret}"
|
||||
write_kv AUTH_SECRET "${auth_secret}"
|
||||
write_kv JWT_SECRET "${jwt_secret}"
|
||||
write_kv OPENROUTER_API_KEY "${openrouter_key}"
|
||||
write_kv NVIDIA_API_KEY "${nvidia_key}"
|
||||
} >"${tmp_file}"
|
||||
|
|
|
|||
|
|
@ -5347,7 +5347,7 @@ deploy_ai_stack() {
|
|||
return 0
|
||||
fi
|
||||
|
||||
if [[ ! -s "${source_dir}/docker-compose.yml" || ! -s "${source_dir}/seed-secrets.sh" ]]; then
|
||||
if [[ ! -s "${source_dir}/docker-compose.yml" || ! -s "${source_dir}/seed-secrets.sh" || ! -s "${source_dir}/bootstrap-omniroute.sh" ]]; then
|
||||
echo "Missing AI stack source files in ${source_dir}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
|
@ -5361,6 +5361,7 @@ deploy_ai_stack() {
|
|||
sudo install -d -m 0750 -o "$(id -u)" -g "$(id -g)" "${install_dir}"
|
||||
install -m 0644 "${source_dir}/docker-compose.yml" "${install_dir}/docker-compose.yml"
|
||||
install -m 0755 "${source_dir}/seed-secrets.sh" "${install_dir}/seed-secrets.sh"
|
||||
install -m 0755 "${source_dir}/bootstrap-omniroute.sh" "${install_dir}/bootstrap-omniroute.sh"
|
||||
install -m 0644 "${source_dir}/README.md" "${install_dir}/README.md"
|
||||
|
||||
LAB_AI_STACK_INSTALL_DIR="${install_dir}" "${install_dir}/seed-secrets.sh"
|
||||
|
|
@ -5377,10 +5378,12 @@ deploy_ai_stack() {
|
|||
(
|
||||
cd "${install_dir}"
|
||||
sudo docker compose --env-file "${install_dir}/.env" pull
|
||||
sudo docker compose --env-file "${install_dir}/.env" up -d --remove-orphans
|
||||
sudo docker compose --env-file "${install_dir}/.env" up -d --remove-orphans --force-recreate omniroute
|
||||
sudo docker compose --env-file "${install_dir}/.env" ps
|
||||
)
|
||||
|
||||
LAB_AI_STACK_INSTALL_DIR="${install_dir}" LAB_OMNIROUTE_PORT="${port}" "${install_dir}/bootstrap-omniroute.sh"
|
||||
|
||||
models_status="$(curl -sS -o /dev/null -w '%{http_code}' --connect-timeout 2 --max-time 5 "http://127.0.0.1:${port}/v1/models" 2>/dev/null || true)"
|
||||
if [[ "${models_status}" == "200" ]]; then
|
||||
echo "AI gateway OpenAI-compatible endpoint is reachable at http://127.0.0.1:${port}/v1."
|
||||
|
|
|
|||
Loading…
Reference in New Issue