summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2022-06-15 16:50:43 -0400
committerCara Salter <cara@devcara.com>2022-06-15 16:50:43 -0400
commita764356ba2934c8bc6114d079e687407dba7ccce (patch)
treedce40c16ea0be9d028ad072a746f1eaffd5ad8c0
parent31761bd5fd244900c03f44e98e2d59323c20bfbe (diff)
downloadsolarctl-a764356ba2934c8bc6114d079e687407dba7ccce.tar.gz
solarctl-a764356ba2934c8bc6114d079e687407dba7ccce.zip
create new VMs
Closes #3
-rw-r--r--Cargo.lock4
-rw-r--r--src/main.rs64
2 files changed, 60 insertions, 8 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 38a729f..d9ccd96 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1045,8 +1045,8 @@ dependencies = [
[[package]]
name = "solarlib"
-version = "1.4.0"
-source = "git+https://git.carathe.dev/solard/solarlib?branch=master#bddaf3af81b836ac8d25199f154fc59a95848dab"
+version = "1.5.0"
+source = "git+https://git.carathe.dev/solard/solarlib?branch=master#945991ae5227bf3de7d715e4d256e11f1753e4fa"
dependencies = [
"mac_address",
"rand",
diff --git a/src/main.rs b/src/main.rs
index fbe2463..e629d0d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,7 +5,8 @@ mod errors;
use errors::CliError;
use tabular::{row, Table};
use reqwest::{blocking::Client, StatusCode};
-use solarlib::planet::Planet;
+use solarlib::planet::{Planet, Memory, CpuCount};
+use solarlib::star::NewPlanet;
/// Manage solard and homeworld instances
#[derive(Parser)]
@@ -46,6 +47,19 @@ enum Action {
},
View {
uuid: String
+ },
+
+ Create {
+ max_mem: u64,
+
+ max_cpus: u64,
+
+ disk_size_mb: u64,
+
+ name: String,
+
+ /// The Sha256 hash of the ship
+ ship: String
}
}
@@ -74,6 +88,9 @@ fn main() {
}
Action::View { uuid } => {
view(root, uuid).unwrap();
+ },
+ Action::Create { max_mem, max_cpus, disk_size_mb, name, ship } => {
+ create(root, client, max_mem, max_cpus, disk_size_mb, name, ship).unwrap();
}
};
}
@@ -82,17 +99,21 @@ fn main() {
fn list(a: Args, c: Client) -> Result<(), CliError> {
let res: Vec<Planet> = c.get(format!("http://{}/planets/list", a.server)).send()?.json()?;
- let mut table = Table::new("{:<} {:<} {:<}");
+ let mut table = Table::new("{:<} | {:<} | {:<} | {:<}");
table.add_row(row!(
- "name", "uuid", "status")
+ "name", "uuid", "status", "running")
+ );
+ table.add_row(row!(
+ "----", "----", "------", "-------")
);
for p in res {
table.add_row(row!(
&p.name,
&p.uuid,
- if p.orbiting { "Running" } else { "Not running" },
+ &p.status,
+ if p.orbiting { "yes" } else { "no" },
));
}
@@ -173,11 +194,42 @@ fn view(server: String, u: String) -> Result<(), CliError> {
let host = server.split(':').next().unwrap();
let qemu_url = format!("qemu+ssh://{}/system", host);
- let output = Command::new("virt-viewer")
+ if let Err(e) = Command::new("virt-viewer")
.arg("-c")
.arg(qemu_url)
.arg(u)
- .output();
+ .output() {
+ println!("Could not run virt-viewer: {}", e);
+ }
+
+ Ok(())
+}
+
+fn create(s: String, c: Client, mem: u64, cpus: u64, disk_size: u64, name: String, ship: String) -> Result<(), CliError> {
+ let url = format!("http://{}/planets/new", s);
+
+ let new_p: NewPlanet = NewPlanet {
+ name,
+ ship,
+ disk_size_mb: disk_size,
+ max_mem: Memory(mem),
+ max_cpus: CpuCount(cpus)
+ };
+
+ println!("Creating new planet...");
+
+ let res = c.post(url).json(&new_p).send()?;
+
+ match res.status() {
+ StatusCode::OK => {
+ let js: Planet = res.json()?;
+
+ println!("Created. UUID: {}", js.uuid);
+ },
+ _ => {
+ return Err(CliError::Cli(format!("Could not create VM: {}", res.text()?)));
+ }
+ };
Ok(())
}