67 lines
2.7 KiB
Python
67 lines
2.7 KiB
Python
"""Where the homelab's services live — the low-level plumbing the core keeps.
|
|
|
|
The Services catalogue (the list, the cards, the detail page, ``ai-agent
|
|
plugin services``) is the ``services`` live plugin's since 2026-11-28
|
|
(``~/projects/ai-agent-plugins/plugins/services``; ``backend/services/`` and
|
|
``/services*`` are gone). The core still needs the directory itself for:
|
|
|
|
* ``services.read`` — the plugin's handlers get it as ``AIA_SERVICES_DIR``
|
|
(``plugins/runner.py``);
|
|
* the file chips (``files/hostfiles.py`` — ``~/homelab/services/…``), the
|
|
artefacts strip (``projects/artefacts.py``), a goal-keeper's ``services/<dir>``
|
|
(``goals/routes.py``), the parser's ``servicesAuto`` and the diff view's
|
|
``service:<slug>`` token;
|
|
* the bucketed git history the plugin reads through
|
|
``hub/services/commits`` (one ``git log`` walk keyed on HEAD, :mod:`githist`).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import githist
|
|
|
|
SERVICES_DIR = Path(os.environ.get("SERVICES_DIR", "/services-src")).resolve()
|
|
# Superproject root (its .git is mounted read-only) — services have no .git of
|
|
# their own, so their history is read through a pathspec.
|
|
REPO_DIR = Path(os.environ.get("REPO_DIR", "/workspace")).resolve()
|
|
# Where the services dir lives *inside the repo* for git pathspecs.
|
|
REPO_SERVICES_REL = os.environ.get("REPO_SERVICES_REL", "services")
|
|
|
|
# One bucketed history walk for all services, cached on the repo HEAD (a
|
|
# per-request `git log` per service once made /api/services take 11 s).
|
|
HISTORY = githist.BucketedHistory(REPO_DIR, REPO_SERVICES_REL)
|
|
|
|
|
|
def safe_entry(slug: str) -> Path | None:
|
|
"""``services/<slug>`` as a directory, traversal-safe; None when absent."""
|
|
name = Path(slug or "").name
|
|
if not name or name.startswith("."):
|
|
return None
|
|
entry = (SERVICES_DIR / name).resolve()
|
|
try:
|
|
entry.relative_to(SERVICES_DIR)
|
|
except ValueError:
|
|
return None
|
|
return entry if entry.is_dir() else None
|
|
|
|
|
|
def commits_of(name: str) -> dict:
|
|
"""``{updatedAt, commits}`` of one service dir (``commits``: newest first,
|
|
``{hash, date, subject}``); ``updatedAt`` None with no commit yet."""
|
|
b = HISTORY.get(name)
|
|
if not b:
|
|
return {"updatedAt": None, "commits": []}
|
|
return {"updatedAt": b.get("iso"), "commits": list(b.get("commits") or [])}
|
|
|
|
|
|
def all_commits() -> dict[str, dict]:
|
|
"""Every service dir on disk → :func:`commits_of` (no git fork per dir)."""
|
|
if not SERVICES_DIR.is_dir():
|
|
return {}
|
|
out: dict[str, dict] = {}
|
|
for e in sorted(SERVICES_DIR.iterdir(), key=lambda p: p.name.lower()):
|
|
if e.is_dir() and not e.name.startswith("."):
|
|
out[e.name] = commits_of(e.name)
|
|
return out
|