forked from unimatrix/web
103 lines
4.3 KiB
Python
103 lines
4.3 KiB
Python
"""Move the verified legacy shared CSS override onto the active built artifact."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
|
|
CURRENT = Path('/home/dunerealms/apps/dunerealms/current')
|
|
TARGET = Path('/etc/nginx/snippets/dunerealms-cookbook-e40883ba.conf')
|
|
LEGACY = '/var/www/dunerealms-cookbook-e40883ba/realm-design.css'
|
|
ROUTE = '''location = /realm-design.css {
|
|
root /home/dunerealms/apps/dunerealms/current/dist/shared-chrome;
|
|
try_files $uri @dunerealms_shared_design_legacy;
|
|
default_type text/css;
|
|
add_header Cache-Control "no-cache" always;
|
|
}
|
|
location @dunerealms_shared_design_legacy {
|
|
root /var/www/dunerealms-cookbook-e40883ba;
|
|
try_files /realm-design.css =404;
|
|
default_type text/css;
|
|
add_header Cache-Control "no-cache" always;
|
|
}'''
|
|
|
|
|
|
def route_content(old):
|
|
matches = list(re.finditer(r'location\s*=\s*/realm-design\.css\s*\{[^{}]*\}', old))
|
|
if len(matches) == 1 and ROUTE in old:
|
|
return old
|
|
if len(matches) != 1 or '@dunerealms_shared_design_legacy' in old:
|
|
raise ValueError('Shared design route is not the verified legacy override')
|
|
match = matches[0]
|
|
if not re.search(r'alias\s+' + re.escape(LEGACY) + r'\s*;', match.group()):
|
|
raise ValueError('Unexpected shared design alias; no changes made')
|
|
return old[:match.start()] + ROUTE + old[match.end():]
|
|
|
|
|
|
def replace_config(target, content, backup, run=subprocess.run):
|
|
before = target.read_bytes()
|
|
if before == content.encode():
|
|
return False
|
|
backup.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
if not backup.exists():
|
|
shutil.copy2(target, backup)
|
|
stat = target.stat()
|
|
temporary = target.with_name(target.name + '.shared-design-new')
|
|
try:
|
|
temporary.write_text(content)
|
|
os.chmod(temporary, stat.st_mode & 0o777)
|
|
if hasattr(os, 'chown'):
|
|
os.chown(temporary, stat.st_uid, stat.st_gid)
|
|
os.replace(temporary, target)
|
|
run(['nginx', '-t'], check=True, capture_output=True)
|
|
run(['systemctl', 'reload', 'nginx'], check=True, capture_output=True)
|
|
except Exception:
|
|
target.write_bytes(before)
|
|
os.chmod(target, stat.st_mode & 0o777)
|
|
run(['nginx', '-t'], check=False, capture_output=True)
|
|
run(['systemctl', 'reload', 'nginx'], check=False, capture_output=True)
|
|
raise
|
|
finally:
|
|
temporary.unlink(missing_ok=True)
|
|
return True
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--expected-release', required=True)
|
|
parser.add_argument('--apply', action='store_true')
|
|
parser.add_argument('--plan-hash')
|
|
args = parser.parse_args()
|
|
if not re.fullmatch('[a-f0-9]{40}', args.expected_release):
|
|
raise ValueError('Exact release commit required')
|
|
release = CURRENT.resolve(strict=True)
|
|
if release.name != 'ci-' + args.expected_release:
|
|
raise ValueError('Active release changed')
|
|
artifact = release / 'dist/shared-chrome/realm-design.css'
|
|
inputs = ['web/realm-design.css', 'scripts/homepage-header.css',
|
|
'scripts/osticket-presentation.css', 'scripts/mybb-presentation.css',
|
|
'scripts/shared-footer.css']
|
|
expected = b''.join((release / name).read_bytes() for name in inputs)
|
|
if artifact.read_bytes() != expected:
|
|
raise ValueError('Built shared CSS differs from reviewed release inputs')
|
|
old = TARGET.read_text()
|
|
new = route_content(old)
|
|
digest = lambda value: hashlib.sha256(value).hexdigest()
|
|
plan = {'release': args.expected_release, 'target': str(TARGET),
|
|
'before': digest(old.encode()), 'after': digest(new.encode()),
|
|
'artifactSHA256': digest(expected), 'rollbackFallback': LEGACY}
|
|
plan_hash = digest(json.dumps(plan, sort_keys=True).encode())
|
|
print(json.dumps({**plan, 'planHash': plan_hash, 'applied': False}))
|
|
if args.apply:
|
|
if os.getuid() != 0 or args.plan_hash != plan_hash:
|
|
raise ValueError('Root and matching reviewed plan hash required')
|
|
backup = Path('/home/dunerealms/backups/shared-design-route-' + args.expected_release) / TARGET.name
|
|
changed = replace_config(TARGET, new, backup)
|
|
print(json.dumps({'applied': True, 'changed': changed, 'backup': str(backup)}))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|

Staff