Mercurial > hg > Members > anatofuz > ie-virsh
changeset 17:5477c26e6984
impl sub commands that use uid
author | AnaTofuZ <k198584@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 02 Nov 2020 16:09:53 +0900 |
parents | 1da4937628be |
children | 9b24d6767a2f |
files | src/command.rs src/main.rs |
diffstat | 2 files changed, 87 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/src/command.rs Sun Nov 01 14:02:35 2020 +0900 +++ b/src/command.rs Mon Nov 02 16:09:53 2020 +0900 @@ -1,6 +1,6 @@ use super::virsh; -pub fn list_command(user_name: &'static str) { +pub fn list(user_name: &'static str) { let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name); println!("{}", ldump_msg.info_msg); println!("{}", ldump_msg.border_line); @@ -9,14 +9,53 @@ } } -pub fn start_command(user_name: &'static str, vm_name: String) { - virsh::command_require_vm_name(get_vm_name(user_name, vm_name), "start"); +pub fn start(user_name: &'static str, vm_name: String) { + virsh::command_require_vm_name(get_vm_name(user_name, &vm_name), "start"); +} + +pub fn shutdown(user_name: &'static str, vm_name: String) { + exec_cmd_from_name_or_id(user_name, &vm_name, "shutdown") +} + +pub fn destroy(user_name: &'static str, vm_name: String) { + exec_cmd_from_name_or_id(user_name, &vm_name, "destroy") +} + +pub fn console(user_name: &'static str, vm_name: String) { + exec_cmd_from_name_or_id(user_name, &vm_name, "console") +} + +pub fn vncdisplay(user_name: &'static str, vm_name: String) { + exec_cmd_from_name_or_id(user_name, &vm_name, "vncdisplay") } -fn get_vm_name(user_name: &'static str, vm_name: String) -> String { +fn exec_cmd_from_name_or_id(user_name: &'static str, name_or_id: &str, command: &str) { + if name_or_id.parse::<u8>().is_ok() { + let vm_name_or_none = get_vm_name_from_id(user_name, &name_or_id); + if let Some(vm_name) = vm_name_or_none { + return virsh::command_require_vm_name(vm_name, command); + } + } + virsh::command_require_vm_name(get_vm_name(user_name, name_or_id), command); +} + +fn get_vm_name_from_id(user_name: &'static str, arg_id: &str) -> Option<String> { + let (_, vm_list_strs) = virsh::get_vm_list(user_name); + for vm in vm_list_strs { + let mut splitted = vm.split_whitespace(); + let vm_id = splitted.next().unwrap(); + if (vm_id != "-") && (vm_id == arg_id) { + let vm_name = splitted.next().unwrap(); + return Some(String::from(vm_name)); + } + } + None +} + +fn get_vm_name(user_name: &'static str, vm_name: &str) -> String { let vm_name = match vm_name.parse::<u8>() { Ok(_) => format!("{}-{}", user_name, vm_name), - Err(_) => vm_name, + Err(_) => vm_name.to_string(), }; if vm_name.starts_with(user_name) {
--- a/src/main.rs Sun Nov 01 14:02:35 2020 +0900 +++ b/src/main.rs Mon Nov 02 16:09:53 2020 +0900 @@ -18,7 +18,15 @@ Console(Console), Start(Start), Ttyconsole(TTyConsole), - VNCDisplay(VNCDisplay), //todo define-gdb dumpxml + VNCDisplay(VNCDisplay), + DumpXML(DumpXML), + DefineGDB(DefineGDB), +} + +/// define the domain in which the gdb port is opened from the template XML file +#[derive(Clap)] +struct DefineGDB { + name: String, } /// define (but don't start) a domain from an template XML file @@ -27,10 +35,16 @@ name: String, } -// vncdisplay +/// domain information in XML +#[derive(Clap)] +struct DumpXML { + name_or_id: String, +} + +/// vncdisplay #[derive(Clap)] struct VNCDisplay { - name_or_id: String, + name: String, } /// undefine a domain @@ -48,7 +62,7 @@ /// tty console #[derive(Clap)] struct TTyConsole { - name_or_id: String, + name: String, } /// gracefully shutdown a domain @@ -70,7 +84,7 @@ /// connect to the guest console #[derive(Clap)] struct Console { - name_or_id: String, + name: String, } /* @@ -92,16 +106,35 @@ match opts.subcmd { SubCommand::List(_) => { user::set_root_id(); - command::list_command(user_name); + command::list(user_name); } + SubCommand::Start(arg) => { user::set_root_id(); - command::start_command(user_name, arg.name); + command::start(user_name, arg.name); } SubCommand::Define(name) => {} - SubCommand::Shutdown(name) => {} - SubCommand::Console(name) => {} - SubCommand::Destroy(name_or_id) => {} + + SubCommand::Shutdown(arg) => { + user::set_root_id(); + command::shutdown(user_name, arg.name); + } + + SubCommand::Console(arg) => { + user::set_root_id(); + command::console(user_name, arg.name); + } + + SubCommand::Destroy(arg) => { + user::set_root_id(); + command::destroy(user_name, arg.name); + } + + SubCommand::VNCDisplay(arg) => { + user::set_root_id(); + command::vncdisplay(user_name, arg.name); + } + _ => {} }