#!/usr/bin/env python3 """Sanitized runtime snapshot: real processes, start times, limits and volumes; no environment/secrets.""" import datetime as dt import json from pathlib import Path import subprocess ROOT = Path(__file__).resolve().parents[1] def command(*args): return subprocess.check_output(list(args), cwd=ROOT, text=True, encoding='utf-8') def main(): ids = command('docker', 'compose', 'ps', '-a', '-q').split() containers = json.loads(command('docker', 'inspect', *ids)) result = {'captured_at': dt.datetime.now(dt.timezone.utc).isoformat(), 'containers': []} for item in containers: # Only these selected fields are persisted; Config.Env is deliberately excluded. result['containers'].append(dict( name=item['Name'].lstrip('/'), image=item['Config']['Image'], status=item['State']['Status'], started_at=item['State']['StartedAt'], health=item['State'].get('Health', {}).get('Status'), memory_limit_bytes=item['HostConfig']['Memory'], cpu_limit_nanos=item['HostConfig']['NanoCpus'], volumes=[dict(name=m.get('Name'), destination=m['Destination']) for m in item['Mounts'] if m['Type'] == 'volume'], published_ports=item['NetworkSettings']['Ports'])) text = json.dumps(result, indent=2) (ROOT / 'evidence/stack.json').write_text(text + '\n', encoding='utf-8') print('Saved evidence/stack.json (process start times, versions, limits, volumes and ports)') if __name__ == '__main__': main()