view src/virsh.rs @ 45:91aea67d88fb

fix path
author AnaTofuZ <anatofuz@gmail.com>
date Sun, 22 Nov 2020 18:06:30 +0900
parents 98138c3f00f0
children d6ef7ea91ff4
line wrap: on
line source

use std::io::{self, Write};
use std::process::Command;

use std::env;
use std::fs;
use std::path::Path;

const TEMPLATE_DIR: &str = "/ie-ryukyu/kvm/images/templates/";
const TEMPLATE_SUFFIX: &str = "template-";
const TEMPLATE_FILE_EXTENSION: &str = ".qcow2";
const QCOW2: &str = "qcow2";

pub struct ListDumpMsg {
    pub info_msg: String,
    pub border_line: String,
}

pub fn get_template_list() -> Result<Vec<String>, io::Error> {
    let mut entries = fs::read_dir(TEMPLATE_DIR)?
        .map(|res| {
            res.map(|e| {
                e.path()
                    .display()
                    .to_string()
                    .replace(TEMPLATE_DIR, "")
                    .replace(TEMPLATE_SUFFIX, "")
                    .replace(TEMPLATE_FILE_EXTENSION, "")
            })
        })
        .collect::<Result<Vec<_>, io::Error>>()?;

    entries.sort();
    Ok(entries)
}

pub fn get_vm_list(user_name: &str) -> (ListDumpMsg, Vec<String>) {
    let output = Command::new("virsh")
        .arg("list")
        .arg("--all")
        .output()
        .expect("failed to virsh");
    let virsh_list = String::from_utf8_lossy(&output.stdout);
    let mut virsh_list = virsh_list.split('\n');

    let info_msg = virsh_list.next().unwrap();
    let border_line = virsh_list.next().unwrap();
    let ldump_msg = ListDumpMsg {
        info_msg: String::from(info_msg),
        border_line: String::from(border_line),
    };

    (
        ldump_msg,
        virsh_list
            .filter(|&x| x.contains(user_name))
            .map(|x| x.to_string())
            .collect(),
    )
}

pub fn command_require_vm_name(vm_name: &str, operation: &str) {
    let output = Command::new("virsh")
        .arg(operation)
        .arg(vm_name)
        .output()
        .unwrap_or_else(|_| panic!("failed to {}", operation));

    io::stdout().write_all(&output.stdout).unwrap();
    io::stderr().write_all(&output.stderr).unwrap();
}

pub fn generate_qemu_from_template(vm_path_string: &str, vm_name: &str, template_path: &str) {
    let vm_path = Path::new(&vm_path_string);
    let ok = env::set_current_dir(&vm_path);
    if ok.is_err() {
        println!("failed cd at {}", &vm_path_string);
        return;
    }
    let output = Command::new("qemu-img")
        .arg("create")
        .arg("-F")
        .arg(QCOW2)
        .arg("-b")
        .arg(template_path)
        .arg("-f")
        .arg(QCOW2)
        .arg(format!("{}.qcow2", vm_name))
        .output()
        .unwrap_or_else(|_| panic!("failed to generate {}", &vm_name));
    io::stdout().write_all(&output.stdout).unwrap();
    io::stderr().write_all(&output.stderr).unwrap();
}

pub fn get_backing_path(template_name: &str) -> Option<String> {
    let template_path = format!(
        "{}{}{}{}",
        TEMPLATE_DIR, TEMPLATE_SUFFIX, &template_name, TEMPLATE_FILE_EXTENSION
    );
    if Path::new(&template_path).exists() {
        return Some(template_path);
    }
    None
}