summaryrefslogtreecommitdiff
path: root/src/cmd/new.rs
blob: 57f69ae473df5c170603ab0f49f27654911ab0a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! The `new` sub-command.

use crate::cloudinit::{CloudInitConfig, CloudInitError};
use crate::image::{ImageError, VirtualMachineImage};
use crate::install::{virt_install, VirtInstallArgs, VirtInstallError};
use crate::spec::Specification;

use bytesize::GIB;
use log::info;
use std::net::TcpStream;

const SSH_PORT: i32 = 22;

/// Errors returned by this module.
#[derive(Debug, thiserror::Error)]
pub enum NewError {
    /// Problem with cloud-init configuration.
    #[error(transparent)]
    CloudInitError(#[from] CloudInitError),

    /// Problem creating VM image.
    #[error(transparent)]
    ImageError(#[from] ImageError),

    /// Problem with libvirt.
    #[error(transparent)]
    VirtInstallError(#[from] VirtInstallError),
}

/// The `new` sub-command.
///
/// Create all the new virtual machines specified by the caller. Wait
/// until each VM's SSH port listens for connections.
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(())
}

fn wait_for_port(name: &str, port: i32) -> Result<(), NewError> {
    let addr = format!("{}:{}", name, port);
    loop {
        if TcpStream::connect(&addr).is_ok() {
            return Ok(());
        }
    }
}