Building an app on a package
Make a sandbox of a package from your app, as your user, run commands, move files and forward ports in it as them, and come back to it later.
This page is your app’s side of a package. Your app runs on your user’s machine, or wherever you run it, and makes a sandbox of the package as the user, as the package’s access lets them in. It then uses that sandbox as the user too, with the same token: it reaches the sandboxes the user made of the package, and nothing else.
AppClient does both: it makes the sandbox, and from then on sends the user’s token with every request, naming the package’s project.
The sandbox stays until it is removed, so the user can come back to it later, such as to a conversation they left.
Before you start
You need:
- The server’s URL, such as
https://virtx.example. - The package’s name, and its project’s
id, whichget_projectanswers. An app names the project by itsid, since its users are in no project and name none of theirs by name. - What the package lets in: a token of the issuer its access names, such as the ID token your app got when the user signed in.
Make a sandbox and use it
AppClient.create makes a sandbox of the package and answers once it is made.
Its sandbox is a sandbox like any other, reached as the user:
import asyncio
from virtx import AppClient, BearerAuth
async def main(id_token: str) -> None: async with await AppClient.create( "https://virtx.example", "<project id>", "workspace", BearerAuth(id_token) ) as app: await app.sandbox.exec(["uname", "-a"]).complete(print=True) await app.sandbox.write_file("notes.txt", "hello\n")const { AppClient } = require('@brekkylab/virtx')
async function main(idToken) { const app = await AppClient.create('https://virtx.example', '<project id>', 'workspace', { type: 'bearer', token: idToken, }) try { await app.sandbox.exec(['uname', '-a']).complete({ print: true }) await app.sandbox.writeFile('notes.txt', 'hello\n') } finally { await app.close() }}use virtx::{AppClient, Auth, SandboxExecArgs};
async fn run(id_token: String) -> Result<(), virtx::Error> { let app = AppClient::create( "https://virtx.example", "<project id>", "workspace", Auth::Bearer { token: id_token }, [], ) .await?; let result = app .sandbox() .exec(&SandboxExecArgs::new(["uname", "-a"])) .complete(true) .await; app.close().await?; result?; Ok(())}The user’s token made the sandbox, and the client sends it with every request after: the server lets each in as the user, by the sandbox’s grant.
Tokens expire, often within the hour, so give the client one it can renew where your app keeps the user signed in for longer: a CommandAuth runs a command for a fresh token as one is needed.
Through sandbox, the app does what Running commands, Reading and writing files and Networks and ports show.
It may also resize the sandbox, up to the package’s max.
Anything else, such as another sandbox, an image or a volume, is refused with 403.
Reach a port in the sandbox
The ports in the sandbox are the package’s. Forward one as any sandbox’s:
from virtx import PortForwarder
await app.sandbox.exec(["httpd", "-f", "-p", "8000"]) # started, left runningwith await PortForwarder.open(app.sandbox, 8000) as forwarder: print(f"http://{forwarder.local}")const { PortForwarder } = require('@brekkylab/virtx')
await app.sandbox.exec(['httpd', '-f', '-p', '8000']) // started, left runningconst forwarder = await PortForwarder.open(app.sandbox, 8000)console.log(`http://${forwarder.local}`)forwarder.close()use virtx::{PortForwarder, PortSide};
app.sandbox().exec(&SandboxExecArgs::new(["httpd", "-f", "-p", "8000"])).await?; // started, left runninglet forwarder = PortForwarder::open(app.sandbox(), 8000, PortSide::Sandbox, None).await?;println!("http://{}", forwarder.local());drop(forwarder);Give the sandbox a port on your user’s machine
The user’s machine is theirs to offer, so ports on local are asked for as the sandbox is made, not by the package.
A process in the sandbox then reaches the port at 10.0.2.2, while the app forwards it, as A port on local describes.
Here the sandbox reaches the user’s browser at its debugging port, 9222, as in Driving your browser from a sandbox:
from virtx import AppClient, BearerAuth, PortForwarder, PortSpec
async with await AppClient.create( "https://virtx.example", "<project id>", "workspace", BearerAuth(id_token), ports=[PortSpec(9222, on="local")],) as app: async with await PortForwarder.open(app.sandbox, 9222, on="local"): await app.sandbox.exec(["wget", "-qO-", "http://10.0.2.2:9222/json/version"]).complete(print=True)const { AppClient, PortForwarder } = require('@brekkylab/virtx')
const auth = { type: 'bearer', token: idToken }const app = await AppClient.create('https://virtx.example', '<project id>', 'workspace', auth, { ports: [{ port: 9222, on: 'local' }],})const forwarder = await PortForwarder.open(app.sandbox, 9222, { on: 'local' })await app.sandbox.exec(['wget', '-qO-', 'http://10.0.2.2:9222/json/version']).complete({ print: true })forwarder.close()await app.close()use virtx::{AppClient, Auth, PortForwarder, PortSide, PortSpec, SandboxExecArgs};
let app = AppClient::create( "https://virtx.example", "<project id>", "workspace", Auth::Bearer { token: id_token }, [PortSpec::new(9222).on(PortSide::Local)],).await?;let forwarder = PortForwarder::open(app.sandbox(), 9222, PortSide::Local, None).await?;let fetch = ["wget", "-qO-", "http://10.0.2.2:9222/json/version"];app.sandbox().exec(&SandboxExecArgs::new(fetch)).complete(true).await?;drop(forwarder);app.close().await?;A port in the sandbox given here is refused with 400: those are the package’s to give.
Come back to the sandbox
close closes the client and leaves the sandbox as it is, as async with in Python, await using in Node.js and a dropped AppClient in Rust do too.
The sandbox’s machine stops while nothing uses it, and starts again when it is next reached, with its files as they were.
To come back to it, keep its id beside what it is for, such as the user’s conversation, and pass it to AppClient.open with the user’s token:
async with await AppClient.create(url, project_id, "workspace", BearerAuth(id_token)) as app: conversation.sandbox = app.sandbox.id
# Later, as the user comes back.async with await AppClient.open(url, project_id, BearerAuth(id_token), conversation.sandbox) as app: await app.sandbox.exec(["ls"]).complete(print=True)const app = await AppClient.create(url, projectId, 'workspace', { type: 'bearer', token: idToken })conversation.sandbox = app.sandbox.idawait app.close()
// Later, as the user comes back.const again = await AppClient.open(url, projectId, { type: 'bearer', token: idToken }, conversation.sandbox)let app = AppClient::create(url, project_id, "workspace", Auth::Bearer { token: id_token.clone() }, []).await?;conversation.sandbox = app.sandbox().id().to_owned();app.close().await?;
// Later, as the user comes back.let again = AppClient::open(url, project_id, Auth::Bearer { token: id_token }, &conversation.sandbox).await?;The id is no secret: only the user who made the sandbox reaches it, with their own token.
To find their sandboxes without a kept id, Client.list_package_sandbox answers those the user made of the package:
client = Client.connect(url, BearerAuth(id_token), project_id)for sandbox in await client.list_package_sandbox("workspace"): print(sandbox.id)Each finds the user’s own sandboxes alone, those made with the same issuer and subject.
Remove the sandbox
remove deletes the sandbox, and closes the client.
Remove it once its user is done with it, such as when they delete the conversation.
Until it is removed, a sandbox keeps its place in the package’s limits, and the project keeps paying for its disk.
A member of the project finds every sandbox made of the package with Package.list_sandbox, and removes those no one comes back to.
Troubleshooting
-
401or403, fromcreateCause: The package does not let this user in: no token, an expired one, or one its
accessdoes not take.Fix: See Letting your users in.
-
404,not_found, fromcreateCause: The project has no package of that name, or no project has that id.
Fix: Check the project’s id and the name the package was kept under with
put_package. -
429,quota, fromcreateCause: As many sandboxes of the package live as its
limitsallow, all of them or this user’s.Fix: Remove the sandboxes the app no longer uses.
-
401or403, fromopenor throughsandboxCause: A request the sandbox’s grant does not reach: a sandbox the user did not make, one removed, a resize past the package’s
max, or a token of another user or one expired.Fix: Pick one
list_package_sandboxanswers, keep to the size the package allows, and send a fresh token of the user’s. For one removed, make a new sandbox withcreate.
Next
- Packages in the SDK reference:
AppClientand every call on packages. - Driving your browser from a sandbox: what a sandbox can do with a port on the user’s machine.