MC/website/ops/apply-cp-v2-cutover.py
2026-11-27 23:43:05 +00:00

44 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Cut the public CP entry over to the V2 shell after the matching release is live."""
from pathlib import Path
import datetime,fcntl,re,shutil,subprocess
lock=open('/var/lib/dunerealms-ci-promote/safe-lock','a');fcntl.flock(lock,fcntl.LOCK_EX)
cp=Path('/etc/nginx/sites-enabled/cp.dunerealms.com.conf');preview=Path('/etc/nginx/dunerealms-cpv2-preview.conf')
originals={p:p.read_text() for p in [cp,preview]}
s=originals[cp]
port=re.search(r'proxy_pass\s+http://127\.0\.0\.1:(\d+)',s).group(1)
folder=Path('/var/lib/dunerealms-maintenance/cp-v2-cutover-'+datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ'));folder.mkdir(mode=0o700)
for p in originals:shutil.copy2(p,folder/p.name)
# All private data remains behind the current API; the public shell owns sign-in.
s=s.replace(' auth_request /_cp_v2_owner_check;\n','').replace(' error_page 401 403 = @cp_signin_entry;\n','')
s=re.sub(r'location @cp_signin_entry \{[^}]*\}\n','',s)
text=re.sub(r'location = /_cp_v2_owner_check \{[^}]*\}\n','',originals[preview]).replace(' auth_request /_cp_v2_owner_check;\n','').replace('# Existing V1 owner authorization remains authoritative via the loopback backend.','# V2 public shell; private account data is authenticated by the API.')
assert '_cp_v2_owner_check' not in s+text
policy=re.search(r'add_header Content-Security-Policy ("[^"\n]+") always;',text).group(1)
root=re.search(r'location = / \{[^}]*\}',s).group(0)
if 'Content-Security-Policy' not in root:s=s.replace(root,root[:-1]+' add_header Content-Security-Policy '+policy+' always;\n}',1)
for path,limit in [('/api/account/avatar','3m'),('/api/internal-mail/messages/send','32m')]:
if 'location = '+path+' {' not in s:
block=''' location = PATH {
client_max_body_size LIMIT;
proxy_pass http://127.0.0.1:PORT;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_hide_header X-Frame-Options;
proxy_read_timeout 120s;
}
'''.replace('PATH',path).replace('LIMIT',limit).replace('PORT',port)
s=s.replace(' location = /api/ban-evidence {',block+' location = /api/ban-evidence {',1)
# Preserve the existing private integration API address without a legacy Node process.
if 'listen 127.0.0.1:3003;' not in s:
s+='\nserver {\n listen 127.0.0.1:3003;\n server_name cp-internal.dunerealms.invalid;\n client_max_body_size 32m;\n location / {\n proxy_pass http://127.0.0.1:'+port+';\n proxy_http_version 1.1;\n proxy_set_header Host cp.dunerealms.com;\n proxy_set_header X-Forwarded-Proto https;\n proxy_set_header X-Real-IP $remote_addr;\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n proxy_read_timeout 120s;\n }\n}\n'
cp.write_text(s);preview.write_text(text)
try:
subprocess.run(['nginx','-t'],check=True,capture_output=True);subprocess.run(['nginx','-s','reload'],check=True,capture_output=True)
except Exception:
for p,content in originals.items():p.write_text(content)
subprocess.run(['nginx','-t'],check=True,capture_output=True);subprocess.run(['nginx','-s','reload'],check=True,capture_output=True);raise
print('V2 public entry enabled; rollback:',folder)