Mercurial > hg > Members > anatofuz > ie-virsh
view src/command.rs @ 39:f667f3a4bbee
backing file cmd
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Sun, 22 Nov 2020 15:47:37 +0900 |
parents | 6f1b90844b7f |
children | d3055f6c6fb7 |
line wrap: on
line source
use super::user; use super::virsh; use super::xml; use std::io; pub fn list(user_name: &str) { let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name); println!("{}", ldump_msg.info_msg); println!("{}", ldump_msg.border_line); for vm_info in vm_list_strs { println!("{}", vm_info); } } pub fn templates() -> Result<(), io::Error> { let templates_list = virsh::get_template_list()?; for name in templates_list { println!("{}", name); } Ok(()) } pub fn define(user: &user::User, vm_name: &str) { let vm_name = generate_vm_name(&user.name, &vm_name); let builder = xml::Builder::new(&user.name, &vm_name).finalize(); if let Ok(xml_path) = builder.generate() { virsh::command_require_vm_name(&xml_path, "define") } } pub fn define_from_template(user: &user::User, vm_name: &str, template: &str) { let vm_name = generate_vm_name(&user.name, &vm_name); let template_path = match virsh::get_template_path(template) { Some(path) => path, None => { println!("not found {}", template); return; } }; let builder = xml::Builder::new(&user.name, &vm_name) .backing(&template_path) .finalize(); if let Ok(xml_path) = builder.generate() { virsh::command_require_vm_name(&xml_path, "define") } } pub fn start(user_name: &str, vm_name: &str) { virsh::command_require_vm_name(&get_vm_name(user_name, vm_name), "start"); } pub fn common(user_name: &str, vm_name: &str, command: &str) { exec_cmd_from_name_or_id(user_name, vm_name, command) } pub fn dumpvncpasswd(_user_name: &str, _vm_name: &str) {} fn exec_cmd_from_name_or_id(user_name: &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: &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: &str, vm_name: &str) -> String { let vm_name = match vm_name.parse::<u8>() { Ok(_) => format!("{}-{}", user_name, vm_name), Err(_) => vm_name.to_string(), }; if vm_name.starts_with(user_name) { return vm_name; } format!("{}-{}", user_name, vm_name) } fn generate_vm_name(user_name: &str, vm_name: &str) -> String { if vm_name.starts_with(user_name) { return vm_name.to_string(); } format!("{}-{}", user_name, vm_name) }