summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-03-24 21:21:00 +0200
committerLars Wirzenius <liw@liw.fi>2021-03-24 21:21:00 +0200
commitecd8eef97006686b8686803b0eaf0a37b87eff08 (patch)
tree1bd1266858ca6d4e78e999aa5c0f8119131ff41b
parentcc7f874e03a6050c47b592fe5ed7b3704eeedfcc (diff)
downloadvmadm-ecd8eef97006686b8686803b0eaf0a37b87eff08.tar.gz
refactor: when starting, stopping do all VMs at once
-rw-r--r--src/cmd/shutdown.rs2
-rw-r--r--src/cmd/start.rs2
-rw-r--r--src/libvirt.rs10
-rw-r--r--src/util.rs2
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() {