#!/usr/bin/env python3 """Restore exact gateway files from the last controlled backup; ordinary shell required.""" import json, subprocess, sys from pathlib import Path REMOTE=r''' import hashlib,json,ssl,subprocess,time,urllib.request from pathlib import Path root=Path('/home/gorios-spark-2/lan-voice') backup=Path(record['backup']).resolve() assert backup.is_relative_to((root/'.runtime/backups').resolve()) assert hashlib.sha256((root/'gateway.py').read_bytes()).hexdigest()==record['gateway_sha256'], 'concurrent change: refusing rollback' ctx=ssl.create_default_context(cafile=str(root/'.runtime/gateway-cert.pem')) opener=urllib.request.build_opener(urllib.request.ProxyHandler({}),urllib.request.HTTPSHandler(context=ctx)) def health(): with opener.open('https://192.168.31.61:5011/health',timeout=3) as response: return json.load(response) assert not health().get('busy'), 'busy: no rollback/restart' manifest=json.loads((backup/'manifest.json').read_text()) for name,existed in manifest.items(): assert name in ('gateway.py','face_context.py') path=root/name if existed: tmp=path.with_name(path.name+'.rollback.tmp') tmp.write_bytes((backup/name).read_bytes());tmp.chmod(0o600);tmp.replace(path) elif path.exists(): path.unlink() subprocess.run(['systemctl','--user','restart','lan-voice-gateway.service'],check=True,timeout=30,stdout=subprocess.PIPE,stderr=subprocess.PIPE) for attempt in range(15): try: if health().get('status')=='ok': print(json.dumps({'status':'rolled_back','backup':str(backup),'health':'ok'}));break except Exception: pass time.sleep(1) else: raise RuntimeError('rollback health failed') ''' root=Path(__file__).resolve().parents[1] record=json.loads((root/'logs/voice-investigation/gateway-deploy.json').read_text()) if '--initial' in sys.argv: record['backup']=record.get('initial_backup',record['backup']) payload='record='+repr({k:record[k] for k in ('backup','gateway_sha256')})+'\n'+REMOTE r=subprocess.run(['ssh','-o','BatchMode=yes','-o','ConnectTimeout=5','gorios-spark-2@192.168.31.61','python3 -'],input=payload.encode(),capture_output=True,timeout=90) if r.stdout: result=json.loads(r.stdout);print(json.dumps(result)) (root/'logs/voice-investigation/gateway-rollback.json').write_text(json.dumps(result,indent=2)+'\n') if r.stderr: print('Remote rollback error; no raw content printed.') raise SystemExit(r.returncode)