26
|
1 use super::user;
|
|
2
|
19
|
3 use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
|
|
4 use quick_xml::{Reader, Writer};
|
|
5 use rand::Rng;
|
20
|
6 use std::fs;
|
19
|
7 use std::fs::File;
|
20
|
8 use std::io::{BufReader, BufWriter, Error};
|
19
|
9 use std::path::Path;
|
|
10 use uuid::Uuid;
|
|
11
|
|
12 const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
|
13 abcdefghijklmnopqrstuvwxyz\
|
20
|
14 0123456789)(*^%$#@!~";
|
19
|
15
|
|
16 const PASSWORD_LEN: usize = 30;
|
|
17
|
|
18 const DOMAIN_XMLNS_QEMU: (&str, &str) =
|
|
19 ("xmlns:qemu", "http://libvirt.org/schemas/domain/qemu/1.0");
|
|
20
|
29
|
21 const IE_VIRSH_TEMPLATE_VM_NAME: &str = "ie-virsh-template";
|
|
22 const VNC_XML_TAG: &str = "graphics";
|
19
|
23
|
29
|
24 const ROOT_START_TAG: &str = "domain";
|
19
|
25
|
29
|
26 const QEMU_COMMAND_LINE_TAG: &str = "qemu:commandline";
|
|
27 const QEMU_ARG_TAG: &str = "qemu:arg";
|
19
|
28
|
20
|
29 const TEMPLATE_XML_FILE: &str = "/etc/libvirt/template.xml";
|
|
30
|
|
31 const LIBVIRT_XML_DIR: &str = "/etc/libvirt/qemu";
|
|
32 const QCOW2_PATH: &str = "/mnt/ie-virsh";
|
|
33
|
|
34 pub struct GenerateVMArg {
|
26
|
35 vm_name: String,
|
|
36 qcow2_path: String,
|
|
37 xml_path: String,
|
|
38 vnc_password: String,
|
|
39 is_debug: bool,
|
|
40 tcp_port: u64,
|
|
41 }
|
|
42
|
|
43 pub fn dump_vnc_passwd(user: user::UserDetail, _vm_name: &str) -> Result<String, Error> {
|
|
44 let user_pass = user.getpass();
|
|
45 let mut reader = Reader::from_reader(BufReader::new(File::open(get_xml_dir(&user_pass))?));
|
|
46 let mut buf = Vec::new();
|
|
47 loop {
|
|
48 match reader.read_event(&mut buf) {
|
|
49 Ok(Event::Eof) => break,
|
29
|
50 Ok(Event::Empty(ref e)) if e.name() == VNC_XML_TAG.as_bytes() => {}
|
26
|
51 Ok(_) => {}
|
|
52 Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
|
|
53 }
|
|
54 }
|
|
55 Ok(String::from("ok"))
|
|
56 }
|
|
57
|
|
58 fn get_xml_dir(user_path: &str) -> String {
|
|
59 format!("{}/{}", LIBVIRT_XML_DIR, user_path)
|
20
|
60 }
|
|
61
|
|
62 impl GenerateVMArg {
|
24
|
63 pub fn new(user_name: &str, vm_name: &str, is_debug: bool) -> GenerateVMArg {
|
20
|
64 let year = user_name.chars().skip(1).take(2).collect::<String>();
|
|
65 let affilication = if year.parse::<u8>().is_ok() {
|
|
66 // /etc/libvirt/qemu/e19/e195729
|
|
67 user_name.chars().take(3).collect::<String>()
|
|
68 } else {
|
|
69 "teacher".to_string()
|
|
70 };
|
|
71
|
|
72 let xml_dir = format!("{}/{}/{}", LIBVIRT_XML_DIR, affilication, user_name);
|
26
|
73 let xml_path = format!("{}/{}.xml", xml_dir, vm_name);
|
20
|
74
|
|
75 if !Path::new(&xml_dir).exists() {
|
|
76 fs::create_dir_all(xml_dir).ok();
|
|
77 }
|
|
78
|
|
79 let qcow2_dir = format!("{}/{}/{}", QCOW2_PATH, affilication, user_name);
|
|
80
|
26
|
81 let qcow2_path = format!("{}/{}.qcow2", qcow2_dir, vm_name);
|
19
|
82
|
20
|
83 if !Path::new(&qcow2_dir).exists() {
|
|
84 fs::create_dir_all(qcow2_dir).ok();
|
|
85 }
|
|
86
|
|
87 let pw = generate_pw();
|
|
88
|
|
89 GenerateVMArg {
|
26
|
90 vm_name: vm_name.to_string(),
|
|
91 qcow2_path,
|
|
92 xml_path,
|
|
93 vnc_password: pw,
|
|
94 is_debug,
|
|
95 tcp_port: 90,
|
20
|
96 }
|
|
97 }
|
19
|
98
|
21
|
99 pub fn generate(self) -> Result<String, Error> {
|
20
|
100 let mut reader = Reader::from_reader(BufReader::new(File::open(TEMPLATE_XML_FILE)?));
|
19
|
101
|
26
|
102 println!("generate xml :{}", self.xml_path);
|
|
103 let mut writer = Writer::new(BufWriter::new(File::create(self.xml_path.clone()).unwrap()));
|
20
|
104 let mut buf = Vec::new();
|
|
105 loop {
|
|
106 match reader.read_event(&mut buf) {
|
|
107 Ok(Event::Start(ref e)) if e.name() == b"uuid" => {
|
|
108 writer
|
|
109 .write_event(Event::Start(e.clone()))
|
|
110 .expect("faild write event");
|
|
111 reader
|
|
112 .read_event(&mut Vec::new())
|
|
113 .expect("faild read event");
|
|
114 let vm_uuid = Uuid::new_v4().to_string();
|
|
115 let elem = BytesText::from_plain_str(&vm_uuid);
|
|
116 writer.write_event(Event::Text(elem)).unwrap();
|
19
|
117 }
|
|
118
|
29
|
119 Ok(Event::Start(ref e))
|
|
120 if (e.name() == ROOT_START_TAG.as_bytes() && self.is_debug) =>
|
|
121 {
|
20
|
122 let mut elem = e.clone();
|
|
123 elem.push_attribute(DOMAIN_XMLNS_QEMU);
|
|
124 writer.write_event(Event::Start(elem)).unwrap();
|
|
125
|
29
|
126 let qemu_command_line_start =
|
|
127 BytesStart::borrowed_name(QEMU_COMMAND_LINE_TAG.as_bytes());
|
20
|
128 writer
|
|
129 .write_event(Event::Start(qemu_command_line_start))
|
|
130 .unwrap();
|
19
|
131
|
26
|
132 for value in ["-S", "-gdb"].iter() {
|
29
|
133 let mut qemu_elem = BytesStart::borrowed_name(QEMU_ARG_TAG.as_bytes());
|
20
|
134 let v: &str = &value;
|
|
135 qemu_elem.push_attribute(("value", v));
|
|
136 writer.write_event(Event::Empty(qemu_elem)).unwrap();
|
|
137 }
|
19
|
138
|
29
|
139 let mut qemu_elem = BytesStart::borrowed_name(QEMU_ARG_TAG.as_bytes());
|
26
|
140 let gdb_port: &str = &format!("tcp::{}", self.tcp_port);
|
|
141 qemu_elem.push_attribute(("value", gdb_port));
|
|
142 writer.write_event(Event::Empty(qemu_elem)).unwrap();
|
|
143
|
29
|
144 let qemu_command_line_end =
|
|
145 BytesEnd::borrowed(QEMU_COMMAND_LINE_TAG.as_bytes());
|
20
|
146 writer
|
|
147 .write_event(Event::End(qemu_command_line_end))
|
|
148 .unwrap();
|
19
|
149 }
|
20
|
150
|
29
|
151 Ok(Event::Empty(ref e)) if e.name() == VNC_XML_TAG.as_bytes() => {
|
20
|
152 let mut elem = e.clone();
|
26
|
153 let pw: &str = &self.vnc_password;
|
20
|
154 elem.push_attribute(("passwd", pw));
|
|
155 writer.write_event(Event::Empty(elem)).ok();
|
|
156 }
|
19
|
157
|
20
|
158 Ok(Event::Empty(ref e)) if (e.name() == b"source") => {
|
|
159 let mut elem = e.clone();
|
|
160 let is_qcow_file = elem
|
|
161 .attributes()
|
|
162 .find(|attr| attr.as_ref().unwrap().key == b"file");
|
|
163 if is_qcow_file.is_some() {
|
|
164 elem.clear_attributes();
|
26
|
165 let qcow2_path: &str = &self.qcow2_path;
|
20
|
166 elem.push_attribute(("file", qcow2_path));
|
|
167 }
|
|
168 writer.write_event(Event::Empty(elem)).ok();
|
|
169 }
|
|
170
|
29
|
171 Ok(Event::Text(ref e)) if e.escaped() == IE_VIRSH_TEMPLATE_VM_NAME.as_bytes() => {
|
26
|
172 let elem = BytesText::from_plain_str(&self.vm_name);
|
20
|
173 writer.write_event(Event::Text(elem)).unwrap();
|
|
174 }
|
|
175 Ok(Event::Eof) => break,
|
|
176 // you can use either `e` or `&e` if you don't want to move the event
|
|
177 Ok(e) => assert!(writer.write_event(&e).is_ok()),
|
|
178 Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
|
19
|
179 }
|
20
|
180 buf.clear();
|
19
|
181 }
|
26
|
182 println!("generate xml : {}", self.xml_path);
|
|
183 println!("vnc password : {}", self.vnc_password);
|
|
184 Ok(self.xml_path)
|
19
|
185 }
|
|
186 }
|
|
187
|
|
188 fn generate_pw() -> String {
|
|
189 let mut rng = rand::thread_rng();
|
|
190
|
|
191 (0..PASSWORD_LEN)
|
|
192 .map(|_| {
|
|
193 let idx = rng.gen_range(0, CHARSET.len());
|
|
194 CHARSET[idx] as char
|
|
195 })
|
|
196 .collect()
|
|
197 }
|