43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
"""Runnable deterministic demo for VM lifecycle tools."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
from pyro_mcp.api import Pyro
|
|
|
|
INTERNET_PROBE_COMMAND = (
|
|
'python3 -c "import urllib.request; '
|
|
"print(urllib.request.urlopen('https://example.com', timeout=10).status)"
|
|
'"'
|
|
)
|
|
|
|
|
|
def _demo_command(status: dict[str, Any]) -> str:
|
|
if bool(status.get("network_enabled")) and status.get("execution_mode") == "guest_vsock":
|
|
return INTERNET_PROBE_COMMAND
|
|
return "git --version"
|
|
|
|
|
|
def run_demo(*, network: bool = False) -> dict[str, Any]:
|
|
"""Create/start/exec/delete a VM and return command output."""
|
|
pyro = Pyro()
|
|
status = {
|
|
"network_enabled": network,
|
|
"execution_mode": "guest_vsock" if network else "host_compat",
|
|
}
|
|
return pyro.run_in_vm(
|
|
profile="debian-git",
|
|
command=_demo_command(status),
|
|
vcpu_count=1,
|
|
mem_mib=512,
|
|
timeout_seconds=30,
|
|
ttl_seconds=600,
|
|
network=network,
|
|
)
|
|
|
|
|
|
def main() -> None:
|
|
"""Run the deterministic lifecycle demo."""
|
|
print(json.dumps(run_demo(), indent=2, sort_keys=True))
|