"""Apply only the prepared Assignment 8 proxy additions, preserving the bind inode.""" import argparse import hashlib import json import os import shutil import socket import subprocess from datetime import datetime, timezone from pathlib import Path parser = argparse.ArgumentParser() parser.add_argument('--candidate', required=True, type=Path) parser.add_argument('--backup', required=True, type=Path) parser.add_argument('--apply', action='store_true') args = parser.parse_args() live = Path('/root/gitea/Caddyfile') marker = '# Assignment 8: apply after both DNS names resolve to The-Ocean.' candidate = args.candidate.read_text() before = live.read_bytes() prefix, additions = candidate.split(marker, 1) expected = '''# Assignment 8: apply after both DNS names resolve to The-Ocean. Containers # publish no host ports; the existing proxy reaches their private aliases. staging.yebilly.me { \treverse_proxy pricing-staging:8000 } app.yebilly.me { \treverse_proxy pricing-production:8000 }''' assert (marker + additions).strip() == expected, 'Unexpected proxy additions' assert prefix.strip() == before.decode().replace('\r\n', '\n').strip(), 'Live earlier routes differ; inspect instead of overwriting' for host in ('staging.yebilly.me', 'app.yebilly.me'): addresses = {item[4][0] for item in socket.getaddrinfo(host, 443, socket.AF_INET)} assert addresses == {'147.182.178.136'}, (host, addresses) temp = '/tmp/ssw590-assignment8-https.Caddyfile' subprocess.run(['docker', 'cp', str(args.candidate), 'gitea-caddy-1:' + temp], check=True) try: subprocess.run(['docker', 'exec', 'gitea-caddy-1', 'caddy', 'validate', '--config', temp, '--adapter', 'caddyfile'], check=True) if not args.apply: print('Candidate validated; no active configuration changed.') raise SystemExit(0) args.backup.mkdir(mode=0o700, parents=True, exist_ok=True) backup = args.backup / 'Caddyfile.before' assert not backup.exists(), 'Existing backup: inspect state before retrying' shutil.copy2(live, backup) prior_stat = live.stat() try: with live.open('wb') as stream: stream.write(candidate.encode()) stream.flush() os.fsync(stream.fileno()) assert live.stat().st_ino == prior_stat.st_ino, 'Bind-mounted inode changed' subprocess.run(['docker', 'exec', 'gitea-caddy-1', 'caddy', 'reload', '--config', '/etc/caddy/Caddyfile', '--adapter', 'caddyfile'], check=True) except Exception: with live.open('wb') as stream: stream.write(before) stream.flush() os.fsync(stream.fileno()) subprocess.run(['docker', 'exec', 'gitea-caddy-1', 'caddy', 'reload', '--config', '/etc/caddy/Caddyfile', '--adapter', 'caddyfile'], check=True) raise report = {'applied_at': datetime.now(timezone.utc).isoformat(), 'before_sha256': hashlib.sha256(before).hexdigest(), 'after_sha256': hashlib.sha256(live.read_bytes()).hexdigest(), 'inode_preserved': live.stat().st_ino == prior_stat.st_ino, 'prior_routes_preserved': True, 'applications_deployed': False, 'new_hosts': ['staging.yebilly.me', 'app.yebilly.me'], 'backup': str(backup), 'result': 'reloaded; certificate verification pending'} (args.backup / 'applied.json').write_text(json.dumps(report, indent=2)) print(json.dumps(report, indent=2)) finally: subprocess.run(['docker', 'exec', 'gitea-caddy-1', 'rm', '-f', temp], check=True)