Add reachability checks and develop/main container pipeline (inactive)

- preflight --live: Ollama/Qdrant reachable from the container, required
  Ollama models present, 768 dimensions; clear abort instead of traceback.
  Static check rejects unset or container-local service URLs.
- deploy/smoke.py: non-writing post-start check via published ports.
- deploy/deploy.sh: test (no network), build, preflight, up --wait, smoke;
  prod rolls back to the last good image tag.
- Workflow templates deploy-dev.yml (push develop) and deploy-prod.yml
  (push main) following the Kansho pattern; not under .gitea/workflows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 5.5 committed 2026-10-28 16:01:37 +01:00
1 parent 7a225ba2b6
commit 8f9f83aa79
14 files changed
+728 -90

No files matched your search

+87 -20
View File
@@ -7,7 +7,10 @@ DESCRIPTION: Container-Entrypoint für Mindnet (API, UI, Preflight).
- explizite Umgebung und Collection-Präfix (Dev nie auf Prod-Präfix)
- Vault: vorhanden, Sentinel lesbar (NAS gemountet), schreibgeschützt
- kein Cloud-LLM-Key ohne ausdrückliche Freigabe
VERSION: 1.0.0
- Dienst-URLs gesetzt und nicht auf den Container selbst (localhost)
Mit 'preflight --live' zusätzlich (nur lesend): Ollama und Qdrant
erreichbar, benötigte Modelle vorhanden, Dimensionen stimmen.
VERSION: 1.1.0
STATUS: Active
USAGE: entrypoint.py api | ui | preflight [--live]
"""
@@ -17,6 +20,7 @@ import json
import os
import sys
import urllib.error
import urllib.parse
import urllib.request
from pathlib import Path
@@ -24,6 +28,8 @@ import yaml
CLOUD_KEYS = ("OPENROUTER_API_KEY", "GOOGLE_API_KEY")
CLOUD_EGRESS_ACK = "MINDNET_ALLOW_CLOUD_EGRESS"
SERVICE_URLS = ("MINDNET_OLLAMA_URL", "QDRANT_URL")
LOOPBACK_HOSTS = {"localhost", "127.0.0.1", "::1", "0.0.0.0"}
class PreflightError(RuntimeError):
@@ -49,6 +55,16 @@ def check_environment() -> str:
return env
def _load_profiles(profiles_path: str | None = None) -> dict:
# Gleicher Pfad wie in der App (Settings kennt keinen Override dafür).
path = Path(profiles_path or "config/llm_profiles.yaml")
try:
data = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
except (OSError, yaml.YAMLError) as e:
raise PreflightError(f"LLM-Profile nicht lesbar: {path} ({type(e).__name__}).") from None
return data.get("profiles") or {}
def check_embeddings(profiles_path: str | None = None) -> None:
expected_dim = int(os.getenv("MINDNET_EXPECTED_VECTOR_DIM", "768"))
expected_model = os.getenv("MINDNET_EXPECTED_EMBEDDING_MODEL", "nomic-embed-text")
@@ -63,13 +79,7 @@ def check_embeddings(profiles_path: str | None = None) -> None:
raise PreflightError(f"MINDNET_EMBEDDING_MODEL='{env_model}' weicht von '{expected_model}' ab.")
# Das Profil hat Vorrang vor der Umgebung; fehlt es, fällt der Client auf das Chat-Modell zurück.
# Gleicher Pfad wie in der App (Settings kennt keinen Override dafür).
path = Path(profiles_path or "config/llm_profiles.yaml")
try:
data = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
except (OSError, yaml.YAMLError) as e:
raise PreflightError(f"LLM-Profile nicht lesbar: {path} ({type(e).__name__}).") from None
profile = (data.get("profiles") or {}).get("embedding_expert") or {}
profile = _load_profiles(profiles_path).get("embedding_expert") or {}
if profile.get("provider", "ollama") != "ollama" or profile.get("model") != expected_model:
raise PreflightError(f"Profil embedding_expert muss ollama/{expected_model} sein.")
if int(profile.get("dimensions") or 0) != expected_dim:
@@ -109,36 +119,92 @@ def check_vault() -> None:
raise PreflightError(f"Vault '{root}' ist nicht schreibgeschützt gemountet (':ro' erforderlich).")
def check_live() -> None:
"""Optionale Laufzeitprüfung: tatsächliche Embedding- und Collection-Dimension (nur lesend)."""
def check_service_urls() -> None:
for name in SERVICE_URLS:
url = os.getenv(name, "").strip()
if not url:
# Die App fiele sonst still auf http://127.0.0.1:... zurück, also auf den Container selbst.
raise PreflightError(f"{name} muss explizit gesetzt sein.")
if urllib.parse.urlsplit(url).hostname in LOOPBACK_HOSTS:
raise PreflightError(
f"{name}='{url}' zeigt im Container auf den Container selbst. "
"Host-Dienste über host.docker.internal ansprechen."
)
def _fetch_json(url: str, payload: dict | None = None, headers: dict | None = None, timeout: float = 10):
"""GET (oder POST mit payload). Netzwerkfehler werden zu PreflightError, HTTPError bleibt erhalten."""
req = urllib.request.Request(
url, method="POST" if payload is not None else "GET",
data=json.dumps(payload).encode() if payload is not None else None,
headers={"Content-Type": "application/json", **(headers or {})},
)
try:
with urllib.request.urlopen(req, timeout=timeout) as res:
return json.load(res)
except urllib.error.HTTPError:
raise
except (urllib.error.URLError, OSError) as e:
hint = ""
if urllib.parse.urlsplit(url).hostname == "host.docker.internal":
hint = " Lauscht der Dienst auf dem Host nur an 127.0.0.1?"
raise PreflightError(f"{url} nicht erreichbar ({getattr(e, 'reason', e)}).{hint}") from None
except ValueError:
raise PreflightError(f"{url} liefert kein JSON.") from None
def required_ollama_models(profiles_path: str | None = None) -> set[str]:
models = {p["model"] for p in _load_profiles(profiles_path).values()
if isinstance(p, dict) and p.get("provider") == "ollama" and p.get("model")}
# Lokales Chat-Modell: Endpunkt der Fallback-Kette ohne Cloud-Keys (Review R4).
if os.getenv("MINDNET_LLM_MODEL", "").strip():
models.add(os.environ["MINDNET_LLM_MODEL"].strip())
return models
def _has_model(required: str, available: set[str]) -> bool:
return required in available or (":" not in required and f"{required}:latest" in available)
def check_live(profiles_path: str | None = None) -> None:
"""Laufzeitprüfung (nur lesend): Erreichbarkeit, Modelle, Embedding- und Collection-Dimension."""
expected_dim = int(os.getenv("MINDNET_EXPECTED_VECTOR_DIM", "768"))
ollama = os.getenv("MINDNET_OLLAMA_URL", "").rstrip("/")
model = os.getenv("MINDNET_EXPECTED_EMBEDDING_MODEL", "nomic-embed-text")
req = urllib.request.Request(
f"{ollama}/api/embeddings", method="POST",
data=json.dumps({"model": model, "prompt": "preflight"}).encode(),
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(req, timeout=30) as res:
got = len(json.load(res).get("embedding") or [])
tags = _fetch_json(f"{ollama}/api/tags")
available = {m.get("name") for m in tags.get("models") or []}
missing = sorted(m for m in required_ollama_models(profiles_path) if not _has_model(m, available))
if missing:
raise PreflightError(f"Ollama ({ollama}) fehlen die Modelle: {', '.join(missing)}.")
embedding = _fetch_json(f"{ollama}/api/embeddings", {"model": model, "prompt": "preflight"}, timeout=30)
got = len(embedding.get("embedding") or [])
if got != expected_dim:
raise PreflightError(f"Ollama liefert {got} Dimensionen für '{model}', erwartet {expected_dim}.")
print(f"preflight: Ollama erreichbar ({ollama}), Modelle vorhanden, '{model}' = {got} Dimensionen.")
qdrant = os.getenv("QDRANT_URL", "").rstrip("/")
headers = {"api-key": os.environ["QDRANT_API_KEY"]} if os.getenv("QDRANT_API_KEY") else {}
try:
_fetch_json(f"{qdrant}/collections", headers=headers)
except urllib.error.HTTPError as e:
raise PreflightError(f"Qdrant ({qdrant}) antwortet mit HTTP {e.code}.") from None
prefix = os.getenv("COLLECTION_PREFIX", "")
for suffix in ("notes", "chunks"):
name = f"{prefix}_{suffix}"
try:
with urllib.request.urlopen(f"{qdrant}/collections/{name}", timeout=10) as res:
vectors = json.load(res)["result"]["config"]["params"]["vectors"]
info = _fetch_json(f"{qdrant}/collections/{name}", headers=headers)
except urllib.error.HTTPError as e:
if e.code == 404:
print(f"preflight: Collection '{name}' existiert noch nicht.")
continue
raise
raise PreflightError(f"Qdrant-Collection '{name}': HTTP {e.code}.") from None
vectors = info["result"]["config"]["params"]["vectors"]
sizes = [vectors["size"]] if "size" in vectors else [v["size"] for v in vectors.values()]
if any(s != expected_dim for s in sizes):
raise PreflightError(f"Collection '{name}' hat Dimension {sizes}, erwartet {expected_dim}.")
print(f"preflight: Qdrant erreichbar ({qdrant}), Collections '{prefix}_*' passen.")
def run_checks(mode: str) -> None:
@@ -146,6 +212,7 @@ def run_checks(mode: str) -> None:
check_embeddings()
check_cloud_egress()
if mode in {"api", "preflight"}:
check_service_urls()
check_vault()