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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
67
29d2a49008e2 s/super/crate/
AnaTofuZ <anatofuz@gmail.com>
parents: 63
diff changeset
1 use crate::user;
29d2a49008e2 s/super/crate/
AnaTofuZ <anatofuz@gmail.com>
parents: 63
diff changeset
2 use crate::virsh;
29d2a49008e2 s/super/crate/
AnaTofuZ <anatofuz@gmail.com>
parents: 63
diff changeset
3 use crate::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;
68
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
6 use std::net::TcpListener;
28
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
7
48
f65b97c93170 first create qcow2
AnaTofuZ <anatofuz@gmail.com>
parents: 43
diff changeset
8 const RENTAL_DIR: &str = "/ie-ryukyu/kvm/images/rental";
42
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
9
24
e8ba0f63c227 remove static variable
AnaTofuZ <anatofuz@gmail.com>
parents: 22
diff changeset
10 pub fn list(user_name: &str) {
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 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
12 println!("{}", ldump_msg.info_msg);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 println!("{}", ldump_msg.border_line);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 for vm_info in vm_list_strs {
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 println!("{}", vm_info);
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 }
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 }
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
18
32
ae7ba39dfcbe return the template vec
AnaTofuZ <anatofuz@gmail.com>
parents: 31
diff changeset
19 pub fn templates() -> Result<(), io::Error> {
ae7ba39dfcbe return the template vec
AnaTofuZ <anatofuz@gmail.com>
parents: 31
diff changeset
20 let templates_list = virsh::get_template_list()?;
28
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
21
32
ae7ba39dfcbe return the template vec
AnaTofuZ <anatofuz@gmail.com>
parents: 31
diff changeset
22 for name in templates_list {
ae7ba39dfcbe return the template vec
AnaTofuZ <anatofuz@gmail.com>
parents: 31
diff changeset
23 println!("{}", name);
28
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
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
26 Ok(())
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
27 }
1c3b6b6ee0b1 add show template
AnaTofuZ <anatofuz@gmail.com>
parents: 26
diff changeset
28
41
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
29 pub fn define(user_detail: &user::UserDetail, vm_name: &str) {
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
30 let vm_name = generate_vm_name(&user_detail.user.name, &vm_name);
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
31 let builder = xml::Builder::new(&user_detail, &vm_name).finalize();
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
32 if let Ok(xml_path) = builder.generate() {
61
b6e9e473678c fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 59
diff changeset
33 virsh::subshell_virsh_command(&xml_path, "define")
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
34 }
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
35 }
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
36
68
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
37 pub fn define_gdb(user_detail: &user::UserDetail, vm_name: &str) {
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
38 let vm_name = generate_vm_name(&user_detail.user.name, &vm_name);
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
39 let port = match get_available_port() {
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
40 Ok(port) => port,
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
41 Err(e) => {
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
42 println!("[error]get_avalable_port {}", e);
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
43 return;
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
44 }
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
45 };
69
2fb3f3207bbc add debug port info
AnaTofuZ <anatofuz@gmail.com>
parents: 68
diff changeset
46 println!("gdb debug port: {}", port);
68
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
47 let builder = xml::Builder::new(&user_detail, &vm_name)
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
48 .debug_tcp_port(port)
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
49 .finalize();
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
50 if let Ok(xml_path) = builder.generate() {
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
51 virsh::subshell_virsh_command(&xml_path, "define")
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
52 }
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
53 }
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
54
41
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
55 pub fn define_from_template(user_detail: &user::UserDetail, vm_name: &str, template: &str) {
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
56 let vm_name = generate_vm_name(&user_detail.user.name, &vm_name);
40
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
57 println!("{}", vm_name);
41
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
58 let backing_path = match virsh::get_backing_path(template) {
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
59 Some(path) => path,
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
60 None => {
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
61 println!("not found {}", template);
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
62 return;
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
63 }
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
64 };
41
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
65 let builder = xml::Builder::new(&user_detail, &vm_name)
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
66 .backing(&backing_path)
42
2787ada7650b fix rental mode
AnaTofuZ <anatofuz@gmail.com>
parents: 41
diff changeset
67 .qcow2_dir(RENTAL_DIR)
39
f667f3a4bbee backing file cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 38
diff changeset
68 .finalize();
40
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
69 let xml_path = match builder.generate() {
41
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
70 Ok(xml_path) => xml_path,
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
71 Err(e) => {
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
72 println!("failed generate xml: {}", e);
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
73 return;
AnaTofuZ <anatofuz@gmail.com>
parents: 40
diff changeset
74 }
40
AnaTofuZ <anatofuz@gmail.com>
parents: 39
diff changeset
75 };
48
f65b97c93170 first create qcow2
AnaTofuZ <anatofuz@gmail.com>
parents: 43
diff changeset
76 virsh::generate_qemu_from_template(RENTAL_DIR, &vm_name, &backing_path);
61
b6e9e473678c fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 59
diff changeset
77 virsh::subshell_virsh_command(&xml_path, "define");
19
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
78 }
d37203a877d9 add xml parse/writer
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
79
36
91a99ff9c4c5 String to &str
AnaTofuZ <anatofuz@gmail.com>
parents: 35
diff changeset
80 pub fn start(user_name: &str, vm_name: &str) {
61
b6e9e473678c fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 59
diff changeset
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
aefbef761450 add edit cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 61
diff changeset
84 pub fn subshell(user_name: &str, vm_name: &str, operation: &str) {
59
d6ef7ea91ff4 fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 48
diff changeset
85 let vm_name = exec_cmd_from_name_or_id(user_name, vm_name);
62
aefbef761450 add edit cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 61
diff changeset
86 virsh::subshell_virsh_command(&vm_name, operation);
59
d6ef7ea91ff4 fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 48
diff changeset
87 }
d6ef7ea91ff4 fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 48
diff changeset
88
63
f0985ebd068b add vol-delete
mk <e175733@ie.u-ryukyu.ac.jp>
parents: 62
diff changeset
89 pub fn vol_delete(user_name: &str, vm_name: &str, operation: &str) {
f0985ebd068b add vol-delete
mk <e175733@ie.u-ryukyu.ac.jp>
parents: 62
diff changeset
90 let vm_name = exec_cmd_from_name_or_id(user_name, vm_name);
f0985ebd068b add vol-delete
mk <e175733@ie.u-ryukyu.ac.jp>
parents: 62
diff changeset
91 virsh::virsh_vol_delete_command(&vm_name, operation);
f0985ebd068b add vol-delete
mk <e175733@ie.u-ryukyu.ac.jp>
parents: 62
diff changeset
92 }
f0985ebd068b add vol-delete
mk <e175733@ie.u-ryukyu.ac.jp>
parents: 62
diff changeset
93
62
aefbef761450 add edit cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 61
diff changeset
94 pub fn exec(user_name: &str, vm_name: &str, operation: &str) {
59
d6ef7ea91ff4 fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 48
diff changeset
95 let vm_name = exec_cmd_from_name_or_id(user_name, vm_name);
62
aefbef761450 add edit cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 61
diff changeset
96 virsh::exec_virsh_command(&vm_name, operation);
16
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
97 }
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
98
36
91a99ff9c4c5 String to &str
AnaTofuZ <anatofuz@gmail.com>
parents: 35
diff changeset
99 pub fn dumpvncpasswd(_user_name: &str, _vm_name: &str) {}
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
100
59
d6ef7ea91ff4 fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 48
diff changeset
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
d6ef7ea91ff4 fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 48
diff changeset
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
d6ef7ea91ff4 fix console cmd
AnaTofuZ <anatofuz@gmail.com>
parents: 48
diff changeset
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
e8ba0f63c227 remove static variable
AnaTofuZ <anatofuz@gmail.com>
parents: 22
diff changeset
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
e8ba0f63c227 remove static variable
AnaTofuZ <anatofuz@gmail.com>
parents: 22
diff changeset
124 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
125 let vm_name = match vm_name.parse::<u8>() {
15
e6dc51066703 cargo clippy
AnaTofuZ <anatofuz@gmail.com>
parents: 13
diff changeset
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
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
128 };
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
129
16
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
130 if vm_name.starts_with(user_name) {
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
131 return vm_name;
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
132 }
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
133 format!("{}-{}", user_name, vm_name)
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
134 }
20
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
135
26
AnaTofuZ <anatofuz@gmail.com>
parents: 25
diff changeset
136 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
137 if vm_name.starts_with(user_name) {
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
138 return vm_name.to_string();
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
139 }
da4858f4658d impl define cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
140 format!("{}-{}", user_name, vm_name)
23
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
141 }
68
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
142
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
143 fn get_available_port() -> Result<u16, std::io::Error> {
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
144 match TcpListener::bind("localhost:0") {
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
145 Ok(listener) => Ok(listener.local_addr().unwrap().port()),
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
146 Err(e) => Err(e),
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
147 }
f9e8680648c9 impl define-gdb
AnaTofuZ <anatofuz@gmail.com>
parents: 67
diff changeset
148 }