13
|
1 use std::io::{self, Write};
|
12
|
2 use std::process::Command;
|
|
3
|
43
|
4 use std::env;
|
32
|
5 use std::fs;
|
39
|
6 use std::path::Path;
|
32
|
7
|
|
8 const TEMPLATE_DIR: &str = "/ie-ryukyu/kvm/images/templates/";
|
|
9 const TEMPLATE_SUFFIX: &str = "template-";
|
|
10 const TEMPLATE_FILE_EXTENSION: &str = ".qcow2";
|
39
|
11 const QCOW2: &str = "qcow2";
|
32
|
12
|
12
|
13 pub struct ListDumpMsg {
|
|
14 pub info_msg: String,
|
|
15 pub border_line: String,
|
|
16 }
|
|
17
|
32
|
18 pub fn get_template_list() -> Result<Vec<String>, io::Error> {
|
|
19 let mut entries = fs::read_dir(TEMPLATE_DIR)?
|
|
20 .map(|res| {
|
|
21 res.map(|e| {
|
|
22 e.path()
|
|
23 .display()
|
|
24 .to_string()
|
|
25 .replace(TEMPLATE_DIR, "")
|
|
26 .replace(TEMPLATE_SUFFIX, "")
|
|
27 .replace(TEMPLATE_FILE_EXTENSION, "")
|
|
28 })
|
|
29 })
|
|
30 .collect::<Result<Vec<_>, io::Error>>()?;
|
|
31
|
|
32 entries.sort();
|
|
33 Ok(entries)
|
|
34 }
|
|
35
|
24
|
36 pub fn get_vm_list(user_name: &str) -> (ListDumpMsg, Vec<String>) {
|
12
|
37 let output = Command::new("virsh")
|
|
38 .arg("list")
|
|
39 .arg("--all")
|
|
40 .output()
|
|
41 .expect("failed to virsh");
|
|
42 let virsh_list = String::from_utf8_lossy(&output.stdout);
|
15
|
43 let mut virsh_list = virsh_list.split('\n');
|
12
|
44
|
|
45 let info_msg = virsh_list.next().unwrap();
|
|
46 let border_line = virsh_list.next().unwrap();
|
|
47 let ldump_msg = ListDumpMsg {
|
|
48 info_msg: String::from(info_msg),
|
|
49 border_line: String::from(border_line),
|
|
50 };
|
|
51
|
15
|
52 (
|
12
|
53 ldump_msg,
|
|
54 virsh_list
|
|
55 .filter(|&x| x.contains(user_name))
|
|
56 .map(|x| x.to_string())
|
|
57 .collect(),
|
15
|
58 )
|
12
|
59 }
|
13
|
60
|
39
|
61 pub fn command_require_vm_name(vm_name: &str, operation: &str) {
|
13
|
62 let output = Command::new("virsh")
|
16
|
63 .arg(operation)
|
13
|
64 .arg(vm_name)
|
|
65 .output()
|
16
|
66 .unwrap_or_else(|_| panic!("failed to {}", operation));
|
13
|
67
|
|
68 io::stdout().write_all(&output.stdout).unwrap();
|
|
69 io::stderr().write_all(&output.stderr).unwrap();
|
|
70 }
|
39
|
71
|
43
|
72 pub fn generate_qemu_from_template(vm_path_string: &str, vm_name: &str, template_path: &str) {
|
|
73 let vm_path = Path::new(&vm_path_string);
|
|
74 let ok = env::set_current_dir(&vm_path);
|
|
75 if ok.is_err() {
|
|
76 println!("failed cd at {}", &vm_path_string);
|
|
77 return;
|
|
78 }
|
39
|
79 let output = Command::new("qemu-img")
|
|
80 .arg("create")
|
|
81 .arg("-F")
|
|
82 .arg(QCOW2)
|
|
83 .arg("-b")
|
|
84 .arg(template_path)
|
|
85 .arg("-f")
|
|
86 .arg(QCOW2)
|
45
|
87 .arg(format!("{}.qcow2", vm_name))
|
39
|
88 .output()
|
43
|
89 .unwrap_or_else(|_| panic!("failed to generate {}", &vm_name));
|
39
|
90 io::stdout().write_all(&output.stdout).unwrap();
|
|
91 io::stderr().write_all(&output.stderr).unwrap();
|
|
92 }
|
|
93
|
41
|
94 pub fn get_backing_path(template_name: &str) -> Option<String> {
|
39
|
95 let template_path = format!(
|
|
96 "{}{}{}{}",
|
|
97 TEMPLATE_DIR, TEMPLATE_SUFFIX, &template_name, TEMPLATE_FILE_EXTENSION
|
|
98 );
|
|
99 if Path::new(&template_path).exists() {
|
|
100 return Some(template_path);
|
|
101 }
|
|
102 None
|
|
103 }
|