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