import os
from boat_sdk import (
ApiClient,
BoatApi,
CommandResponse,
Configuration,
CreateSandboxRequest,
NetworkPolicyRequest,
)
from boat_sdk.boat_helpers import exec_command, stop_and_remove, wait_until_ready
from onecli import OneCLI
config = Configuration(
host="https://boat.dev/api/v1",
access_token=os.environ["BOAT_API_KEY"],
)
with OneCLI() as onecli, ApiClient(config) as client: # OneCLI reads ONECLI_API_KEY
boat = BoatApi(client)
# 1. An agent on your Personal vault. It ends by itself after an hour,
# and leaving the block disconnects it.
with onecli.agents.create(
vault_name="Personal",
provider="boat",
ttl_seconds=3600,
) as agent:
# 2. The sandbox: the agent's settings, and a network to OneCLI only.
created = boat.create(
create_sandbox_request=CreateSandboxRequest(
ttl_seconds=3600,
no_env=True, # none of your boat account's stored secrets
env=agent.env,
network=NetworkPolicyRequest(mode="allow", allow=agent.allow_hosts),
)
)
sandbox_id = created.sandbox.id
def run(line: str) -> str:
"""Run one shell line and return what it printed."""
result = exec_command(boat, sandbox_id, line).actual_instance
if not isinstance(result, CommandResponse) or result.exit_code != 0:
raise RuntimeError(f"failed: {line}")
return result.stdout
try:
wait_until_ready(boat, sandbox_id)
# 3. Once: write the certificate files that agent.env points at.
run(agent.setup_command)
# httpbin.org/bearer answers 401 with no key.
# 200 means the gateway added the Test key.
call = 'curl -s -o /dev/null -w "%{http_code}" https://httpbin.org/bearer'
print(run(call)) # 200
finally:
stop_and_remove(boat, sandbox_id, delete=True)