diff src/xml.rs @ 41:63e77a9de3ab

...
author AnaTofuZ <anatofuz@gmail.com>
date Sun, 22 Nov 2020 16:48:05 +0900
parents f667f3a4bbee
children 2787ada7650b
line wrap: on
line diff
--- 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,