mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-30 14:56:11 +00:00
- Delete group_vars/all/20_services.yml; all service metadata now lives in each role's config/main.yml under compose.services.<name> - Rename generic 'database' service key to explicit 'mariadb'/'postgres' across all app roles; remove legacy type: field - Add utils/service_registry.py and plugins/lookup/service_registry.py as the single source of truth for service discovery - Add utils/database_service.py to centralize DB service resolution - Delete service_load_enabled filter and service_should_load lookup; replaced by service lookup plugin - Update sys-utils-service-loader to run in constructor stage and use generic load_service.yml; remove load_frontend_service.yml - Add _service_loader_preloading guard to sys-svc-compose, sys-svc-webserver-core and sys-ctl-alm-compose to prevent health-checks/alarms from firing during preload phase - Replace manual load_app.yml calls for shared DB/Mailu with assert- based preload validation in sys-svc-rdbms and sys-svc-mail - Update networks.yml.j2 templates to resolve LDAP/AI networks via service_registry instead of hardcoded role names - Migrate all tests to new service discovery API; delete obsolete tests - Update docs/contributing/code/services.md to reflect role-local model Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
228 lines
7.2 KiB
Python
228 lines
7.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, List, Optional, Set, Tuple
|
|
|
|
from ansible.errors import AnsibleError
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
from utils.config_utils import get_app_conf
|
|
from utils.service_registry import (
|
|
build_role_to_primary_service_key,
|
|
build_service_registry_from_applications,
|
|
canonical_service_key,
|
|
equivalent_service_keys,
|
|
)
|
|
|
|
|
|
def _get_service_flag(
|
|
applications: Dict[str, Any],
|
|
app_id: str,
|
|
service_keys: List[str],
|
|
flag: str,
|
|
) -> bool:
|
|
return any(
|
|
bool(
|
|
get_app_conf(
|
|
applications=applications,
|
|
application_id=app_id,
|
|
config_path=f"compose.services.{service_key}.{flag}",
|
|
strict=False,
|
|
default=False,
|
|
skip_missing_app=True,
|
|
)
|
|
)
|
|
for service_key in service_keys
|
|
)
|
|
|
|
|
|
def _get_enabled_service_keys(
|
|
applications: Dict[str, Any],
|
|
app_id: str,
|
|
) -> List[str]:
|
|
services = get_app_conf(
|
|
applications=applications,
|
|
application_id=app_id,
|
|
config_path="compose.services",
|
|
strict=False,
|
|
default={},
|
|
skip_missing_app=True,
|
|
)
|
|
if not isinstance(services, dict):
|
|
return []
|
|
return [
|
|
k
|
|
for k, v in services.items()
|
|
if isinstance(v, dict) and v.get("enabled", False)
|
|
]
|
|
|
|
|
|
def _resolve_service_provider_app_id(
|
|
applications: Dict[str, Any],
|
|
service_registry: Dict[str, Any],
|
|
service_key: str,
|
|
) -> Optional[str]:
|
|
entry = service_registry.get(service_key)
|
|
if not isinstance(entry, dict):
|
|
return None
|
|
|
|
role = entry.get("role")
|
|
if isinstance(role, str) and role:
|
|
return role
|
|
return None
|
|
|
|
|
|
def _is_service_needed(
|
|
applications: Dict[str, Any],
|
|
service_registry: Dict[str, Any],
|
|
app_id: str,
|
|
service_key: str,
|
|
visited: Set[str],
|
|
) -> bool:
|
|
"""
|
|
Return True if app_id directly or transitively (via its enabled services)
|
|
has service_key with both enabled: true AND shared: true.
|
|
Uses visited to prevent infinite loops.
|
|
"""
|
|
if app_id in visited:
|
|
return False
|
|
visited.add(app_id)
|
|
|
|
service_keys = equivalent_service_keys(service_registry, service_key)
|
|
enabled = _get_service_flag(applications, app_id, service_keys, "enabled")
|
|
shared = _get_service_flag(applications, app_id, service_keys, "shared")
|
|
if enabled and shared:
|
|
return True
|
|
|
|
for svc in _get_enabled_service_keys(applications, app_id):
|
|
if svc == service_key:
|
|
continue
|
|
dep_app_id = _resolve_service_provider_app_id(
|
|
applications, service_registry, svc
|
|
)
|
|
if dep_app_id and dep_app_id in applications:
|
|
if _is_service_needed(
|
|
applications, service_registry, dep_app_id, service_key, visited
|
|
):
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
def _build_role_to_key(service_registry: Dict[str, Any]) -> Dict[str, str]:
|
|
return build_role_to_primary_service_key(service_registry)
|
|
|
|
|
|
def _resolve_term(
|
|
term: str,
|
|
service_registry: Dict[str, Any],
|
|
role_to_key: Dict[str, str],
|
|
) -> Tuple[str, str]:
|
|
"""
|
|
Resolve a term (service key or role name) to (service_key, role).
|
|
Raises AnsibleError if the term is not a known key or role.
|
|
"""
|
|
if term in service_registry:
|
|
entry = service_registry[term]
|
|
role = entry.get("role", "")
|
|
return term, str(role)
|
|
if term in role_to_key:
|
|
key = role_to_key[term]
|
|
entry = service_registry[key]
|
|
role = entry.get("role", "")
|
|
return key, str(role)
|
|
raise AnsibleError(
|
|
f"service: '{term}' is neither a known service key nor a known role name. "
|
|
f"Known keys: {sorted(service_registry)}. "
|
|
f"Known roles: {sorted(role_to_key)}."
|
|
)
|
|
|
|
|
|
def _compute_flags(
|
|
applications: Dict[str, Any],
|
|
group_names: List[str],
|
|
service_registry: Dict[str, Any],
|
|
service_key: str,
|
|
) -> Dict[str, bool]:
|
|
deployed = [app_id for app_id in group_names if app_id in applications]
|
|
equivalent_keys = equivalent_service_keys(service_registry, service_key)
|
|
any_enabled = any(
|
|
_get_service_flag(applications, app_id, equivalent_keys, "enabled")
|
|
for app_id in deployed
|
|
)
|
|
any_shared = any(
|
|
_get_service_flag(applications, app_id, equivalent_keys, "shared")
|
|
for app_id in deployed
|
|
)
|
|
primary_key = canonical_service_key(service_registry, service_key)
|
|
any_needed = any(
|
|
_is_service_needed(applications, service_registry, app_id, primary_key, set())
|
|
for app_id in deployed
|
|
)
|
|
return {"enabled": any_enabled, "shared": any_shared, "needed": any_needed}
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
"""
|
|
Resolve a service by key or role name and return its aggregated deployment flags.
|
|
|
|
Usage:
|
|
lookup('service', 'matomo')
|
|
lookup('service', 'web-app-matomo') # resolved via reverse mapping
|
|
|
|
Reads 'applications' and 'group_names' from Ansible variables and discovers
|
|
service providers from the role-local compose.services metadata.
|
|
|
|
Returns a dict per term:
|
|
id — canonical service key (e.g. 'matomo')
|
|
role — provider role name (e.g. 'web-app-matomo')
|
|
enabled — True if any deployed app has compose.services.<key>.enabled: true
|
|
shared — True if any deployed app has compose.services.<key>.shared: true
|
|
needed — True if any deployed app has both enabled AND shared (direct or
|
|
transitively via its own enabled service dependencies)
|
|
"""
|
|
|
|
def run(
|
|
self,
|
|
terms: List[Any],
|
|
variables: Optional[Dict[str, Any]] = None,
|
|
**kwargs: Any,
|
|
) -> List[Dict[str, Any]]:
|
|
if not terms:
|
|
return []
|
|
|
|
vars_ = variables or getattr(self._templar, "available_variables", {}) or {}
|
|
|
|
applications = kwargs.get("applications", vars_.get("applications"))
|
|
if not isinstance(applications, dict):
|
|
raise AnsibleError(
|
|
"service: required variable 'applications' must be a mapping"
|
|
)
|
|
|
|
group_names = kwargs.get("group_names", vars_.get("group_names", []))
|
|
if not isinstance(group_names, list):
|
|
raise AnsibleError(
|
|
"service: required variable 'group_names' must be a list"
|
|
)
|
|
|
|
service_registry = kwargs.get("service_registry")
|
|
if service_registry is None:
|
|
service_registry = build_service_registry_from_applications(applications)
|
|
if not isinstance(service_registry, dict):
|
|
raise AnsibleError("service: 'service_registry' must be a mapping")
|
|
|
|
role_to_key = _build_role_to_key(service_registry)
|
|
|
|
results: List[Dict[str, Any]] = []
|
|
for term in terms:
|
|
term_str = str(term).strip()
|
|
if not term_str:
|
|
raise AnsibleError("service: service key/role must not be empty")
|
|
|
|
service_key, role = _resolve_term(term_str, service_registry, role_to_key)
|
|
flags = _compute_flags(
|
|
applications, group_names, service_registry, service_key
|
|
)
|
|
results.append({"id": service_key, "role": role, **flags})
|
|
|
|
return results
|