Files
mtusi2026lab2/scripts/restart.py
T
glebossokin ef96551b7d
Lab 2 application / application (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
ЛР 2: PostgreSQL, Redis-сессии и проверки на оценку 4
2026-10-26 23:25:37 +03:00

39 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Restart precisely this lab's API, PostgreSQL and Redis, keeping both volumes."""
import json
from pathlib import Path
import subprocess
ROOT = Path(__file__).resolve().parents[1]
def compose(*args):
return subprocess.check_output(['docker', 'compose', '--env-file', '.env', *args], cwd=ROOT, text=True)
def states():
result = {}
for service in ('api', 'db', 'redis'):
identifier = compose('ps', '-q', service).strip()
data = json.loads(subprocess.check_output(['docker', 'inspect', identifier], text=True))[0]
result[service] = dict(id=identifier, started_at=data['State']['StartedAt'],
mounts=[dict(name=m.get('Name'), destination=m['Destination'])
for m in data['Mounts'] if m['Type'] == 'volume'])
return result
def main():
before = states()
subprocess.run(['docker', 'compose', '--env-file', '.env', 'restart', 'api', 'db', 'redis'],
cwd=ROOT, check=True)
after = states()
for service in before:
assert before[service]['started_at'] != after[service]['started_at'], service + ' did not restart'
assert before[service]['mounts'] == after[service]['mounts'], service + ' changed volumes'
(ROOT / 'evidence' / 'restart.json').write_text(json.dumps(dict(before=before, after=after), indent=2) + '\n')
print('Restarted api/db/redis; volume names unchanged. Evidence: evidence/restart.json')
if __name__ == '__main__':
main()