Files
canina/scripts/publishers/e4c9b52eb040/release_helper.py
T

73 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Exact one-file publisher release; called only by narrow CI, no site deployment."""
import argparse
import hashlib
import fcntl
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
TARGET = Path('/opt/data/publishers/e4c9b52eb040/editorial_evidence.py')
OLD = '99539a31b498d2fc922650a9971802e2caa077b6eb165f864f50e2c6c9073792'
def install(source, commit, approval, target=TARGET):
lock = target.parent / '.helper-release.lock'
fd = os.open(lock, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
return _install(source, commit, approval, target)
finally:
os.close(fd)
def _install(source, commit, approval, target):
digest = hashlib.sha256(source).hexdigest()
if not re.fullmatch('[0-9a-f]{40}', commit) or approval != digest:
raise ValueError('Reviewed content digest required')
for p in (target, *target.parents):
if p.is_symlink():
raise ValueError('Symlink refused')
st = target.stat()
if st.st_uid != os.getuid() or st.st_nlink != 1:
raise ValueError('Runtime identity or hardlink mismatch')
current = hashlib.sha256(target.read_bytes()).hexdigest()
if current not in (OLD, digest):
raise ValueError('Unreviewed before-image')
compile(source, str(target), 'exec')
fd, name = tempfile.mkstemp(prefix='.editorial-release-', dir=target.parent)
try:
with os.fdopen(fd, 'wb') as f:
f.write(source)
f.flush()
os.fsync(f.fileno())
os.fchmod(f.fileno(), st.st_mode & 0o777)
if target.stat().st_ino != st.st_ino or hashlib.sha256(target.read_bytes()).hexdigest() != current:
raise ValueError('Concurrent before-image change')
os.replace(name, target)
finally:
if os.path.exists(name):
os.unlink(name)
actual = hashlib.sha256(target.read_bytes()).hexdigest()
if actual != digest:
raise ValueError('Readback mismatch')
return {'commit': commit, 'sha256': actual, 'target': str(target), 'site_deploy': False, 'gateway_restart': False}
def main():
p = argparse.ArgumentParser()
p.add_argument('--sha', required=True)
p.add_argument('--approval', required=True)
args = p.parse_args()
if os.environ.get('GITHUB_ACTIONS') != 'true' or os.environ.get('GITHUB_REF') != 'refs/heads/ops/prod-publisher-helper-20261112':
raise ValueError('Exact CI push route required')
root = Path(__file__).parent
source = (root/'editorial_evidence.py').read_bytes()
package = {'code': Path(__file__).read_text(), 'source': source.decode(), 'commit': args.sha, 'approval': args.approval}
code = "import json,sys; p=json.load(sys.stdin); ns={'__name__':'ci_release'}; exec(compile(p['code'],'<reviewed-ci-release>','exec'),ns); print(json.dumps(ns['install'](p['source'].encode(),p['commit'],p['approval'])))"
result = subprocess.check_output(['docker','exec','--user','10000:10000','-i','canina-hermes','/opt/hermes/.venv/bin/python','-B','-c',code], input=json.dumps(package), text=True, timeout=60)
print(result.strip())
if __name__ == '__main__':
main()