Files
computer-playbook/roles/sys-svc-compose/files/pull.py
T
kevinveenbirkenbachandClaude Opus 4.7 a145f93271 feat(sys-svc-compose): fall back to plain compose build when build --pull fails
`pull.py` always ran `compose build --pull` so BuildKit refetched the
FROM image even when it was already local. When the registry was
unreachable mid-handshake (sandboxed bridge, MTU mismatch dropping
TLS-ServerHello fragments) the build aborted on
`failed to resolve source metadata` even though the base image was
on disk.

The buildable path now runs `compose build --pull` first and falls
back to plain `compose build` on non-zero exit. The fallback uses the
local FROM image directly; if that build also fails, run_or_fail
still raises with the labelled error.

Test suite reworked to track every `run_cmd` invocation (the build
path no longer routes through run_or_fail on the happy path) and
covers both the fallback branch and the local-images-only pull
tolerance path that was already in main.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 14:55:10 +02:00

167 lines
5.0 KiB
Python
Executable File

#!/usr/bin/env python3
from __future__ import annotations
import argparse
import os
import subprocess
import sys
from pathlib import Path
def run_cmd(cmd: list[str], *, cwd: Path, env: dict[str, str]) -> tuple[int, str]:
p = subprocess.run(
cmd,
cwd=str(cwd),
env=env,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=False,
)
return p.returncode, p.stdout or ""
def run_or_fail(cmd: list[str], *, cwd: Path, env: dict[str, str], label: str) -> None:
print(f">>> {' '.join(cmd)}", file=sys.stderr)
rc, out = run_cmd(cmd, cwd=cwd, env=env)
if out.strip():
if rc == 0:
print(out, end="" if out.endswith("\n") else "\n")
else:
print(out, file=sys.stderr, end="" if out.endswith("\n") else "\n")
if rc != 0:
raise RuntimeError(f"{label} failed (rc={rc})")
def base_compose_cmd(*, project: str, cwd: Path) -> list[str]:
return ["/usr/bin/compose", "--chdir", str(cwd), "--project", project, "--"]
def has_buildable_services(
*, base_cmd: list[str], cwd: Path, env: dict[str, str]
) -> bool:
rc, out = run_cmd([*base_cmd, "config"], cwd=cwd, env=env)
if rc != 0:
if out.strip():
print(out, file=sys.stderr)
raise RuntimeError(
"docker compose config failed; cannot detect buildable services"
)
return any(
line.lstrip() != line and line.strip().startswith("build:")
for line in out.splitlines()
)
def main() -> int:
ap = argparse.ArgumentParser(
description="docker compose pull/build (retry handled by Ansible)"
)
ap.add_argument("--chdir", required=True, help="Compose instance directory")
ap.add_argument("--project", required=True, help="Compose project name (-p)")
ap.add_argument(
"--compose-files",
required=True,
help='Compose files args string like: "-f compose.yml -f compose.override.yml"',
)
ap.add_argument("--env-file", default="", help="Optional env file path")
ap.add_argument("--lock-dir", required=True, help="Directory for lock files")
ap.add_argument(
"--lock-key", required=True, help="Unique lock key (e.g. sha1 of instance dir)"
)
ap.add_argument(
"--skip-build",
action="store_true",
help="Skip docker compose build --pull",
)
ap.add_argument(
"--ignore-buildable",
action="store_true",
help="Use --ignore-buildable for pull when supported",
)
args = ap.parse_args()
cwd = Path(args.chdir)
lock_dir = Path(args.lock_dir)
lock_file = lock_dir / f"{args.lock_key}.lock"
# Lock present = work already done.
if lock_file.exists():
return 0
lock_dir.mkdir(parents=True, exist_ok=True)
env = dict(os.environ)
base_cmd = base_compose_cmd(project=args.project, cwd=cwd)
if not args.skip_build and has_buildable_services(
base_cmd=base_cmd, cwd=cwd, env=env
):
build_pull_cmd = [*base_cmd, "build", "--pull"]
print(f">>> {' '.join(build_pull_cmd)}", file=sys.stderr)
rc, out = run_cmd(build_pull_cmd, cwd=cwd, env=env)
if out.strip():
print(out, file=sys.stderr, end="" if out.endswith("\n") else "\n")
if rc != 0:
run_or_fail(
[*base_cmd, "build"],
cwd=cwd,
env=env,
label="docker compose build",
)
pull_cmd = [*base_cmd, "pull"]
if args.ignore_buildable:
rc, help_out = run_cmd([*base_cmd, "pull", "--help"], cwd=cwd, env=env)
if rc == 0 and "--ignore-buildable" in help_out:
pull_cmd.append("--ignore-buildable")
print(f">>> {' '.join(pull_cmd)}", file=sys.stderr)
rc, out = run_cmd(pull_cmd, cwd=cwd, env=env)
if out.strip():
print(out, file=sys.stderr, end="" if out.endswith("\n") else "\n")
if rc != 0:
rc_images, images_out = run_cmd(
[*base_cmd, "config", "--images"], cwd=cwd, env=env
)
if rc_images != 0:
raise RuntimeError(f"docker compose pull failed (rc={rc})")
required = [line.strip() for line in images_out.splitlines() if line.strip()]
missing = []
for image in required:
rc_inspect, _ = run_cmd(
["docker", "image", "inspect", image], cwd=cwd, env=env
)
if rc_inspect != 0:
missing.append(image)
if missing:
raise RuntimeError(
f"docker compose pull failed (rc={rc}); images missing locally: {missing}"
)
print(
f"docker compose pull failed (rc={rc}) but all images present locally",
file=sys.stderr,
)
lock_file.write_text("ok\n", encoding="utf-8")
print("pulled")
return 0
if __name__ == "__main__":
try:
rc = main()
except Exception as exc:
print(f"[ERROR] {exc}", file=sys.stderr)
raise SystemExit(1) from exc
raise SystemExit(rc)