MC/website/ops/install-user-voice-catalogue.py

34 lines
2.5 KiB
Python

"""Add only the three reviewed player voice nodes to the native permission bridge."""
import argparse,hashlib,json,os,shutil,subprocess,tempfile
from pathlib import Path
TARGET=Path('/opt/dunerealms/server-control/managed-game-permissions.json')
NODES=('voicechat.listen','voicechat.speak','voicechat.groups')
BASE='bcb6f06da0e0a29a600ca577225ac4c4f94910fd086f5ca7b3be3c59ba71646b'
def extend(old,catalogue):
before=json.loads(old);source=json.loads(catalogue);by_name={n['node']:n for n in source['nodes']};have={n['node']:n for n in before['nodes']}
for name in NODES:
item=by_name[name]
if item.get('highRisk') is not False:raise ValueError('Invalid player voice node')
if name in have and have[name]!=item:raise ValueError('Existing voice node differs')
if name not in have:before['nodes'].append(item)
return (json.dumps(before,indent=2)+'\n').encode()
def main():
p=argparse.ArgumentParser(description=__doc__);p.add_argument('--catalogue',required=True);p.add_argument('--apply',action='store_true');a=p.parse_args()
old=TARGET.read_bytes();new=extend(old,Path(a.catalogue).read_bytes());digest=lambda b:hashlib.sha256(b).hexdigest()
if digest(old)!=BASE and json.loads(old)!=json.loads(new):raise ValueError('Native bridge baseline changed')
print(json.dumps({'before':digest(old),'after':digest(new),'nodes':NODES,'changed':json.loads(old)!=json.loads(new)}))
if not a.apply or json.loads(old)==json.loads(new):return
if os.getuid()!=0:raise ValueError('Root required')
backup=Path('/var/lib/dunerealms-operations/user-voice-catalogue-before.json');backup.parent.mkdir(mode=0o700,parents=True,exist_ok=True)
if backup.exists() and backup.read_bytes()!=old:raise ValueError('Prior recovery record differs')
backup.write_bytes(old);backup.chmod(0o600);st=TARGET.stat();temp=TARGET.with_name(TARGET.name+'.voice-new')
try:
temp.write_bytes(new);os.chmod(temp,st.st_mode&0o777);os.chown(temp,st.st_uid,st.st_gid)
if TARGET.read_bytes()!=old:raise ValueError('Native bridge catalogue changed before write')
os.replace(temp,TARGET)
try:subprocess.run(['systemctl','restart','dunerealms-server-control.service'],check=True);subprocess.run(['systemctl','is-active','--quiet','dunerealms-server-control.service'],check=True)
except Exception:
TARGET.write_bytes(old);subprocess.run(['systemctl','restart','dunerealms-server-control.service'],check=False);raise
finally:temp.unlink(missing_ok=True)
print(json.dumps({'applied':True,'voiceAdminGranted':False}))
if __name__=='__main__':main()