1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
extern crate users;
use error::CSDError;
use users::*;
use std::process::Command;
pub fn call_ceph(cmd: &str) -> Result<String, CSDError> {
debug!("calling ceph {} -f json", cmd);
let ceph = Command::new("/usr/bin/env")
.args(&["sh", "-c", &format!("ceph {} -f json", cmd)])
.output()?;
if ceph.status.success() {
let stdout = String::from_utf8(ceph.stdout)?;
trace!("ceph_cmd stdout: {}", stdout.trim_start());
Ok(stdout.trim_start().to_string())
} else {
let stderr = String::from_utf8(ceph.stderr)?;
Err(CSDError::CephExecError(stderr))
}
}
pub fn check_user() -> Result<(), CSDError> {
match get_current_username() {
Some(user) => {
let user = user.to_string_lossy();
match user.as_ref() {
"ceph" => Ok(()),
"root" => Ok(()),
_ => Err(CSDError::ExecError),
}
}
None => Err(CSDError::ExecError),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn check_user_panic() {
assert!(check_user().is_ok());
}
}