Mercurial > hg > Members > anatofuz > ie-virsh
view src/command.rs @ 75:b4d2ef1897f9 default tip master
update README
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Sun, 20 Dec 2020 23:04:41 +0900 |
parents | 2fb3f3207bbc |
children |
line wrap: on
line source
use crate::user; use crate::virsh; use crate::xml; use std::io; use std::net::TcpListener; const RENTAL_DIR: &str = "/ie-ryukyu/kvm/images/rental"; 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_detail: &user::UserDetail, vm_name: &str) { let vm_name = generate_vm_name(&user_detail.user.name, &vm_name); let builder = xml::Builder::new(&user_detail, &vm_name).finalize(); if let Ok(xml_path) = builder.generate() { virsh::subshell_virsh_command(&xml_path, "define") } } pub fn define_gdb(user_detail: &user::UserDetail, vm_name: &str) { let vm_name = generate_vm_name(&user_detail.user.name, &vm_name); let port = match get_available_port() { Ok(port) => port, Err(e) => { println!("[error]get_avalable_port {}", e); return; } }; println!("gdb debug port: {}", port); let builder = xml::Builder::new(&user_detail, &vm_name) .debug_tcp_port(port) .finalize(); if let Ok(xml_path) = builder.generate() { virsh::subshell_virsh_command(&xml_path, "define") } } pub fn define_from_template(user_detail: &user::UserDetail, vm_name: &str, template: &str) { let vm_name = generate_vm_name(&user_detail.user.name, &vm_name); println!("{}", vm_name); let backing_path = match virsh::get_backing_path(template) { Some(path) => path, None => { println!("not found {}", template); return; } }; let builder = xml::Builder::new(&user_detail, &vm_name) .backing(&backing_path) .qcow2_dir(RENTAL_DIR) .finalize(); let xml_path = match builder.generate() { Ok(xml_path) => xml_path, Err(e) => { println!("failed generate xml: {}", e); return; } }; virsh::generate_qemu_from_template(RENTAL_DIR, &vm_name, &backing_path); virsh::subshell_virsh_command(&xml_path, "define"); } pub fn start(user_name: &str, vm_name: &str) { virsh::subshell_virsh_command(&get_vm_name(user_name, vm_name), "start"); } pub fn subshell(user_name: &str, vm_name: &str, operation: &str) { let vm_name = exec_cmd_from_name_or_id(user_name, vm_name); virsh::subshell_virsh_command(&vm_name, operation); } pub fn vol_delete(user_name: &str, vm_name: &str, operation: &str) { let vm_name = exec_cmd_from_name_or_id(user_name, vm_name); virsh::virsh_vol_delete_command(&vm_name, operation); } pub fn exec(user_name: &str, vm_name: &str, operation: &str) { let vm_name = exec_cmd_from_name_or_id(user_name, vm_name); virsh::exec_virsh_command(&vm_name, operation); } pub fn dumpvncpasswd(_user_name: &str, _vm_name: &str) {} fn exec_cmd_from_name_or_id(user_name: &str, name_or_id: &str) -> String { 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 vm_name; } } get_vm_name(user_name, name_or_id) } 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) } fn get_available_port() -> Result<u16, std::io::Error> { match TcpListener::bind("localhost:0") { Ok(listener) => Ok(listener.local_addr().unwrap().port()), Err(e) => Err(e), } }