comparison src/xml.rs @ 41:63e77a9de3ab

...
author AnaTofuZ <anatofuz@gmail.com>
date Sun, 22 Nov 2020 16:48:05 +0900
parents f667f3a4bbee
children 2787ada7650b
comparison
equal deleted inserted replaced
40:d3055f6c6fb7 41:63e77a9de3ab
54 user_name: String, 54 user_name: String,
55 vm_name: String, 55 vm_name: String,
56 debug_tcp_port: Option<u64>, 56 debug_tcp_port: Option<u64>,
57 backing_file: String, 57 backing_file: String,
58 is_backing: bool, 58 is_backing: bool,
59 xml_dir: String,
60 qcow2_dir: String,
59 } 61 }
60 62
61 pub struct GenerateVM { 63 pub struct GenerateVM {
62 vm_name: String, 64 vm_name: String,
63 qcow2_path: String, 65 qcow2_path: String,
67 backing_file: String, 69 backing_file: String,
68 is_backing: bool, 70 is_backing: bool,
69 } 71 }
70 72
71 impl Builder { 73 impl Builder {
72 pub fn new(user_name: &str, vm_name: &str) -> Builder { 74 pub fn new(user_detail: &user::UserDetail, vm_name: &str) -> Builder {
75 let xml_dir = format!(
76 "{}/{}/{}",
77 LIBVIRT_XML_DIR, user_detail.affilication, &user_detail.user.name
78 );
79
80 if !Path::new(&xml_dir).exists() {
81 fs::create_dir_all(xml_dir).ok();
82 }
83
84 let qcow2_dir = format!(
85 "{}/{}/{}",
86 QCOW2_PATH, user_detail.affilication, &user_detail.user.name
87 );
88
73 Builder { 89 Builder {
74 user_name: user_name.to_string(), 90 user_name: user_detail.user.name.clone(),
75 vm_name: vm_name.to_string(), 91 vm_name: vm_name.to_string(),
76 debug_tcp_port: None, 92 debug_tcp_port: None,
77 backing_file: "".to_string(), 93 backing_file: "".to_string(),
78 is_backing: false, 94 is_backing: false,
95 xml_dir,
96 qcow2_dir,
79 } 97 }
80 } 98 }
81 99
82 pub fn debug_tcp_port(&mut self, port: Option<u64>) -> &mut Builder { 100 pub fn debug_tcp_port(&mut self, port: Option<u64>) -> &mut Builder {
83 self.debug_tcp_port = port; 101 self.debug_tcp_port = port;
88 self.backing_file = backing_file.to_string(); 106 self.backing_file = backing_file.to_string();
89 self 107 self
90 } 108 }
91 109
92 pub fn finalize(&self) -> GenerateVM { 110 pub fn finalize(&self) -> GenerateVM {
93 let year = self.user_name.chars().skip(1).take(2).collect::<String>(); 111 let xml_path = format!("{}/{}.xml", &self.xml_dir, self.vm_name);
94 let affilication = if year.parse::<u8>().is_ok() { 112
95 // /etc/libvirt/qemu/e19/e195729 113 if !Path::new(&self.xml_dir).exists() {
96 self.user_name.chars().take(3).collect::<String>() 114 fs::create_dir_all(&self.xml_dir).ok();
97 } else { 115 }
98 "teacher".to_string() 116
99 }; 117 let qcow2_path = format!("{}/{}.qcow2", &self.qcow2_dir, self.vm_name);
100 118
101 let xml_dir = format!("{}/{}/{}", LIBVIRT_XML_DIR, affilication, self.user_name); 119 if !Path::new(&self.qcow2_dir).exists() {
102 let xml_path = format!("{}/{}.xml", xml_dir, self.vm_name); 120 fs::create_dir_all(&self.qcow2_dir).ok();
103
104 if !Path::new(&xml_dir).exists() {
105 fs::create_dir_all(xml_dir).ok();
106 }
107
108 let qcow2_dir = format!("{}/{}/{}", QCOW2_PATH, affilication, self.user_name);
109
110 let qcow2_path = format!("{}/{}.qcow2", qcow2_dir, self.vm_name);
111
112 if !Path::new(&qcow2_dir).exists() {
113 fs::create_dir_all(qcow2_dir).ok();
114 } 121 }
115 122
116 let pw = generate_pw(); 123 let pw = generate_pw();
117
118 124
119 GenerateVM { 125 GenerateVM {
120 vm_name: self.vm_name.clone(), 126 vm_name: self.vm_name.clone(),
121 qcow2_path, 127 qcow2_path,
122 xml_path, 128 xml_path,