#!/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()