94 lines
4.0 KiB
Python
94 lines
4.0 KiB
Python
"""Live end-to-end on boat.dev VMs (costs VM time; needs BOAT_API_KEY, see pipewright.boat.yaml).
|
|
|
|
1. run graph 1 (build) with sandboxes on the `boat` host pool: up to 2 VMs are created,
|
|
set up (Docker kvm runtime), and containers are packed onto them by CPU/memory;
|
|
2. run graph 2 (harden) on the same VMs (adopted: `persist: true`);
|
|
3. open a sandbox on the pool and show /dev/kvm inside a container;
|
|
4. let the pool scale down on idle, then delete everything and verify via the API.
|
|
|
|
uv run python examples/hardening/live_boat.py
|
|
"""
|
|
|
|
import asyncio
|
|
import json
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from pipewright import ops
|
|
from pipewright.project import Project
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
PROJECT = HERE / "pipewright.boat.yaml"
|
|
|
|
|
|
def log(msg):
|
|
print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)
|
|
|
|
|
|
def summarize(res):
|
|
f = res["funnel"]["nodes"]
|
|
return {"status": res["status"], "bucket": f.get("bucket", {}).get("out"),
|
|
"validate": f.get("validate", {}).get("events"), "probe": f.get("probe", {}).get("events")}
|
|
|
|
|
|
async def kvm_and_scale_down():
|
|
proj = Project.load(PROJECT)
|
|
pool = proj.host_pool("boat")
|
|
sbp = proj.sandbox_provider("sandbox")
|
|
out = {}
|
|
async with sbp.lease() as sb:
|
|
r = await sb.exec("stat -c '%a %t:%T %F' /dev/kvm && python3 -c \"import os; "
|
|
"fd=os.open('/dev/kvm', os.O_RDWR); print('kvm open ok', fd)\" && nproc && "
|
|
"cat /sys/fs/cgroup/memory.max", timeout=60)
|
|
out["kvm_exec"] = {"host": sb.host.id, "exit": r.exit_code, "stdout": r.stdout.strip()}
|
|
log(f"/dev/kvm in container on {sb.host.id}: {r.stdout.strip()!r} (exit {r.exit_code})")
|
|
log(f"hosts before scale-down: {[h.id for h in pool.hosts.values()]}")
|
|
pool.idle_shutdown, pool.tick, pool.min = 5, 2, 0
|
|
pool._ticker.cancel()
|
|
pool._ticker = asyncio.create_task(pool._tick_loop())
|
|
t = time.time()
|
|
while [h for h in pool.hosts.values() if h.state != "deleted"] and time.time() - t < 180:
|
|
await asyncio.sleep(2)
|
|
out["after_scale_down"] = [h.id for h in pool.hosts.values()]
|
|
out["scale_down_s"] = round(time.time() - t, 1)
|
|
log(f"hosts after idle scale-down: {out['after_scale_down']} ({out['scale_down_s']}s)")
|
|
pool.persist = False
|
|
await proj.aclose()
|
|
await pool.provider.aclose()
|
|
out["pool_stats"] = {k: v for k, v in pool.snapshot().items() if k != "hosts"}
|
|
return out
|
|
|
|
|
|
def main():
|
|
report = {}
|
|
t0 = time.time()
|
|
try:
|
|
log("graph 1 (build) on boat VMs")
|
|
report["build"] = summarize(ops.run_graph(HERE / "graph_build.py", project=PROJECT, on_log=None))
|
|
log(json.dumps(report["build"]))
|
|
log("graph 2 (harden) on the same VMs")
|
|
report["harden"] = summarize(ops.run_graph(HERE / "graph_harden.py", project=PROJECT, on_log=None))
|
|
log(json.dumps(report["harden"]))
|
|
report["kvm"] = asyncio.run(kvm_and_scale_down())
|
|
finally:
|
|
log("cleanup: pw hosts down + orphans")
|
|
report["down"] = ops.hosts("down", project=PROJECT, start=HERE)
|
|
report["orphans"] = ops.hosts("orphans", project=PROJECT, start=HERE)
|
|
report["remaining_remote"] = ops.hosts("list", project=PROJECT, start=HERE)["remote"]
|
|
events = [json.loads(x) for x in (HERE / ".pipewright-boat/hosts/boat/events.jsonl").read_text().splitlines()] \
|
|
if (HERE / ".pipewright-boat/hosts/boat/events.jsonl").exists() else []
|
|
report["host_events"] = events
|
|
created = {e["host"]: e["at"] for e in events if e["event"] == "creating"}
|
|
deleted = {e["host"]: e["at"] for e in events if e["event"] == "deleted"}
|
|
report["vm_hours"] = round(sum((deleted.get(h, time.time()) - c) for h, c in created.items())
|
|
/ 3600, 3)
|
|
report["wall_s"] = round(time.time() - t0, 1)
|
|
print(json.dumps(report, indent=1, default=str))
|
|
if report["remaining_remote"]:
|
|
print("WARNING: VMs remain:", report["remaining_remote"], file=sys.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|