| .. | .. |
|---|
| 1 | | -import yaml |
|---|
| 2 | | -from pathlib import Path |
|---|
| 1 | +import sys |
|---|
| 3 | 2 | from typing import Any |
|---|
| 4 | 3 | |
|---|
| 5 | 4 | from fastapi import APIRouter, Depends |
|---|
| 6 | 5 | |
|---|
| 7 | 6 | from app.auth import verify_token |
|---|
| 8 | 7 | |
|---|
| 8 | +sys.path.insert(0, "/opt/infrastructure") |
|---|
| 9 | +from toolkit.discovery import all_projects # noqa: E402 |
|---|
| 10 | + |
|---|
| 9 | 11 | router = APIRouter() |
|---|
| 10 | 12 | |
|---|
| 11 | | -_REGISTRY_PATH = Path("/opt/infrastructure/servers/hetzner-vps/registry.yaml") |
|---|
| 12 | 13 | |
|---|
| 14 | +def _serialize_project(desc: Any) -> dict: |
|---|
| 15 | + """Serialize a ProjectDescriptor to a response dict.""" |
|---|
| 16 | + environments = [ |
|---|
| 17 | + { |
|---|
| 18 | + "name": e.name, |
|---|
| 19 | + "domain": e.domain, |
|---|
| 20 | + "compose_dir": e.compose_dir, |
|---|
| 21 | + } |
|---|
| 22 | + for e in desc.environments |
|---|
| 23 | + ] |
|---|
| 13 | 24 | |
|---|
| 14 | | -def _load_registry() -> dict: |
|---|
| 15 | | - """Load and return the registry YAML.""" |
|---|
| 16 | | - with open(_REGISTRY_PATH) as f: |
|---|
| 17 | | - return yaml.safe_load(f) |
|---|
| 25 | + return { |
|---|
| 26 | + "environments": environments, |
|---|
| 27 | + "domains": desc.domains, |
|---|
| 28 | + "promote": desc.promote or None, |
|---|
| 29 | + "has_cli": bool(desc.sync.get("type")), |
|---|
| 30 | + "backup": desc.backup or None, |
|---|
| 31 | + "type": desc.type, |
|---|
| 32 | + } |
|---|
| 18 | 33 | |
|---|
| 19 | 34 | |
|---|
| 20 | 35 | @router.get("/", summary="Get project registry") |
|---|
| .. | .. |
|---|
| 22 | 37 | _: str = Depends(verify_token), |
|---|
| 23 | 38 | ) -> dict[str, Any]: |
|---|
| 24 | 39 | """Return project list with environments, promote config, and domains.""" |
|---|
| 25 | | - registry = _load_registry() |
|---|
| 26 | | - projects = {} |
|---|
| 27 | | - |
|---|
| 28 | | - for name, cfg in registry.get("projects", {}).items(): |
|---|
| 29 | | - projects[name] = { |
|---|
| 30 | | - "environments": cfg.get("environments", []), |
|---|
| 31 | | - "domains": cfg.get("domains", {}), |
|---|
| 32 | | - "promote": cfg.get("promote"), |
|---|
| 33 | | - "has_cli": bool(cfg.get("cli")), |
|---|
| 34 | | - "static": cfg.get("static", False), |
|---|
| 35 | | - "infrastructure": cfg.get("infrastructure", False), |
|---|
| 36 | | - "backup_dir": cfg.get("backup_dir"), |
|---|
| 37 | | - "has_coolify": bool(cfg.get("coolify_uuids")), |
|---|
| 38 | | - } |
|---|
| 39 | | - |
|---|
| 40 | + projects = { |
|---|
| 41 | + name: _serialize_project(desc) |
|---|
| 42 | + for name, desc in all_projects().items() |
|---|
| 43 | + } |
|---|
| 40 | 44 | return {"projects": projects} |
|---|