Mercurial > hg > Members > anatofuz > ie-virsh
changeset 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 | 06b9390774dd |
files | src/command.rs src/main.rs src/virsh.rs |
diffstat | 3 files changed, 32 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/command.rs Thu Oct 29 16:15:52 2020 +0900 +++ b/src/command.rs Thu Oct 29 17:12:09 2020 +0900 @@ -8,3 +8,17 @@ println!("{}", vm_info); } } + +pub fn start_command(user_name: &'static str, vm_name: String) { + let vm_name = match vm_name.parse::<u8>() { + Ok(_) => String::from(format!("{}-{}", user_name, vm_name)), + Err(_) => vm_name, + }; + + let vm_name = if vm_name.starts_with(user_name) { + vm_name + } else { + format!("{}-{}", user_name, vm_name) + }; + virsh::start_vm(vm_name); +}
--- a/src/main.rs Thu Oct 29 16:15:52 2020 +0900 +++ b/src/main.rs Thu Oct 29 17:12:09 2020 +0900 @@ -73,11 +73,13 @@ name_or_id: String, } +/* struct VM { id: u32, name: String, is_vm_running: bool, } +*/ fn main() { let opts: Opts = Opts::parse(); @@ -92,10 +94,13 @@ user::set_root_id(); command::list_command(user_name); } + SubCommand::Start(arg) => { + user::set_root_id(); + command::start_command(user_name, arg.name); + } SubCommand::Define(name) => {} SubCommand::Shutdown(name) => {} SubCommand::Console(name) => {} - SubCommand::Destroy(name_or_id) => {} _ => {} }
--- a/src/virsh.rs Thu Oct 29 16:15:52 2020 +0900 +++ b/src/virsh.rs Thu Oct 29 17:12:09 2020 +0900 @@ -1,3 +1,4 @@ +use std::io::{self, Write}; use std::process::Command; pub struct ListDumpMsg { @@ -29,3 +30,14 @@ .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(); +}