Files
computer-playbook/cli/administration/deploy/development/binfmt.py
T
kevinveenbirkenbachandClaude Opus 5 4e9ced67b7 feat(swarm): let the local loop drive the architecture axis
The axis was complete in CI and unusable from a workstation. Four separate
gaps, each invisible on a real runner, and each found by running the swarm loop
against it:

`INFINITO_ARCHITECTURES` as an exported variable did nothing, because the
workflow reads its pool from `inputs.architectures`. Passing the input alone did
not help either: ACT_ENV sets INFINITO_APP_DISCOVERY_RUNNER=host, so the
discovery that builds the matrix runs outside the act container, where a
workflow-level `env:` does not reach. This is why the distro axis is handed over
twice, as `distros=` and as `INFINITO_DISTROS`, and SWARM_ARCH now does the same
for the architecture.

The third gap is the sharpest: act does not resolve `${{ matrix.* }}` inside a
job-level `env:` block, so `INFINITO_ARCHITECTURE` reached the assertion empty
every time. Earlier runs only appeared to work because a stale local `.env`
supplied a value, and the assertion was therefore checking a leftover rather
than the row. It surfaced the moment that file was regenerated.

Fourth, act had no mapping for `ubuntu-24.04-arm`, the label RUNNERS
introduced, and skipped the job with a hint instead of failing. It maps to the
same runner image: locally the architecture is delivered by emulation, and the
runner's own hardware is not the promise being made.

`make binfmt` registers that emulation. The handler already existed for
compose-up; it gains a subcommand so the registration is reachable without a
privileged docker invocation, which is what an agent would otherwise have to
ask for every time. The emulation does not survive a swarm run, because act
starts further containers whose systemd clears the shared binfmt registry, so
this is needed often.

Without `arch=`, SWARM_ARCH resolves to the host's own architecture rather than
staying empty, which would now abort every unpinned local swarm run at the
assertion.

Verified: `make binfmt arch=s390x` takes a platform from `exec format error` to
`s390x`; `make -n swarm-zombie arch=arm64` carries arm64 through all three
levers and defaults to amd64 without it; shellcheck passes; a live run reached
`"architecture":"arm64"` with the arm64 glyph in its job title.

Not verified: that the runner-label mapping carries a swarm deploy to the end.
That run was stopped so a parallel agent could use the shared stack.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-19 11:57:03 +02:00

100 lines
2.4 KiB
Python

"""Register the emulation an architecture-pinned deploy needs."""
from __future__ import annotations
import argparse
import os
import platform
import subprocess
PROBE_IMAGE = "alpine:3"
INSTALLER_IMAGE = "tonistiigi/binfmt"
_HOST_ALIASES = {
"x86_64": "amd64",
"amd64": "amd64",
"aarch64": "arm64",
"arm64": "arm64",
}
def host_architecture() -> str:
machine = platform.machine().lower()
return _HOST_ALIASES.get(machine, machine)
def _can_execute(architecture: str) -> bool:
return (
subprocess.run(
[
"docker",
"run",
"--rm",
"--platform",
f"linux/{architecture}",
PROBE_IMAGE,
"true",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
).returncode
== 0
)
def ensure(platform_ref: str | None = None) -> None:
"""No-op when nothing is pinned or it is the host's own; raises SystemExit
when the handler cannot be registered."""
raw = (
platform_ref
if platform_ref is not None
else os.environ.get("INFINITO_DOCKER_PLATFORM")
)
pinned = (raw or "").strip()
if not pinned:
return
architecture = pinned.rsplit("/", 1)[-1]
if architecture == host_architecture() or _can_execute(architecture):
return
print(f">>> Registering binfmt emulation for {pinned}")
subprocess.run(
[
"docker",
"run",
"--privileged",
"--rm",
INSTALLER_IMAGE,
"--install",
architecture,
],
stdout=subprocess.DEVNULL,
check=False,
)
if not _can_execute(architecture):
raise SystemExit(
f"linux/{architecture} still cannot execute after registering a handler. "
"Check whether this host permits --privileged containers."
)
def add_parser(sub: argparse._SubParsersAction) -> None:
p = sub.add_parser(
"binfmt", help="Make an architecture executable here through emulation."
)
p.add_argument(
"--architecture",
default="",
help="amd64 | arm64; empty takes the platform from INFINITO_DOCKER_PLATFORM.",
)
p.set_defaults(_handler=handler)
def handler(args: argparse.Namespace) -> int:
named = args.architecture.strip()
ensure(f"linux/{named}" if named else None)
return 0