"""Cross-platform operator commands for the local lab; never delete volumes.""" import argparse import datetime as dt import json from pathlib import Path import subprocess import sys import time import urllib.request ROOT = Path(__file__).resolve().parents[1] def docker(*args, capture=False, env=None): result = subprocess.run(['docker', *args], cwd=ROOT, check=True, text=True, encoding='utf-8', capture_output=capture, env=env) return result.stdout.strip() if capture else None def replicas(): ids = docker('compose', 'ps', '-q', 'api', capture=True).split() if not ids: return [] # Only operational fields; never persist Env or secret values. raw = docker('inspect', '--format', '{{json .Config.Hostname}} {{json .Id}}', *ids, capture=True) return [dict(zip(('instance', 'id'), (json.loads(x) for x in line.split()))) for line in raw.splitlines()] def ready(count=2, base='http://127.0.0.1:8080'): deadline = time.monotonic()+90 observed = set() while time.monotonic() < deadline: try: with urllib.request.urlopen(base+'/health/ready', timeout=2) as response: observed.add(response.headers['X-Instance-Id']) if len(observed) >= count: return except OSError: observed.clear() time.sleep(.2) raise RuntimeError('expected ready replicas did not appear through LB') def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument('action', choices=['start','stop','restart','restore','status','stop-one','rolling']) p.add_argument('--instance') args = p.parse_args() if args.action == 'start': subprocess.run([sys.executable, '-X', 'utf8', 'scripts/bootstrap.py'], cwd=ROOT, check=True) docker('compose', 'up', '-d', '--build', '--wait') ready() elif args.action == 'stop': docker('compose', 'down') elif args.action == 'restart': docker('compose', 'restart', 'db', 'valkey', 'api', 'lb') ready() elif args.action == 'restore': docker('compose', 'up', '-d', '--no-deps', '--scale', 'api=2', 'api') ready() elif args.action == 'status': docker('compose', 'ps', '-a') print(json.dumps(replicas(), indent=2)) ids = docker('compose','ps','-a','-q',capture=True).split() template = '[{{json .Id}},{{json .Name}},{{json .Config.Hostname}},{{json .State.Status}},{{json .State.StartedAt}},{{.HostConfig.NanoCpus}},{{.HostConfig.Memory}},{{json .NetworkSettings.Ports}},{{json .Config.Image}}]' containers = [dict(zip(('id','name','instance','state','started_at','nano_cpus','memory_bytes','ports','image'),json.loads(line))) for line in docker('inspect','--format',template,*ids,capture=True).splitlines()] query = "SELECT json_build_object('users',(SELECT count(*) FROM users),'chats',(SELECT count(*) FROM chats),'messages',(SELECT count(*) FROM messages),'idempotency_keys',(SELECT count(*) FROM message_keys),'database_bytes',pg_database_size(current_database()),'fsync',current_setting('fsync'),'synchronous_commit',current_setting('synchronous_commit'),'full_page_writes',current_setting('full_page_writes'))" counts = json.loads(docker('compose','exec','-T','db','psql','-U','messenger','-d','messenger','-At','-c',query,capture=True)) snapshot = {'timestamp_utc':dt.datetime.now(dt.timezone.utc).isoformat(),'containers':containers,'database':counts} folder = ROOT/'evidence' folder.mkdir(exist_ok=True) (folder/'infrastructure.json').write_text(json.dumps(snapshot,indent=2)+'\n',encoding='utf-8') elif args.action == 'stop-one': selected = [r for r in replicas() if r['instance']==args.instance] if len(selected) != 1: p.error('--instance must match exactly one running API hostname (see status)') docker('stop', '--time', '9', selected[0]['id']) else: original = replicas() if len(original) != 2: p.error('rolling replacement requires exactly two running replicas') for old in original: docker('compose', 'up', '-d', '--no-deps', '--scale', 'api=3', 'api') ready(3) docker('stop', '--time', '9', old['id']) docker('rm', old['id']) ready(2) print(json.dumps({'before':original, 'after':replicas()}, indent=2)) if __name__ == '__main__': main()