From c3fca2f891372827e37d8aae07bd2223bb3a3327 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Thu, 9 Jun 2022 15:42:37 -0400 Subject: state: Manipulate VM state Closes #1 --- src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index d55df8e..83d572c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,9 @@ enum Action { Stop { /// The UUID of the machine to stop uuid: String, + + #[clap(long, short)] + force: bool }, Start { /// The UUID of the machine to start @@ -33,6 +36,12 @@ enum Action { /// The UUID of the machine to pause uuid: String, }, + Reboot { + uuid: String, + + #[clap(long, short)] + force: bool + } } fn main() { @@ -46,14 +55,17 @@ fn main() { Action::List => { list(args, client).unwrap(); }, - Action::Stop { uuid } => { - stop(root, client, uuid).unwrap(); + Action::Stop { uuid, force } => { + stop(root, client, uuid, force).unwrap(); }, Action::Start { uuid } => { start(root, client, uuid).unwrap(); }, Action::Pause { uuid } => { pause(root, client, uuid).unwrap(); + }, + Action::Reboot { uuid, force } => { + reboot(root, client, uuid, force).unwrap(); } }; } @@ -81,8 +93,12 @@ fn list(a: Args, c: Client) -> Result<(), CliError> { Ok(()) } -fn stop(server: String, c: Client, u: String) -> Result<(), CliError> { - let res = c.post(format!("http://{}/planets/{}/shutdown", server, u)).send()?; +fn stop(server: String, c: Client, u: String, f: bool) -> Result<(), CliError> { + let mut url = format!("http://{}/planets/{}/shutdown", server, u); + if f { + url.push_str("/hard"); + } + let res = c.post(url).send()?; match res.status() { StatusCode::OK => { @@ -125,3 +141,22 @@ fn pause(server: String, c: Client, u: String) -> Result<(), CliError> { Ok(()) } + +fn reboot(server: String, c: Client, u: String, f: bool) -> Result<(), CliError> { + let mut url = format!("http://{}/planets/{}/reboot", server, u); + if f { + url.push_str("/hard"); + } + let res = c.post(url).send()?; + + match res.status() { + StatusCode::OK => { + println!("Rebooted."); + }, + _ => { + return Err(CliError::Cli(format!("Could not reboot VM: {}", res.text()?))); + }, + }; + + Ok(()) +} -- cgit v1.2.3