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
18 changes: 18 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,12 @@ pub enum SandboxesCommands {
#[arg(long)]
name: Option<String>,

/// Seconds its machine stays attached after the last command, 0 to 86400,
/// so the next command skips a boot. They bill as a running machine.
/// Default: floo's, 30.
#[arg(long, value_name = "SECONDS", value_parser = keep_warm_seconds)]
keep_warm: Option<f64>,

/// A tag to list it by, as name=value. Repeatable.
#[arg(long = "tag", value_name = "NAME=VALUE")]
tags: Vec<String>,
Expand Down Expand Up @@ -2137,6 +2143,16 @@ pub enum CronCommands {
/// Agents and `--json` runs update too: they never run `floo update` on their
/// own, so a gate on an interactive terminal left them on old releases. Update
/// notices go to stderr, so stdout and `--json` output stay clean.
/// `--keep-warm`: seconds, 0 to a day, as the API takes them.
fn keep_warm_seconds(given: &str) -> Result<f64, String> {
match given.parse::<f64>() {
Ok(seconds) if (0.0..=86400.0).contains(&seconds) => Ok(seconds),
_ => Err(format!(
"must be a number of seconds, 0 to 86400, got: {given}"
)),
}
}

fn should_check_version(cli: &Cli) -> bool {
if crate::config::is_local_binary() || crate::config::is_dev_binary() {
return false;
Expand Down Expand Up @@ -2904,6 +2920,7 @@ pub fn run() {
timeout,
env,
name,
keep_warm,
tags,
} => commands::sandboxes::create(&commands::sandboxes::CreateArgs {
app: &app,
Expand All @@ -2912,6 +2929,7 @@ pub fn run() {
timeout,
env: &env,
name: name.as_deref(),
keep_warm,
tags: &tags,
}),
SandboxesCommands::Exec {
Expand Down
7 changes: 6 additions & 1 deletion src/commands/sandboxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct CreateArgs<'a> {
pub timeout: u64,
pub env: &'a [String],
pub name: Option<&'a str>,
pub keep_warm: Option<f64>,
pub tags: &'a [String],
}

Expand All @@ -37,7 +38,7 @@ pub fn create(args: &CreateArgs) {
process::exit(1);
})
});
let body = serde_json::json!({
let mut body = serde_json::json!({
"app": args.app,
"image": image,
"size": args.size,
Expand All @@ -46,6 +47,10 @@ pub fn create(args: &CreateArgs) {
"name": args.name,
"tags": pairs(args.tags, "--tag", "task=build"),
});
if let Some(seconds) = args.keep_warm {
// Sent only when given: an API from before it refuses the field, even as null.
body["keep_warm"] = serde_json::json!(seconds);
}
if !output::is_json_mode() {
output::info(
"Creating the sandbox. A new image's first build takes a few minutes.",
Expand Down
26 changes: 25 additions & 1 deletion tests/sandboxes_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ fn create_sends_what_was_asked_and_prints_only_the_id_on_stdout() {
let created = server
.mock("POST", "/v1/orgs/sandboxes")
.match_body(Matcher::PartialJsonString(
r#"{"app":"agents","size":"medium","timeout":900,"env":{"DEBUG":"1"},"tags":{"task":"build"}}"#
r#"{"app":"agents","size":"medium","timeout":900,"env":{"DEBUG":"1"},"tags":{"task":"build"},"keep_warm":5.0}"#
.to_string(),
))
.with_status(201)
Expand All @@ -255,6 +255,8 @@ fn create_sends_what_was_asked_and_prints_only_the_id_on_stdout() {
"DEBUG=1",
"--tag",
"task=build",
"--keep-warm",
"5",
])
.env("HOME", home.path())
.assert()
Expand All @@ -263,6 +265,28 @@ fn create_sends_what_was_asked_and_prints_only_the_id_on_stdout() {
created.assert();
}

#[test]
fn create_refuses_a_keep_warm_past_a_day_before_asking_the_api() {
let mut server = Server::new();
let home = setup_config(&server);
let created = server.mock("POST", "/v1/orgs/sandboxes").expect(0).create();

floo()
.args([
"sandboxes",
"create",
"--app",
"agents",
"--keep-warm",
"86401",
])
.env("HOME", home.path())
.assert()
.failure()
.stderr(predicate::str::contains("0 to 86400"));
created.assert();
}

#[test]
fn exec_prints_each_stream_and_exits_with_the_commands_code() {
let mut server = Server::new();
Expand Down