annotate src/command.rs @ 39:f667f3a4bbee

backing file cmd
author AnaTofuZ <anatofuz@gmail.com>
date Sun, 22 Nov 2020 15:47:37 +0900
parents 6f1b90844b7f
children d3055f6c6fb7
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;
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 use super::virsh;
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
3 use super::xml;
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
32
ae7ba39dfcbe return the template vec
AnaTofuZ <anatofuz@gmail.com>
parents: 31
diff changeset
5 use std::io;
28
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
6
24
e8ba0f63c227 remove static variable
AnaTofuZ <anatofuz@gmail.com>
parents: 22
diff changeset
7 pub fn list(user_name: &str) {
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 println!("{}", ldump_msg.info_msg);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 println!("{}", ldump_msg.border_line);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 for vm_info in vm_list_strs {
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 println!("{}", vm_info);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 }
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 }
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
15
32
ae7ba39dfcbe return the template vec
AnaTofuZ <anatofuz@gmail.com>
parents: 31
diff changeset
16 pub fn templates() -> Result<(), io::Error> {
ae7ba39dfcbe return the template vec
AnaTofuZ <anatofuz@gmail.com>
parents: 31
diff changeset
17 let templates_list = virsh::get_template_list()?;
28
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
18
32
ae7ba39dfcbe return the template vec
AnaTofuZ <anatofuz@gmail.com>
parents: 31
diff changeset
19 for name in templates_list {
ae7ba39dfcbe return the template vec
AnaTofuZ <anatofuz@gmail.com>
parents: 31
diff changeset
20 println!("{}", name);
28
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
21 }
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
22
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
23 Ok(())
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
24 }
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
25
36
91a99ff9c4c5 String to &str
AnaTofuZ <anatofuz@gmail.com>
parents: 35
diff changeset
26 pub fn define(user: &user::User, vm_name: &str) {
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
27 let vm_name = generate_vm_name(&user.name, &vm_name);
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
28 let builder = xml::Builder::new(&user.name, &vm_name).finalize();
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
29 if let Ok(xml_path) = builder.generate() {
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
30 virsh::command_require_vm_name(&xml_path, "define")
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
31 }
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
32 }
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
33
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
34 pub fn define_from_template(user: &user::User, vm_name: &str, template: &str) {
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
35 let vm_name = generate_vm_name(&user.name, &vm_name);
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
36 let template_path = match virsh::get_template_path(template) {
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
37 Some(path) => path,
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
38 None => {
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
39 println!("not found {}", template);
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
40 return;
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
41 }
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
42 };
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
43 let builder = xml::Builder::new(&user.name, &vm_name)
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
44 .backing(&template_path)
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
45 .finalize();
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
46 if let Ok(xml_path) = builder.generate() {
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
47 virsh::command_require_vm_name(&xml_path, "define")
21
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
48 }
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
49 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
50
36
91a99ff9c4c5 String to &str
AnaTofuZ <anatofuz@gmail.com>
parents: 35
diff changeset
51 pub fn start(user_name: &str, vm_name: &str) {
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
52 virsh::command_require_vm_name(&get_vm_name(user_name, vm_name), "start");
17
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
53 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
54
38
6f1b90844b7f s/common_cmd/common/
AnaTofuZ <anatofuz@gmail.com>
parents: 37
diff changeset
55 pub fn common(user_name: &str, vm_name: &str, command: &str) {
37
0f966575a56a use commond_cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 36
diff changeset
56 exec_cmd_from_name_or_id(user_name, vm_name, command)
16
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
57 }
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
58
36
91a99ff9c4c5 String to &str
AnaTofuZ <anatofuz@gmail.com>
parents: 35
diff changeset
59 pub fn dumpvncpasswd(_user_name: &str, _vm_name: &str) {}
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
60
24
e8ba0f63c227 remove static variable
AnaTofuZ <anatofuz@gmail.com>
parents: 22
diff changeset
61 fn exec_cmd_from_name_or_id(user_name: &str, name_or_id: &str, command: &str) {
17
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
62 if name_or_id.parse::<u8>().is_ok() {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
63 let vm_name_or_none = get_vm_name_from_id(user_name, &name_or_id);
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
64 if let Some(vm_name) = vm_name_or_none {
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
65 return virsh::command_require_vm_name(&vm_name, command);
17
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
66 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
67 }
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
68 virsh::command_require_vm_name(&get_vm_name(user_name, name_or_id), command);
17
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
69 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
70
24
e8ba0f63c227 remove static variable
AnaTofuZ <anatofuz@gmail.com>
parents: 22
diff changeset
71 fn get_vm_name_from_id(user_name: &str, arg_id: &str) -> Option<String> {
17
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
72 let (_, vm_list_strs) = virsh::get_vm_list(user_name);
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
73 for vm in vm_list_strs {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
74 let mut splitted = vm.split_whitespace();
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
75 let vm_id = splitted.next().unwrap();
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
76 if (vm_id != "-") && (vm_id == arg_id) {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
77 let vm_name = splitted.next().unwrap();
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
78 return Some(String::from(vm_name));
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
79 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
80 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
81 None
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
82 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
83
24
e8ba0f63c227 remove static variable
AnaTofuZ <anatofuz@gmail.com>
parents: 22
diff changeset
84 fn get_vm_name(user_name: &str, vm_name: &str) -> String {
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
85 let vm_name = match vm_name.parse::<u8>() {
15
e6dc51066703 cargo clippy
AnaTofuZ <anatofuz@gmail.com>
parents: 13
diff changeset
86 Ok(_) => format!("{}-{}", user_name, vm_name),
17
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
87 Err(_) => vm_name.to_string(),
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
88 };
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
89
16
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
90 if vm_name.starts_with(user_name) {
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
91 return vm_name;
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
92 }
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
93 format!("{}-{}", user_name, vm_name)
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
94 }
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
95
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
96 fn generate_vm_name(user_name: &str, vm_name: &str) -> String {
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
97 if vm_name.starts_with(user_name) {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
98 return vm_name.to_string();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
99 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
100 format!("{}-{}", user_name, vm_name)
23
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
101 }