#!/usr/bin/env python3 """Write or check the production runner's service.env and unit, for scripts/cutover-finish-thin.sh and scripts/upgrade-thin.sh. runner-config.py render --exec PATH KEY=VALUE... runner-config.py check --exec PATH KEY=VALUE... render reads the recorded snapshot (service.env, equipo-runner.service), never the live files, so running it again gives the same result. Each KEY must appear exactly once in service.env; ExecStart, RestartSec and EnvironmentFile (naming /service.env) exactly once in the unit. Values are restricted to plain path, origin and id characters, so nothing needs escaping. RestartPreventExitStatus=3 follows RestartSec. Both files are written to a temporary file, synced and renamed into place, then checked. check refuses unless the live files hold exactly those values. """ import os from pathlib import Path import re import sys SAFE = re.compile(r'[A-Za-z0-9/._:-]{1,512}') ENV, UNIT = 'service.env', 'equipo-runner.service' def fail(msg): print('refused: ' + msg, file=sys.stderr) sys.exit(1) def once(lines, prefix, where): hits = [i for i, line in enumerate(lines) if line.startswith(prefix)] if len(hits) != 1: fail(f'{where} has {len(hits)} {prefix} lines, not one') return hits[0] def args(argv): if len(argv) < 2 or argv[0] != '--exec': fail('usage: --exec PATH KEY=VALUE...') exe, pairs = argv[1], argv[2:] values = {} for pair in pairs: key, _, value = pair.partition('=') if not re.fullmatch(r'EQUIPO_[A-Z_]+', key) or key in values: fail('bad key ' + key) values[key] = value for value in [exe, *values.values()]: if not SAFE.fullmatch(value): fail('unexpected characters in ' + repr(value)) if not values: fail('no keys') return exe, values def rendered(env, unit, root, exe, values): env = env.splitlines() for key, value in values.items(): env[once(env, key + '=', ENV)] = f'{key}="{value}"' unit = [line for line in unit.splitlines() if not line.startswith('RestartPreventExitStatus=')] if unit[once(unit, 'EnvironmentFile=', UNIT)] != f'EnvironmentFile={root}/{ENV}': fail(f'{UNIT} does not read {root}/{ENV}') unit[once(unit, 'ExecStart=', UNIT)] = f'ExecStart={exe} run' unit.insert(once(unit, 'RestartSec=', UNIT) + 1, 'RestartPreventExitStatus=3') return '\n'.join(env) + '\n', '\n'.join(unit) + '\n' def write(path, text): tmp = path.with_name('.' + path.name + '.new') fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) with os.fdopen(fd, 'w') as f: f.write(text) f.flush() os.fsync(f.fileno()) os.replace(tmp, path) dirfd = os.open(path.parent, os.O_RDONLY) try: os.fsync(dirfd) finally: os.close(dirfd) def check(root, exe, values): env = (root / ENV).read_text().splitlines() unit = (root / UNIT).read_text().splitlines() for key, value in values.items(): if env[once(env, key + '=', ENV)] != f'{key}="{value}"': fail(f'{ENV} does not hold {key}={value}') if unit[once(unit, 'ExecStart=', UNIT)] != f'ExecStart={exe} run': fail(f'{UNIT} does not start {exe}') if unit[once(unit, 'RestartSec=', UNIT) + 1:][:1] != ['RestartPreventExitStatus=3']: fail(f'{UNIT} lacks RestartPreventExitStatus=3 after RestartSec') if unit[once(unit, 'EnvironmentFile=', UNIT)] != f'EnvironmentFile={root}/{ENV}': fail(f'{UNIT} does not read {root}/{ENV}') def main(argv): if len(argv) >= 3 and argv[0] == 'render': snap, root = Path(argv[1]), Path(argv[2]) exe, values = args(argv[3:]) env, unit = rendered((snap / ENV).read_text(), (snap / UNIT).read_text(), root, exe, values) write(root / ENV, env) write(root / UNIT, unit) check(root, exe, values) return 0 if len(argv) >= 2 and argv[0] == 'check': root = Path(argv[1]) exe, values = args(argv[2:]) check(root, exe, values) return 0 print(__doc__, file=sys.stderr) return 2 if __name__ == '__main__': sys.exit(main(sys.argv[1:]))