summaryrefslogtreecommitdiff
path: root/src/cmd/new.rs
blob: 99303793e6aab855e48dfcce7fd48f0b650d35d3 (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
use crate::cloudinit::CloudInitConfig;
use crate::image::VirtualMachineImage;
use crate::install::{virt_install, VirtInstallArgs};
use crate::spec::Specification;

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

const SSH_PORT: i32 = 22;

pub fn new(spec: &Specification) -> anyhow::Result<()> {
    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) -> anyhow::Result<()> {
    let addr = format!("{}:{}", name, port);
    loop {
        if TcpStream::connect(&addr).is_ok() {
            return Ok(());
        }
    }
}