annotate src/command.rs @ 20:da4858f4658d

impl define cmd
author AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
date Tue, 03 Nov 2020 17:21:27 +0900
parents d37203a877d9
children 9f3d29b8561a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 use super::virsh;
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
2 use super::xml;
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
17
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
4 pub fn list(user_name: &'static str) {
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 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
6 println!("{}", ldump_msg.info_msg);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 println!("{}", ldump_msg.border_line);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 for vm_info in vm_list_strs {
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 println!("{}", vm_info);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 }
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 }
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
12
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
13 pub fn define(user_name: &'static str, vm_name: String) {
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
14 let vm_name = generate_vm_name(user_name, &vm_name);
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
15 let vm_arg = xml::GenerateVMArg::new(user_name, vm_name, false);
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
16 vm_arg.generate().ok();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
17 exec_cmd_from_name_or_id(user_name, &vm_name, "define")
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
18 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
19
17
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
20 pub fn start(user_name: &'static str, vm_name: String) {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
21 virsh::command_require_vm_name(get_vm_name(user_name, &vm_name), "start");
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
22 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
23
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
24 pub fn shutdown(user_name: &'static str, vm_name: String) {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
25 exec_cmd_from_name_or_id(user_name, &vm_name, "shutdown")
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
26 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
27
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
28 pub fn destroy(user_name: &'static str, vm_name: String) {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
29 exec_cmd_from_name_or_id(user_name, &vm_name, "destroy")
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
30 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
31
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
32 pub fn console(user_name: &'static str, vm_name: String) {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
33 exec_cmd_from_name_or_id(user_name, &vm_name, "console")
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
34 }
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
35
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
36 pub fn vncdisplay(user_name: &'static str, vm_name: String) {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
37 exec_cmd_from_name_or_id(user_name, &vm_name, "vncdisplay")
16
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
38 }
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
39
18
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
40 pub fn ttyconsole(user_name: &'static str, vm_name: String) {
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
41 exec_cmd_from_name_or_id(user_name, &vm_name, "ttyconsole")
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
42 }
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
43
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
44 pub fn dumpxml(user_name: &'static str, vm_name: String) {
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
45 exec_cmd_from_name_or_id(user_name, &vm_name, "dumpxml")
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
46 }
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
47
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
48 pub fn undefine(user_name: &'static str, vm_name: String) {
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
49 exec_cmd_from_name_or_id(user_name, &vm_name, "undefine")
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
50 }
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
51
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
52 pub fn domiflist(user_name: &'static str, vm_name: String) {
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
53 exec_cmd_from_name_or_id(user_name, &vm_name, "domiflist")
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
54 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
55
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
56 pub fn dominfo(user_name: &'static str, vm_name: String) {
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
57 exec_cmd_from_name_or_id(user_name, &vm_name, "dominfo")
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
58 }
18
9b24d6767a2f define other command
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
59
17
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
60 fn exec_cmd_from_name_or_id(user_name: &'static str, name_or_id: &str, command: &str) {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
61 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
62 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
63 if let Some(vm_name) = vm_name_or_none {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
64 return virsh::command_require_vm_name(vm_name, command);
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
65 }
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 virsh::command_require_vm_name(get_vm_name(user_name, name_or_id), command);
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
68 }
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 fn get_vm_name_from_id(user_name: &'static str, arg_id: &str) -> Option<String> {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
71 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
72 for vm in vm_list_strs {
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
73 let mut splitted = vm.split_whitespace();
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
74 let vm_id = splitted.next().unwrap();
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
75 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
76 let vm_name = splitted.next().unwrap();
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
77 return Some(String::from(vm_name));
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
78 }
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 None
5477c26e6984 impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
81 }
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 fn get_vm_name(user_name: &'static str, vm_name: &str) -> String {
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
84 let vm_name = match vm_name.parse::<u8>() {
15
e6dc51066703 cargo clippy
AnaTofuZ <anatofuz@gmail.com>
parents: 13
diff changeset
85 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
86 Err(_) => vm_name.to_string(),
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
87 };
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
88
16
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
89 if vm_name.starts_with(user_name) {
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
90 return vm_name;
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
91 }
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
92 format!("{}-{}", user_name, vm_name)
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
93 }
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
94
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
95
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
96 fn generate_vm_name(user_name:&'static str, vm_name: &str) -> String {
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)
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
101 }