#!/usr/bin/env python3 # -*- coding: utf-8 -*- """check-unclean-boot-images.py — an app's power-cut repair runs only on the images it was PROVEN on (R-1007, decision 236). WHAT GOES WRONG WITHOUT IT. A template's `after_unclean_boot:` block lists the images the repair was proven on (`images:`). The repair uses the app's INTERNALS (Immich: its BullMQ queue `thumbnailGeneration`, the job `AssetGenerateThumbnails`, the `integrity_report` table), so the controller runs it only when the installed image of `after_unclean_boot.service` is in that list; any other image is SKIPPED, with a reason in the box's log and the app record only (controller `internal/stacks/after_unclean_boot.go`). An image bump in the compose that forgets the list therefore turns the repair off on every box that updates — and nobody is told. This gate refuses that push. THE FACT, and the labels that are not it: * The fact is the `image:` line of the service NAMED by `after_unclean_boot.service`, directly under that service's key in the compose's top-level `services:` (the controller keys `installed_images` by the compose service name), and the `images:` list INSIDE the top-level, uncommented `after_unclean_boot:` block. * NOT the fact: the image in a COMMENT, the image of ANOTHER service (immich-machine-learning moves with the server, but the repair never runs in it), a block that is commented out (an older controller would not read it either). * The same rule for each `steps/.felhom.yml` that carries the block, against its own `steps/.yml` (the update ladder: a box pinned to a step reads that step's files). * A block the gate cannot read (no `service:`, no `images:`, the service absent from the compose, a service with no `image:`) is refused too — a list nobody can match is not a proof. stdlib only, a line reader: the catalog CI has no PyYAML. USAGE python3 scripts/check-unclean-boot-images.py # every template directory python3 scripts/check-unclean-boot-images.py immich # only these python3 scripts/check-unclean-boot-images.py --root= # judge another checkout (the decoy suite) (`--all` is accepted and changes nothing.) Exit 0 clean · 1 refused. Decoys: scripts/test_gate_decoys.py `unclean_boot_cases` (COVERS "unclean-boot-images"). """ import io import os import re import sys TOP_BLOCK = re.compile(r"^after_unclean_boot:\s*(#.*)?$") TOP_KEY = re.compile(r"^\S") CHILD = re.compile(r"^(\s+)([A-Za-z_][A-Za-z0-9_]*):\s*(.*?)\s*$") ITEM = re.compile(r'^\s+-\s*(?:"([^"]*)"|\'([^\']*)\'|([^\s#]+))\s*(#.*)?$') FLOW_ITEM = re.compile(r'"([^"]*)"|\'([^\']*)\'|([^,\s\[\]"\']+)') METHOD = "felhom.eu/documentation/audits/immich-repair-2026-10-14/" def strip_comment(v): """A value with its trailing ` # comment` removed (quotes respected).""" out, q = [], None for i, c in enumerate(v): if q: if c == q: q = None elif c in "\"'": q = c elif c == "#" and (i == 0 or v[i - 1].isspace()): break out.append(c) return "".join(out).strip() def unquote(v): v = v.strip() if len(v) >= 2 and v[0] == v[-1] and v[0] in "\"'": return v[1:-1] return v def parse_block(text): """(present, service, images or None) from a .felhom.yml text. Only the top-level, uncommented block counts.""" lines = text.splitlines() for i, ln in enumerate(lines): if not TOP_BLOCK.match(ln): continue body = [] for nxt in lines[i + 1:]: if nxt.strip() and TOP_KEY.match(nxt): break body.append(nxt) indent = None for b in body: if b.strip() and not b.lstrip().startswith("#"): indent = len(b) - len(b.lstrip()) break service, images = None, None for j, b in enumerate(body): m = CHILD.match(b) if not m or len(m.group(1)) != indent: continue key, val = m.group(2), strip_comment(m.group(3)) if key == "service": service = unquote(val) or None elif key == "images": if val.startswith("["): images = [next(g for g in t if g) for t in FLOW_ITEM.findall(val.strip("[]"))] elif val == "": images = [] for nxt in body[j + 1:]: if not nxt.strip() or nxt.lstrip().startswith("#"): continue mi = ITEM.match(nxt) if not mi or len(nxt) - len(nxt.lstrip()) <= indent: break images.append(next(g for g in mi.groups()[:3] if g is not None)) return True, service, images return False, None, None def service_image(text, service): """The `image:` of `services.` in a compose text, or None.""" lines = text.splitlines() in_services, svc_indent, in_svc, key_indent = False, None, False, None for ln in lines: if not ln.strip() or ln.lstrip().startswith("#"): continue ind = len(ln) - len(ln.lstrip()) if ind == 0: in_services = ln.rstrip().split("#")[0].strip() == "services:" in_svc = False continue if not in_services: continue m = CHILD.match(ln) or re.match(r"^(\s+)([A-Za-z0-9_.-]+):\s*(.*?)\s*$", ln) if svc_indent is None: svc_indent = ind if ind == svc_indent: name = unquote(ln.strip().rstrip(":").split(":")[0]) in_svc = name == service key_indent = None continue if in_svc and ind > svc_indent: if key_indent is None: key_indent = ind if ind == key_indent and m and m.group(2) == "image": return unquote(strip_comment(m.group(3))) return None def judge(app, label, felhom_text, compose_text, compose_name): present, service, images = parse_block(felhom_text) if not present: return None, [] fails = [] if not service: fails.append("%s (%s): the after_unclean_boot block names no `service:` — the repair cannot be matched to an " "image" % (app, label)) return True, fails if not images: fails.append("%s (%s): the after_unclean_boot block lists no `images:` — the repair was proven on nothing" % (app, label)) return True, fails if compose_text is None: fails.append("%s (%s): no %s to read the image of %s from" % (app, label, compose_name, service)) return True, fails img = service_image(compose_text, service) if img is None: fails.append("%s (%s): %s has no service %r with an `image:` — the block names a service the app does not " "run" % (app, label, compose_name, service)) elif img not in images: fails.append("%s (%s): the repair after a power cut was proven on %s; the compose now runs %s. Re-prove the " "repair on 9202 (the method in %s), then add the image." % (app, label, ", ".join(images), img, METHOD)) return True, fails def read(path): if not os.path.isfile(path): return None return io.open(path, encoding="utf-8").read() def main(argv): root = os.getcwd() for a in argv: if a.startswith("--root="): root = a.split("=", 1)[1] names = [a for a in argv if not a.startswith("-")] tdir = os.path.join(root, "templates") apps = names or sorted(os.listdir(tdir)) fails, carriers = [], [] for app in apps: d = os.path.join(tdir, app) fy = read(os.path.join(d, ".felhom.yml")) if fy is not None: has, f = judge(app, "template", fy, read(os.path.join(d, "docker-compose.yml")), "docker-compose.yml") if has: carriers.append(app) fails += f sd = os.path.join(d, "steps") if os.path.isdir(sd): for fn in sorted(os.listdir(sd)): if not fn.endswith(".felhom.yml"): continue key = fn[:-len(".felhom.yml")] has, f = judge(app, "steps/" + fn, read(os.path.join(sd, fn)), read(os.path.join(sd, key + ".yml")), "steps/" + key + ".yml") if has: carriers.append("%s/steps/%s" % (app, key)) fails += f for f in fails: print(" REFUSED " + f) if fails: print("unclean-boot-images gate: %d refusal(s)" % len(fails)) return 1 print("unclean-boot-images gate OK: %d after_unclean_boot block(s) on a proven image %s" % (len(carriers), carriers)) return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))