Files
computer-playbook/utils/storage/nfs.py
T
kevinveenbirkenbachandClaude Opus 5.5 ae40a573c2 fix(web-app-weblate): give the static tmpfs mountpoint its mode on swarm NFS volumes
CI run 40979252521 Weblate#1 (swarm) never converged: every task after
the first died in collectstatic with "PermissionError: [Errno 13]
Permission denied: '/app/cache/static/weblate_fonts'". The tmpfs at
/app/cache/static sits inside the shared weblate_cache volume; the first
task's runc creates the mountpoint as root 0755, and runc gives a tmpfs
on an existing mountpoint that directory's mode. 99a7d28067 declared
mode 1777, which compose honours, but docker stack deploy keeps only the
tmpfs size (docker/cli 29.9.2 convert/volume.go), so swarm still got a
root 0755 tmpfs.

- utils/storage/nfs.py: nested_tmpfs_mountpoints() derives every tmpfs
  mountpoint whose innermost enclosing volume of the same service is
  NFS-backed, with its declared mode; a nested tmpfs without a mode, or
  one mountpoint declared with two modes, raises.
- plugins/lookup/nfs_tmpfs_mountpoints.py feeds it the rendered compose
  volumes, so compose_volumes stays the source of which volumes live on
  NFS.
- sys-svc-compose nfs_prep.yml creates those mountpoints on the NFS
  server with the declared mode before every stack deploy, which also
  repairs a mountpoint an earlier deploy left at 0755.
- web-app-weblate: the cache volume declares nfs uid 1000, gid 0,
  mode 0770; a pre-created mountpoint makes the volume non-empty, so
  Docker no longer copies the image's /app/cache root in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 02:05:59 +01:00

241 lines
8.7 KiB
Python

