Mercurial > hg > Members > anatofuz > ie-virsh
annotate src/command.rs @ 37:0f966575a56a
use commond_cmd
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Sun, 22 Nov 2020 11:20:34 +0900 |
parents | 91a99ff9c4c5 |
children | 6f1b90844b7f |
rev | line source |
---|---|
26 | 1 use super::user; |
12 | 2 use super::virsh; |
19 | 3 use super::xml; |
12 | 4 |
32 | 5 use std::io; |
28 | 6 |
24 | 7 pub fn list(user_name: &str) { |
12 | 8 let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name); |
9 println!("{}", ldump_msg.info_msg); | |
10 println!("{}", ldump_msg.border_line); | |
11 for vm_info in vm_list_strs { | |
12 println!("{}", vm_info); | |
13 } | |
14 } | |
13 | 15 |
32 | 16 pub fn templates() -> Result<(), io::Error> { |
17 let templates_list = virsh::get_template_list()?; | |
28 | 18 |
32 | 19 for name in templates_list { |
20 println!("{}", name); | |
28 | 21 } |
22 | |
23 Ok(()) | |
24 } | |
25 | |
36 | 26 pub fn define(user: &user::User, vm_name: &str) { |
26 | 27 let vm_name = generate_vm_name(&user.name, &vm_name); |
36 | 28 let vm_xml = xml::Builder::new(&user.name, &vm_name).finalize(); |
29 if let Ok(xml_path) = vm_xml.generate() { | |
26 | 30 virsh::command_require_vm_name(xml_path, "define") |
21 | 31 } |
19 | 32 } |
33 | |
36 | 34 pub fn start(user_name: &str, vm_name: &str) { |
35 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
|
36 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
37 |
37 | 38 pub fn common_cmd(user_name: &str, vm_name: &str, command: &str) { |
39 exec_cmd_from_name_or_id(user_name, vm_name, command) | |
16 | 40 } |
41 | |
18 | 42 |
36 | 43 pub fn dumpvncpasswd(_user_name: &str, _vm_name: &str) {} |
26 | 44 |
24 | 45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
51 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
52 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
|
53 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
54 |
24 | 55 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
|
56 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
|
57 for vm in vm_list_strs { |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
58 let mut splitted = vm.split_whitespace(); |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
59 let vm_id = splitted.next().unwrap(); |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
60 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
|
61 let vm_name = splitted.next().unwrap(); |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
62 return Some(String::from(vm_name)); |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
63 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
64 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
65 None |
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 |
24 | 68 fn get_vm_name(user_name: &str, vm_name: &str) -> String { |
13 | 69 let vm_name = match vm_name.parse::<u8>() { |
15 | 70 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
|
71 Err(_) => vm_name.to_string(), |
13 | 72 }; |
73 | |
16 | 74 if vm_name.starts_with(user_name) { |
75 return vm_name; | |
76 } | |
77 format!("{}-{}", user_name, vm_name) | |
13 | 78 } |
20 | 79 |
26 | 80 fn generate_vm_name(user_name: &str, vm_name: &str) -> String { |
20 | 81 if vm_name.starts_with(user_name) { |
82 return vm_name.to_string(); | |
83 } | |
84 format!("{}-{}", user_name, vm_name) | |
23 | 85 } |