Expand description
§epochal-sandbox
Run commands, move files and serve local ports in Epochal sandboxes: Linux machines (boxes). Their one mode today is the Time Portal, which holds a box’s clock and network to a past instant, so nothing run in it can see what came after it.
Calls are made by epochal-api, and retried here when a failure may pass on its own: every call is safe to repeat.
§Setup
cargo add epochal-sandbox
cargo add tokio --features macros,rt-multi-thread
export EPOCHAL_API_KEY=... # from https://console.epochal.com/keysRequires Rust 1.88 or later. EPOCHAL_BASE_URL overrides the API address (default
https://api.epochal.com); SandboxBuilder::with_client takes a client you built yourself.
§Example
use epochal_sandbox::chrono::{TimeZone, Utc};
use epochal_sandbox::{Network, Sandbox, Stream};
#[tokio::main]
async fn main() -> Result<(), epochal_sandbox::Error> {
let as_of = Utc.with_ymd_and_hms(2025, 3, 12, 0, 0, 0).unwrap();
let sandbox = Sandbox::time_portal(as_of)
.with_network(Network::TimePortal)
.with_vcpus(1)
.with_memory_gib(2)
.build()
.await?;
let out = sandbox.command(["date", "-u"]).output().await?;
println!("{}", String::from_utf8_lossy(&out.stdout));
sandbox.files().upload_dir("./src", "/home/epochal/src").await?;
let run = sandbox
.command(["python3", "-u", "analyze.py"])
.with_cwd("/home/epochal/src")
.start()
.await?;
while let Some(chunk) = run.next().await? {
if chunk.stream == Stream::Stdout {
print!("{}", String::from_utf8_lossy(&chunk.data));
}
}
sandbox.files().download_dir("/home/epochal/out", "./out").await?;
sandbox.terminate().await?;
Ok(())
}§What’s in it
- Boxes:
Sandbox::time_portal(as_of)sets up a time-portal box, and takeswith_network,with_vcpus,with_memory_gib,with_timeout,with_idle_timeout,with_env,with_label,with_app,with_relay,with_clientandwith_idempotency_key. Every box boots the newest Ubuntu build dated on or beforeas_of, and its package mirrors are held toas_oftoo. An app is a current build, so a box that uses one is not strictly held toas_of. Unset, a box gets no network, 1 vCPU, 2 GiB, a 1 h lifetime and a 10 min idle timeout.buildwaits until the box is live, and terminates one that fails to come up. Callterminatewhen done: a box left running is billed until its timeout. - Commands:
sandbox.command(argv)takeswith_cwd,with_env,with_timeout,with_stdin,with_open_stdinandwith_idempotency_key.outputruns it to its end;startreturns anExecwhosenextyields eachExecFrameof output as it comes. A started command takeswrite_stdinandclose_stdin(afterwith_open_stdin),signalandkill; each input is applied once, in order, even across retries. What awrite_stdinwhose outcome is unknown left unsent goes ahead of the next input; a piece the API refuses is dropped with the rest. Commands run asrootin/home/epochal. - Files:
files()givesstat,upload_file,download_file,upload_diranddownload_dir, which move a tree as one tar. A file moves in chunks, four in flight at once. Uploads carry no host mtimes or owners, and downloads never write outside the target directory. - Serving a box from here: on a box built
with_relay(),serve_http(port, router)answers each connection to the box’s127.0.0.1:<port>with anaxumRouterrunning on your machine, with noDateheader, so the box isn’t told today’s date. The box needs no network for this.Tunnel::stopends it. - Errors:
ErrorisApi(from the API crate),NotLive(the box failed before it was live),CommandFailed,Io,TunnelorUnexpected.
chrono is re-exported, so as_of instants need no extra dependency.
License: Apache-2.0.
Re-exports§
pub use chrono;
Structs§
- Client
- An Epochal API client, from
Client::builder. Each API module is a handle from it, such astime_portal. Cheap to clone; clones share one connection pool. - Command
- A command to run in a box, from
Sandbox::command: options, thenstartto follow its output oroutputto collect it. - Exec
- A started command, from
Command::start. Output is read by offset, so each poll resumes where the last stopped. - Exec
Frame - A piece of a command’s output.
- File
Stat - One path’s metadata.
- Files
- A box’s filesystem, from
Sandbox::files. Paths in the box are absolute. - Output
- A finished command’s output.
- Sandbox
- A live box, from
Sandbox::time_portal. Callterminatewhen done; a box left running ends at its timeout and is billed until then. - Sandbox
Builder - The box to get, from
Sandbox::time_portal. Anything not set takes the API’s default: no network, 1 vCPU, 2 GiB, a 1 h lifetime. - Tunnel
- A served port, from
Sandbox::serve_http. Dropping it kills the relay without waiting;stopwaits.