diff options
author | Cara Salter <cara@devcara.com> | 2022-04-23 21:31:34 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-04-23 21:31:34 -0400 |
commit | c06184b2ff121ad829db9cc65e92af56f66d442e (patch) | |
tree | 8345f4b4cb52e6ba3a861a3f1ec9e607443ab5b0 | |
parent | 013a7488c8d2d11daf2cb6184f7fe2d25f6da8a4 (diff) | |
download | solarlib-c06184b2ff121ad829db9cc65e92af56f66d442e.tar.gz solarlib-c06184b2ff121ad829db9cc65e92af56f66d442e.zip |
teach Houses how to download Vans
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/errors.rs | 2 | ||||
-rw-r--r-- | src/house.rs | 11 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/van.rs | 80 | ||||
-rw-r--r-- | src/waifu.rs | 14 |
6 files changed, 93 insertions, 18 deletions
@@ -1,6 +1,6 @@ [package] name = "waifulib" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/errors.rs b/src/errors.rs index 29f7f4f..3c261c7 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -14,5 +14,7 @@ pub enum Error { Allocation(String), #[error("I/O: {0}")] Io(#[from] std::io::Error), + #[error("Remote command error: {0}")] + RemoteCommand(String), } diff --git a/src/house.rs b/src/house.rs index ad6e055..33d3573 100644 --- a/src/house.rs +++ b/src/house.rs @@ -4,6 +4,7 @@ */ use crate::waifu::*; use crate::errors::Error; +use crate::van::Van; use serde::{Serialize, Deserialize}; use virt::{connect::Connect, domain::Domain}; use std::process::{ExitStatus}; @@ -98,7 +99,7 @@ impl House { /// /// h.introduce("test-2", 1024, 1, 20000, "test.iso").unwrap(); /// ``` - pub async fn introduce(&mut self, name: String, max_mem: Memory, max_cpus: CpuCount, disk_size_mb: u64, image_name: String) -> Result<Waifu, Error> { + 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") @@ -106,13 +107,13 @@ impl House { "-oStrictHostKeyChecking=accept-new", &self.address.clone(), "stat", - &format!("/var/lib/libvirt/images/{}", image_name.clone()) + &format!("/var/lib/libvirt/images/{}", van.make_pretty_name().clone()) ]) .output() .await?; if output.status != ExitStatus::from_raw(0) { - return Err(Error::MissingImage(image_name.clone())); + return Err(Error::MissingImage(van.name.clone())); } // Allocate VM disk @@ -145,8 +146,8 @@ impl House { uuid.to_string(), random_mac().clone(), true, - max_mem.count(), - max_cpus.count(), + max_mem.0, + max_cpus.0, "blank".to_string() )?; @@ -4,4 +4,6 @@ pub mod waifu; pub mod house; +pub mod van; + include!(concat!(env!("OUT_DIR"), "/templates.rs")); diff --git a/src/van.rs b/src/van.rs new file mode 100644 index 0000000..7024f73 --- /dev/null +++ b/src/van.rs @@ -0,0 +1,80 @@ +/*! A Van is a moving van, or an installation ISO */ + +use tokio::process::Command; +use std::process::ExitStatus; +use std::os::unix::process::ExitStatusExt; +use std::str::FromStr; + +use crate::errors::Error; + +/// Describes a hash of a file +pub struct Sha256(pub String); + +impl FromStr for Sha256 { + type Err = Error; + fn from_str(s: &str) -> Result<Self, Self::Err> { + let sum = s.clone(); + + Ok(Sha256(sum.to_string())) + } +} + +impl ToString for Sha256 { + fn to_string(&self) -> String { + self.0.clone() + } +} + +/// Describes a moving Van, or a way to install a distribution +pub struct Van { + /// The common name of the distribution (e.g "Arch Linux") + pub name: String, + /// The SHA-256 hash of the downloaded file + pub shasum: Sha256, + /// Where the ISO can be downloaded from + pub download_url: String, + /// The commonly accepted version (e.g "rolling", "21.11", "unstable") + pub version: String, +} + +impl Van { + pub fn new(name: String, + shasum: String, + download_url: String, + version: String + ) -> Self { + Self { + name, + shasum: Sha256(shasum), + download_url, + version + } + } + + pub async fn download(&self, target: String) -> Result<(), Error> { + let mut output = Command::new("ssh") + .args([ + "-oStrictHostKeyChecking=accept-new", + &target.clone(), + "wget", + "-O", + &self.download_url.clone(), + &format!("/var/lib/libvirt/images/{}", self.make_pretty_name().clone()) + ]) + .output() + .await?; + + if output.status != ExitStatus::from_raw(0) { + Err(Error::RemoteCommand(String::from_utf8(output.stdout).unwrap())) + } else { + Ok(()) + } + } + + pub fn make_pretty_name(&self) -> String { + let safe_name = self.name.clone().to_lowercase().replace(" ", "-"); + let file_name = format!("{}-{}-{}.van", safe_name, self.version.clone(), self.shasum.0.clone()); + + file_name + } +} diff --git a/src/waifu.rs b/src/waifu.rs index da9653f..bb99d2b 100644 --- a/src/waifu.rs +++ b/src/waifu.rs @@ -8,7 +8,7 @@ use crate::errors::Error; * Defines the amount of memory a waifu has */ #[derive(Debug, Serialize, Deserialize)] -pub struct Memory(u64); +pub struct Memory(pub u64); impl From<u64> for Memory { fn from(u: u64) -> Self { @@ -16,16 +16,11 @@ impl From<u64> for Memory { } } -impl Memory { - pub fn count(&self) -> u64 { - self.0 - } -} /** * Defines the number of vCPUs a waifu has */ #[derive(Debug, Serialize, Deserialize)] -pub struct CpuCount(u64); +pub struct CpuCount(pub u64); impl From<u64> for CpuCount { fn from(u: u64) -> Self { @@ -33,11 +28,6 @@ impl From<u64> for CpuCount { } } -impl CpuCount { - pub fn count(&self) -> u64 { - self.0 - } -} /** * Represents a virtual machine, that's active on some server * |