annotate src/xml.rs @ 47:9a7ceb63efbc

fix emit xml
author AnaTofuZ <anatofuz@gmail.com>
date Sun, 22 Nov 2020 18:30:16 +0900
parents 868d51208aa3
children 871a98179dfe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
1 use super::user;
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
2
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 use quick_xml::{Reader, Writer};
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 use rand::Rng;
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
6 use std::fs;
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 use std::fs::File;
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
8 use std::io::{BufReader, BufWriter, Error};
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 use std::path::Path;
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 use uuid::Uuid;
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 abcdefghijklmnopqrstuvwxyz\
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
14 0123456789)(*^%$#@!~";
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 const PASSWORD_LEN: usize = 30;
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 const DOMAIN_XMLNS_QEMU: (&str, &str) =
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 ("xmlns:qemu", "http://libvirt.org/schemas/domain/qemu/1.0");
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
21 const IE_VIRSH_TEMPLATE_VM_NAME: &str = "ie-virsh-template";
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
22 const VNC_XML_TAG: &str = "graphics";
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
24 const ROOT_START_TAG: &str = "domain";
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
26 const QEMU_COMMAND_LINE_TAG: &str = "qemu:commandline";
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
27 const QEMU_ARG_TAG: &str = "qemu:arg";
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
29 const TEMPLATE_XML_FILE: &str = "/etc/libvirt/template.xml";
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
30
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
31 const LIBVIRT_XML_DIR: &str = "/etc/libvirt/qemu";
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
32 const QCOW2_PATH: &str = "/mnt/ie-virsh";
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
33
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
34 pub fn dump_vnc_passwd(user: user::UserDetail, _vm_name: &str) -> Result<String, Error> {
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
35 let user_pass = user.getpass();
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
36 let mut reader = Reader::from_reader(BufReader::new(File::open(get_xml_dir(&user_pass))?));
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
37 let mut buf = Vec::new();
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
38 loop {
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
39 match reader.read_event(&mut buf) {
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
40 Ok(Event::Eof) => break,
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
41 Ok(Event::Empty(ref e)) if e.name() == VNC_XML_TAG.as_bytes() => {}
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
42 Ok(_) => {}
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
43 Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
44 }
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
45 }
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
46 Ok(String::from("ok"))
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
47 }
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
48
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
49 fn get_xml_dir(user_path: &str) -> String {
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
50 format!("{}/{}", LIBVIRT_XML_DIR, user_path)
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
51 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
52
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
53 pub struct Builder {
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
54 vm_name: String,
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
55 debug_tcp_port: Option<u64>,
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
56 backing_file: String,
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
57 is_backing: bool,
41
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
58 xml_dir: String,
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
59 qcow2_dir: String,
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
60 }
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
61
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
62 pub struct GenerateVM {
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
63 vm_name: String,
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
64 qcow2_path: String,
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
65 xml_path: String,
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
66 vnc_password: String,
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
67 debug_tcp_port: Option<u64>,
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
68 backing_file: String,
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
69 is_backing: bool,
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
70 }
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
71
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
72 impl Builder {
41
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
73 pub fn new(user_detail: &user::UserDetail, vm_name: &str) -> Builder {
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
74 let xml_dir = format!(
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
75 "{}/{}/{}",
42
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
76 LIBVIRT_XML_DIR, &user_detail.affilication, &user_detail.user.name
41
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
77 );
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
78
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
79 let qcow2_dir = format!(
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
80 "{}/{}/{}",
42
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
81 QCOW2_PATH, &user_detail.affilication, &user_detail.user.name
41
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
82 );
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
83
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
84 Builder {
36
91a99ff9c4c5 String to &str
AnaTofuZ <anatofuz@gmail.com>
parents: 35
diff changeset
85 vm_name: vm_name.to_string(),
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
86 debug_tcp_port: None,
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
87 backing_file: "".to_string(),
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
88 is_backing: false,
41
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
89 xml_dir,
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
90 qcow2_dir,
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
91 }
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
92 }
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
93
42
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
94 pub fn xml_dir(&mut self, xml_dir: &str) -> &mut Builder {
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
95 self.xml_dir = xml_dir.to_string();
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
96 self
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
97 }
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
98
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
99 pub fn qcow2_dir(&mut self, qcow2_dir: &str) -> &mut Builder {
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
100 self.qcow2_dir = qcow2_dir.to_string();
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
101 self
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
102 }
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
103
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
104 pub fn debug_tcp_port(&mut self, port: Option<u64>) -> &mut Builder {
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
105 self.debug_tcp_port = port;
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
106 self
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
107 }
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
108
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
109 pub fn backing(&mut self, backing_file: &str) -> &mut Builder {
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
110 self.backing_file = backing_file.to_string();
44
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
111 self.is_backing = true;
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
112 self
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
113 }
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
114
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
115 pub fn finalize(&self) -> GenerateVM {
42
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
116 let xml_path = format!("{}/{}.xml", &self.xml_dir, &self.vm_name);
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
117
41
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
118 if !Path::new(&self.xml_dir).exists() {
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
119 fs::create_dir_all(&self.xml_dir).ok();
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
120 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
121
42
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
122 let qcow2_path = format!("{}/{}.qcow2", &self.qcow2_dir, &self.vm_name);
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
123
41
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
124 if !Path::new(&self.qcow2_dir).exists() {
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
125 fs::create_dir_all(&self.qcow2_dir).ok();
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
126 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
127
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
128 let pw = generate_pw();
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
129
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
130 GenerateVM {
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
131 vm_name: self.vm_name.clone(),
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
132 qcow2_path,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
133 xml_path,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
134 vnc_password: pw,
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
135 debug_tcp_port: self.debug_tcp_port,
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
136 backing_file: self.backing_file.clone(),
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
137 is_backing: self.is_backing,
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
138 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
139 }
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
140 }
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141
35
f589d5baab5f use builder
AnaTofuZ <anatofuz@gmail.com>
parents: 34
diff changeset
142 impl GenerateVM {
21
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
143 pub fn generate(self) -> Result<String, Error> {
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
144 let mut reader = Reader::from_reader(BufReader::new(File::open(TEMPLATE_XML_FILE)?));
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
146 println!("generate xml :{}", self.xml_path);
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
147 let mut writer = Writer::new(BufWriter::new(File::create(self.xml_path.clone()).unwrap()));
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
148 let mut buf = Vec::new();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
149 loop {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
150 match reader.read_event(&mut buf) {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
151 Ok(Event::Start(ref e)) if e.name() == b"uuid" => {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
152 writer
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
153 .write_event(Event::Start(e.clone()))
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
154 .expect("faild write event");
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
155 reader
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
156 .read_event(&mut Vec::new())
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
157 .expect("faild read event");
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
158 let vm_uuid = Uuid::new_v4().to_string();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
159 let elem = BytesText::from_plain_str(&vm_uuid);
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
160 writer.write_event(Event::Text(elem)).unwrap();
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
163 Ok(Event::Start(ref e))
33
ba66504b5256 emit backingfile at xml
AnaTofuZ <anatofuz@gmail.com>
parents: 29
diff changeset
164 if (e.name() == ROOT_START_TAG.as_bytes() && self.debug_tcp_port.is_some()) =>
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
165 {
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
166 let mut elem = e.clone();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
167 elem.push_attribute(DOMAIN_XMLNS_QEMU);
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
168 writer.write_event(Event::Start(elem)).unwrap();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
169
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
170 let qemu_command_line_start =
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
171 BytesStart::borrowed_name(QEMU_COMMAND_LINE_TAG.as_bytes());
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
172 writer
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
173 .write_event(Event::Start(qemu_command_line_start))
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
174 .unwrap();
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
176 for value in ["-S", "-gdb"].iter() {
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
177 let mut qemu_elem = BytesStart::borrowed_name(QEMU_ARG_TAG.as_bytes());
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
178 let v: &str = &value;
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
179 qemu_elem.push_attribute(("value", v));
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
180 writer.write_event(Event::Empty(qemu_elem)).unwrap();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
181 }
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
183 let mut qemu_elem = BytesStart::borrowed_name(QEMU_ARG_TAG.as_bytes());
33
ba66504b5256 emit backingfile at xml
AnaTofuZ <anatofuz@gmail.com>
parents: 29
diff changeset
184 let gdb_port: &str = &format!("tcp::{}", self.debug_tcp_port.unwrap());
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
185 qemu_elem.push_attribute(("value", gdb_port));
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
186 writer.write_event(Event::Empty(qemu_elem)).unwrap();
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
187
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
188 let qemu_command_line_end =
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
189 BytesEnd::borrowed(QEMU_COMMAND_LINE_TAG.as_bytes());
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
190 writer
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
191 .write_event(Event::End(qemu_command_line_end))
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
192 .unwrap();
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 }
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
194
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
195 Ok(Event::Empty(ref e)) if e.name() == VNC_XML_TAG.as_bytes() => {
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
196 let mut elem = e.clone();
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
197 let pw: &str = &self.vnc_password;
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
198 elem.push_attribute(("passwd", pw));
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
199 writer.write_event(Event::Empty(elem)).ok();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
200 }
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201
34
AnaTofuZ <anatofuz@gmail.com>
parents: 33
diff changeset
202 // replace qcow2 file
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
203 Ok(Event::Empty(ref e)) if (e.name() == b"source") => {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
204 let mut elem = e.clone();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
205 let is_qcow_file = elem
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
206 .attributes()
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
207 .find(|attr| attr.as_ref().unwrap().key == b"file");
46
868d51208aa3 fix path
AnaTofuZ <anatofuz@gmail.com>
parents: 44
diff changeset
208 let mut is_qcow_elem = false;
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
209 if is_qcow_file.is_some() {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
210 elem.clear_attributes();
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
211 let qcow2_path: &str = &self.qcow2_path;
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
212 elem.push_attribute(("file", qcow2_path));
46
868d51208aa3 fix path
AnaTofuZ <anatofuz@gmail.com>
parents: 44
diff changeset
213 is_qcow_elem = true;
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
214 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
215 writer.write_event(Event::Empty(elem)).ok();
44
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
216
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
217 // use template qcow2
46
868d51208aa3 fix path
AnaTofuZ <anatofuz@gmail.com>
parents: 44
diff changeset
218 if is_qcow_elem && self.is_backing {
47
9a7ceb63efbc fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 46
diff changeset
219 writer.write(b"\n").unwrap();
44
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
220 let mut backing_store_start = BytesStart::borrowed_name(b"backingStore");
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
221 backing_store_start.push_attribute(("type", "file"));
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
222 backing_store_start.push_attribute(("index", "3"));
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
223 writer
47
9a7ceb63efbc fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 46
diff changeset
224 .write_event(Event::Start(backing_store_start))
44
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
225 .unwrap();
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
226
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
227 writer.write(b"\n").unwrap();
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
228
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
229 let mut format_elem = BytesStart::borrowed_name(b"format");
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
230 format_elem.push_attribute(("type", "qcow2"));
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
231 writer.write_event(Event::Empty(format_elem)).unwrap();
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
232 writer.write(b"\n").unwrap();
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
233
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
234 let mut backing_sorce = BytesStart::borrowed_name(b"sorce");
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
235 let backing_file: &str = &self.backing_file;
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
236 backing_sorce.push_attribute(("file", backing_file));
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
237 writer.write_event(Event::Empty(backing_sorce)).unwrap();
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
238 writer.write(b"\n").unwrap();
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
239
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
240 let backing_store_end = BytesEnd::borrowed(b"backingStore");
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
241 writer.write_event(Event::End(backing_store_end)).unwrap();
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
242 writer.write(b"\n").unwrap();
9d5cb6b2046e fix emit xml
AnaTofuZ <anatofuz@gmail.com>
parents: 42
diff changeset
243 }
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
244 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
245
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
246 Ok(Event::Text(ref e)) if e.escaped() == IE_VIRSH_TEMPLATE_VM_NAME.as_bytes() => {
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
247 let elem = BytesText::from_plain_str(&self.vm_name);
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
248 writer.write_event(Event::Text(elem)).unwrap();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
249 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
250 Ok(Event::Eof) => break,
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
251 // you can use either `e` or `&e` if you don't want to move the event
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
252 Ok(e) => assert!(writer.write_event(&e).is_ok()),
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
253 Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 }
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
255 buf.clear();
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 }
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
257 println!("generate xml : {}", self.xml_path);
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
258 println!("vnc password : {}", self.vnc_password);
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
259 Ok(self.xml_path)
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 fn generate_pw() -> String {
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 let mut rng = rand::thread_rng();
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 (0..PASSWORD_LEN)
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 .map(|_| {
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 let idx = rng.gen_range(0, CHARSET.len());
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 CHARSET[idx] as char
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 })
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 .collect()
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 }