Files
mtusi2026lab6/scripts/prove-persistence.py
T
2026-11-17 13:08:13 +03:00

23 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""Attach container/volume evidence to the existing public persistence scenario."""
import json,subprocess,time
from pathlib import Path
root=Path(__file__).resolve().parents[1]
compose=[str(root/'scripts/compose.sh')]
def snapshot():
ids=subprocess.check_output(compose+['ps','-q'],text=True).split()
rows=json.loads(subprocess.check_output(['docker','inspect',*ids],text=True))
return {r['Name'].lstrip('/'):{'service':r['Config']['Labels']['com.docker.compose.service'],'started_at':r['State']['StartedAt'],'volumes':sorted(m['Name'] for m in r['Mounts'] if m['Type']=='volume')} for r in rows}
before=snapshot();start=time.time()
p=subprocess.run(['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh'],cwd=root,text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
(root/'evidence/persistence.txt').write_text(p.stdout)
after=snapshot();stores=['postgres','redis','s3','broker','prometheus','loki','tempo','grafana']
checks={}
for service in stores:
old=next(v for v in before.values() if v['service']==service);new=next(v for v in after.values() if v['service']==service)
checks[service]={'restarted':old['started_at']!=new['started_at'],'same_volumes':old['volumes']==new['volumes']}
out={'started':start,'finished':time.time(),'command':['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh'],'returncode':p.returncode,'before':before,'after':after,'checks':checks}
(root/'evidence/persistence-processes.json').write_text(json.dumps(out,indent=2)+'\n')
if p.returncode or not all(v['restarted'] and v['same_volumes'] for v in checks.values()):raise SystemExit('FAIL: persistence or actual store restart evidence')
print('PASS: persistence, real restart of all stores, unchanged volume identities')