diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-12-08 12:12:09 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-12-08 12:12:09 +0200 |
commit | 94bc8dc8b4537d228121f877513aafcae42cbb44 (patch) | |
tree | c5a8d68f5fc9e9814b8918d7a7f3ceabddaea685 /src | |
parent | 31dc6df2d478e66eb39388dedd48080da0e72645 (diff) | |
download | vmadm-94bc8dc8b4537d228121f877513aafcae42cbb44.tar.gz |
feat: give a more useful error message when VM image already exists
Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r-- | src/cmd/new.rs | 4 | ||||
-rw-r--r-- | src/image.rs | 17 | ||||
-rw-r--r-- | src/progress.rs | 9 |
3 files changed, 29 insertions, 1 deletions
diff --git a/src/cmd/new.rs b/src/cmd/new.rs index 8583ae9..487914a 100644 --- a/src/cmd/new.rs +++ b/src/cmd/new.rs @@ -9,6 +9,7 @@ use crate::spec::Specification; use crate::util::wait_for_ssh; use bytesize::GIB; +use log::debug; use tempfile::tempdir; /// Errors returned by this module. @@ -48,13 +49,16 @@ pub fn new(specs: &[Specification], progress: &Progress) -> Result<(), NewError> progress.chatty("creating cloud-init config"); let init = CloudInitConfig::from(spec)?; + debug!("finished creating cloud-init config"); progress.chatty(&format!( "creating VM image {} from {}", spec.image.display(), spec.base.display() )); + debug!("creating VM image"); let image = VirtualMachineImage::new_from_base(&spec.base, &spec.image)?; + debug!("finished creating VM image"); progress.chatty(&format!("resizing image to {} GiB", spec.image_size_gib)); image.resize(spec.image_size_gib * GIB)?; diff --git a/src/image.rs b/src/image.rs index 7b7c2ce..ffe5b56 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,5 +1,6 @@ //! Virtual machine image handling. +use log::debug; use std::fs::copy; use std::path::{Path, PathBuf}; use std::process::Command; @@ -8,6 +9,10 @@ use std::result::Result; /// Errors from this module. #[derive(Debug, thiserror::Error)] pub enum ImageError { + /// The image file for a new VM already exists. + #[error("image file already exists: {0}")] + ImageExists(PathBuf), + /// Error resizing image. #[error("qemu-img resize failed: {0}")] ResizeError(String), @@ -34,9 +39,21 @@ pub struct VirtualMachineImage { impl VirtualMachineImage { /// Create new image from a base image. pub fn new_from_base(base_image: &Path, image: &Path) -> Result<Self, ImageError> { + debug!( + "creating new image {} from base image {}", + image.display(), + base_image.display() + ); + debug!("does {} exixt? {}", image.display(), image.exists()); + if image.exists() { + debug!("image already exists: {}", image.display()); + return Err(ImageError::ImageExists(image.to_path_buf())); + } + debug!("copying base image to new image"); copy(base_image, image).map_err(|err| { ImageError::BaseImageCopy(base_image.to_path_buf(), image.to_path_buf(), err) })?; + debug!("all good"); Ok(Self { filename: image.to_path_buf(), }) diff --git a/src/progress.rs b/src/progress.rs index 52ade35..5ce56af 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -1,5 +1,7 @@ //! Show progress, or not. +use log::{debug, error, info}; + #[derive(Debug, Clone, Copy)] pub enum MessageKind { OnlyErrors, @@ -21,6 +23,7 @@ impl Progress { } pub fn chatty(&self, msg: &str) { + debug!("{}", msg); if let MessageKind::Everything = self.level { self.message("DEBUG", msg); } @@ -28,13 +31,17 @@ impl Progress { pub fn step(&self, msg: &str) { match self.level { - MessageKind::Everything | MessageKind::Steps => self.message("INFO", msg), + MessageKind::Everything | MessageKind::Steps => { + info!("{}", msg); + self.message("INFO", msg) + } _ => (), } } pub fn error(&self, msg: &str) { // Errors are always written out. + error!("{}", msg); self.message("ERROR", msg); } } |