annotate src/main.rs @ 7:07b763b84401

rebarse use macro
author AnaTofuZ <anatofuz@gmail.com>
date Wed, 28 Oct 2020 16:32:25 +0900
parents 08247e54d78d
children 017344e337e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
08247e54d78d cargo fmt
AnaTofuZ <anatofuz@gmail.com>
parents: 5
diff changeset
1 use clap::{App, Arg, Clap};
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
2 use nix;
3
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
3 use std::process::Command;
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
4
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
5 #[derive(Clap)]
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
6 #[clap(version = "1.0", author = "AnaTofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")]
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
7 struct Opts {
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
8 #[clap(subcommand)]
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
9 subcmd: SubCommand,
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
10 }
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
11
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
12 #[derive(Clap)]
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
13 enum SubCommand {
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
14 define(Define),
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
15 shutdown(Shutdown),
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
16 list(List),
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
17 }
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
18
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
19 /// define (but don't start) a domain from an template XML file
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
20 #[derive(Clap)]
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
21 struct Define {
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
22 name: String,
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
23 }
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
24
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
25 /// gracefully shutdown a domain
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
26 #[derive(Clap)]
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
27 struct Shutdown {
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
28 name: String,
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
29 }
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
30
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
31 /// list domains
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
32 #[derive(Clap)]
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
33 struct List {}
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
34
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
35 struct list_dump_msg {
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
36 info_msg: String,
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
37 border_line: String,
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
38 }
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
39
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
40 struct VM {
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
41 id: u32,
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
42 name: String,
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
43 is_vm_running: bool,
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
44 }
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
45
0
3ef828bc5261 cargo new
AnaTofuZ <anatofuz@gmail.com>
parents:
diff changeset
46 fn main() {
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
47 let opts: Opts = Opts::parse();
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
48
4
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
49 let uid = getuid();
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
50 let gid = getgid();
3
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
51 let user_name = getlogin(uid);
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
52 println!("uid: {} gid: {} name: {}", uid, gid, user_name);
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
53
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
54 match opts.subcmd {
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
55 SubCommand::define(name) => {}
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
56 SubCommand::shutdown(name) => {}
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
57
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
58 SubCommand::list(list) => {
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
59 set_root_id();
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
60 let (vm_list, list_dump_msg) = list_command(user_name);
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
61 println!("{}\n{}", list_dump_msg.info_msg, list_dump_msg.border_line);
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
62 for vm in vm_list.iter() {
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
63 println!("{} {}", vm.id, vm.name);
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
64 }
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
65 }
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
66 }
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
67
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
68 //set_root_id();
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
69 //list_command(user_name);
0
3ef828bc5261 cargo new
AnaTofuZ <anatofuz@gmail.com>
parents:
diff changeset
70 }
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
71
2
1632a34a3f6c fix function name
AnaTofuZ <anatofuz@gmail.com>
parents: 1
diff changeset
72 fn getlogin(uid: u32) -> &'static str {
1632a34a3f6c fix function name
AnaTofuZ <anatofuz@gmail.com>
parents: 1
diff changeset
73 use std::ffi::CStr;
4
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
74 let user_passwd = unsafe { nix::libc::getpwuid(uid) };
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
75 let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name) };
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
76 return c_str.to_str().unwrap();
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
77 }
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
78
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
79 fn getuid() -> u32 {
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
80 let uid_struct = nix::unistd::getuid();
3
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
81 return uid_struct.into();
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
82 }
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
83
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
84 fn getgid() -> u32 {
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
85 let gid_struct = nix::unistd::getgid();
3
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
86 return gid_struct.into();
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
87 }
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
88
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
89 fn list_command(user_name: &'static str) -> (Vec<VM>, list_dump_msg) {
4
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
90 let output = Command::new("virsh")
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
91 .arg("list")
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
92 .arg("--all")
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
93 .output()
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
94 .expect("failed to virsh");
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
95 let virsh_list = String::from_utf8_lossy(&output.stdout);
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
96 let mut virsh_list = virsh_list.split("\n");
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
97
4
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
98 let info_msg = virsh_list.next().unwrap();
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
99 let border_line = virsh_list.next().unwrap();
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
100 let ldump_msg = list_dump_msg {
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
101 info_msg: String::from(info_msg),
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
102 border_line: String::from(border_line),
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
103 };
4
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
104
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
105 let virsh_emit_vm_list = virsh_list.filter(|&x| x.contains(user_name));
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
106
6
08247e54d78d cargo fmt
AnaTofuZ <anatofuz@gmail.com>
parents: 5
diff changeset
107 let mut vm_list: Vec<VM> = Vec::new();
4
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
108
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
109 for emit_vm in virsh_emit_vm_list {
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
110 let mut vm_infos = emit_vm.split(" ");
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
111 let vm_status = vm_infos.next().unwrap();
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
112 let is_vm_running = if vm_status != "-" { true } else { false };
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
113 let name = vm_infos.next().unwrap();
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
114 vm_list.push(VM {
6
08247e54d78d cargo fmt
AnaTofuZ <anatofuz@gmail.com>
parents: 5
diff changeset
115 id: 0,
08247e54d78d cargo fmt
AnaTofuZ <anatofuz@gmail.com>
parents: 5
diff changeset
116 is_vm_running: is_vm_running,
08247e54d78d cargo fmt
AnaTofuZ <anatofuz@gmail.com>
parents: 5
diff changeset
117 name: String::from(name),
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
118 });
4
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
119 }
7
07b763b84401 rebarse use macro
AnaTofuZ <anatofuz@gmail.com>
parents: 6
diff changeset
120 return (vm_list, ldump_msg);
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
121 }
3
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
122
4
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
123 fn set_root_id() {
3
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
124 let id = 0;
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
125 match nix::unistd::seteuid(nix::unistd::Uid::from_raw(id)) {
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
126 Err(err) => panic!("failed seteuid {}", err),
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
127 Ok(_) => {}
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
128 }
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
129 match nix::unistd::setegid(nix::unistd::Gid::from_raw(id)) {
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
130 Err(err) => panic!("failed setedid{}", err),
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
131 Ok(_) => {}
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
132 }
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
133
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
134 match nix::unistd::setuid(nix::unistd::Uid::from_raw(id)) {
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
135 Err(err) => panic!("failed setuid {}", err),
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
136 Ok(_) => {}
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
137 }
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
138
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
139 match nix::unistd::setgid(nix::unistd::Gid::from_raw(id)) {
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
140 Err(err) => panic!("failed setdid{}", err),
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
141 Ok(_) => {}
5bdb02e05c86 show virsh list --all
AnaTofuZ <anatofuz@gmail.com>
parents: 2
diff changeset
142 }
4
668a91cd7be8 impl virsh list --all filter
AnaTofuZ <anatofuz@gmail.com>
parents: 3
diff changeset
143 }
5
ef1358b993e1 mmm.....
AnaTofuZ <anatofuz@gmail.com>
parents: 4
diff changeset
144
6
08247e54d78d cargo fmt
AnaTofuZ <anatofuz@gmail.com>
parents: 5
diff changeset
145 fn create_new_vm(user_name: &'static str, vm_name: &'static str, debug_kernel: bool) {}