Public changes compared with the previous published export: 89 files changed. Affected areas: - backend/app: 21 files - dependencies: 5 files - deployment and CI: 6 files - documentation: 2 files - frontend: 3 files - frontend/src: 20 files - project files: 1 file - tests: 30 files - unraid: 1 file Changed files: - Updated: ".env.example" - Updated: "Dockerfile.unraid" - Updated: "README.md" - Updated: "backend/Dockerfile" - Added: "backend/app/alerts.py" - Updated: "backend/app/auth.py" - Added: "backend/app/backups.py" - Updated: "backend/app/config_io.py" - Updated: "backend/app/demo.py" - Updated: "backend/app/discovery.py" - Updated: "backend/app/discovery_store.py" - Updated: "backend/app/docker_discovery.py" - Updated: "backend/app/events.py" - Updated: "backend/app/evidence.py" - Updated: "backend/app/health.py" - Updated: "backend/app/incidents.py" - Added: "backend/app/loops.py" - Updated: "backend/app/main.py" - Updated: "backend/app/models.py" - Updated: "backend/app/observation.py" - Updated: "backend/app/probes.py" - Added: "backend/app/response_cache.py" - Updated: "backend/app/scheduler.py" - Updated: "backend/app/semantic.py" - Updated: "backend/app/store.py" - Updated: "backend/pyproject.toml" - Updated: "backend/tests/test_api.py" - Updated: "backend/tests/test_config_io.py" - Updated: "backend/tests/test_demo.py" - Updated: "backend/tests/test_discovery_v120.py" - Updated: "backend/tests/test_health.py" - Updated: "backend/tests/test_probes.py" - Updated: "backend/tests/test_scheduler.py" - Updated: "backend/tests/test_v130_probe_reachability.py" - Updated: "backend/tests/test_v160_evidence_changes.py" - Added: "backend/tests/test_v163_alerts.py" - Added: "backend/tests/test_v163_backups.py" - Added: "backend/tests/test_v163_data_integrity.py" - Added: "backend/tests/test_v163_efficiency.py" - Added: "backend/tests/test_v163_health_performance.py" - Added: "backend/tests/test_v163_loops.py" - Added: "backend/tests/test_v163_management.py" - Added: "backend/tests/test_v163_realtime.py" - Added: "backend/tests/test_v163_security.py" - Added: "backend/tests/test_v163_semantic_etag.py" - Added: "compose.demo.yaml" - Updated: "compose.yaml" - Added: "docker-compose.broker.yml" - Updated: "docker-compose.yml" - Updated: "docs/ARCHITECTURE.md" - Added: "frontend/e2e/a11y.spec.ts" - Updated: "frontend/e2e/demo.spec.ts" - Updated: "frontend/index.html" - Updated: "frontend/nginx.conf" - Updated: "frontend/package-lock.json" - Updated: "frontend/package.json" - Updated: "frontend/playwright.config.ts" - Updated: "frontend/src/App.tsx" - Updated: "frontend/src/LivingEdge.tsx" - Added: "frontend/src/activityView.test.ts" - Added: "frontend/src/activityView.ts" - Updated: "frontend/src/api.ts" - Added: "frontend/src/apiAuth.test.ts" - Added: "frontend/src/components/ActionsMenu.tsx" - Added: "frontend/src/components/ConfirmDialog.test.tsx" - Added: "frontend/src/components/ConfirmDialog.tsx" - Added: "frontend/src/components/StatusIcon.tsx" - Updated: "frontend/src/demo/DemoWalkthrough.test.ts" - Updated: "frontend/src/demo/DemoWalkthrough.tsx" - Added: "frontend/src/elk-worker-url.d.ts" - Added: "frontend/src/labels.ts" - Updated: "frontend/src/main.tsx" - Added: "frontend/src/realtime/refreshPlan.test.ts" - Added: "frontend/src/realtime/refreshPlan.ts" - Updated: "frontend/src/realtime/useRealtimeEvents.ts" - Added: "frontend/src/routing/urlState.test.ts" - Added: "frontend/src/routing/urlState.ts" - Updated: "frontend/src/styles.css" - Added: "frontend/src/topology/GraphListView.test.tsx" - Added: "frontend/src/topology/GraphListView.tsx" - Updated: "frontend/src/topology/ServiceNode.tsx" - Updated: "frontend/src/topology/TopologyCommandBar.tsx" - Added: "frontend/src/topology/semanticGraph.test.ts" - Updated: "frontend/src/topology/semanticGraph.ts" - Added: "frontend/src/topologyLayout.test.ts" - Updated: "frontend/src/topologyLayout.ts" - Updated: "package-lock.json" - Updated: "package.json" - Updated: "unraid/my-servicemap.xml" Export provenance/manifests refreshed; excluded from the change summary. Publish curated ServiceMap source from af46f684e468dffd3a5190e28d3b572d4c26bfd7
151 lines
6.1 KiB
Python
151 lines
6.1 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import yaml
|
|
from pydantic import ValidationError
|
|
|
|
from .models import ConfigDocument, ConfigImportResult, ConfigService, Service, ServiceCreate
|
|
from .store import Store
|
|
|
|
# Observed runtime state is not configuration: it is re-learned from Docker
|
|
# and probes and must not be frozen into an exported document.
|
|
OBSERVED_FIELDS = frozenset(
|
|
{"runtime_state", "runtime_health", "first_seen_at", "last_seen_at", "last_discovery_status"}
|
|
)
|
|
CONFIG_FIELDS = frozenset(ServiceCreate.model_fields) - OBSERVED_FIELDS - {"depends_on"}
|
|
|
|
|
|
def config_service(service: Service, names_by_id: dict[str, str]) -> ConfigService:
|
|
"""Every operator-owned field of a service, dependencies by name.
|
|
|
|
The previous field-by-field copy omitted application/domain/role, their
|
|
locks, category, visibility, lifecycle, monitoring and probe metadata and
|
|
layout, so an export/import round trip silently lost them.
|
|
"""
|
|
|
|
return ConfigService(
|
|
**service.model_dump(include=CONFIG_FIELDS),
|
|
depends_on=[names_by_id[item] for item in service.depends_on if item in names_by_id],
|
|
)
|
|
|
|
|
|
def _reject_yaml_aliases(content: str) -> None:
|
|
"""Refuse YAML anchors/aliases: nested aliases expand exponentially
|
|
("billion laughs") and a configuration document never needs them."""
|
|
|
|
for event in yaml.parse(content, Loader=yaml.SafeLoader):
|
|
if isinstance(event, yaml.AliasEvent) or getattr(event, "anchor", None):
|
|
raise ValueError("De configuratie mag geen YAML-anchors of -aliassen gebruiken")
|
|
|
|
|
|
def parse_document(content: str, format_name: str) -> ConfigDocument:
|
|
try:
|
|
if format_name != "json":
|
|
_reject_yaml_aliases(content)
|
|
raw = json.loads(content) if format_name == "json" else yaml.safe_load(content)
|
|
except (json.JSONDecodeError, yaml.YAMLError) as exc:
|
|
raise ValueError(f"Invalid {format_name.upper()} configuration: {exc}") from exc
|
|
if raw is None:
|
|
raw = {"version": 1, "services": []}
|
|
try:
|
|
document = ConfigDocument.model_validate(raw)
|
|
except ValidationError as exc:
|
|
raise ValueError(f"De configuratie voldoet niet aan het schema: {exc}") from exc
|
|
if document.version == 2 and not document.services:
|
|
# Preserve the v1 contract's explicit unsupported-empty-v2 guard while
|
|
# accepting real v2 documents that carry the richer service schema.
|
|
raise ValueError("Voor een leeg document moet de configuratieversie 1 of lager zijn")
|
|
|
|
names = [service.name.casefold() for service in document.services]
|
|
if len(names) != len(set(names)):
|
|
raise ValueError("Servicenamen moeten uniek zijn in de configuratie")
|
|
|
|
for service in document.services:
|
|
dependencies = [dependency.casefold() for dependency in service.depends_on]
|
|
if len(dependencies) != len(set(dependencies)):
|
|
raise ValueError(f"Service {service.name} heeft dubbele afhankelijkheden")
|
|
if service.name.casefold() in dependencies:
|
|
raise ValueError(f"Service {service.name} kan niet van zichzelf afhangen")
|
|
|
|
# A merge document may reference services that already exist in the store,
|
|
# so missing-reference validation belongs to import_document(). Cycles that
|
|
# are fully contained in this document can still be rejected immediately.
|
|
_validate_acyclic(document.services)
|
|
return document
|
|
|
|
|
|
def _validate_acyclic(services: list[ConfigService]) -> None:
|
|
graph = {
|
|
service.name.casefold(): [item.casefold() for item in service.depends_on]
|
|
for service in services
|
|
}
|
|
visiting: set[str] = set()
|
|
visited: set[str] = set()
|
|
|
|
def visit(node: str) -> None:
|
|
if node in visiting:
|
|
raise ValueError("De afhankelijkheidsgrafiek bevat een kring")
|
|
if node in visited:
|
|
return
|
|
visiting.add(node)
|
|
for dependency in graph.get(node, []):
|
|
visit(dependency)
|
|
visiting.remove(node)
|
|
visited.add(node)
|
|
|
|
for node in graph:
|
|
visit(node)
|
|
|
|
|
|
def _validate_import_graph(store: Store, document: ConfigDocument, mode: str) -> None:
|
|
if mode not in {"merge", "replace"}:
|
|
raise ValueError("Importmodus moet merge of replace zijn")
|
|
|
|
projected: dict[str, ConfigService] = {}
|
|
if mode == "merge":
|
|
existing = store.list_services()
|
|
names_by_id = {service.id: service.name for service in existing}
|
|
for service in existing:
|
|
projected[service.name.casefold()] = config_service(service, names_by_id)
|
|
|
|
for service in document.services:
|
|
key = service.name.casefold()
|
|
previous = projected.get(key)
|
|
if previous is not None and "depends_on" not in service.model_fields_set:
|
|
# A merge keeps dependencies the document does not mention.
|
|
service = service.model_copy(update={"depends_on": previous.depends_on})
|
|
projected[key] = service
|
|
|
|
available = set(projected)
|
|
for service in projected.values():
|
|
missing = [
|
|
dependency
|
|
for dependency in service.depends_on
|
|
if dependency.casefold() not in available
|
|
]
|
|
if missing:
|
|
raise ValueError(
|
|
f"Service {service.name} verwijst naar onbekende afhankelijkheden: {', '.join(missing)}"
|
|
)
|
|
|
|
_validate_acyclic(list(projected.values()))
|
|
|
|
|
|
def export_document(store: Store) -> ConfigDocument:
|
|
services = store.list_services()
|
|
names = {service.id: service.name for service in services}
|
|
return ConfigDocument(version=2, services=[config_service(service, names) for service in services])
|
|
|
|
|
|
def serialize_document(document: ConfigDocument, format_name: str) -> str:
|
|
payload = document.model_dump(mode="json", exclude={"services": {"__all__": set(OBSERVED_FIELDS)}})
|
|
if format_name == "json":
|
|
return json.dumps(payload, indent=2, sort_keys=False)
|
|
return yaml.safe_dump(payload, sort_keys=False, allow_unicode=True)
|
|
|
|
|
|
def import_document(store: Store, document: ConfigDocument, mode: str) -> ConfigImportResult:
|
|
_validate_import_graph(store, document, mode)
|
|
return store.import_config_services(document.services, mode)
|