diff --git a/tools/build-dunemenus-release.py b/tools/build-dunemenus-release.py index 3952cf8e..c2dfd847 100644 --- a/tools/build-dunemenus-release.py +++ b/tools/build-dunemenus-release.py @@ -23,6 +23,8 @@ with zipfile.ZipFile(pack,'w',zipfile.ZIP_DEFLATED,compresslevel=9) as z: for module,name in [('DuneMenus','DuneMenus.jar'),('DuneEss','DuneRealms-Essentials.jar'),('DuneLobby','DuneRealms-Lobby.jar'),('DuneRealms-minecraft-only','DuneRealms-Core.jar'),('JarvisConnector','jarvistranslator.jar')]: shutil.copy2(root/module/'build/libs'/name,out/name) shutil.copy2(root/'tools/install-dunemenus-release.py',out/'install-dunemenus-release.py') +shutil.copy2(root/'network/resource-pack/dunemenus-v7.nginx.conf',out/'dunemenus-v7.nginx.conf') +shutil.copy2(root/'tools/publish-dunemenus-pack.py',out/'publish-dunemenus-pack.py') manifest={'source':subprocess.check_output(['git','rev-parse','HEAD'],cwd=root,text=True).strip(),'input_zip_sha256':'6d3cde343ba09fb7088a3a07b0c54c96eeb629c82c527993a76000013c305b60','pack_url':'https://www.dunerealms.com/downloads/'+pack.name,'files':{}} for p in sorted(out.iterdir()): if p.is_file() and p.name!='release.json': diff --git a/tools/publish-dunemenus-pack.py b/tools/publish-dunemenus-pack.py new file mode 100644 index 00000000..e2b0ebca --- /dev/null +++ b/tools/publish-dunemenus-pack.py @@ -0,0 +1,59 @@ +"""Publish the recorded v7 pack at its exact existing DuneRealms download host.""" +from pathlib import Path +import datetime +import hashlib +import json +import os +import shutil +import subprocess + +bundle = Path(__file__).resolve().parent +manifest = json.loads((bundle / 'release.json').read_text()) +names = ['DuneRealms-Network-Artwork-v7.zip', 'dunemenus-v7.nginx.conf'] +for name in names: + if hashlib.sha256((bundle / name).read_bytes()).hexdigest() != manifest['files'][name]['sha256']: + raise ValueError('Publication checksum differs: ' + name) +downloads = Path('/var/lib/dunerealms-public-downloads') +parent = Path('/etc/nginx/snippets/dunerealms-gui-download-v3.conf') +route = Path('/etc/nginx/snippets/dunemenus-v7.nginx.conf') +pack = downloads / names[0] +if not downloads.is_dir() or not parent.is_file() or any(p.is_symlink() for p in [parent, route, pack]): + raise ValueError('Unexpected publication paths') +include = 'include /etc/nginx/snippets/dunemenus-v7.nginx.conf;' +old_parent = parent.read_bytes() +if include not in old_parent.decode() and names[0] in old_parent.decode(): + raise ValueError('An unmanaged v7 route already exists') +for path, source in [(pack, bundle / names[0]), (route, bundle / names[1])]: + if path.exists() and path.read_bytes() != source.read_bytes(): + raise ValueError('Refusing to overwrite different versioned content: ' + str(path)) +stamp = datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%S%fZ') +backup = Path('/root/dunemenus-pack-before-' + stamp) +backup.mkdir(mode=0o700) +originals = {p: p.read_bytes() if p.exists() else None for p in [pack, route, parent]} +for path, content in originals.items(): + if content is not None: + shutil.copy2(path, backup / path.name) +try: + for destination, source in [(pack, bundle / names[0]), (route, bundle / names[1])]: + staged = destination.with_name(destination.name + '.build-staged') + if staged.exists() or staged.is_symlink(): + raise ValueError('Staging path exists') + shutil.copyfile(source, staged) + os.chmod(staged, 0o644) + os.replace(staged, destination) + if include not in old_parent.decode(): + parent.write_bytes(old_parent.rstrip() + b'\n' + include.encode() + b'\n') + subprocess.run(['nginx', '-t'], check=True, timeout=30) + subprocess.run(['systemctl', 'reload', 'nginx'], check=True, timeout=30) +except Exception: + for path, content in originals.items(): + if content is None: + if path.exists(): + path.unlink() + else: + path.write_bytes(content) + subprocess.run(['nginx', '-t'], check=True, timeout=30) + subprocess.run(['systemctl', 'reload', 'nginx'], check=True, timeout=30) + raise +print(json.dumps({'source': manifest['source'], 'backup': str(backup), 'url': manifest['pack_url'], + 'sha256': manifest['files'][names[0]]['sha256']}))