#!/usr/bin/env python3 """Restore only exact deployed files, from their own backup; no model/container actions.""" import json,subprocess from pathlib import Path REMOTE=r''' import hashlib,json,ssl,subprocess,time,urllib.request,shutil 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()) for name,expected in record['hashes'].items(): assert (name.endswith('.py') or name in ('personas/lvbu.lorebook.json','personas/yangmi.lorebook.json')) and not Path(name).is_absolute() and '..' not in Path(name).parts assert hashlib.sha256((root/name).read_bytes()).hexdigest()==expected, 'concurrent source 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 r:return json.load(r) assert not health().get('busy'), 'busy: refusing restart' manifest=json.loads((backup/'manifest.json').read_text());assert set(manifest)==set(record['hashes']) metadata=json.loads((backup/'metadata.json').read_text()) assert hashlib.sha256(subprocess.check_output(['systemctl','--user','cat','lan-voice-gateway.service'])).hexdigest()==metadata['unit_sha256'], 'unit changed: refusing rollback' for name,row in metadata['private_files'].items(): assert hashlib.sha256(Path(name).read_bytes()).hexdigest()==row['sha256'], 'private environment changed: refusing rollback' assert not health().get('busy'), 'busy before restore: refusing restart' for name,expected in record['hashes'].items(): assert hashlib.sha256((root/name).read_bytes()).hexdigest()==expected, 'source changed before restore' for name,present in manifest.items(): path=root/name if present: tmp=path.with_name(path.name+'.rollback.tmp');shutil.copy2(backup/name,tmp);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-emotion-deploy.json').read_text()) r=subprocess.run(['ssh','-o','BatchMode=yes','-o','ConnectTimeout=5','gorios-spark-2@192.168.31.61','python3 -'],input=('record='+repr(record)+'\n'+REMOTE).encode(),capture_output=True,timeout=90) if r.stdout: data=json.loads(r.stdout);(root/'logs/voice-investigation/gateway-emotion-rollback.json').write_text(json.dumps(data,indent=2)+'\n');print(json.dumps(data)) if r.stderr:print('Remote rollback stderr present (suppressed).') raise SystemExit(r.returncode)