81 lines
3.9 KiB
Python
81 lines
3.9 KiB
Python
"""Point existing shared aliases at the atomic release; retain legacy rollback."""
|
|
from pathlib import Path
|
|
import argparse
|
|
import json
|
|
import os
|
|
import re
|
|
import subprocess
|
|
|
|
LEGACY = '/var/lib/dunerealms-public-rates/'
|
|
CURRENT = '/home/dunerealms/apps/dunerealms/current/dist/shared-chrome/'
|
|
ASSETS = ('portal-navigation.js', 'portal-navigation.css', 'release.json',
|
|
'menu-new-items.json', 'rates-and-charges.html', 'bank-rates.css')
|
|
CONFIGS = ('/etc/nginx/dunerealms-forums.conf',
|
|
'/etc/nginx/sites-enabled/cp.dunerealms.com.conf',
|
|
'/etc/nginx/sites-enabled/dunerealms-community-sites.conf',
|
|
'/etc/nginx/sites-enabled/dunerealms-wiki.conf')
|
|
|
|
def rewrite_paths(text, legacy=False):
|
|
source, destination = (CURRENT, LEGACY) if legacy else (LEGACY, CURRENT)
|
|
pattern = r'(\balias\s+)' + re.escape(source) + '(' + '|'.join(map(re.escape, ASSETS)) + r')(\s*;)'
|
|
return re.sub(pattern, lambda m: m[1]+destination+m[2]+m[3], text)
|
|
|
|
def atomic(path, content, metadata):
|
|
temporary = path.with_name(path.name+'.shared-chrome-new')
|
|
temporary.write_text(content)
|
|
temporary.chmod(metadata.st_mode & 0o777)
|
|
os.chown(temporary, metadata.st_uid, metadata.st_gid)
|
|
os.replace(temporary, path)
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--apply', action='store_true')
|
|
parser.add_argument('--legacy', action='store_true', help='Restore existing legacy aliases')
|
|
args = parser.parse_args()
|
|
root = Path(LEGACY if args.legacy else CURRENT)
|
|
# nginx's global if_not_owner rule rejects the deployment pointer when its
|
|
# owner differs from the release directory. POSIX readability alone passes
|
|
# in that situation, but actual requests fail with HTTP 403.
|
|
if not args.legacy:
|
|
pointer = Path('/home/dunerealms/apps/dunerealms/current')
|
|
if pointer.is_symlink() and pointer.lstat().st_uid != pointer.stat().st_uid:
|
|
raise RuntimeError('Release pointer ownership is incompatible with nginx; retain the existing shared aliases.')
|
|
for asset in ASSETS:
|
|
if not (root/asset).is_file():
|
|
raise RuntimeError('Publish the complete shared-chrome artifact first: '+asset)
|
|
release = json.loads((root/'release.json').read_text())
|
|
if not re.fullmatch(r'\d+(?:\.\d+){3}', release.get('version', '')):
|
|
raise RuntimeError('Invalid published release metadata')
|
|
user = re.search(r'^\s*user\s+([\w-]+)', Path('/etc/nginx/nginx.conf').read_text(), re.M)
|
|
nginx_user = user[1] if user else 'www-data'
|
|
for asset in ASSETS:
|
|
subprocess.run(['runuser','-u',nginx_user,'--','test','-r',str(root/asset)], check=True)
|
|
changes = {}
|
|
for name in CONFIGS:
|
|
path = Path(name).resolve(strict=True)
|
|
if not path.is_relative_to('/etc/nginx'):
|
|
raise RuntimeError('Unexpected nginx configuration target')
|
|
original = path.read_text()
|
|
updated = rewrite_paths(original, args.legacy)
|
|
if updated != original:
|
|
changes[path] = (original, updated, path.stat())
|
|
if not args.apply:
|
|
print(json.dumps({'plan':True,'configurations':len(changes),'assets':len(ASSETS),'legacy':args.legacy}))
|
|
return
|
|
if os.geteuid()!=0:
|
|
raise RuntimeError('Run as the authorised deployment operator')
|
|
try:
|
|
for path, (_, updated, metadata) in changes.items():
|
|
atomic(path, updated, metadata)
|
|
subprocess.run(['nginx','-t'], check=True, capture_output=True)
|
|
subprocess.run(['systemctl','reload','nginx'], check=True)
|
|
except Exception:
|
|
for path, (original, _, metadata) in changes.items():
|
|
atomic(path, original, metadata)
|
|
subprocess.run(['nginx','-t'], check=False, capture_output=True)
|
|
subprocess.run(['systemctl','reload','nginx'], check=False)
|
|
raise
|
|
print(json.dumps({'applied':True,'configurations':len(changes),'assets':len(ASSETS),'legacy':args.legacy}))
|
|
|
|
if __name__=='__main__':
|
|
main()
|

Staff