MC/network/rollout-jarvis-fleet.py
2026-11-27 23:43:05 +00:00

65 lines
4.4 KiB
Python

"""Explicit five-world rollout. Preserve terrain, Core, credentials and native data."""
import pathlib,zipfile,hashlib,json,subprocess,os,pwd,grp,time,socket,re
ROOT=pathlib.Path('/opt/dunerealms/servers').resolve()
WORLDS=['caladan','giedi_prime','kaitain','sigma_draconis_iv','arrakis']
package=pathlib.Path('/opt/dunerealms/rollouts/jarvis-fleet-20261122.zip')
assert hashlib.sha256(package.read_bytes()).hexdigest()=='1c30015eb974303f03907a0d1dd584d746fa11ed41ca1a53e57dd41898023687'
archive=zipfile.ZipFile(package);manifest=json.loads(archive.read('manifest.json'))
assert set(archive.namelist())==set(manifest)|{'manifest.json'}
for name,sha in manifest.items():
assert not pathlib.PurePosixPath(name).is_absolute() and '..' not in pathlib.PurePosixPath(name).parts
assert hashlib.sha256(archive.read(name)).hexdigest()==sha
NEW={}
def plugin_name(data):
import io
with zipfile.ZipFile(io.BytesIO(data)) as jar:
text=jar.read('plugin.yml').decode();return re.search(r'(?m)^name:\s*(.+)$',text).group(1).strip().strip('"\'')
for name in manifest:
if name.endswith('.jar'):NEW[plugin_name(archive.read(name))]=name
OBSOLETE={'AxiomPaper','AzLink','DuneAtelier','DuneBank','DuneClans','DuneHeraldry','DuneItems','DuneMenus','DuneQuests'}
def run(*args):return subprocess.run(args,check=True,capture_output=True,text=True).stdout
def log(**data):print(json.dumps(data),flush=True)
uid=pwd.getpwnam('dune').pw_uid;gid=grp.getgrnam('dune').gr_gid
for world in WORLDS:
base=(ROOT/world).resolve();assert base.parent==ROOT and base.is_dir()
plugins=base/'plugins';assert plugins.is_dir() and not plugins.is_symlink()
assert (plugins/'DuneEss/website-bridge.yml').is_file()
unit='dune-'+world+'.service';log(server=world,step='stopping');run('systemctl','stop',unit)
removed=[]
for jar in plugins.glob('*.jar'):
assert not jar.is_symlink()
try:name=plugin_name(jar.read_bytes())
except (KeyError,AttributeError,zipfile.BadZipFile):continue
if name in OBSOLETE or name in NEW and jar.name!=NEW[name]:jar.unlink();removed.append(jar.name)
# Never copy Towny/MF databases or another world's credentials/receipts.
for name in manifest:
dest=plugins/name;assert dest.resolve().is_relative_to(plugins.resolve())
dest.parent.mkdir(parents=True,exist_ok=True);os.chown(dest.parent,uid,gid);os.chmod(dest.parent,0o755)
tmp=dest.with_name(dest.name+'.jarvis-stage');tmp.write_bytes(archive.read(name));os.chown(tmp,uid,gid);os.chmod(tmp,0o644);os.replace(tmp,dest)
cfg=plugins/'JarvisTranslator/config.yml';cfg.parent.mkdir(exist_ok=True)
# This file contains only connector display/scope settings, never a credential.
if cfg.exists():
text=cfg.read_text()
if re.search(r'(?m)^\s*server-key:',text):text=re.sub(r'(?m)^(\s*server-key:)\s*.*$',r'\1 '+world.replace('_','-'),text)
elif re.search(r'(?m)^native:\s*$',text):text=re.sub(r'(?m)^native:\s*$',lambda _:'native:\n server-key: '+world.replace('_','-'),text)
else:text+='\nnative:\n server-key: '+world.replace('_','-')+'\n'
else:text='native:\n server-key: '+world.replace('_','-')+'\n'
cfg.write_text(text);os.chown(cfg.parent,uid,gid);os.chown(cfg,uid,gid);os.chmod(cfg,0o644)
properties=base/'server.properties';text=properties.read_text()
values={'resource-pack':'https\\://www.dunerealms.com/downloads/DuneRealms-GUI-Houses-1.21.8-v7.zip','resource-pack-sha1':'f3d49666f2c695d90a1849c155b8165714d2693b','resource-pack-id':'a7b5ab7d-d8d8-5b51-b016-71a5f4c00007'}
for key,value in values.items():
if re.search(r'(?m)^'+re.escape(key)+'=',text):text=re.sub(r'(?m)^'+re.escape(key)+'=.*$',lambda _:key+'='+value,text)
else:text+='\n'+key+'='+value
properties.write_text(text);os.chown(properties,uid,gid)
run('systemctl','start',unit);log(server=world,step='starting',removed=removed)
deadline=time.monotonic()+180;ready=False
while time.monotonic()<deadline:
time.sleep(3)
status=run('systemctl','is-active',unit).strip()
latest=(base/'logs/latest.log').read_text(errors='replace')
if 'Done (' in latest and 'Enabling JarvisTranslator v0.5.5' in latest and 'Enabling DunerealmsGUI v1.1.5' in latest:
if re.search(r'Error occurred while enabling (JarvisTranslator|DunerealmsGUI)|Could not load.*(Jarvis|DunerealmsGUI)',latest):raise RuntimeError(world+' failed to enable connector/GUI')
ready=True;break
if not ready:raise RuntimeError(world+' did not finish startup')
log(server=world,step='ready',jarvis='0.5.5',gui='1.1.5')
log(complete=True,servers=WORLDS)