Files
computer-playbook/utils/storage/nfs.py
kevinveenbirkenbachandClaude Opus 5.5 eb7056dafd fix(storage): keep the NFS client off the ports the platform publishes
Main CI run 40976399687, socialhome#0 (swarm, tor, centos): the CLI tests
of svc-net-tor (onion:25 "socks CONNECT failed with reply 0x01") and
web-app-stalwart (probe_mail relay: ConnectionRefusedError on port 25)
failed. dockerd on the manager had aborted the whole ingress program of
the mail stack:

  Failed to add ingress: failed to program ingress ports:
  failed to bind host port 0.0.0.0:993/tcp: address already in use

/proc/net/tcp named the squatter: 192.168.244.10:993 -> :2049, the
node's own NFS client. The mount carries no noresvport, so the kernel
drew a source port from the 665-1023 reserved window and landed on
imaps. None of 25/465/993/995/4190/8095 got a DNAT rule or a
docker-proxy listener while every other published port did, and
stalwart still reported healthy because its healthcheck runs inside the
container. 993 and 995 are the only published ports inside that window,
so this is luck of the draw on any NFS-backed swarm host that runs the
mail role.

mount_opts now emits noresvport. insecure on the kernel-flavor export is
the other half: kernel nfsd defaults to secure and answers a mount from
an unprivileged source port with EACCES. It reaches the kernel flavor
only; ganesha already accepts unprivileged clients.

What it costs: the next deploy of every existing NFS client sees a
pending opts change and remounts the shared state; a host-side job
holding that path at that instant fails loudly. A plain remount may keep
a running mount on its old reserved port until the next real mount.

Port of f46c4d0bab from feature/store-i18n, applied identically so the
later store-i18n -> main merge stays conflict-free.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 23:02:41 +01:00

130 lines
4.4 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.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
# Exception: imported lazily because this module stays import-safe for the
# .env bootstrap, which runs before PyYAML exists.
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)