#!/usr/bin/env python3 """Operate only the lab4 Compose project; restart hooks never delete volumes.""" import argparse import json import os from pathlib import Path import subprocess import time ROOT = Path(__file__).resolve().parents[1] def command(*args): return subprocess.check_output(list(args), cwd=ROOT, text=True, encoding='utf-8').strip() def compose(*args): return command('docker', 'compose', '--env-file', '.env', *args) def containers(service=None, all_states=False): ids = compose('ps', '-q', *(['--all'] if all_states else []), *([service] if service else [])).split() if not ids: return [] result = [] for item in json.loads(command('docker', 'inspect', *ids)): result.append(dict(id=item['Id'], name=item['Name'].lstrip('/'), service=item['Config']['Labels']['com.docker.compose.service'], hostname=item['Config']['Hostname'], pid=item['State']['Pid'], started_at=item['State']['StartedAt'], status=item['State']['Status'], health=item['State'].get('Health', {}).get('Status'), image=item['Image'], memory_limit=item['HostConfig']['Memory'], cpu_limit=item['HostConfig']['NanoCpus'] / 1e9, published_ports=item['NetworkSettings']['Ports'], mounts=[dict(name=m.get('Name'), destination=m['Destination']) for m in item['Mounts'] if m['Type'] == 'volume'])) return sorted(result, key=lambda x: x['name']) def save(name, data): folder = ROOT / 'evidence' folder.mkdir(exist_ok=True) (folder / name).write_text(json.dumps(data, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') def restore(): compose('up', '-d', '--no-deps', '--no-recreate', 'api') deadline = time.monotonic() + 45 while time.monotonic() < deadline: items = containers('api') if len(items) == 2 and all(x['health'] == 'healthy' for x in items): time.sleep(3) return time.sleep(.5) raise RuntimeError('Two API replicas did not become healthy') def stop_one(): target = os.environ.get('TARGET_INSTANCE_ID') before = containers('api') assert len(before) >= 2, 'Need two live replicas' selected = [x for x in before if target and target.startswith(x['hostname'] + '-')] assert len(selected) == 1, 'TARGET_INSTANCE_ID must identify one live API process' started = time.monotonic() command('docker', 'stop', '--time', '8', selected[0]['id']) after = containers('api', True) assert next(x for x in after if x['id'] == selected[0]['id'])['status'] == 'exited' save('stop-one.json', dict(target_instance=target, before=before, after=after, hook_seconds=time.monotonic() - started)) print('Selected API stopped; another replica remains running.') def restart(): before = containers() compose('restart', 'api', 'db', 'redis', 's3', 'lb') after = containers() for old in before: new = next(x for x in after if x['id'] == old['id']) assert old['started_at'] != new['started_at'], old['name'] + ' did not restart' assert old['mounts'] == new['mounts'], 'Persistent volumes changed' save('restart.json', dict(before=before, after=after)) print('API, PostgreSQL, Redis, MinIO and LB restarted; same volumes preserved.') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('action', choices=['restart', 'stop-one', 'restore', 'state']) action = parser.parse_args().action if action == 'state': print(json.dumps(containers(all_states=True), indent=2)) else: {'restart': restart, 'stop-one': stop_one, 'restore': restore}[action]()