from __future__ import annotations
import re
from collections.abc import Mapping
from pathlib import PurePosixPath
from utils import PROJECT_ROOT
from utils.cache.files import read_text
from utils.roles.applications.mounts import normalize_volumes_meta
from utils.roles.mapping import ROLE_FILE_META_SERVICES
STATE_SUBDIR = "infinito-state"
NFS_SERVER_SERVICES_YML = str(
PROJECT_ROOT / "roles" / "svc-storage-nfs-server" / ROLE_FILE_META_SERVICES
)
NFS_CLIENT_SERVICES_YML = str(
PROJECT_ROOT / "roles" / "svc-storage-nfs-client" / ROLE_FILE_META_SERVICES
)
def _read_spot_value(services_yml: str, key: str) -> str:
"""Read a scalar entity value via line parse.
Exception: stdlib-only on purpose - these SPOTs feed the .env
generation, which bootstraps fresh hosts before PyYAML exists.
Args:
services_yml: repo-relative services.yml path.
key: two-space-indented scalar key to read.
"""
pattern = re.compile(rf"^ {re.escape(key)}:\s*(\S+)\s*$")
for line in read_text(services_yml).splitlines():
match = pattern.match(line)
if match:
return match.group(1)
raise KeyError(f"{key} not found in {services_yml}")
def get_export_base() -> str:
"""NFS export base from the provider's services.yml SPOT."""
return _read_spot_value(NFS_SERVER_SERVICES_YML, "export_base")
def get_client_version() -> int:
"""NFS mount protocol version from the client's services.yml SPOT."""
return int(_read_spot_value(NFS_CLIENT_SERVICES_YML, "nfs_version"))
def state_path(export_base, subdir):
return str(PurePosixPath(str(export_base)) / str(subdir))
def fstype(version):
return "nfs4" if int(version) >= 4 else "nfs"
def mount_opts(version, runtime):
reliability = (
"soft,timeo=50,retrans=3"
if runtime in ("dev", "act", "github")
else "hard,timeo=600"
)
locking = "" if int(version) >= 4 else ",nolock"
return f"vers={version},rw,noresvport,{reliability}{locking}"
def client_src(server, version, flavor, state_path_value):
use_root = flavor == "kernel" and int(version) >= 4
return f"{server}:{'/' if use_root else state_path_value}"
def _role_forces_other_mode(application_id):
"""True unless the role certainly runs at the cluster's deployment mode.
``compose_mode_force`` overrides the cluster mode for one role only
(roles/sys-svc-compose/defaults/main.yml). A value this function cannot
resolve statically - a Jinja expression - counts as forcing, so an
unknown mode keeps a volume backed up rather than silently dropping it
from both capture paths.
Args:
application_id: role whose ``vars/main.yml`` is read.
"""
from utils import PROJECT_ROOT
from utils.cache.yaml import load_yaml_any
from utils.roles.mapping import ROLE_FILE_VARS_MAIN
path = PROJECT_ROOT / "roles" / str(application_id) / ROLE_FILE_VARS_MAIN
if not path.is_file():
return False
loaded = load_yaml_any(str(path))
if not isinstance(loaded, Mapping):
return False
forced = loaded.get("compose_mode_force")
if forced is None:
return False
forced = str(forced).strip()
if not forced:
return False
if "{{" in forced or "{%" in forced:
return True
return forced.lower() != "swarm"
def swarm_nfs_backed(entry, *, application_id, deployment_mode, storage_backend):
"""True when the swarm NFS rewrite backs this ``meta/volumes.yml`` entry.
Args:
entry: volume entry mapping; ``nfs: false`` opts it out of the rewrite.
application_id: role the volume belongs to; a manager-pinned role or one
forcing a non-swarm mode keeps its volumes node-local.
deployment_mode: cluster-wide ``compose`` or ``swarm``.
storage_backend: the ``storage.backend`` value, e.g. ``local`` or ``nfs``.
"""
if str(deployment_mode).strip().lower() != "swarm":
return False
if str(storage_backend).strip().lower() != "nfs":
return False
if not isinstance(entry, Mapping):
return False
if entry.get("nfs") is False:
return False
from utils.roles.meta_lookup import get_role_placement
if str(get_role_placement(application_id) or "").strip().lower() == "manager":
return False
return not _role_forces_other_mode(application_id)
def volume_dir(semantic_name, entry):
"""Directory under the NFS state path the swarm rewrite binds a volume to.
Args:
semantic_name: the volume's key in the compose ``volumes:`` block.
entry: the volume's spec; a pinned ``name`` wins over the key.
"""
pinned = entry.get("name") if isinstance(entry, Mapping) else None
return str(pinned) if pinned else str(semantic_name)
def _tmpfs_mode(value):
if isinstance(value, bool) or not isinstance(value, (int, str)):
raise TypeError(f"tmpfs mode must be an octal string or int, got {value!r}")
return value if isinstance(value, int) else int(value, 8)
def nested_tmpfs_mountpoints(volumes_meta, nfs_dirs):
"""Tmpfs mountpoints whose innermost volume of the same service is NFS-backed.
Args:
volumes_meta: the role's ``meta/volumes.yml`` mapping.
nfs_dirs: NFS state directory per semantic name of every NFS-backed volume.
Returns:
One ``{"path": str, "mode": int}`` per mountpoint, sorted by ``path``,
which is relative to the NFS state path.
Raises:
ValueError: a nested tmpfs declares no ``mode``, or two tmpfs mounts
declare different modes for the same mountpoint.
TypeError: a nested tmpfs ``mode`` is neither an octal string nor an int.
"""
entries = normalize_volumes_meta(volumes_meta)
parents: dict[str, list[tuple[str, str]]] = {}
for name, entry in entries.items():
if entry["type"] != "volume":
continue
for mount in entry.get("mounts") or []:
parents.setdefault(mount["service"], []).append(
(str(mount["target"]), name)
)
modes: dict[str, int] = {}
for name, entry in entries.items():
if entry["type"] != "tmpfs":
continue
for mount in entry.get("mounts") or []:
target = str(mount["target"])
enclosing = [
(parent.rstrip("/"), volume)
for parent, volume in parents.get(mount["service"], [])
if target == parent or target.startswith(parent.rstrip("/") + "/")
]
if not enclosing:
continue
base, volume = max(enclosing, key=lambda pair: len(pair[0]))
if volume not in nfs_dirs:
continue
if "mode" not in mount:
raise ValueError(
f"tmpfs {name!r} of service {mount['service']!r} at {target} "
f"lies inside the NFS-backed volume {volume!r} and declares "
f"no 'mode'"
)
relative = target[len(base) :].lstrip("/")
path = str(PurePosixPath(nfs_dirs[volume], relative))
mode = _tmpfs_mode(mount["mode"])
if modes.setdefault(path, mode) != mode:
raise ValueError(
f"tmpfs mountpoint {path} is declared with modes "
f"{modes[path]:o} and {mode:o}"
)
return [{"path": path, "mode": mode} for path, mode in sorted(modes.items())]
def backup_disabled_dirs(applications, *, deployment_mode, storage_backend):
"""State-path directories of the NFS-backed volumes declared ``backup: false``.
Args:
applications: merged applications mapping whose ``volumes`` carry the
canonical ``meta/volumes.yml`` entries.
deployment_mode: cluster-wide ``compose`` or ``swarm``.
storage_backend: the ``storage.backend`` value, e.g. ``local`` or ``nfs``.
Returns:
The sorted directory names, without any a backed-up volume also binds to.
"""
disabled: set[str] = set()
kept: set[str] = set()
for app_id, app in applications.items():
volumes = app.get("volumes") if isinstance(app, Mapping) else None
if not isinstance(volumes, Mapping):
continue
for semantic_name, entry in volumes.items():
if (
not isinstance(entry, Mapping)
or entry.get("type", "volume") != "volume"
):
continue
if not swarm_nfs_backed(
entry,
application_id=str(app_id),
deployment_mode=deployment_mode,
storage_backend=storage_backend,
):
continue
target = disabled if entry.get("backup") is False else kept
target.add(volume_dir(semantic_name, entry))
return sorted(disabled - kept)