Mercurial > hg > Members > anatofuz > ie-virsh
changeset 41:63e77a9de3ab
...
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Sun, 22 Nov 2020 16:48:05 +0900 |
parents | d3055f6c6fb7 |
children | 2787ada7650b |
files | src/command.rs src/main.rs src/user.rs src/virsh.rs src/xml.rs |
diffstat | 5 files changed, 57 insertions(+), 43 deletions(-) [+] |
line wrap: on
line diff
--- a/src/command.rs Sun Nov 22 15:57:31 2020 +0900 +++ b/src/command.rs Sun Nov 22 16:48:05 2020 +0900 @@ -23,32 +23,36 @@ 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(); +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::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); +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 template_path = match virsh::get_template_path(template) { + let backing_path = match virsh::get_backing_path(template) { Some(path) => path, None => { println!("not found {}", template); return; } }; - let builder = xml::Builder::new(&user.name, &vm_name) - .backing(&template_path) + let builder = xml::Builder::new(&user_detail, &vm_name) + .backing(&backing_path) .finalize(); let xml_path = match builder.generate() { - Ok(xml_path) => xml_path, - Err(e) => { println!("{}", e); return;} + Ok(xml_path) => xml_path, + Err(e) => { + println!("failed generate xml: {}", e); + return; + } }; - virsh::command_require_vm_name(&xml_path, "define") + virsh::command_require_vm_name(&xml_path, "define"); + virsh::generate_qemu_from_template(&vm_name, "hoge"); } pub fn start(user_name: &str, vm_name: &str) {
--- a/src/main.rs Sun Nov 22 15:57:31 2020 +0900 +++ b/src/main.rs Sun Nov 22 16:48:05 2020 +0900 @@ -136,11 +136,13 @@ name: user_name, }; + let userdetail = user::UserDetail::new(&user); + if let Some(template) = arg.template { - command::define_from_template(&user, &arg.name, &template); + command::define_from_template(&userdetail, &arg.name, &template); return; } - command::define(&user, &arg.name); + command::define(&userdetail, &arg.name); } SubCommand::Shutdown(arg) => {
--- a/src/user.rs Sun Nov 22 15:57:31 2020 +0900 +++ b/src/user.rs Sun Nov 22 16:48:05 2020 +0900 @@ -6,7 +6,8 @@ pub struct UserDetail<'a> { pub user: &'a User, - pub grade: String, + pub year: String, + pub affilication: String, } impl<'a> UserDetail<'a> { @@ -14,16 +15,20 @@ // 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() { + let affilication = if year.parse::<u8>().is_ok() { user.name.chars().take(3).collect::<String>() } else { "teacher".to_string() }; - UserDetail { user, grade } + UserDetail { + user, + year, + affilication, + } } pub fn getpass(self) -> String { - format!("{}/{}", self.grade, &self.user.name) + format!("{}/{}", self.affilication, &self.user.name) } }
--- a/src/virsh.rs Sun Nov 22 15:57:31 2020 +0900 +++ b/src/virsh.rs Sun Nov 22 16:48:05 2020 +0900 @@ -5,7 +5,7 @@ use std::path::Path; const TEMPLATE_DIR: &str = "/ie-ryukyu/kvm/images/templates/"; -const RENTAL_DIR : &str = "/ie-ryukyu/kvm/images/rental/"; +const RENTAL_DIR: &str = "/ie-ryukyu/kvm/images/rental/"; const TEMPLATE_SUFFIX: &str = "template-"; const TEMPLATE_FILE_EXTENSION: &str = ".qcow2"; const QCOW2: &str = "qcow2"; @@ -70,10 +70,7 @@ } pub fn generate_qemu_from_template(vm_name: &str, template_path: &str) { - let vm_path = format!( - "{}{}{}", - RENTAL_DIR, &vm_name, TEMPLATE_FILE_EXTENSION - ); + let vm_path = format!("{}{}{}", RENTAL_DIR, &vm_name, TEMPLATE_FILE_EXTENSION); //$qemu-img create -F qcow2 -b ibm-master.qcow2 -f qcow2 ibm-02.qcow2 let output = Command::new("qemu-img") .arg("create") @@ -90,7 +87,7 @@ io::stderr().write_all(&output.stderr).unwrap(); } -pub fn get_template_path(template_name: &str) -> Option<String> { +pub fn get_backing_path(template_name: &str) -> Option<String> { let template_path = format!( "{}{}{}{}", TEMPLATE_DIR, TEMPLATE_SUFFIX, &template_name, TEMPLATE_FILE_EXTENSION
--- a/src/xml.rs Sun Nov 22 15:57:31 2020 +0900 +++ b/src/xml.rs Sun Nov 22 16:48:05 2020 +0900 @@ -56,6 +56,8 @@ debug_tcp_port: Option<u64>, backing_file: String, is_backing: bool, + xml_dir: String, + qcow2_dir: String, } pub struct GenerateVM { @@ -69,13 +71,29 @@ } impl Builder { - pub fn new(user_name: &str, vm_name: &str) -> Builder { + pub fn new(user_detail: &user::UserDetail, vm_name: &str) -> Builder { + let xml_dir = format!( + "{}/{}/{}", + LIBVIRT_XML_DIR, user_detail.affilication, &user_detail.user.name + ); + + if !Path::new(&xml_dir).exists() { + fs::create_dir_all(xml_dir).ok(); + } + + let qcow2_dir = format!( + "{}/{}/{}", + QCOW2_PATH, user_detail.affilication, &user_detail.user.name + ); + Builder { - user_name: user_name.to_string(), + user_name: user_detail.user.name.clone(), vm_name: vm_name.to_string(), debug_tcp_port: None, backing_file: "".to_string(), is_backing: false, + xml_dir, + qcow2_dir, } } @@ -90,32 +108,20 @@ } pub fn finalize(&self) -> GenerateVM { - let year = self.user_name.chars().skip(1).take(2).collect::<String>(); - let affilication = if year.parse::<u8>().is_ok() { - // /etc/libvirt/qemu/e19/e195729 - self.user_name.chars().take(3).collect::<String>() - } else { - "teacher".to_string() - }; + let xml_path = format!("{}/{}.xml", &self.xml_dir, self.vm_name); - let xml_dir = format!("{}/{}/{}", LIBVIRT_XML_DIR, affilication, self.user_name); - let xml_path = format!("{}/{}.xml", xml_dir, self.vm_name); - - if !Path::new(&xml_dir).exists() { - fs::create_dir_all(xml_dir).ok(); + if !Path::new(&self.xml_dir).exists() { + fs::create_dir_all(&self.xml_dir).ok(); } - let qcow2_dir = format!("{}/{}/{}", QCOW2_PATH, affilication, self.user_name); + let qcow2_path = format!("{}/{}.qcow2", &self.qcow2_dir, self.vm_name); - let qcow2_path = format!("{}/{}.qcow2", qcow2_dir, self.vm_name); - - if !Path::new(&qcow2_dir).exists() { - fs::create_dir_all(qcow2_dir).ok(); + if !Path::new(&self.qcow2_dir).exists() { + fs::create_dir_all(&self.qcow2_dir).ok(); } let pw = generate_pw(); - GenerateVM { vm_name: self.vm_name.clone(), qcow2_path,