33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
"""Resolve the requested process alias using each actual container, never a fake header."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import time
|
|
|
|
|
|
def docker(*args):
|
|
return subprocess.check_output(['docker', *args], text=True).strip()
|
|
|
|
|
|
def instance(container):
|
|
code = "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=1).headers['X-Instance-Id'])"
|
|
return docker('exec', container, 'python', '-c', code)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
target = os.environ['TARGET_INSTANCE_ID']
|
|
started = time.monotonic()
|
|
for container in docker('compose', 'ps', '-q', 'api').splitlines():
|
|
if instance(container) == target:
|
|
docker('stop', '--time', '8', container)
|
|
state = json.loads(docker('inspect', '--format', '{{json .State}}', container))
|
|
assert not state['Running'], 'target must remain stopped'
|
|
record = {'instance': target, 'container': container, 'running': state['Running'], 'exit_code': state['ExitCode'], 'seconds': round(time.monotonic() - started, 3)}
|
|
with Path('evidence/stopped-instances.jsonl').open('a') as out:
|
|
out.write(json.dumps(record) + '\n')
|
|
print(json.dumps(record))
|
|
break
|
|
else:
|
|
raise SystemExit('Requested live API instance not found')
|