mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-08 11:46:35 +00:00
Move files that share a delimiter-bounded name prefix within a folder (threshold 2) into a subfolder named after the prefix, dropping the prefix from the filename. Covers plugins/filter (docker, generate, has, merge, get/all), env handler subpackages, utils/roles/entity, utils/tests/swarm/write, role SQL/scripts/templates, keycloak/mailu task-includes, shared tasks/utils/load, and CI scripts. Update every import, query_file/script/include_tasks/src/lookup target, compose bind mount, and python -m path to the new locations. Extend the prefix-grouped-files lint to threshold 2 with dir-level .nocheck and file-head nocheck opt-outs, and skip groups whose shortest member's stem equals the shared prefix. Keep flat only where a loader forbids nesting: WordPress mu-plugins and the docs/requirements namespace. Ansible filter discovery is recursive, so nested filters stay Jinja-resolvable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
98 lines
3.3 KiB
Python
98 lines
3.3 KiB
Python
"""Lookup ``scrape_target``: mode-aware ``<host>:<port>`` for a Prometheus
|
|
scrape target. Swarm resolves ``tasks.<entity>_<service_key>`` (per-task DNS);
|
|
compose uses ``compose_host`` or the service container name."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import contextlib
|
|
from typing import Any
|
|
|
|
from ansible.errors import AnsibleError
|
|
from ansible.plugins.loader import lookup_loader
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
from utils.roles.applications.config import get
|
|
from utils.roles.entity.name import get_entity_name
|
|
|
|
|
|
def _as_str(value: Any) -> str:
|
|
return "" if value is None else str(value).strip()
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
def run(
|
|
self,
|
|
terms: list[Any] | None,
|
|
variables: dict[str, Any] | None = None,
|
|
**kwargs: Any,
|
|
) -> list[str]:
|
|
if not terms:
|
|
raise AnsibleError("scrape_target lookup requires the application_id term")
|
|
|
|
application_id = _as_str(terms[0])
|
|
if not application_id:
|
|
raise AnsibleError("scrape_target: application_id must be non-empty")
|
|
|
|
service_key = _as_str(terms[1]) if len(terms) > 1 else ""
|
|
if not service_key:
|
|
raise AnsibleError("scrape_target: service_key must be non-empty")
|
|
|
|
port_kind = (
|
|
_as_str(terms[2]) if len(terms) > 2 else _as_str(kwargs.get("port_kind"))
|
|
) or "http"
|
|
compose_host = (
|
|
_as_str(terms[3]) if len(terms) > 3 else _as_str(kwargs.get("compose_host"))
|
|
)
|
|
|
|
vars_ = variables or getattr(self._templar, "available_variables", {}) or {}
|
|
templar = getattr(self, "_templar", None)
|
|
|
|
raw_mode = vars_.get("DEPLOYMENT_MODE", "compose")
|
|
if templar is not None:
|
|
with contextlib.suppress(Exception):
|
|
raw_mode = templar.template(raw_mode)
|
|
deployment_mode = str(raw_mode).strip()
|
|
|
|
applications = lookup_loader.get(
|
|
"applications", loader=self._loader, templar=getattr(self, "_templar", None)
|
|
).run([], variables=vars_)[0]
|
|
|
|
port = _as_str(
|
|
get(
|
|
applications=applications,
|
|
application_id=application_id,
|
|
config_path=f"services.{service_key}.ports.internal.{port_kind}",
|
|
strict=False,
|
|
default="",
|
|
)
|
|
)
|
|
if not port:
|
|
raise AnsibleError(
|
|
f"scrape_target: no internal port for {application_id!r} "
|
|
f"(services.{service_key}.ports.internal.{port_kind})"
|
|
)
|
|
|
|
if deployment_mode == "swarm":
|
|
entity = get_entity_name(application_id)
|
|
if not entity:
|
|
raise AnsibleError(
|
|
f"scrape_target: cannot derive entity from {application_id!r}"
|
|
)
|
|
return [f"tasks.{entity}_{service_key}:{port}"]
|
|
|
|
host = compose_host or _as_str(
|
|
get(
|
|
applications=applications,
|
|
application_id=application_id,
|
|
config_path=f"services.{service_key}.name",
|
|
strict=False,
|
|
default="",
|
|
)
|
|
)
|
|
if not host:
|
|
raise AnsibleError(
|
|
f"scrape_target: no compose host for {application_id!r} "
|
|
f"(services.{service_key}.name)"
|
|
)
|
|
return [f"{host}:{port}"]
|