annotate src/virsh.rs @ 19:d37203a877d9

add xml parse/writer
author AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
date Tue, 03 Nov 2020 11:10:24 +0900
parents 1da4937628be
children e8ba0f63c227
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
1 use std::io::{self, Write};
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 use std::process::Command;
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 pub struct ListDumpMsg {
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 pub info_msg: String,
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 pub border_line: String,
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 }
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 pub fn get_vm_list(user_name: &'static str) -> (ListDumpMsg, Vec<String>) {
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 let output = Command::new("virsh")
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 .arg("list")
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 .arg("--all")
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 .output()
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 .expect("failed to virsh");
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 let virsh_list = String::from_utf8_lossy(&output.stdout);
15
e6dc51066703 cargo clippy
AnaTofuZ <anatofuz@gmail.com>
parents: 13
diff changeset
16 let mut virsh_list = virsh_list.split('\n');
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 let info_msg = virsh_list.next().unwrap();
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 let border_line = virsh_list.next().unwrap();
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 let ldump_msg = ListDumpMsg {
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 info_msg: String::from(info_msg),
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 border_line: String::from(border_line),
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 };
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
15
e6dc51066703 cargo clippy
AnaTofuZ <anatofuz@gmail.com>
parents: 13
diff changeset
25 (
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 ldump_msg,
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 virsh_list
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 .filter(|&x| x.contains(user_name))
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 .map(|x| x.to_string())
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 .collect(),
15
e6dc51066703 cargo clippy
AnaTofuZ <anatofuz@gmail.com>
parents: 13
diff changeset
31 )
12
0df4d067badb move each crates
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 }
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
33
16
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
34 pub fn command_require_vm_name(vm_name: String, operation: &str) {
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
35 let output = Command::new("virsh")
16
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
36 .arg(operation)
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
37 .arg(vm_name)
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
38 .output()
16
AnaTofuZ <anatofuz@gmail.com>
parents: 15
diff changeset
39 .unwrap_or_else(|_| panic!("failed to {}", operation));
13
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
40
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
41 io::stdout().write_all(&output.stdout).unwrap();
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
42 io::stderr().write_all(&output.stderr).unwrap();
56e9763abeef define start cmd
AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
43 }