Mercurial > hg > Members > anatofuz > ie-virsh
annotate src/command.rs @ 75:b4d2ef1897f9 default tip master
update README
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Sun, 20 Dec 2020 23:04:41 +0900 |
parents | 2fb3f3207bbc |
children |
rev | line source |
---|---|
67 | 1 use crate::user; |
2 use crate::virsh; | |
3 use crate::xml; | |
12 | 4 |
32 | 5 use std::io; |
68 | 6 use std::net::TcpListener; |
28 | 7 |
48 | 8 const RENTAL_DIR: &str = "/ie-ryukyu/kvm/images/rental"; |
42 | 9 |
24 | 10 pub fn list(user_name: &str) { |
12 | 11 let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name); |
12 println!("{}", ldump_msg.info_msg); | |
13 println!("{}", ldump_msg.border_line); | |
14 for vm_info in vm_list_strs { | |
15 println!("{}", vm_info); | |
16 } | |
17 } | |
13 | 18 |
32 | 19 pub fn templates() -> Result<(), io::Error> { |
20 let templates_list = virsh::get_template_list()?; | |
28 | 21 |
32 | 22 for name in templates_list { |
23 println!("{}", name); | |
28 | 24 } |
25 | |
26 Ok(()) | |
27 } | |
28 | |
41 | 29 pub fn define(user_detail: &user::UserDetail, vm_name: &str) { |
30 let vm_name = generate_vm_name(&user_detail.user.name, &vm_name); | |
31 let builder = xml::Builder::new(&user_detail, &vm_name).finalize(); | |
39 | 32 if let Ok(xml_path) = builder.generate() { |
61 | 33 virsh::subshell_virsh_command(&xml_path, "define") |
39 | 34 } |
35 } | |
36 | |
68 | 37 pub fn define_gdb(user_detail: &user::UserDetail, vm_name: &str) { |
38 let vm_name = generate_vm_name(&user_detail.user.name, &vm_name); | |
39 let port = match get_available_port() { | |
40 Ok(port) => port, | |
41 Err(e) => { | |
42 println!("[error]get_avalable_port {}", e); | |
43 return; | |
44 } | |
45 }; | |
69 | 46 println!("gdb debug port: {}", port); |
68 | 47 let builder = xml::Builder::new(&user_detail, &vm_name) |
48 .debug_tcp_port(port) | |
49 .finalize(); | |
50 if let Ok(xml_path) = builder.generate() { | |
51 virsh::subshell_virsh_command(&xml_path, "define") | |
52 } | |
53 } | |
54 | |
41 | 55 pub fn define_from_template(user_detail: &user::UserDetail, vm_name: &str, template: &str) { |
56 let vm_name = generate_vm_name(&user_detail.user.name, &vm_name); | |
40 | 57 println!("{}", vm_name); |
41 | 58 let backing_path = match virsh::get_backing_path(template) { |
39 | 59 Some(path) => path, |
60 None => { | |
61 println!("not found {}", template); | |
62 return; | |
63 } | |
64 }; | |
41 | 65 let builder = xml::Builder::new(&user_detail, &vm_name) |
66 .backing(&backing_path) | |
42 | 67 .qcow2_dir(RENTAL_DIR) |
39 | 68 .finalize(); |
40 | 69 let xml_path = match builder.generate() { |
41 | 70 Ok(xml_path) => xml_path, |
71 Err(e) => { | |
72 println!("failed generate xml: {}", e); | |
73 return; | |
74 } | |
40 | 75 }; |
48 | 76 virsh::generate_qemu_from_template(RENTAL_DIR, &vm_name, &backing_path); |
61 | 77 virsh::subshell_virsh_command(&xml_path, "define"); |
19 | 78 } |
79 | |
36 | 80 pub fn start(user_name: &str, vm_name: &str) { |
61 | 81 virsh::subshell_virsh_command(&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
|
82 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
83 |
62 | 84 pub fn subshell(user_name: &str, vm_name: &str, operation: &str) { |
59 | 85 let vm_name = exec_cmd_from_name_or_id(user_name, vm_name); |
62 | 86 virsh::subshell_virsh_command(&vm_name, operation); |
59 | 87 } |
88 | |
63 | 89 pub fn vol_delete(user_name: &str, vm_name: &str, operation: &str) { |
90 let vm_name = exec_cmd_from_name_or_id(user_name, vm_name); | |
91 virsh::virsh_vol_delete_command(&vm_name, operation); | |
92 } | |
93 | |
62 | 94 pub fn exec(user_name: &str, vm_name: &str, operation: &str) { |
59 | 95 let vm_name = exec_cmd_from_name_or_id(user_name, vm_name); |
62 | 96 virsh::exec_virsh_command(&vm_name, operation); |
16 | 97 } |
98 | |
36 | 99 pub fn dumpvncpasswd(_user_name: &str, _vm_name: &str) {} |
26 | 100 |
59 | 101 fn exec_cmd_from_name_or_id(user_name: &str, name_or_id: &str) -> String { |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
102 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
|
103 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
|
104 if let Some(vm_name) = vm_name_or_none { |
59 | 105 return vm_name; |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
106 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
107 } |
59 | 108 get_vm_name(user_name, name_or_id) |
17
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
109 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
110 |
24 | 111 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
|
112 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
|
113 for vm in vm_list_strs { |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
114 let mut splitted = vm.split_whitespace(); |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
115 let vm_id = splitted.next().unwrap(); |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
116 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
|
117 let vm_name = splitted.next().unwrap(); |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
118 return Some(String::from(vm_name)); |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
119 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
120 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
121 None |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
122 } |
5477c26e6984
impl sub commands that use uid
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
123 |
24 | 124 fn get_vm_name(user_name: &str, vm_name: &str) -> String { |
13 | 125 let vm_name = match vm_name.parse::<u8>() { |
15 | 126 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
|
127 Err(_) => vm_name.to_string(), |
13 | 128 }; |
129 | |
16 | 130 if vm_name.starts_with(user_name) { |
131 return vm_name; | |
132 } | |
133 format!("{}-{}", user_name, vm_name) | |
13 | 134 } |
20 | 135 |
26 | 136 fn generate_vm_name(user_name: &str, vm_name: &str) -> String { |
20 | 137 if vm_name.starts_with(user_name) { |
138 return vm_name.to_string(); | |
139 } | |
140 format!("{}-{}", user_name, vm_name) | |
23 | 141 } |
68 | 142 |
143 fn get_available_port() -> Result<u16, std::io::Error> { | |
144 match TcpListener::bind("localhost:0") { | |
145 Ok(listener) => Ok(listener.local_addr().unwrap().port()), | |
146 Err(e) => Err(e), | |
147 } | |
148 } |