49 lines
3.9 KiB
Python
49 lines
3.9 KiB
Python
"""Install the reviewed paired GUI/connector batch; never replace native data or Core."""
|
|
import pathlib,zipfile,hashlib,json,subprocess,os,pwd,grp,time,re,sys,io
|
|
mode,archive_path,expected=sys.argv[1:]
|
|
assert mode in ('lobby','worlds') and re.fullmatch('[a-f0-9]{64}',expected)
|
|
package=pathlib.Path(archive_path)
|
|
assert package.resolve().parent==pathlib.Path('/opt/dunerealms/rollouts') and not package.is_symlink()
|
|
assert hashlib.sha256(package.read_bytes()).hexdigest()==expected
|
|
archive=zipfile.ZipFile(package);manifest=json.loads(archive.read('manifest.json'))
|
|
assert set(manifest)=={'DunerealmsGUI.jar','jarvistranslator.jar','gui-pack.yml'}
|
|
assert set(archive.namelist())==set(manifest)|{'manifest.json'} and len(archive.namelist())==4
|
|
for name,digest in manifest.items():assert hashlib.sha256(archive.read(name)).hexdigest()==digest
|
|
for name,version in [('DunerealmsGUI.jar','1.1.7'),('jarvistranslator.jar','0.5.9')]:
|
|
with zipfile.ZipFile(io.BytesIO(archive.read(name))) as jar:
|
|
assert re.search(r'(?m)^version:\s*'+re.escape(version)+r'\s*$',jar.read('plugin.yml').decode())
|
|
uid=pwd.getpwnam('dune').pw_uid;gid=grp.getgrnam('dune').gr_gid
|
|
def run(*args):return subprocess.run(args,check=True,capture_output=True,text=True).stdout
|
|
def log(**data):print(json.dumps(data),flush=True)
|
|
root=pathlib.Path('/opt/dunerealms' if mode=='lobby' else '/opt/dunerealms/servers').resolve()
|
|
worlds=['lobby'] if mode=='lobby' else ['giedi_prime','kaitain','sigma_draconis_iv','arrakis','caladan']
|
|
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() and (plugins/'DuneEss/website-bridge.yml').is_file()
|
|
preserved={p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in plugins.glob('*.jar') if p.name not in ('DunerealmsGUI.jar','jarvistranslator.jar')}
|
|
unit='dune-'+world+'.service';log(server=world,step='stopping');run('systemctl','stop',unit)
|
|
for name,target in [('DunerealmsGUI.jar','DunerealmsGUI.jar'),('jarvistranslator.jar','jarvistranslator.jar'),('gui-pack.yml','DunerealmsGUI/gui-pack.yml')]:
|
|
dest=plugins/target;assert dest.resolve().is_relative_to(plugins.resolve()) and not dest.is_symlink()
|
|
dest.parent.mkdir(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/'Towny/settings/config.yml'
|
|
if cfg.is_file():
|
|
text=cfg.read_text();text,count=re.subn(r"(?m)^(\s*using_economy:)\s*[^\n]*$",r"\1 'false'",text);assert count==1
|
|
cfg.write_text(text);os.chown(cfg,uid,gid)
|
|
properties=base/'server.properties';text=properties.read_text()
|
|
values={'resource-pack':'https\\://www.dunerealms.com/downloads/DuneRealms-GUI-Houses-1.21.8-v8.zip','resource-pack-sha1':'e4612a6ba0a95d1f031298a0d0a666e7fa2f109f','resource-pack-id':'a7b5ab7d-d8d8-5b51-b016-71a5f4c00008'}
|
|
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+'\n'
|
|
properties.write_text(text);os.chown(properties,uid,gid)
|
|
for name,digest in preserved.items():assert hashlib.sha256((plugins/name).read_bytes()).hexdigest()==digest
|
|
run('systemctl','start',unit);log(server=world,step='starting')
|
|
deadline=time.monotonic()+180
|
|
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.9' in latest and 'Enabling DunerealmsGUI v1.1.7' in latest:
|
|
assert not re.search(r'Error occurred while enabling (JarvisTranslator|DunerealmsGUI)|Could not load.*(Jarvis|DunerealmsGUI)',latest)
|
|
break
|
|
else:raise RuntimeError(world+' did not finish startup')
|
|
log(server=world,step='ready',jarvis='0.5.9',gui='1.1.7')
|
|
log(complete=True,servers=worlds)
|

Staff