Files
computer-playbook/roles/web-app-nextcloud/lookup_plugins/nextcloud_extra_volumes.py
kevinveenbirkenbachandClaude Opus 5 acc9e25c0f refactor(volumes): one canonical mount surface, context-wired lookups
The mount pipeline was split across two layers: compose_volumes and
container_volumes lived as filter modules that ansible's filter loader
rejected on every start ("has no attribute 'FilterModule'", two warnings
per run), while the same-named lookups wrapped them thinly. The filter
bodies now live inside their lookups (the tor_socks precedent), the
filter files are gone, and the render tests moved next to the lookup
tests.

As lookups they see the templating context, so the application_id term
every call site passed back to its own role is gone too: both lookups
read application_id from context and fail loudly when it is missing.
107 templates and nfs_prep.yml drop the term; the calls collapse to
lookup('compose_volumes') and lookup('container_volumes', service_name).

The container_volumes call is now canonical - four leading spaces,
service_name in the service position, | indent(4) - enforced by a new
lint (test_container_volumes_call_shape). Normalizing the 85 literal
call sites surfaced that erpnext keyed its meta/volumes.yml mounts by
container name instead of compose service key, the only role to do so;
its meta now uses the service keys. wordpress's column-0 call, which
rendered its first line at top level, is indented like every other.

Template logic that only restated meta/volumes.yml is deleted: mailu fed
compose_volumes an extra_volumes dict whose eight names were looked up
from the very meta file compose_volumes reads itself; zammad's init and
railsserver extra mounts were the volumes.yml entries minus the two
services, which the meta now lists (the when-gated OIDC binds included).
Conditionality that meta cannot express stays in code, but in role-local
lookup plugins instead of template loops: matrix_bridge_mounts renders
the per-bridge registration mounts, nextcloud_extra_volumes the
feature-gated whiteboard/recording volumes (type: volume entries are
declared unconditionally by compose_volumes, so gating must happen
before it).

Parity is proven, not assumed: every mechanically rewritten template is
byte-identical to HEAD after the transformation regexes (95/95), and the
semantic roles render identically through the real functions against
HEAD's meta - zammad across six sso/tls polarities, all seven erpnext
services, mailu's top level. make test-lint passes complete.

Also: suitecrm's compose template pins container_extra_hosts with
host_alias (the SOCKS proxy URL host.docker.internal never resolved in
compose+tor), and the five disabled mautrix bridge addons use the
empty-default config lookup so templating the applications dict stops
emitting 125 errors per batch in every CI job.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-18 23:28:16 +02:00

74 lines
2.6 KiB
Python

"""Lookup ``nextcloud_extra_volumes``: the conditional top-level volume
declarations that must only exist while their feature is on. compose_volumes
declares every ``type: volume`` entry from ``meta/volumes.yml``
unconditionally, so feature-gated volumes are injected here instead of being
listed there - otherwise a disabled whiteboard/recording would still
materialise its volume (and, in swarm, its NFS subdir).
Usage (compose template):
extra_volumes=lookup('nextcloud_extra_volumes')
Reads from the templating context:
application_id
NEXTCLOUD_WHITEBOARD_ENABLED -- adds whiteboard_tmp + whiteboard_fontcache
NEXTCLOUD_RECORDING_ENABLED -- adds talk_recording_tmp
Volume names come from the ``volume`` lookup, so ``meta/volumes.yml`` stays
the single source for naming.
"""
from __future__ import annotations
from typing import Any
from ansible.errors import AnsibleError
from ansible.module_utils.parsing.convert_bool import boolean
from ansible.plugins.loader import lookup_loader
from ansible.plugins.lookup import LookupBase
class LookupModule(LookupBase):
def run(
self,
terms: list[Any] | None,
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[dict[str, dict[str, str]]]:
if terms:
raise AnsibleError("nextcloud_extra_volumes lookup expects no terms.")
vars_ = variables or getattr(self._templar, "available_variables", {}) or {}
templar = getattr(self, "_templar", None)
def ctx(name: str) -> Any:
if name not in vars_:
raise AnsibleError(
f"nextcloud_extra_volumes lookup: '{name}' is not set in "
"the templating context"
)
value = vars_[name]
if templar is not None:
value = templar.template(value)
return value
application_id = str(ctx("application_id")).strip()
volume_lookup = lookup_loader.get(
"volume", loader=self._loader, templar=templar
)
def volume_name(key: str) -> str:
return str(
volume_lookup.run([application_id, key], variables=vars_)[0]["name"]
)
extra: dict[str, dict[str, str]] = {}
if boolean(ctx("NEXTCLOUD_WHITEBOARD_ENABLED")):
extra["whiteboard_tmp"] = {"name": volume_name("whiteboard_tmp")}
extra["whiteboard_fontcache"] = {
"name": volume_name("whiteboard_fontcache")
}
if boolean(ctx("NEXTCLOUD_RECORDING_ENABLED")):
extra["talk_recording_tmp"] = {"name": volume_name("talk_recording_tmp")}
return [extra]