Mercurial > hg > Members > anatofuz > ie-virsh
changeset 26:afec42bdd5ab
...
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Fri, 20 Nov 2020 14:00:52 +0900 |
parents | da27437a94b0 |
children | c0f0181ee91a |
files | src/command.rs src/main.rs src/user.rs src/xml.rs |
diffstat | 4 files changed, 89 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/src/command.rs Tue Nov 03 18:38:03 2020 +0900 +++ b/src/command.rs Fri Nov 20 14:00:52 2020 +0900 @@ -1,6 +1,6 @@ +use super::user; use super::virsh; use super::xml; -use super::user; pub fn list(user_name: &str) { let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name); @@ -11,12 +11,11 @@ } } -pub fn define(user : &user::User , vm_name: String) { - let vm_name = generate_vm_name(&user.Name, &vm_name); - let vm_arg = xml::GenerateVMArg::new(&user.Name, &vm_name, false); - let result = vm_arg.generate(); - if result.is_ok() { - virsh::command_require_vm_name(result.unwrap(), "define") +pub fn define(user: &user::User, vm_name: String) { + let vm_name = generate_vm_name(&user.name, &vm_name); + let vm_arg = xml::GenerateVMArg::new(&user.name, &vm_name, false); + if let Ok(xml_path) = vm_arg.generate() { + virsh::command_require_vm_name(xml_path, "define") } } @@ -60,6 +59,8 @@ exec_cmd_from_name_or_id(user_name, &vm_name, "dominfo") } +pub fn dumpvncpasswd(user_name: &str, vm_name: String) {} + 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); @@ -95,8 +96,7 @@ format!("{}-{}", user_name, vm_name) } - -fn generate_vm_name(user_name:&str, vm_name: &str) -> String { +fn generate_vm_name(user_name: &str, vm_name: &str) -> String { if vm_name.starts_with(user_name) { return vm_name.to_string(); }
--- a/src/main.rs Tue Nov 03 18:38:03 2020 +0900 +++ b/src/main.rs Fri Nov 20 14:00:52 2020 +0900 @@ -122,10 +122,10 @@ SubCommand::Define(arg) => { user::set_root_id(); - let user = user::User{ - Uid: uid, - Gid: gid, - Name: user_name.to_string() + let user = user::User { + uid, + gid, + name: user_name, }; command::define(&user, arg.name); }
--- a/src/user.rs Tue Nov 03 18:38:03 2020 +0900 +++ b/src/user.rs Fri Nov 20 14:00:52 2020 +0900 @@ -1,7 +1,30 @@ pub struct User { - pub Uid: u32, - pub Gid: u32, - pub Name: String + pub uid: u32, + pub gid: u32, + pub name: String, +} + +pub struct UserDetail<'a> { + pub user: &'a User, + pub grade: String, +} + +impl<'a> UserDetail<'a> { + pub fn new(user: &'a User) -> UserDetail { + // e195730 -> 19 + let year = user.name.chars().skip(1).take(2).collect::<String>(); + // e195730 -> e19, tnal -> teacher + let grade = if year.parse::<u8>().is_ok() { + user.name.chars().take(3).collect::<String>() + } else { + "teacher".to_string() + }; + UserDetail { user, grade } + } + + pub fn getpass(self) -> String { + format!("{}/{}", self.grade, &self.user.name) + } } pub fn getlogin(uid: u32) -> String {
--- a/src/xml.rs Tue Nov 03 18:38:03 2020 +0900 +++ b/src/xml.rs Fri Nov 20 14:00:52 2020 +0900 @@ -1,3 +1,5 @@ +use super::user; + use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; use quick_xml::{Reader, Writer}; use rand::Rng; @@ -30,12 +32,31 @@ const QCOW2_PATH: &str = "/mnt/ie-virsh"; pub struct GenerateVMArg { - VMName: String, - Qcow2PATH: String, - XMLPATH: String, - VNCPassword: String, - IsDebug: bool, - TCPPort: u64, + vm_name: String, + qcow2_path: String, + xml_path: String, + vnc_password: String, + is_debug: bool, + tcp_port: u64, +} + +pub fn dump_vnc_passwd(user: user::UserDetail, _vm_name: &str) -> Result<String, Error> { + let user_pass = user.getpass(); + let mut reader = Reader::from_reader(BufReader::new(File::open(get_xml_dir(&user_pass))?)); + let mut buf = Vec::new(); + loop { + match reader.read_event(&mut buf) { + Ok(Event::Eof) => break, + Ok(Event::Empty(ref e)) if e.name() == VNC_XML_TAG => {} + Ok(_) => {} + Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e), + } + } + Ok(String::from("ok")) +} + +fn get_xml_dir(user_path: &str) -> String { + format!("{}/{}", LIBVIRT_XML_DIR, user_path) } impl GenerateVMArg { @@ -49,7 +70,7 @@ }; let xml_dir = format!("{}/{}/{}", LIBVIRT_XML_DIR, affilication, user_name); - let xml_path = format!("{}/{}.xml", xml_dir.clone(), vm_name); + let xml_path = format!("{}/{}.xml", xml_dir, vm_name); if !Path::new(&xml_dir).exists() { fs::create_dir_all(xml_dir).ok(); @@ -57,7 +78,7 @@ let qcow2_dir = format!("{}/{}/{}", QCOW2_PATH, affilication, user_name); - let qcow2_path = format!("{}/{}.qcow2", qcow2_dir.clone(), vm_name); + let qcow2_path = format!("{}/{}.qcow2", qcow2_dir, vm_name); if !Path::new(&qcow2_dir).exists() { fs::create_dir_all(qcow2_dir).ok(); @@ -66,20 +87,20 @@ let pw = generate_pw(); GenerateVMArg { - VMName: vm_name.to_string(), - Qcow2PATH: qcow2_path, - XMLPATH: xml_path, - VNCPassword: pw, - IsDebug: is_debug, - TCPPort: 90, + vm_name: vm_name.to_string(), + qcow2_path, + xml_path, + vnc_password: pw, + is_debug, + tcp_port: 90, } } pub fn generate(self) -> Result<String, Error> { let mut reader = Reader::from_reader(BufReader::new(File::open(TEMPLATE_XML_FILE)?)); - print!("generate xml :{}\n", self.XMLPATH); - let mut writer = Writer::new(BufWriter::new(File::create(self.XMLPATH.clone()).unwrap())); + println!("generate xml :{}", self.xml_path); + let mut writer = Writer::new(BufWriter::new(File::create(self.xml_path.clone()).unwrap())); let mut buf = Vec::new(); loop { match reader.read_event(&mut buf) { @@ -95,7 +116,7 @@ writer.write_event(Event::Text(elem)).unwrap(); } - Ok(Event::Start(ref e)) if (e.name() == ROOT_START_TAG && self.IsDebug) => { + Ok(Event::Start(ref e)) if (e.name() == ROOT_START_TAG && self.is_debug) => { let mut elem = e.clone(); elem.push_attribute(DOMAIN_XMLNS_QEMU); writer.write_event(Event::Start(elem)).unwrap(); @@ -105,13 +126,18 @@ .write_event(Event::Start(qemu_command_line_start)) .unwrap(); - for value in ["-S", "-gdb", "tcp"].iter() { + for value in ["-S", "-gdb"].iter() { let mut qemu_elem = BytesStart::borrowed_name(QEMU_ARG_TAG); let v: &str = &value; qemu_elem.push_attribute(("value", v)); writer.write_event(Event::Empty(qemu_elem)).unwrap(); } + let mut qemu_elem = BytesStart::borrowed_name(QEMU_ARG_TAG); + let gdb_port: &str = &format!("tcp::{}", self.tcp_port); + qemu_elem.push_attribute(("value", gdb_port)); + writer.write_event(Event::Empty(qemu_elem)).unwrap(); + let qemu_command_line_end = BytesEnd::borrowed(QEMU_COMMAND_LINE_TAG); writer .write_event(Event::End(qemu_command_line_end)) @@ -120,7 +146,7 @@ Ok(Event::Empty(ref e)) if e.name() == VNC_XML_TAG => { let mut elem = e.clone(); - let pw: &str = &self.VNCPassword; + let pw: &str = &self.vnc_password; elem.push_attribute(("passwd", pw)); writer.write_event(Event::Empty(elem)).ok(); } @@ -132,14 +158,14 @@ .find(|attr| attr.as_ref().unwrap().key == b"file"); if is_qcow_file.is_some() { elem.clear_attributes(); - let qcow2_path: &str = &self.Qcow2PATH; + let qcow2_path: &str = &self.qcow2_path; elem.push_attribute(("file", qcow2_path)); } writer.write_event(Event::Empty(elem)).ok(); } Ok(Event::Text(ref e)) if e.escaped() == IE_VIRSH_TEMPLATE_VM_NAME => { - let elem = BytesText::from_plain_str(&self.VMName); + let elem = BytesText::from_plain_str(&self.vm_name); writer.write_event(Event::Text(elem)).unwrap(); } Ok(Event::Eof) => break, @@ -149,9 +175,9 @@ } buf.clear(); } - print!("generate xml : {}\n", self.XMLPATH); - print!("vnc password : {}\n", self.VNCPassword); - Ok(self.XMLPATH) + println!("generate xml : {}", self.xml_path); + println!("vnc password : {}", self.vnc_password); + Ok(self.xml_path) } }