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
7 changes: 6 additions & 1 deletion src/api/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn checkpoint_path(id: &str) -> String {
pub struct RestoreCheckpoint {
pub timeout_seconds: Option<u32>,
pub auto_pause: Option<bool>,
pub idle_timeout_seconds: Option<u32>,
}

impl RestoreCheckpoint {
Expand All @@ -24,6 +25,9 @@ impl RestoreCheckpoint {
if let Some(auto_pause) = self.auto_pause {
body["autoPause"] = json!(auto_pause);
}
if let Some(idle_timeout) = self.idle_timeout_seconds {
body["idleTimeoutSeconds"] = json!(idle_timeout);
}
body
}
}
Expand Down Expand Up @@ -140,10 +144,11 @@ mod tests {
let request = RestoreCheckpoint {
timeout_seconds: Some(120),
auto_pause: Some(true),
idle_timeout_seconds: Some(60),
};
assert_eq!(
request.body(),
json!({ "timeoutSeconds": 120, "autoPause": true })
json!({ "timeoutSeconds": 120, "autoPause": true, "idleTimeoutSeconds": 60 })
);
}
}
23 changes: 23 additions & 0 deletions src/api/computers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::BTreeMap;

use serde_json::{Value, json};

use crate::api::client::{ApiError, SteelClient};
Expand All @@ -15,6 +17,8 @@ pub struct CreateComputer {
pub memory_mib: Option<u32>,
pub timeout_seconds: Option<u32>,
pub auto_pause: Option<bool>,
pub idle_timeout_seconds: Option<u32>,
pub env: BTreeMap<String, String>,
}

impl CreateComputer {
Expand All @@ -35,6 +39,12 @@ impl CreateComputer {
if let Some(auto_pause) = self.auto_pause {
body["autoPause"] = json!(auto_pause);
}
if let Some(idle_timeout) = self.idle_timeout_seconds {
body["idleTimeoutSeconds"] = json!(idle_timeout);
}
if !self.env.is_empty() {
body["env"] = json!(self.env);
}
body
}
}
Expand Down Expand Up @@ -258,6 +268,8 @@ mod tests {
memory_mib: Some(4096),
timeout_seconds: Some(600),
auto_pause: Some(true),
idle_timeout_seconds: Some(90),
env: BTreeMap::from([("TOKEN".into(), "abc".into())]),
};
assert_eq!(
full.body(),
Expand All @@ -267,10 +279,21 @@ mod tests {
"memoryMib": 4096,
"timeoutSeconds": 600,
"autoPause": true,
"idleTimeoutSeconds": 90,
"env": { "TOKEN": "abc" },
})
);
}

#[test]
fn zero_idle_timeout_is_sent_and_empty_env_is_not() {
let request = CreateComputer {
idle_timeout_seconds: Some(0),
..Default::default()
};
assert_eq!(request.body(), json!({ "idleTimeoutSeconds": 0 }));
}

