annotate src/xml.rs @ 29:0b43942c8950

use &str at const
author AnaTofuZ <anatofuz@gmail.com>
date Sat, 21 Nov 2020 16:46:21 +0900
parents afec42bdd5ab
children ba66504b5256
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
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
34 pub struct GenerateVMArg {
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
35 vm_name: String,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
36 qcow2_path: String,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
37 xml_path: String,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
38 vnc_password: String,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
39 is_debug: bool,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
40 tcp_port: u64,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
41 }
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
42
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
43 pub fn dump_vnc_passwd(user: user::UserDetail, _vm_name: &str) -> Result<String, Error> {
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
44 let user_pass = user.getpass();
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
45 let mut reader = Reader::from_reader(BufReader::new(File::open(get_xml_dir(&user_pass))?));
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
46 let mut buf = Vec::new();
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
47 loop {
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
48 match reader.read_event(&mut buf) {
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
49 Ok(Event::Eof) => break,
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
50 Ok(Event::Empty(ref e)) if e.name() == VNC_XML_TAG.as_bytes() => {}
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
51 Ok(_) => {}
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
52 Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
53 }
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
54 }
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
55 Ok(String::from("ok"))
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
56 }
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
57
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
58 fn get_xml_dir(user_path: &str) -> String {
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
59 format!("{}/{}", LIBVIRT_XML_DIR, user_path)
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
60 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
61
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
62 impl GenerateVMArg {
24
e8ba0f63c227 remove static variable
AnaTofuZ <anatofuz@gmail.com>
parents: 21
diff changeset
63 pub fn new(user_name: &str, vm_name: &str, is_debug: bool) -> GenerateVMArg {
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
64 let year = user_name.chars().skip(1).take(2).collect::<String>();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
65 let affilication = if year.parse::<u8>().is_ok() {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
66 // /etc/libvirt/qemu/e19/e195729
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
67 user_name.chars().take(3).collect::<String>()
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
68 } else {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
69 "teacher".to_string()
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
70 };
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
71
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
72 let xml_dir = format!("{}/{}/{}", LIBVIRT_XML_DIR, affilication, user_name);
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
73 let xml_path = format!("{}/{}.xml", xml_dir, vm_name);
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
74
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
75 if !Path::new(&xml_dir).exists() {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
76 fs::create_dir_all(xml_dir).ok();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
77 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
78
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
79 let qcow2_dir = format!("{}/{}/{}", QCOW2_PATH, affilication, user_name);
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
80
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
81 let qcow2_path = format!("{}/{}.qcow2", qcow2_dir, vm_name);
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
83 if !Path::new(&qcow2_dir).exists() {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
84 fs::create_dir_all(qcow2_dir).ok();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
85 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
86
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
87 let pw = generate_pw();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
88
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
89 GenerateVMArg {
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
90 vm_name: vm_name.to_string(),
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
91 qcow2_path,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
92 xml_path,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
93 vnc_password: pw,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
94 is_debug,
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
95 tcp_port: 90,
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
96 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
97 }
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98
21
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
99 pub fn generate(self) -> Result<String, Error> {
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
100 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
101
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
102 println!("generate xml :{}", self.xml_path);
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
103 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
104 let mut buf = Vec::new();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
105 loop {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
106 match reader.read_event(&mut buf) {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
107 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
108 writer
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
109 .write_event(Event::Start(e.clone()))
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
110 .expect("faild write event");
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
111 reader
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
112 .read_event(&mut Vec::new())
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
113 .expect("faild read event");
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
114 let vm_uuid = Uuid::new_v4().to_string();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
115 let elem = BytesText::from_plain_str(&vm_uuid);
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
116 writer.write_event(Event::Text(elem)).unwrap();
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
119 Ok(Event::Start(ref e))
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
120 if (e.name() == ROOT_START_TAG.as_bytes() && self.is_debug) =>
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
121 {
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
122 let mut elem = e.clone();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
123 elem.push_attribute(DOMAIN_XMLNS_QEMU);
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
124 writer.write_event(Event::Start(elem)).unwrap();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
125
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
126 let qemu_command_line_start =
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
127 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
128 writer
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
129 .write_event(Event::Start(qemu_command_line_start))
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
130 .unwrap();
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
132 for value in ["-S", "-gdb"].iter() {
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
133 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
134 let v: &str = &value;
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
135 qemu_elem.push_attribute(("value", v));
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
136 writer.write_event(Event::Empty(qemu_elem)).unwrap();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
137 }
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
139 let mut qemu_elem = BytesStart::borrowed_name(QEMU_ARG_TAG.as_bytes());
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
140 let gdb_port: &str = &format!("tcp::{}", self.tcp_port);
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
141 qemu_elem.push_attribute(("value", gdb_port));
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
142 writer.write_event(Event::Empty(qemu_elem)).unwrap();
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
143
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
144 let qemu_command_line_end =
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
145 BytesEnd::borrowed(QEMU_COMMAND_LINE_TAG.as_bytes());
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
146 writer
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
147 .write_event(Event::End(qemu_command_line_end))
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
148 .unwrap();
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 }
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
150
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
151 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
152 let mut elem = e.clone();
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
153 let pw: &str = &self.vnc_password;
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
154 elem.push_attribute(("passwd", pw));
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
155 writer.write_event(Event::Empty(elem)).ok();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
156 }
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
158 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
159 let mut elem = e.clone();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
160 let is_qcow_file = elem
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
161 .attributes()
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
162 .find(|attr| attr.as_ref().unwrap().key == b"file");
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
163 if is_qcow_file.is_some() {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
164 elem.clear_attributes();
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
165 let qcow2_path: &str = &self.qcow2_path;
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
166 elem.push_attribute(("file", qcow2_path));
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
167 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
168 writer.write_event(Event::Empty(elem)).ok();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
169 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
170
29
0b43942c8950 use &str at const
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
171 Ok(Event::Text(ref e)) if e.escaped() == IE_VIRSH_TEMPLATE_VM_NAME.as_bytes() => {
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
172 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
173 writer.write_event(Event::Text(elem)).unwrap();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
174 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
175 Ok(Event::Eof) => break,
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
176 // 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
177 Ok(e) => assert!(writer.write_event(&e).is_ok()),
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
178 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
179 }
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
180 buf.clear();
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 }
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
182 println!("generate xml : {}", self.xml_path);
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
183 println!("vnc password : {}", self.vnc_password);
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
184 Ok(self.xml_path)
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 fn generate_pw() -> String {
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 let mut rng = rand::thread_rng();
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 (0..PASSWORD_LEN)
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 .map(|_| {
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 let idx = rng.gen_range(0, CHARSET.len());
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 CHARSET[idx] as char
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 })
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 .collect()
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 }