From 38114b3dfabb3a57f80c86f86099a714986f58eb Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Thu, 19 May 2022 14:56:35 -0400 Subject: house: Add "remote" flag Let's us figure out if a House is remote or not, because if it's not we shouldn't be using SSH to connect. --- src/house.rs | 103 ++++++++++++++++++++++++++++++++++++++++------------------- src/van.rs | 2 +- 2 files changed, 72 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/house.rs b/src/house.rs index 33d3573..abd0eef 100644 --- a/src/house.rs +++ b/src/house.rs @@ -11,7 +11,7 @@ use std::process::{ExitStatus}; use std::os::unix::process::ExitStatusExt; use rand::Rng; -use tokio::{process::Command, task::spawn_blocking}; +use tokio::{process::Command}; #[derive(Serialize, Deserialize, Debug)] pub enum Address { @@ -36,6 +36,9 @@ pub struct House { /// FQDN or IP address, a way to talk to the house pub address: String, + /// Whether or not the House is local (same machine) or remote (networked machine) + pub remote: bool, + /// Connection to the House, if available #[serde(skip)] con: Option, @@ -50,12 +53,19 @@ impl House { /// let mut h: House = House::new("test:///default".to_string()).unwrap(); /// ``` pub fn new(url: String) -> Result { - let mut c = Connect::open(&url.clone())?; + let c = Connect::open(&url.clone())?; + + let remote = if url.contains("qemu:///") || url.contains("localhost") || url.contains("127.0.0.1") { + true + } else { + false + }; // If the connection succeeds, we've got one! Ok(Self { name: c.get_hostname()?, address: c.get_uri()?, + remote, con: Some(c), }) } @@ -102,37 +112,66 @@ impl House { pub async fn introduce(&mut self, name: String, max_mem: Memory, max_cpus: CpuCount, disk_size_mb: u64, van: Van) -> Result { // Check for image on host machine - let mut output = Command::new("ssh") - .args([ - "-oStrictHostKeyChecking=accept-new", - &self.address.clone(), - "stat", - &format!("/var/lib/libvirt/images/{}", van.make_pretty_name().clone()) - ]) - .output() - .await?; - - if output.status != ExitStatus::from_raw(0) { - return Err(Error::MissingImage(van.name.clone())); - } + if self.remote { + let mut output = Command::new("ssh") + .args([ + "-oStrictHostKeyChecking=accept-new", + &self.address.clone(), + "stat", + &format!("/var/lib/libvirt/images/{}", van.make_pretty_name().clone()) + ]) + .output() + .await?; + + if output.status != ExitStatus::from_raw(0) { + return Err(Error::MissingImage(van.name.clone())); + } - // Allocate VM disk - output = Command::new("ssh") - .args([ - "-oStrictHostKeyChecking=accept-new", - &self.address.clone(), - "qemu-img", - "create", - "-f", - "qcow2", - &format!("/var/lib/libvirt/images/{}.qcow2", name.clone()), - &format!("{}M", disk_size_mb) - ]) - .output() - .await?; - - if output.status != ExitStatus::from_raw(0) { - return Err(Error::Allocation(String::from_utf8(output.stdout).unwrap())); + // Allocate VM disk + output = Command::new("ssh") + .args([ + "-oStrictHostKeyChecking=accept-new", + &self.address.clone(), + "qemu-img", + "create", + "-f", + "qcow2", + &format!("/var/lib/libvirt/images/{}.qcow2", name.clone()), + &format!("{}M", disk_size_mb) + ]) + .output() + .await?; + + if output.status != ExitStatus::from_raw(0) { + return Err(Error::Allocation(String::from_utf8(output.stdout).unwrap())); + } + } else { + // It's local + let mut output = Command::new("stat") + .args([ + &format!("/var/lib/libvirt/images/{}", van.make_pretty_name().clone()), + ]) + .output() + .await?; + + if output.status != ExitStatus::from_raw(0) { + return Err(Error::MissingImage(van.name.clone())); + } + + output = Command::new("qemu-img") + .args([ + "create", + "-f", + "qcow2", + &format!("/var/lib/libvirt/images/{}.qcow2", name.clone()), + &format!("{}M", disk_size_mb) + ]) + .output() + .await?; + + if output.status != ExitStatus::from_raw(0) { + return Err(Error::Allocation(String::from_utf8(output.stdout).unwrap())); + } } let uuid = uuid::Uuid::new_v4(); diff --git a/src/van.rs b/src/van.rs index 7024f73..64e7295 100644 --- a/src/van.rs +++ b/src/van.rs @@ -52,7 +52,7 @@ impl Van { } pub async fn download(&self, target: String) -> Result<(), Error> { - let mut output = Command::new("ssh") + let output = Command::new("ssh") .args([ "-oStrictHostKeyChecking=accept-new", &target.clone(), -- cgit v1.2.3