Something went wrong. Try again.
Instant MicroVMs/Unikernels for macOS and Linux
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124#!/usr/bin/env python3"""An interactive console with the bsdkrun SDK preloaded.
cd sdk/python && uv run console.py
Starts IPython (it ships in the ``dev`` dependency group) and falls back to thestdlib REPL if it isn't installed, so the console works from a bare checkouttoo.
By default the SDK finds the binary the usual way — ``$BSDKRUN_BIN``, then``bsdkrun`` on ``$PATH``, then an in-repo dev build. Point it somewhere elsefor the session with ``--bin``:
uv run console.py --bin ../../target/release/bsdkrun"""
from __future__ import annotations
import argparsefrom typing import Any
import bsdkrunfrom bsdkrun import ( Sandbox, build_create_args, images, networks, reset_binary_cache, resolve_binary, run, run_checked, set_binary_path, spawn, system, volumes,)from bsdkrun.errors import ( BinaryNotFound, BsdkrunError, CommandFailed, SandboxNotFound,)
BANNER = """\bsdkrun {version} — interactive consolebinary: {binary}
Sandbox create / get / list machines images, volumes host-level image and volume operations networks, system global networks; probe, fetch, versions, grow_disk run, run_checked low-level CLI helpers (argv in, RawResult out) ps() shorthand for Sandbox.list(all=True)
sbx = Sandbox.create(os="linux", image="alpine") sbx.exec(["uname", "-a"]).text() sbx.stop()"""
def ps(all: bool = True) -> list[Any]: """Every machine, exited ones included — a shorthand for poking around.""" return Sandbox.list(all=all)
def _binary_line() -> str: try: return resolve_binary() except BinaryNotFound as exc: return f"NOT FOUND ({exc})"
def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--bin", metavar="PATH", help="use this bsdkrun binary instead of the discovered one", ) args = parser.parse_args()
if args.bin: set_binary_path(args.bin)
namespace = { "bsdkrun": bsdkrun, "Sandbox": Sandbox, "images": images, "volumes": volumes, "networks": networks, "system": system, "run": run, "run_checked": run_checked, "spawn": spawn, "build_create_args": build_create_args, "set_binary_path": set_binary_path, "resolve_binary": resolve_binary, "reset_binary_cache": reset_binary_cache, "BsdkrunError": BsdkrunError, "BinaryNotFound": BinaryNotFound, "CommandFailed": CommandFailed, "SandboxNotFound": SandboxNotFound, "ps": ps, }
banner = BANNER.format(version=bsdkrun.__version__, binary=_binary_line())
try: from IPython import start_ipython from traitlets.config import Config except ImportError: import code
code.interact(banner=banner, local=namespace, exitmsg="") return
config = Config() config.TerminalInteractiveShell.banner1 = banner config.TerminalInteractiveShell.confirm_exit = False start_ipython(argv=[], user_ns=namespace, config=config)
if __name__ == "__main__": main()