diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-07-25 10:33:56 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-07-25 10:33:56 +0000 |
commit | c15ca42605d760d519721a3e39e1834ac5b1cb15 (patch) | |
tree | aa63fa87ab905c2bf6e874ad3448bc3c3b71deef | |
parent | 1cab04fbfc732ff0cbcd0394fa0374300e1176e8 (diff) | |
parent | bd87e253000b8a0a73c831877c99291fb430607f (diff) | |
download | vmadm-c15ca42605d760d519721a3e39e1834ac5b1cb15.tar.gz |
Merge branch 'log' into 'main'
fix adding networks to VMs
See merge request larswirzenius/vmadm!39
-rw-r--r-- | src/cmd/new.rs | 3 | ||||
-rw-r--r-- | src/install.rs | 22 | ||||
-rw-r--r-- | src/spec.rs | 14 | ||||
-rw-r--r-- | subplot/vmadm.py | 2 | ||||
-rw-r--r-- | vmadm.md | 9 |
5 files changed, 38 insertions, 12 deletions
diff --git a/src/cmd/new.rs b/src/cmd/new.rs index 4b6a4a3..3e89c1d 100644 --- a/src/cmd/new.rs +++ b/src/cmd/new.rs @@ -63,6 +63,9 @@ pub fn new(specs: &[Specification]) -> Result<(), NewError> { let mut args = VirtInstallArgs::new(&spec.name, &image, &init); args.set_memory(spec.memory_mib); args.set_vcpus(spec.cpus); + for network in spec.networks.iter() { + args.add_network(network); + } virt_install(&args, &iso)?; } diff --git a/src/install.rs b/src/install.rs index c84f30a..ce2cc0b 100644 --- a/src/install.rs +++ b/src/install.rs @@ -6,6 +6,7 @@ use crate::cloudinit::{CloudInitConfig, CloudInitError}; use crate::image::VirtualMachineImage; +use log::debug; use std::path::{Path, PathBuf}; use std::process::Command; use std::result::Result; @@ -21,6 +22,10 @@ pub enum VirtInstallError { #[error("couldn't run virt-install")] Run(#[source] std::io::Error), + /// No networks defined. + #[error("no networks defined for {0}")] + NoNetworks(String), + /// Error parsing a string as UTF8. #[error(transparent)] StringError(#[from] std::string::FromUtf8Error), @@ -106,14 +111,15 @@ impl VirtInstallArgs { pub fn virt_install(args: &VirtInstallArgs, iso: &Path) -> Result<PathBuf, VirtInstallError> { args.init().create_iso(&iso)?; - let networks: Vec<String> = if args.networks.is_empty() { - vec!["--network=default".to_string()] - } else { - args.networks - .iter() - .map(|s| format!("--network={}", s)) - .collect() - }; + let networks: Vec<String> = args + .networks + .iter() + .map(|s| format!("--network=network={}", s)) + .collect(); + debug!("virt-install networks: {:?}", networks); + if networks.is_empty() { + return Err(VirtInstallError::NoNetworks(args.name.clone())); + } let r = Command::new("virt-install") .arg("--name") diff --git a/src/spec.rs b/src/spec.rs index 1470fdb..c3c6b59 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -110,6 +110,16 @@ impl OneVmInputSpecification { false } } + + fn networks(&self, config: &Configuration) -> Vec<String> { + if let Some(ref x) = self.networks { + x.clone() + } else if let Some(ref x) = config.default_networks { + x.clone() + } else { + vec!["default".to_string()] + } + } } fn get<'a, T>( @@ -188,6 +198,9 @@ pub struct Specification { /// Path to CA key for creating host certificate. pub ca_key: Option<PathBuf>, + + /// List of networks to which host should be added. + pub networks: Vec<String>, } /// Errors from this module. @@ -302,6 +315,7 @@ impl Specification { generate_host_certificate: gen_cert, autostart: input.autostart(config), ca_key, + networks: input.networks(config), }; debug!("specification as with defaults applied: {:#?}", spec); diff --git a/subplot/vmadm.py b/subplot/vmadm.py index 52233ed..acca754 100644 --- a/subplot/vmadm.py +++ b/subplot/vmadm.py @@ -142,6 +142,8 @@ def _assert_equal_objects(a, b): for key in a: assert key in b, f"wanted b to have key {key!r}" _assert_equal_objects(a[key], b[key]) + for key in b: + assert key in a, f"wanted a to have key {key!r}" elif isinstance(a, list): assert len(a) == len( b @@ -51,7 +51,8 @@ default_cpus: 1 default_generate_host_certificate: true default_autostart: true default_networks: -- default +- lan +- wan ca_key: ca_key authorized_keys: - ~/.ssh/id_rsa.pub @@ -67,7 +68,8 @@ authorized_keys: "default_generate_host_certificate": true, "default_autostart": true, "default_networks": [ - "default" + "lan", + "wan" ], "ca_key": "ca_key", "authorized_keys": [ @@ -77,8 +79,7 @@ authorized_keys: ~~~ ~~~{#spec.yaml .file .yaml} -foo: - networks: ["lan", "wan"] +foo: {} ~~~ ~~~{#fullspec.json .file .json} |