#[test]
fn computer_paths_escape_ids() {
assert_eq!(
Expand Down
5 changes: 5 additions & 0 deletions src/commands/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub struct RestoreArgs {
#[arg(long = "auto-pause")]
pub auto_pause: bool,

/// Pause after this many seconds without incoming traffic (0 disables it)
#[arg(long = "idle-timeout", value_name = "SECONDS")]
pub idle_timeout_seconds: Option<u32>,

/// Wait until the computer is running
#[arg(long)]
pub wait: bool,
Expand Down Expand Up @@ -117,6 +121,7 @@ async fn run_restore(args: RestoreArgs) -> Result<()> {
let request = RestoreCheckpoint {
timeout_seconds: args.timeout_seconds,
auto_pause: args.auto_pause.then_some(true),
idle_timeout_seconds: args.idle_timeout_seconds,
};
let restored = client
.restore_checkpoint(&base_url, mode, &auth, &args.checkpoint_id, &request)
Expand Down
39 changes: 39 additions & 0 deletions src/commands/computer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod exec;
pub mod ssh;
pub mod wsio;

use std::collections::BTreeMap;
use std::time::{Duration, Instant};

use anyhow::{Context, Result, bail};
Expand Down Expand Up @@ -106,6 +107,14 @@ pub struct CreateArgs {
#[arg(long = "auto-pause")]
pub auto_pause: bool,

/// Pause after this many seconds without incoming traffic (0 disables it)
#[arg(long = "idle-timeout", value_name = "SECONDS")]
pub idle_timeout_seconds: Option<u32>,

/// Environment variable for the computer, repeatable
#[arg(long = "env", value_name = "KEY=VALUE")]
pub env: Vec<String>,

/// Wait until the computer is running
#[arg(long)]
pub wait: bool,
Expand Down Expand Up @@ -203,6 +212,22 @@ pub fn remember_computer(id: Option<&str>) -> Result<()> {
settings::write_config(&config).context("Failed to save the default computer")
}

pub fn parse_env(pairs: &[String]) -> Result<BTreeMap<String, String>> {
let mut env = BTreeMap::new();
for pair in pairs {
let Some((name, value)) = pair.split_once('=') else {
bail!("--env wants KEY=VALUE, got {pair:?}.");
};
if name.is_empty() {
bail!("--env wants a name before the '=', got {pair:?}.");
}
if env.insert(name.to_string(), value.to_string()).is_some() {
bail!("--env {name} was given twice.");
}
}
Ok(env)
}

pub fn status_of(computer: &Value) -> &str {
computer["status"].as_str().unwrap_or("unknown")
}
Expand All @@ -223,6 +248,8 @@ async fn run_create(args: CreateArgs) -> Result<()> {
memory_mib: args.memory_mib,
timeout_seconds: args.timeout_seconds,
auto_pause: args.auto_pause.then_some(true),
idle_timeout_seconds: args.idle_timeout_seconds,
env: parse_env(&args.env)?,
};
let created = client
.create_computer(&base_url, mode, &auth, &request)
Expand Down Expand Up @@ -529,6 +556,18 @@ fn print_computers(data: &Value) {
mod tests {
use super::*;

#[test]
fn parse_env_reads_pairs_and_refuses_bad_ones() {
assert!(parse_env(&[]).unwrap().is_empty());
let parsed = parse_env(&["A=1".into(), "B=x=y".into(), "C=".into()]).unwrap();
assert_eq!(parsed["A"], "1");
assert_eq!(parsed["B"], "x=y");
assert_eq!(parsed["C"], "");
assert!(parse_env(&["NOPE".into()]).is_err());
assert!(parse_env(&["=1".into()]).is_err());
assert!(parse_env(&["A=1".into(), "A=2".into()]).is_err());
}

#[test]
fn explicit_id_wins_and_blank_ids_are_ignored() {
assert_eq!(
Expand Down
46 changes: 46 additions & 0 deletions tests/computer_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,49 @@ async fn restart_calls_restart_and_not_start() {
let text = String::from_utf8_lossy(&output.stdout).to_string();
assert!(text.contains(&format!("{ID} is running.")));
}

#[tokio::test(flavor = "multi_thread")]
async fn create_sends_the_idle_timeout_and_env() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v1/computers"))
.and(wiremock::matchers::body_partial_json(json!({
"idleTimeoutSeconds": 120,
"env": { "A": "1", "TOKEN": "x=y" }
})))
.respond_with(ResponseTemplate::new(201).set_body_json(json!({
"id": ID,
"status": "running"
})))
.expect(1)
.mount(&server)
.await;

let output = run_steel(
&server,
&[
"computer",
"create",
"--idle-timeout",
"120",
"--env",
"A=1",
"--env",
"TOKEN=x=y",
],
)
.await;

assert!(output.status.success());
}

#[tokio::test(flavor = "multi_thread")]
async fn create_refuses_an_env_without_an_equals() {
let server = MockServer::start().await;

let output = run_steel(&server, &["computer", "create", "--env", "NOPE"]).await;

assert!(!output.status.success());
let text = String::from_utf8_lossy(&output.stderr).to_string();
assert!(text.contains("KEY=VALUE"));
}
Loading