diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-03-04 10:14:15 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-03-04 10:14:24 +0200 |
commit | 4eded18d1cffe2bcbc432e4c14e4712b0c1ab4cb (patch) | |
tree | 903be1d7643439007ef4e39ef3d566171913f0ee /src/cmd | |
parent | 5141d3a654e3abc8064a6ba252220426701a5f89 (diff) | |
download | vmadm-4eded18d1cffe2bcbc432e4c14e4712b0c1ab4cb.tar.gz |
feat! allow specification files to have any number of machines
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/cloud_init.rs | 35 | ||||
-rw-r--r-- | src/cmd/delete.rs | 60 | ||||
-rw-r--r-- | src/cmd/mod.rs | 2 | ||||
-rw-r--r-- | src/cmd/new.rs | 50 |
4 files changed, 73 insertions, 74 deletions
diff --git a/src/cmd/cloud_init.rs b/src/cmd/cloud_init.rs index 615b2a8..a9b3588 100644 --- a/src/cmd/cloud_init.rs +++ b/src/cmd/cloud_init.rs @@ -12,26 +12,21 @@ pub enum CloudInitCommandError { IoError(#[from] std::io::Error), } -pub fn cloud_init(spec: &Specification, dirname: &Path) -> Result<(), CloudInitCommandError> { - info!( - "generating cloud-init configuration into {}", - dirname.display() - ); - - let init = CloudInitConfig::from(&spec)?; - - debug!("creating directory {}", dirname.display()); - std::fs::create_dir_all(dirname)?; - init.create_dir(dirname)?; - - Ok(()) -} - -pub fn cloud_init_iso(spec: &Specification, iso: &Path) -> Result<(), CloudInitCommandError> { - info!("generating cloud-init ISO into {}", iso.display()); - - let init = CloudInitConfig::from(&spec)?; - init.create_iso(iso)?; +pub fn cloud_init(specs: &[Specification], dirname: &Path) -> Result<(), CloudInitCommandError> { + for spec in specs { + let dirname = dirname.join(&spec.name); + info!( + "generating cloud-init configuration for {} into {}", + spec.name, + dirname.display() + ); + + let init = CloudInitConfig::from(&spec)?; + + debug!("creating directory {}", dirname.display()); + std::fs::create_dir_all(&dirname)?; + init.create_dir(&dirname)?; + } Ok(()) } diff --git a/src/cmd/delete.rs b/src/cmd/delete.rs index ac8c5b4..3f60e27 100644 --- a/src/cmd/delete.rs +++ b/src/cmd/delete.rs @@ -13,39 +13,41 @@ pub enum DeleteError { IoError(#[from] std::io::Error), } -pub fn delete(spec: &Specification) -> Result<(), DeleteError> { - info!("deleting virtual machine {}", spec.name); - - debug!("connecting to libvirtd"); - let conn = Connect::open("qemu:///system")?; - - debug!("listing all domains"); - let domains = conn.list_all_domains(0)?; - - for domain in domains { - debug!("considering {}", domain.get_name()?); - if domain.get_name()? == spec.name { - debug!("shutdown {}", spec.name); - domain.shutdown().ok(); - - let briefly = Duration::from_millis(1000); - loop { - thread::sleep(briefly); - match domain.is_active() { - Ok(true) => (), - Ok(false) => break, - Err(err) => { - debug!("is_active: {}", err); +pub fn delete(specs: &[Specification]) -> Result<(), DeleteError> { + for spec in specs { + info!("deleting virtual machine {}", spec.name); + + debug!("connecting to libvirtd"); + let conn = Connect::open("qemu:///system")?; + + debug!("listing all domains"); + let domains = conn.list_all_domains(0)?; + + for domain in domains { + debug!("considering {}", domain.get_name()?); + if domain.get_name()? == spec.name { + debug!("shutdown {}", spec.name); + domain.shutdown().ok(); + + let briefly = Duration::from_millis(1000); + loop { + thread::sleep(briefly); + match domain.is_active() { + Ok(true) => (), + Ok(false) => break, + Err(err) => { + debug!("is_active: {}", err); + } } + debug!("{} is still running", spec.name); } - debug!("{} is still running", spec.name); - } - debug!("undefine {}", spec.name); - domain.undefine()?; + debug!("undefine {}", spec.name); + domain.undefine()?; - debug!("removing image file {}", spec.image.display()); - std::fs::remove_file(&spec.image)?; + debug!("removing image file {}", spec.image.display()); + std::fs::remove_file(&spec.image)?; + } } } Ok(()) diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index ad32e49..f1e029c 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -8,4 +8,4 @@ pub mod delete; pub use delete::delete; pub mod cloud_init; -pub use cloud_init::{cloud_init, cloud_init_iso}; +pub use cloud_init::cloud_init; diff --git a/src/cmd/new.rs b/src/cmd/new.rs index 0a4b3ab..0f7bc94 100644 --- a/src/cmd/new.rs +++ b/src/cmd/new.rs @@ -21,30 +21,32 @@ pub enum NewError { VirtInstallError(#[from] VirtInstallError), } -pub fn new(spec: &Specification) -> Result<(), NewError> { - info!("creating new VM {}", spec.name); - - info!("creating cloud-init config"); - let init = CloudInitConfig::from(&spec)?; - - info!( - "creating VM image {} from {}", - spec.image.display(), - spec.base.display() - ); - let image = VirtualMachineImage::new_from_base(&spec.base, &spec.image)?; - - info!("resizing image to {} GiB", spec.image_size_gib); - image.resize(spec.image_size_gib * GIB)?; - - info!("creating VM"); - let mut args = VirtInstallArgs::new(&spec.name, &image, &init); - args.set_memory(spec.memory_mib); - args.set_vcpus(spec.cpus); - virt_install(&args)?; - - info!("waiting for {} to open its SSH port", spec.name); - wait_for_port(&spec.name, SSH_PORT)?; +pub fn new(specs: &[Specification]) -> Result<(), NewError> { + for spec in specs { + info!("creating new VM {}", spec.name); + + info!("creating cloud-init config"); + let init = CloudInitConfig::from(&spec)?; + + info!( + "creating VM image {} from {}", + spec.image.display(), + spec.base.display() + ); + let image = VirtualMachineImage::new_from_base(&spec.base, &spec.image)?; + + info!("resizing image to {} GiB", spec.image_size_gib); + image.resize(spec.image_size_gib * GIB)?; + + info!("creating VM"); + let mut args = VirtInstallArgs::new(&spec.name, &image, &init); + args.set_memory(spec.memory_mib); + args.set_vcpus(spec.cpus); + virt_install(&args)?; + + info!("waiting for {} to open its SSH port", spec.name); + wait_for_port(&spec.name, SSH_PORT)?; + } Ok(()) } |