Files
mtusi2026lab2/scripts/restart.py
T

21 lines
861 B
Python

"""Restart all three services without deleting volumes; record actual start times."""
import json
from pathlib import Path
import subprocess
ROOT=Path(__file__).resolve().parents[1]
def command(*args):
return subprocess.check_output(['docker',*args],cwd=ROOT,text=True,encoding='utf-8').strip()
def snapshot():
result={}
for service in ('api','db','cache'):
cid=command('compose','ps','-q',service)
result[service]=json.loads(command('inspect','--format','{{json .State}}',cid))['StartedAt']
return result
before=snapshot()
command('compose','restart','api','db','cache')
after=snapshot()
assert all(before[k]!=after[k] for k in before)
(ROOT/'evidence').mkdir(exist_ok=True)
(ROOT/'evidence/restart-processes.json').write_text(json.dumps({'before':before,'after':after,'volumes_removed':False},indent=2)+'\n',encoding='utf-8')