From ecd8eef97006686b8686803b0eaf0a37b87eff08 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 24 Mar 2021 21:21:00 +0200 Subject: refactor: when starting, stopping do all VMs at once --- src/cmd/shutdown.rs | 2 +- src/cmd/start.rs | 2 +- src/libvirt.rs | 10 +++++++++- src/util.rs | 2 ++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/cmd/shutdown.rs b/src/cmd/shutdown.rs index b53ebd3..cdd3069 100644 --- a/src/cmd/shutdown.rs +++ b/src/cmd/shutdown.rs @@ -9,7 +9,7 @@ pub fn shutdown(specs: &[Specification]) -> Result<(), VirtError> { let libvirt = Libvirt::connect("qemu:///system")?; for spec in specs { info!("shutting down virtual machine {}", spec.name); - libvirt.shutdown(&spec.name)?; + libvirt.trigger_shutdown(&spec.name)?; } for spec in specs { debug!("waiting for {} to become inactive", spec.name); diff --git a/src/cmd/start.rs b/src/cmd/start.rs index a69f54c..8e74c49 100644 --- a/src/cmd/start.rs +++ b/src/cmd/start.rs @@ -22,7 +22,7 @@ pub fn start(specs: &[Specification]) -> Result<(), StartError> { let libvirt = Libvirt::connect("qemu:///system")?; for spec in specs { info!("starting virtual machine {}", spec.name); - libvirt.start(&spec.name)?; + libvirt.trigger_start(&spec.name)?; } for spec in specs { wait_for_ssh(&spec.name); diff --git a/src/libvirt.rs b/src/libvirt.rs index 7d8235d..ade63d3 100644 --- a/src/libvirt.rs +++ b/src/libvirt.rs @@ -87,9 +87,17 @@ impl Libvirt { Ok(()) } - pub fn start(&self, name: &str) -> Result<(), VirtError> { + pub fn trigger_start(&self, name: &str) -> Result<(), VirtError> { if let Some(domain) = self.get_domain(name)? { + debug!("starting {}", name); domain.create()?; + } + Ok(()) + } + + pub fn start(&self, name: &str) -> Result<(), VirtError> { + if let Some(_) = self.get_domain(name)? { + self.trigger_start(name)?; wait_for_ssh(name); } Ok(()) diff --git a/src/util.rs b/src/util.rs index 9933f03..f3e104b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,11 +1,13 @@ //! Utilities. +use log::debug; use std::net::TcpStream; const SSH_PORT: i32 = 22; // Wait for a virtual machine to have opened its SSH port. pub fn wait_for_ssh(name: &str) { + debug!("waiting for {} to respond to SSH", name); let addr = format!("{}:{}", name, SSH_PORT); loop { if TcpStream::connect(&addr).is_ok() { -- cgit v1.2.1