Files
dpl/scripts/fuc-xb-test-proxy/deploy.py
T
hellcat e951bd2f40 feat(fuc-xb): add isolated tomtojapan staging reverse proxy
Preserve source authentication and strict upstream TLS. Add only a dedicated white-machine vhost; no shared maps or application changes.
2026-11-04 15:05:36 +08:00

52 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Install only this Git checkout's new staging alias on the registered white host."""
import datetime
import hashlib
import json
import os
import pathlib
import subprocess
def sha(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def main():
if os.geteuid() != 0:
raise SystemExit('Run on the registered white server as root')
source = pathlib.Path(__file__).with_name('nginx.conf').resolve()
root = source.parents[2]
if root != pathlib.Path('/var/www/wwwroot/fuc-xb-test-proxy'):
raise RuntimeError('Wrong deployment checkout')
target = pathlib.Path('/etc/nginx/conf.d/fuc-xb-test-proxy.conf')
if target.exists() or target.is_symlink():
raise RuntimeError('Refuse to overwrite an existing mapping')
cert = pathlib.Path('/etc/white-proxy-origin/tomtojapan.online/fullchain.pem')
subprocess.run(['openssl','x509','-in',str(cert),'-noout','-checkend','604800'], check=True, capture_output=True)
subprocess.run(['nginx','-t'], check=True, capture_output=True)
os.umask(0o077)
stamp = datetime.datetime.now(datetime.UTC).strftime('%Y%m%d-%H%M%S')
backup = pathlib.Path('/root/codex-backups/fuc-xb-test-proxy') / stamp
backup.mkdir(parents=True, mode=0o700)
before = {str(p):sha(p) for p in pathlib.Path('/etc/nginx/conf.d').glob('*.conf') if p.is_file()}
head = subprocess.check_output(['git','-C',str(root),'rev-parse','HEAD'],text=True).strip()
manifest = {'commit':head, 'existing_configs':before, 'new_target':str(target), 'new_source':str(source), 'source_sha256':sha(source), 'previous_mapping':'absent'}
(backup/'manifest.json').write_text(json.dumps(manifest,indent=2))
target.symlink_to(source)
try:
subprocess.run(['nginx','-t'], check=True, capture_output=True)
subprocess.run(['systemctl','reload','nginx'], check=True, capture_output=True)
if any(not pathlib.Path(p).is_file() or sha(pathlib.Path(p)) != h for p,h in before.items()):
raise RuntimeError('An existing configuration changed during deployment')
except BaseException:
if target.is_symlink() and target.resolve() == source:
target.unlink()
subprocess.run(['nginx','-t'], check=True, capture_output=True)
subprocess.run(['systemctl','reload','nginx'], check=True, capture_output=True)
raise
result = {'installed':True,'commit':head,'target':str(target),'backup':str(backup),'existing_config_hashes_unchanged':True,'dns_or_certificate_changed':False}
(backup/'result.json').write_text(json.dumps(result,indent=2))
print(json.dumps(result))
if __name__ == '__main__':
main()