"""Administrator-only diagnostic; never deploys a pricing environment.""" import json import subprocess import time from datetime import datetime, timezone IMAGE = 'git.yebilly.me/ssw590-group1/pricing@sha256:63e2e3d49a5119b1064868b0b15b39a1719a60a632ff0ecdc3f676fcdc55de3b' NAME = 'ssw590-pricing-health-trial' def command(*args): return subprocess.check_output(args, text=True).strip() def health(container): return json.loads(command('docker', 'inspect', '--format', '{{json .State.Health}}', container)) def now(): return datetime.now(timezone.utc).isoformat() existing = subprocess.run(['docker','container','inspect',NAME],capture_output=True) assert existing.returncode != 0, 'Existing diagnostic container must be reviewed' report = {'started':now(), 'image':IMAGE, 'existing_staging_health':health('pricing-staging-app-1'), 'existing_staging_memory_events':command('docker','exec','pricing-staging-app-1','cat','/sys/fs/cgroup/memory.events'), 'candidate_timeout_seconds':10, 'samples':[]} container = command('docker','run','-d','--pull','never','--name',NAME, '--network','none','--read-only','--tmpfs','/tmp:size=16m,mode=1777', '--cap-drop','ALL','--security-opt','no-new-privileges:true','--user','10001:10001', '--memory','64m','--memory-swap','96m','--cpus','0.25','--pids-limit','64', '--health-timeout','10s',IMAGE) assert len(container) == 64 and all(c in '0123456789abcdef' for c in container) seen = set() try: deadline = time.monotonic() + 170 while time.monotonic() < deadline: state = health(container) for sample in state.get('Log',[]): if sample['Start'] not in seen: seen.add(sample['Start']) report['samples'].append(sample) if len(report['samples']) >= 8: break time.sleep(5) report['final_health'] = health(container) report['memory_events'] = command('docker','exec',container,'cat','/sys/fs/cgroup/memory.events') report['cpu_stat'] = command('docker','exec',container,'cat','/sys/fs/cgroup/cpu.stat') report['passed'] = len(report['samples']) >= 8 and all(s['ExitCode'] == 0 for s in report['samples']) and report['final_health']['Status'] == 'healthy' finally: subprocess.run(['docker','rm','-f',container],check=True,capture_output=True) report['completed'] = now() report['temporary_container_removed'] = True print(json.dumps(report,indent=2)) assert report['passed'], 'Health trial did not pass'