Files
cloud-apache-container/scripts/verify-image-arch.py
T
shadowdaoandClaude Sonnet 5.5 344c5e8d0d ci: address Opus review of the arm64 policy PR
- job timeouts 130 (cac, cac-fpm) / 80 (shared-httpd) so the retry fits; test pins the budget
- verify-image-arch.py: bounded docker calls, pull retried 3x, exit 4 'could not pull' (not an
  arch verdict), pulled image removed afterwards
- ARM64_DIGEST only taken from the first attempt when it succeeded
- comments/docs: max-parallel is per-job and unverified; guard also fires on skipped/cancelled

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
2026-11-19 21:12:56 -08:00

126 lines
5.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""verify-image-arch.py - prove an image platform really contains binaries of that architecture.
An image whose config says linux/arm64 can still carry x86-64 binaries (the "x86 in arm64"
exec-format-error loop seen with haproxy-manager-base). The registry manifest cannot tell; the ELF
header can. This pulls REF for one platform, copies the named key binaries out of a created (never
started, never executed) container and checks e_machine of each: aarch64 = 0xB7, x86-64 = 0x3E.
Same idea as haproxy-manager-base scripts/verify-image-arch.py (PR #22), adapted to docker cp so
no QEMU execution is needed.
verify-image-arch.py --image REF --platform linux/arm64|linux/amd64 --bin /usr/bin/php [--bin ...]
Exit 0 = every --bin is an ELF64 little-endian binary of the platform's machine type.
Exit 1 = a binary is missing, not an ELF, or of the wrong architecture (or docker failed).
Exit 2 = usage. Exit 4 = could not pull the image (3 attempts): no architecture verdict.
Env: DOCKER overrides the docker command (tests use a stub). Nothing in REF is ever run.
"""
import argparse
import os
import shlex
import subprocess
import sys
import tarfile
import threading
import time
MACHINES = {'linux/arm64': (0xB7, 'AArch64'), 'linux/amd64': (0x3E, 'x86-64')}
NAMES = {0x3E: 'x86-64', 0xB7: 'AArch64', 0x03: 'x86', 0x28: 'ARM', 0xF3: 'RISC-V', 0x15: 'PowerPC64', 0x16: 'S390'}
MAX_STREAM = 1 << 31 # refuse to read more than 2 GiB of one tar stream
def elf_machine(head):
"""Return e_machine of an ELF64 little-endian header, or raise ValueError."""
if len(head) < 20 or head[:4] != b'\x7fELF':
raise ValueError('not an ELF file')
if head[4] != 2:
raise ValueError('not ELF64 (EI_CLASS=%d)' % head[4])
if head[5] != 1:
raise ValueError('not little-endian (EI_DATA=%d)' % head[5])
return int.from_bytes(head[18:20], 'little')
def docker_cmd():
return shlex.split(os.environ.get('DOCKER') or 'docker')
def run(args, timeout=300):
try:
return subprocess.run(docker_cmd() + args, capture_output=True, timeout=timeout)
except subprocess.TimeoutExpired:
return subprocess.CompletedProcess(args, 124, b'', b'timed out after %ds' % timeout)
def first_bytes_of(cid, path):
"""First 64 bytes of the regular file at path (symlinks followed) inside container cid."""
p = subprocess.Popen(docker_cmd() + ['cp', '-L', '%s:%s' % (cid, path), '-'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
timer = threading.Timer(120, p.kill) # a stalled docker cp must not hang the job
timer.start()
try:
tf = tarfile.open(fileobj=p.stdout, mode='r|')
m = tf.next()
if m is None or not m.isreg():
raise ValueError('%s is not a regular file' % path)
return tf.extractfile(m).read(64)
except tarfile.TarError as e:
err = p.stderr.read(300).decode('utf-8', 'replace').strip()
raise ValueError('cannot read %s: %s %s' % (path, e, err))
finally:
timer.cancel()
p.kill()
p.wait()
def main(argv):
ap = argparse.ArgumentParser(description=__doc__.split('\n')[0])
ap.add_argument('--image', required=True)
ap.add_argument('--platform', required=True, choices=sorted(MACHINES))
ap.add_argument('--bin', dest='bins', action='append', required=True)
ap.add_argument('--no-pull', action='store_true', help='image is already in the local store')
a = ap.parse_args(argv)
want, label = MACHINES[a.platform]
pulled = False
if not a.no_pull:
r = None
for attempt in range(3):
r = run(['pull', '--quiet', '--platform', a.platform, a.image], timeout=900)
if r.returncode == 0:
break
time.sleep(float(os.environ.get('VERIFY_PULL_SLEEP', '15')))
if r.returncode != 0:
print('verify-image-arch: could not pull %s after 3 attempts (not an architecture finding): %s' % (
a.image, r.stderr.decode('utf-8', 'replace')[:300].strip()), file=sys.stderr)
return 4
pulled = True
r = run(['create', '--platform', a.platform, '--entrypoint', '/nonexistent', a.image])
cid = r.stdout.decode().strip()
if r.returncode != 0 or not cid:
print('verify-image-arch: docker create failed: %s' % r.stderr.decode('utf-8', 'replace')[:300].strip(), file=sys.stderr)
return 1
rc = 0
try:
for path in a.bins:
try:
m = elf_machine(first_bytes_of(cid, path))
except ValueError as e:
print('FAIL %s %s %s: %s' % (a.image, a.platform, path, e))
rc = 1
continue
if m != want:
print('FAIL %s %s %s: e_machine=0x%X (%s), want 0x%X (%s)' % (
a.image, a.platform, path, m, NAMES.get(m, '?'), want, label))
rc = 1
else:
print('OK %s %s %s: e_machine=0x%X (%s)' % (a.image, a.platform, path, m, label))
finally:
run(['rm', '-f', cid], timeout=120)
if pulled:
run(['image', 'rm', a.image], timeout=120) # keep the runner disk from filling
return rc
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))