#!/usr/bin/env python3 """Restart all lab2 services, preserving named volumes; safe evidence only.""" from pathlib import Path import subprocess import sys ROOT=Path(__file__).resolve().parents[1] EVIDENCE=ROOT/'evidence' def run(*args, timeout=75): return subprocess.run(args,cwd=ROOT,check=True,capture_output=True, encoding='utf-8',errors='replace',timeout=timeout).stdout def snapshot(name): ids=run('docker','compose','ps','-a','-q').split() if len(ids)!=3: raise RuntimeError('Expected three lab2 containers before/after restart') data=run('docker','inspect','--format','{{.Name}} {{.State.StartedAt}} {{.State.Status}}',*ids) (EVIDENCE/name).write_text(data,encoding='utf-8') def main(): EVIDENCE.mkdir(exist_ok=True) snapshot('restart-before.txt') run('docker','compose','stop','--timeout','5','api','postgres','redis',timeout=25) run('docker','compose','up','-d','--wait','--wait-timeout','60',timeout=70) snapshot('restart-after.txt') print('Restarted API, PostgreSQL and Redis; named volumes preserved.') if __name__=='__main__': try: main() except Exception as ex: EVIDENCE.mkdir(exist_ok=True) # Do not dump Docker configuration, environment or secret values. message='Restart failed: '+type(ex).__name__+'; inspect docker compose ps and local service logs.' (EVIDENCE/'restart-error.txt').write_text(message+'\n',encoding='utf-8') print(message,file=sys.stderr) sys.exit(1)