Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ fn timezone_header() -> reqwest::header::HeaderMap {
headers
}

/// The longest `floo sandboxes create` waits: an image's first build, under the API's own limit.
const SANDBOX_CREATE_WAIT: Duration = Duration::from_secs(26 * 60);

impl FlooClient {
pub fn new(config: Option<FlooConfig>) -> Result<Self, FlooApiError> {
let config = config.unwrap_or_else(load_config);
Expand Down Expand Up @@ -1436,6 +1439,69 @@ impl FlooClient {
self.handle_response_value(resp)
}

/// Create a sandbox. Its image builds the first time it is asked for, which
/// can take minutes, so this waits as long as the API does.
pub fn create_sandbox(&self, body: &Value) -> Result<Value, FlooApiError> {
let req = self.client.post(self.url("/v1/orgs/sandboxes")).json(body);
let resp = self.send_waiting(req, SANDBOX_CREATE_WAIT)?;
self.handle_response_value(resp)
}

/// Run a command in a sandbox and wait for it to end, up to its own timeout.
pub fn exec_in_sandbox(
&self,
sandbox_id: &str,
body: &Value,
timeout_secs: u64,
) -> Result<Value, FlooApiError> {
let path = format!("/v1/orgs/sandboxes/{sandbox_id}/exec");
let req = self.client.post(self.url(&path)).json(body);
let resp = self.send_waiting(req, Duration::from_secs(timeout_secs + 60))?;
self.handle_response_value(resp)
}

pub fn list_sandbox_files(&self, sandbox_id: &str, path: &str) -> Result<Value, FlooApiError> {
let url = format!("/v1/orgs/sandboxes/{sandbox_id}/files");
let resp = self.get_with_query(&url, &[("path", path)])?;
self.handle_response_value(resp)
}

pub fn read_sandbox_file(&self, sandbox_id: &str, path: &str) -> Result<Value, FlooApiError> {
let url = format!("/v1/orgs/sandboxes/{sandbox_id}/files/content");
let resp = self.get_with_query(&url, &[("path", path)])?;
self.handle_response_value(resp)
}

pub fn write_sandbox_file(
&self,
sandbox_id: &str,
path: &str,
body: &Value,
) -> Result<(), FlooApiError> {
let url = format!("/v1/orgs/sandboxes/{sandbox_id}/files/content");
let req = self
.client
.put(self.url(&url))
.query(&[("path", path)])
.json(body);
let resp = self.send_waiting(req, Duration::from_secs(120))?;
if !resp.status().is_success() {
self.handle_response_value(resp)?;
}
Ok(())
}

/// Send a request that may take longer than the client's usual 30 s.
fn send_waiting(
&self,
req: reqwest::blocking::RequestBuilder,
wait: Duration,
) -> Result<reqwest::blocking::Response, FlooApiError> {
self.apply_headers(req.timeout(wait))
.send()
.map_err(|e| FlooApiError::new(0, "CONNECTION_ERROR", e.to_string()))
}

pub fn terminate_sandbox(&self, sandbox_id: &str) -> Result<(), FlooApiError> {
let path = format!("/v1/orgs/sandboxes/{sandbox_id}/terminate");
let resp = self.post_json(&path, &serde_json::json!({}))?;
Expand Down
125 changes: 122 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,18 +573,22 @@ read-only + manual trigger; new jobs are added by editing config and deploying."
#[command(subcommand)]
Events(EventsCommands),

/// Find your running sandboxes, read what one printed, tag it, or end it.
/// Create sandboxes, run commands and copy files in them, find them, and end them.
#[command(
subcommand,
after_help = "\
Examples:
floo sandboxes list Every running sandbox in the org
floo sandboxes create --app agents Prints the new sandbox's id
floo sandboxes exec sb-abc123 -- python -c 'print(1 + 1)'
floo sandboxes cp ./app.py sb-abc123:/work/app.py
floo sandboxes cp sb-abc123:/work/out.json ./out.json
floo sandboxes ls sb-abc123 /work
floo sandboxes list --app agents --tag task=build
floo sandboxes logs sb-abc123 What its own command printed last
floo sandboxes tag sb-abc123 task=build user=ada
floo sandboxes terminate sb-abc123 End it (asks you to type its id)

Programs create sandboxes with the floo-sandbox SDK (pip or npm); the CLI never does."
Programs do the same with the floo-sandbox SDK (pip or npm)."
)]
Sandboxes(SandboxesCommands),

Expand Down Expand Up @@ -1836,6 +1840,88 @@ Examples:

#[derive(Subcommand)]
pub enum SandboxesCommands {
/// Create a sandbox and print its ID.
///
/// It is only files until a command runs: a machine attaches for each
/// command and lets go soon after, so an idle sandbox bills no compute.
Create {
/// The app it belongs to, by name. Made if the org has none by that name.
#[arg(short, long)]
app: String,

/// A Dockerfile to build its image from. Default: Debian with Python
/// 3.13, pip, uv and a compiler.
#[arg(long, value_name = "FILE")]
image_file: Option<String>,

/// small (1 vCPU, 512 MiB), medium (2 vCPUs, 2 GiB) or large (4 vCPUs, 4 GiB).
#[arg(long, default_value = "small", value_parser = ["small", "medium", "large"])]
size: String,

/// Seconds it lives before it ends, up to 86400.
#[arg(long, default_value_t = 600)]
timeout: u64,

/// A variable every command in it runs with, as NAME=VALUE. Repeatable.
#[arg(long = "env", value_name = "NAME=VALUE")]
env: Vec<String>,

/// A name unique among the app's running sandboxes.
#[arg(long)]
name: Option<String>,

/// A tag to list it by, as name=value. Repeatable.
#[arg(long = "tag", value_name = "NAME=VALUE")]
tags: Vec<String>,
},

/// Run a command in a sandbox, print its output, and exit with its exit code.
///
/// Put the command after `--`. It runs as given, with no shell: use
/// `sh -c '...'` for pipes and `&&`.
Exec {
/// The sandbox's ID, from `floo sandboxes create` or `list`.
sandbox_id: String,

/// The directory it runs in.
#[arg(short, long)]
workdir: Option<String>,

/// A variable it runs with, as NAME=VALUE. Repeatable.
#[arg(long = "env", value_name = "NAME=VALUE")]
env: Vec<String>,

/// Seconds it may run before it is ended, up to 1500.
#[arg(long, default_value_t = 300)]
timeout: u64,

/// The program and its arguments.
#[arg(last = true, required = true, value_name = "COMMAND")]
command: Vec<String>,
},

/// Copy a file into or out of a sandbox.
///
/// Write the sandbox side as ID:/path. The local side may be `-` for
/// stdin or stdout. Missing directories in the sandbox are made.
Cp {
/// Where to copy from: a local path, `-`, or ID:/path.
source: String,

/// Where to copy to: a local path, `-`, or ID:/path.
destination: String,
},

/// List what a directory in a sandbox holds.
Ls {
/// The sandbox's ID, from `floo sandboxes create` or `list`.
sandbox_id: String,

/// The directory, as an absolute path.
#[arg(default_value = "/")]
path: String,
},

/// List the org's sandboxes that have not finished, newest first.
List {
/// Only this app's sandboxes (name or ID).
Expand Down Expand Up @@ -2811,6 +2897,39 @@ pub fn run() {
},

Commands::Sandboxes(sub) => match sub {
SandboxesCommands::Create {
app,
image_file,
size,
timeout,
env,
name,
tags,
} => commands::sandboxes::create(&commands::sandboxes::CreateArgs {
app: &app,
image_file: image_file.as_deref(),
size: &size,
timeout,
env: &env,
name: name.as_deref(),
tags: &tags,
}),
SandboxesCommands::Exec {
sandbox_id,
workdir,
env,
timeout,
command,
} => {
commands::sandboxes::exec(&sandbox_id, &command, workdir.as_deref(), &env, timeout)
}
SandboxesCommands::Cp {
source,
destination,
} => commands::sandboxes::cp(&source, &destination),
SandboxesCommands::Ls { sandbox_id, path } => {
commands::sandboxes::ls(&sandbox_id, &path)
}
SandboxesCommands::List { app, tags } => {
commands::sandboxes::list(app.as_deref(), &tags)
}
Expand Down
Loading