86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
"""Run and save the isolated experiment, plus a small sanitized host manifest."""
|
|
|
|
import argparse
|
|
import json
|
|
import platform
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--output", default="evidence/reconcile-study.json")
|
|
args = parser.parse_args()
|
|
output = ROOT / args.output
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
run = subprocess.run(
|
|
["docker", "compose", "run", "--rm", "--no-deps", "research"],
|
|
cwd=ROOT,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
output.with_suffix(".raw.txt").write_text(run.stdout + run.stderr)
|
|
if run.returncode:
|
|
raise SystemExit(f"Experiment failed; inspect {output.with_suffix('.raw.txt')}")
|
|
marker = next(
|
|
(line[7:] for line in run.stdout.splitlines() if line.startswith("RESULT ")), None
|
|
)
|
|
if marker is None:
|
|
raise SystemExit("Experiment did not emit RESULT")
|
|
result = json.loads(marker)
|
|
result["host_manifest"] = {
|
|
"system": platform.system(),
|
|
"machine": platform.machine(),
|
|
"release": platform.release(),
|
|
"git_revision": subprocess.check_output(
|
|
["git", "rev-parse", "HEAD"], cwd=ROOT, text=True
|
|
).strip(),
|
|
}
|
|
result["host_manifest"]["docker"] = json.loads(
|
|
subprocess.check_output(
|
|
[
|
|
"docker",
|
|
"info",
|
|
"--format",
|
|
'{"version":"{{.ServerVersion}}","cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}',
|
|
],
|
|
text=True,
|
|
)
|
|
)
|
|
containers = subprocess.check_output(
|
|
["docker", "compose", "ps", "-q"], cwd=ROOT, text=True
|
|
).split()
|
|
result["host_manifest"]["containers"] = []
|
|
for container in containers:
|
|
record = json.loads(subprocess.check_output(["docker", "inspect", container], text=True))[0]
|
|
result["host_manifest"]["containers"].append(
|
|
{
|
|
"service": record["Config"]["Labels"].get("com.docker.compose.service"),
|
|
"image": record["Image"],
|
|
"cpu_nano": record["HostConfig"]["NanoCpus"],
|
|
"memory_limit": record["HostConfig"]["Memory"],
|
|
}
|
|
)
|
|
output.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n")
|
|
subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"research/analyze.py",
|
|
str(output),
|
|
"--output",
|
|
str(output.with_suffix(".summary.md")),
|
|
],
|
|
cwd=ROOT,
|
|
check=True,
|
|
)
|
|
print(
|
|
f"Saved {len(result['checks'])} checks and {len(result['scan_cost'])} scan runs to {output}"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|