diff options
| author | Cara Salter <cara@devcara.com> | 2022-05-19 14:56:35 -0400 | 
|---|---|---|
| committer | Cara Salter <cara@devcara.com> | 2022-05-19 14:56:35 -0400 | 
| commit | 38114b3dfabb3a57f80c86f86099a714986f58eb (patch) | |
| tree | 2ab766d7045f368b4cdbb17232e470bb92094dd4 /src/house.rs | |
| parent | c06184b2ff121ad829db9cc65e92af56f66d442e (diff) | |
| download | solarlib-38114b3dfabb3a57f80c86f86099a714986f58eb.tar.gz solarlib-38114b3dfabb3a57f80c86f86099a714986f58eb.zip | |
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.
Diffstat (limited to 'src/house.rs')
| -rw-r--r-- | src/house.rs | 103 | 
1 files changed, 71 insertions, 32 deletions
| 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<Connect>, @@ -50,12 +53,19 @@ impl House {      /// let mut h: House = House::new("test:///default".to_string()).unwrap();      /// ```      pub fn new(url: String) -> Result<Self, Error> { -        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<Waifu, Error> {          // 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(); | 
