From e0882d077996f1d69b19a42c6f72fccc0dae84de Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Thu, 8 Oct 2026 08:23:25 -0600 Subject: [PATCH] feat: add Debian AI stack deploy command --- .gitignore | 4 ++ homelab.yml | 5 +++ infra/ai-stack/README.md | 30 +++++++++++++ infra/ai-stack/docker-compose.yml | 30 +++++++++++++ infra/ai-stack/seed-secrets.sh | 73 +++++++++++++++++++++++++++++++ jeannie | 5 +++ lib/jeannie/pipeline.sh | 63 ++++++++++++++++++++++++++ 7 files changed, 210 insertions(+) create mode 100644 infra/ai-stack/README.md create mode 100644 infra/ai-stack/docker-compose.yml create mode 100755 infra/ai-stack/seed-secrets.sh diff --git a/.gitignore b/.gitignore index 30d687d..35de334 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,10 @@ infra/matrix-server/synapse-data/ infra/matrix-server/postgres-data/ infra/matrix-server/.env infra/omniroute/.env +infra/ai-stack/.env +homelab-ai-stack/.env +nvidia_key +open-router_key infra/youtube-backup/.env infra/youtube-backup/cookies.txt infra/youtube-backup/archive.txt diff --git a/homelab.yml b/homelab.yml index 37c9fe6..621216a 100644 --- a/homelab.yml +++ b/homelab.yml @@ -206,6 +206,11 @@ services: port: 20128 image: diegosouzapw/omniroute:latest container_name: omniroute + ai_stack: + host: debian + install_dir: /data/homelab-ai-stack + port: 20128 + gateway: omniroute hermes: host: debian home: /home/jv/.hermes diff --git a/infra/ai-stack/README.md b/infra/ai-stack/README.md new file mode 100644 index 0000000..8785f97 --- /dev/null +++ b/infra/ai-stack/README.md @@ -0,0 +1,30 @@ +# Homelab AI stack + +This stack keeps model routing outside Kubernetes so it can help diagnose +Kubernetes/Pimox failures while the cluster is degraded. + +Run: + +```bash +./jeannie deploy-ai-stack +``` + +Runtime split: + +- Container: OmniRoute, because it is a restartable HTTP gateway with provider + API keys and no need for host-level SSH/Kubernetes mutation. +- Bare metal: Ollama, because it benefits from direct host hardware/runtime + access and already has a systemd lifecycle. +- Bare metal: `goose`, `holmes`, `hermes`, and Jeannie doctors, because they + need local kubeconfig, SSH to Pimox/worker nodes, and guarded deterministic + action paths. + +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`. +- Key files and generated env files are ignored by git. + +The container name defaults to `omniroute` so this command owns the same gateway +identity used by Hermes and existing OpenAI-compatible clients. diff --git a/infra/ai-stack/docker-compose.yml b/infra/ai-stack/docker-compose.yml new file mode 100644 index 0000000..871f448 --- /dev/null +++ b/infra/ai-stack/docker-compose.yml @@ -0,0 +1,30 @@ +services: + omniroute: + image: ${OMNIROUTE_IMAGE:-diegosouzapw/omniroute:latest} + container_name: ${OMNIROUTE_CONTAINER_NAME:-omniroute} + restart: unless-stopped + env_file: + - .env + environment: + # Keep local model traffic on the Debian host. Docker Desktop uses this + # hostname automatically; on Linux the extra_hosts entry below maps it to + # the host gateway. + OLLAMA_BASE_URL: ${OLLAMA_BASE_URL:-http://host.docker.internal:11434} + OLLAMA_HOST: ${OLLAMA_HOST:-http://host.docker.internal:11434} + + # Provider keys are seeded into .env by seed-secrets.sh. Multiple common + # variable names are exported so the gateway keeps working across + # OmniRoute/provider naming changes. + OPENROUTER_API_KEY: ${OPENROUTER_API_KEY:-} + OPEN_ROUTER_API_KEY: ${OPENROUTER_API_KEY:-} + NVIDIA_API_KEY: ${NVIDIA_API_KEY:-} + NVIDIA_NIM_API_KEY: ${NVIDIA_API_KEY:-} + ports: + - "${OMNIROUTE_PORT:-20128}:20128" + extra_hosts: + - "host.docker.internal:host-gateway" + volumes: + - omniroute-data:/app/data + +volumes: + omniroute-data: diff --git a/infra/ai-stack/seed-secrets.sh b/infra/ai-stack/seed-secrets.sh new file mode 100755 index 0000000..228a75d --- /dev/null +++ b/infra/ai-stack/seed-secrets.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# Seed the Debian-hosted AI stack env file from local, non-git key files. +# This script never prints key 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}" +OPENROUTER_KEY_FILE="${LAB_AI_STACK_OPENROUTER_KEY_FILE:-${HOME}/open-router_key}" +NVIDIA_KEY_FILE="${LAB_AI_STACK_NVIDIA_KEY_FILE:-${HOME}/nvidia_key}" + +read_first_line() { + local file="$1" + + if [[ ! -s "${file}" ]]; then + return 1 + fi + sed -n '1{s/\r$//;p;q;}' "${file}" +} + +existing_env_value() { + local key="$1" + local file="$2" + + if [[ ! -s "${file}" ]]; then + return 1 + fi + awk -F= -v key="${key}" '$1 == key { sub(/^[^=]*=/, ""); print; found = 1 } END { exit found ? 0 : 1 }' "${file}" +} + +write_kv() { + local key="$1" + local value="$2" + + if [[ "${value}" == *$'\n'* ]]; then + echo "${key} cannot contain a newline." >&2 + exit 1 + fi + printf '%s=%s\n' "${key}" "${value}" +} + +mkdir -p "${INSTALL_DIR}" +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)" + +tmp_file="$(mktemp)" +trap 'rm -f "${tmp_file}"' EXIT +umask 077 +{ + printf '# Generated by jeannie deploy-ai-stack. Do not commit.\n' + write_kv OMNIROUTE_IMAGE "${LAB_OMNIROUTE_IMAGE:-diegosouzapw/omniroute:latest}" + write_kv OMNIROUTE_CONTAINER_NAME "${LAB_OMNIROUTE_CONTAINER_NAME:-omniroute}" + 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 OPENROUTER_API_KEY "${openrouter_key}" + write_kv NVIDIA_API_KEY "${nvidia_key}" +} >"${tmp_file}" + +install -m 0600 "${tmp_file}" "${ENV_FILE}" + +if [[ -n "${openrouter_key}" ]]; then + echo "OpenRouter key: seeded" +else + echo "OpenRouter key: missing (${OPENROUTER_KEY_FILE})" +fi +if [[ -n "${nvidia_key}" ]]; then + echo "NVIDIA key: seeded" +else + echo "NVIDIA key: missing (${NVIDIA_KEY_FILE})" +fi +echo "AI stack env ready: ${ENV_FILE}" diff --git a/jeannie b/jeannie index cf2f376..6416069 100755 --- a/jeannie +++ b/jeannie @@ -56,6 +56,7 @@ Build And Bootstrap deploy-heimdall Deploy the Debian-hosted Heimdall dashboard. deploy-dex Deploy the Debian-hosted Dex SSO stack. deploy-matrix Deploy the Matrix homeserver (Synapse + Element + Postgres). + deploy-ai-stack Deploy the Debian-hosted AI gateway stack. deploy-omniroute Deploy the OmniRoute LLM routing gateway. deploy-hermes Deploy the Hermes agent (git-install + venv + gateway service). bootstrap-gitea-repo Ensure the Gitea repo and SSH key wiring exist. @@ -401,6 +402,10 @@ case "${1:-}" in record_change_journal "deploy-matrix" "$@" deploy_matrix ;; + deploy-ai-stack) + record_change_journal "deploy-ai-stack" "$@" + deploy_ai_stack + ;; deploy-omniroute) record_change_journal "deploy-omniroute" "$@" deploy_omniroute diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 035f0ce..7f04eb9 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -5331,6 +5331,69 @@ ENV_EOT echo "OmniRoute is reachable at http://127.0.0.1:${LAB_OMNIROUTE_PORT:-20128}." } +deploy_ai_stack() { + local mode="${LAB_AI_STACK_DEPLOY:-true}" + local install_dir="${LAB_AI_STACK_INSTALL_DIR:-/data/homelab-ai-stack}" + local install_docker="${LAB_AI_STACK_INSTALL_DOCKER:-false}" + local source_dir="${REPO_ROOT}/infra/ai-stack" + local port="${LAB_OMNIROUTE_PORT:-20128}" + local container_name="${LAB_OMNIROUTE_CONTAINER_NAME:-omniroute}" + local existing_workdir + + require_debian_server "deploy-ai-stack" + + if disabled_value "${mode}"; then + return 0 + fi + + if [[ ! -s "${source_dir}/docker-compose.yml" || ! -s "${source_dir}/seed-secrets.sh" ]]; then + echo "Missing AI stack source files in ${source_dir}" >&2 + exit 1 + fi + + echo "Deploying Debian-hosted AI stack at ${install_dir}..." + echo "Containerized: OmniRoute gateway. Bare metal: Ollama, goose, holmes, hermes, Jeannie doctors." + + ensure_local_docker_compose "AI_STACK" "${install_docker}" + repair_local_docker_iptables + + 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 0644 "${source_dir}/README.md" "${install_dir}/README.md" + + LAB_AI_STACK_INSTALL_DIR="${install_dir}" "${install_dir}/seed-secrets.sh" + + if sudo docker ps -a --format '{{.Names}}' | grep -qx "${container_name}"; then + existing_workdir="$(sudo docker inspect -f '{{ index .Config.Labels "com.docker.compose.project.working_dir" }}' "${container_name}" 2>/dev/null || true)" + if [[ -n "${existing_workdir}" && "${existing_workdir}" != "${install_dir}" ]]; then + echo "Migrating existing ${container_name} container from ${existing_workdir} to ${install_dir}..." + sudo docker stop "${container_name}" >/dev/null 2>&1 || true + sudo docker rm "${container_name}" >/dev/null 2>&1 || true + fi + fi + + ( + 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" ps + ) + + if curl -fsS --connect-timeout 2 --max-time 5 "http://127.0.0.1:${port}/v1/models" >/dev/null 2>&1; then + echo "AI gateway OpenAI-compatible endpoint is reachable at http://127.0.0.1:${port}/v1." + else + echo "AI gateway container started, but /v1/models did not respond yet; check logs with:" >&2 + echo " cd ${install_dir} && sudo docker compose --env-file .env logs --tail=120" >&2 + fi + + if curl -fsS --connect-timeout 2 --max-time 5 "http://127.0.0.1:11434/api/tags" >/dev/null 2>&1; then + echo "Bare-metal Ollama is reachable at http://127.0.0.1:11434." + else + echo "Ollama is not reachable locally; run ./jeannie ollama-setup if local model fallback is required." >&2 + fi +} + deploy_hermes() { local mode="${LAB_HERMES_DEPLOY:-true}" local hermes_home="${LAB_HERMES_HOME:-/home/jv/.hermes}"