view src/virsh.rs @ 13:56e9763abeef

define start cmd
author AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
date Thu, 29 Oct 2020 17:12:09 +0900
parents 0df4d067badb
children e6dc51066703
line wrap: on
line source

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

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

pub fn get_vm_list(user_name: &'static 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),
    };

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

pub fn start_vm(vm_name: String) {
    let output = Command::new("virsh")
        .arg("start")
        .arg(vm_name)
        .output()
        .expect("failed to start vm");

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