Rust

Orchestration

Container orchestration on Docker, through the Docker CLI or the Engine API on its Unix socket: images, containers, executions, networks and usage statistics.
Rust 2,095 lines (1,387 code)split
PartCodeCommentsBlankTotalFiles
Rust source1,3875381702,0957

Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.

Install#

The crate lives in the Appwrite repository; depend on it from Git.

Cargo.toml
[dependencies]
utopia-orchestration = { git = "https://github.com/appwrite/appwrite" }

Getting started#

Common tasks with Orchestration, each a complete program you can copy and run.

Getting started

utopia-orchestration runs containers on Docker, through the docker command-line client (DockerCli) or the Engine API on Docker's Unix socket (DockerApi). Both offer the same operations behind the Adapter trait, and Orchestration fronts either one with the settings every container gets.

Run a container with limits and labels

Set the label namespace and limits once, then run containers with RunOptions; every container gets <namespace>-type and <namespace>-created labels besides yours.

Rust
use utopia_orchestration::{DockerCli, Orchestration, Restart, RunOptions};

let mut orchestration = Orchestration::new(DockerCli::new());
orchestration.set_namespace("executor").set_cpus(0.5).set_memory(256);

let options = RunOptions {
    command: vec!["sh".into(), "-c".into(), "tail -f /dev/null".into()],
    workdir: "/usr/local/src".into(),
    vars: vec![("APP_ENV".into(), "production".into())],
    labels: vec![("function".into(), "thumbnails".into())],
    restart: Restart::UnlessStopped,
    ..RunOptions::default()
};
let id = orchestration.run("alpine:3.20", "thumbnails-1", &options).await?;
println!("started {id}");
Compiles in CI; needs a running service to execute.

Execute a command and read its output

execute sets the output whether or not the command succeeds, and fails with Error::Timeout (API) or Error::Orchestration when it runs out of time or exits with an error.

Rust
use utopia_orchestration::{DockerApi, Error, Orchestration};

let orchestration = Orchestration::new(DockerApi::new());
let mut output = Vec::new();
match orchestration.execute("thumbnails-1", &["sh".into(), "-c".into(), "echo -n ready".into()], &mut output, &[], 10).await {
    Ok(_) => assert_eq!(output, b"ready"),
    Err(e) => eprintln!("{e}: {}", String::from_utf8_lossy(&output)),
}
Compiles in CI; needs a running service to execute.

Split a command line

parse_command splits a command at spaces, keeping a '-quoted script whole so it can follow sh -c.

Rust
use utopia_orchestration::parse_command;

let args = parse_command(b"sh -c 'cp /tmp/code.tar.gz . && tar -zxf code.tar.gz'")?;
assert_eq!(args.len(), 3);
assert_eq!(args[2], b"'cp /tmp/code.tar.gz . && tar -zxf code.tar.gz'");

Find containers and their usage

List containers by Docker filter, and read the usage of the ones they select.

Rust
use utopia_orchestration::{DockerCli, Orchestration};

let orchestration = Orchestration::new(DockerCli::new());
let filters = [("label".to_owned(), "function=thumbnails".to_owned())];
for container in orchestration.list(&filters).await? {
    println!("{} {}", container.name, container.status);
}
for stats in orchestration.stats(None, &filters).await? {
    println!("{}: {:.1}% CPU, {:.1}% memory", stats.container_name, stats.cpu_usage * 100.0, stats.memory_usage);
}
Compiles in CI; needs a running service to execute.

Reference#

Container orchestration: pull images, run containers with limits, labels, volumes and networks, execute commands in them, read their usage, and remove them, on Docker through its command-line client (DockerCli) or its Engine API (DockerApi).

Orchestration fronts an Adapter and holds the Settings every container gets: a label namespace and CPU, memory and swap limits.

Rust
use utopia_orchestration::{DockerApi, Orchestration, RunOptions};

let mut orchestration = Orchestration::new(DockerApi::new());
orchestration.set_namespace("executor").set_memory(256);

let options = RunOptions {
    command: vec!["sh".into(), "-c".into(), "tail -f /dev/null".into()],
    labels: vec![("function".into(), "resize-images".into())],
    ..RunOptions::default()
};
orchestration.run("alpine:3.20", "runtime-1", &options).await?;
let mut output = Vec::new();
orchestration.execute("runtime-1", &["uname".into()], &mut output, &[], 10).await?;
orchestration.remove("runtime-1", true).await?;
Compiles in CI; needs a running service to execute.

Modules#

Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.

API#

What utopia_orchestration exports at its root.