Remove symbol error type
This commit is contained in:
parent
55f2cfae95
commit
4be608a002
10 changed files with 85 additions and 108 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
// rustfmt
|
// rustfmt
|
||||||
|
|
||||||
#![deny(fat_ptr_transmutes,
|
#![deny(fat_ptr_transmutes,
|
||||||
trivial_casts, trivial_numeric_casts, unsafe_code,
|
trivial_numeric_casts, unsafe_code,
|
||||||
unstable_features, unused_extern_crates,
|
unstable_features, unused_extern_crates,
|
||||||
unused_import_braces, unused_qualifications,
|
unused_import_braces, unused_qualifications,
|
||||||
variant_size_differences
|
variant_size_differences
|
||||||
|
|
@ -12,7 +12,7 @@ unused_results
|
||||||
)]
|
)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#![warn(missing_docs,
|
#![warn(missing_docs, trivial_casts,
|
||||||
missing_copy_implementations,
|
missing_copy_implementations,
|
||||||
missing_debug_implementations
|
missing_debug_implementations
|
||||||
)]
|
)]
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,13 @@ use std::fmt;
|
||||||
use loggers::Logger;
|
use loggers::Logger;
|
||||||
use symbols::Symbol;
|
use symbols::Symbol;
|
||||||
|
|
||||||
#[derive(Debug,PartialEq)]
|
#[derive(Debug)]
|
||||||
pub enum SymbolRunError<E: Error> {
|
pub enum SymbolRunError {
|
||||||
Symbol(E),
|
Symbol(Box<Error>),
|
||||||
ExecuteDidNotReach(())
|
ExecuteDidNotReach(())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Error> Error for SymbolRunError<E> {
|
impl Error for SymbolRunError {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
&SymbolRunError::Symbol(_) => "Symbol execution error",
|
&SymbolRunError::Symbol(_) => "Symbol execution error",
|
||||||
|
|
@ -19,13 +19,13 @@ impl<E: Error> Error for SymbolRunError<E> {
|
||||||
}
|
}
|
||||||
fn cause(&self) -> Option<&Error> {
|
fn cause(&self) -> Option<&Error> {
|
||||||
match self {
|
match self {
|
||||||
&SymbolRunError::Symbol(ref e) => Some(e),
|
&SymbolRunError::Symbol(ref e) => Some(&**e),
|
||||||
&SymbolRunError::ExecuteDidNotReach(_) => None
|
&SymbolRunError::ExecuteDidNotReach(_) => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Error> fmt::Display for SymbolRunError<E> {
|
impl fmt::Display for SymbolRunError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
&SymbolRunError::Symbol(ref e) => write!(f, "{}", e),
|
&SymbolRunError::Symbol(ref e) => write!(f, "{}", e),
|
||||||
|
|
@ -34,22 +34,14 @@ impl<E: Error> fmt::Display for SymbolRunError<E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Error> From<E> for SymbolRunError<E> {
|
|
||||||
fn from(err: E) -> SymbolRunError<E> {
|
|
||||||
SymbolRunError::Symbol(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait SymbolRunner {
|
pub trait SymbolRunner {
|
||||||
fn run_symbol<S: ?Sized + Symbol + fmt::Display>(&self, logger: &mut Logger, symbol: &S) -> Result<(), SymbolRunError<S::Error>>
|
fn run_symbol<S: ?Sized + Symbol + fmt::Display>(&self, logger: &mut Logger, symbol: &S) -> Result<(), Box<Error>>;
|
||||||
where S::Error: Error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InitializingSymbolRunner;
|
pub struct InitializingSymbolRunner;
|
||||||
|
|
||||||
impl SymbolRunner for InitializingSymbolRunner {
|
impl SymbolRunner for InitializingSymbolRunner {
|
||||||
fn run_symbol<S: ?Sized + Symbol + fmt::Display>(&self, logger: &mut Logger, symbol: &S) -> Result<(), SymbolRunError<S::Error>>
|
fn run_symbol<S: ?Sized + Symbol + fmt::Display>(&self, logger: &mut Logger, symbol: &S) -> Result<(), Box<Error>>
|
||||||
where S::Error: Error
|
|
||||||
{
|
{
|
||||||
let target_reached = try!(symbol.target_reached());
|
let target_reached = try!(symbol.target_reached());
|
||||||
if target_reached {
|
if target_reached {
|
||||||
|
|
@ -61,7 +53,7 @@ impl SymbolRunner for InitializingSymbolRunner {
|
||||||
let target_reached = try!(symbol.target_reached());
|
let target_reached = try!(symbol.target_reached());
|
||||||
logger.debug(format!("Symbol reports target_reached: {:?} (should be true)", target_reached).as_str());
|
logger.debug(format!("Symbol reports target_reached: {:?} (should be true)", target_reached).as_str());
|
||||||
if !target_reached {
|
if !target_reached {
|
||||||
return Err(SymbolRunError::ExecuteDidNotReach(()));
|
return Err(Box::new(SymbolRunError::ExecuteDidNotReach(())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -71,8 +63,7 @@ impl SymbolRunner for InitializingSymbolRunner {
|
||||||
pub struct DrySymbolRunner;
|
pub struct DrySymbolRunner;
|
||||||
|
|
||||||
impl SymbolRunner for DrySymbolRunner {
|
impl SymbolRunner for DrySymbolRunner {
|
||||||
fn run_symbol<S: ?Sized + Symbol + fmt::Display>(&self, logger: &mut Logger, symbol: &S) -> Result<(), SymbolRunError<S::Error>>
|
fn run_symbol<S: ?Sized + Symbol + fmt::Display>(&self, logger: &mut Logger, symbol: &S) -> Result<(), Box<Error>>
|
||||||
where S::Error: Error
|
|
||||||
{
|
{
|
||||||
let target_reached = try!(symbol.target_reached());
|
let target_reached = try!(symbol.target_reached());
|
||||||
logger.debug(format!("Symbol reports target_reached: {:?}", target_reached).as_str());
|
logger.debug(format!("Symbol reports target_reached: {:?}", target_reached).as_str());
|
||||||
|
|
@ -95,7 +86,6 @@ mod test {
|
||||||
use symbols::Symbol;
|
use symbols::Symbol;
|
||||||
use schema::SymbolRunner;
|
use schema::SymbolRunner;
|
||||||
use schema::InitializingSymbolRunner;
|
use schema::InitializingSymbolRunner;
|
||||||
use schema::SymbolRunError;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
enum DummySymbolError {
|
enum DummySymbolError {
|
||||||
|
|
@ -114,34 +104,29 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
struct DummySymbol<'a> {
|
||||||
struct DummySymbol {
|
_execute: &'a Fn() -> Result<(), Box<Error>>,
|
||||||
_target_reacheds: Vec<Result<bool, DummySymbolError>>,
|
_target_reached: &'a Fn() -> Result<bool, Box<Error>>
|
||||||
_cur_target_reached: RefCell<usize>,
|
|
||||||
_execute: Result<(), DummySymbolError>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Symbol for DummySymbol {
|
impl<'a> Symbol for DummySymbol<'a> {
|
||||||
type Error = DummySymbolError;
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||||
fn target_reached(&self) -> Result<bool, Self::Error> {
|
(self._target_reached)()
|
||||||
let mut cur = self._cur_target_reached.borrow_mut();
|
}
|
||||||
let ret = self._target_reacheds[*cur].clone();
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||||
*cur = *cur + 1;
|
(self._execute)()
|
||||||
ret
|
|
||||||
}
|
}
|
||||||
fn execute(&self) -> Result<(), Self::Error> { self._execute.clone() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for DummySymbol {
|
impl<'a> fmt::Display for DummySymbol<'a> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "Dummy symbol")
|
write!(f, "Dummy symbol")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DummySymbol {
|
impl<'a> DummySymbol<'a> {
|
||||||
fn new(target_reached: Vec<Result<bool, DummySymbolError>>,
|
fn new(target_reached: &'a Fn() -> Result<bool, Box<Error>>, execute: &'a Fn() -> Result<(), Box<Error>>) -> DummySymbol<'a> {
|
||||||
execute: Result<(), DummySymbolError>) -> DummySymbol {
|
DummySymbol { _target_reached: target_reached, _execute: execute }
|
||||||
DummySymbol { _target_reacheds: target_reached, _cur_target_reached: RefCell::new(0), _execute: execute }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -166,36 +151,37 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nothing_needed_to_be_done() {
|
fn nothing_needed_to_be_done() {
|
||||||
let result: Result<(), SymbolRunError<DummySymbolError>> = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(vec![Ok(true)], Ok(())) );
|
let result = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(&|| Ok(true), &|| Ok(())) );
|
||||||
assert_eq!(
|
assert!(result.is_ok());
|
||||||
result,
|
|
||||||
Ok(())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn everything_is_ok() {
|
fn everything_is_ok() {
|
||||||
let result: Result<(), SymbolRunError<DummySymbolError>> = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(vec![Ok(false), Ok(true)], Ok(())) );
|
let first = RefCell::new(true);
|
||||||
assert_eq!(
|
let result = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(&|| {
|
||||||
result,
|
let mut _first = first.borrow_mut();
|
||||||
Ok(())
|
Ok(if *_first {
|
||||||
);
|
*_first = false;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
})
|
||||||
|
}, &|| Ok(())) );
|
||||||
|
assert!(result.is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn executing_did_not_change_state() {
|
fn executing_did_not_change_state() {
|
||||||
let result: Result<(), SymbolRunError<DummySymbolError>> = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(vec![Ok(false), Ok(false)], Ok(())));
|
let result = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(&|| Ok(false), &|| Ok(())));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result.unwrap_err().description(),
|
||||||
Err(SymbolRunError::ExecuteDidNotReach(()))
|
"Target not reached after executing symbol"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn executing_did_not_work() {
|
fn executing_did_not_work() {
|
||||||
assert_eq!(
|
let err = InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(&|| Ok(false), &|| Err(Box::new(DummySymbolError::Error(()))) )).unwrap_err();
|
||||||
InitializingSymbolRunner.run_symbol( &mut DummyLogger::new(), &DummySymbol::new(vec![Ok(false)], Err(DummySymbolError::Error(()))) ),
|
assert_eq!(err.description(), "Description");
|
||||||
Err(SymbolRunError::Symbol(DummySymbolError::Error(())))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fs::File as FsFile;
|
use std::fs::File as FsFile;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
@ -23,16 +24,14 @@ impl<C, D> File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
|
impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
|
||||||
type Error = io::Error;
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||||
|
|
||||||
fn target_reached(&self) -> Result<bool, Self::Error> {
|
|
||||||
let file = FsFile::open(self.path.as_ref());
|
let file = FsFile::open(self.path.as_ref());
|
||||||
// Check if file exists
|
// Check if file exists
|
||||||
if let Err(e) = file {
|
if let Err(e) = file {
|
||||||
return if e.kind() == io::ErrorKind::NotFound {
|
return if e.kind() == io::ErrorKind::NotFound {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
} else {
|
} else {
|
||||||
Err(e)
|
Err(Box::new(e))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// Check if content is the same
|
// Check if content is the same
|
||||||
|
|
@ -42,13 +41,13 @@ impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt
|
||||||
match (file_content.next(), target_content.next()) {
|
match (file_content.next(), target_content.next()) {
|
||||||
(None, None) => return Ok(true),
|
(None, None) => return Ok(true),
|
||||||
(Some(Ok(a)), Some(b)) if a == b => {},
|
(Some(Ok(a)), Some(b)) if a == b => {},
|
||||||
(Some(Err(e)), _) => return Err(e),
|
(Some(Err(e)), _) => return Err(Box::new(e)),
|
||||||
(_, _) => return Ok(false)
|
(_, _) => return Ok(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&self) -> Result<(), Self::Error> {
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||||
let mut file = try!(FsFile::create(self.path.as_ref()));
|
let mut file = try!(FsFile::create(self.path.as_ref()));
|
||||||
try!(file.write_all(self.content.as_bytes()));
|
try!(file.write_all(self.content.as_bytes()));
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
|
@ -39,13 +40,12 @@ impl<'a> GitCheckout<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Symbol for GitCheckout<'a> {
|
impl<'a> Symbol for GitCheckout<'a> {
|
||||||
type Error = io::Error;
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||||
fn target_reached(&self) -> Result<bool, Self::Error> {
|
|
||||||
if let Err(e) = metadata(self.target) {
|
if let Err(e) = metadata(self.target) {
|
||||||
return if e.kind() == io::ErrorKind::NotFound {
|
return if e.kind() == io::ErrorKind::NotFound {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
} else {
|
} else {
|
||||||
Err(e)
|
Err(Box::new(e))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
try!(self._run_in_target_repo(&["fetch", self.source, self.branch]));
|
try!(self._run_in_target_repo(&["fetch", self.source, self.branch]));
|
||||||
|
|
@ -54,13 +54,13 @@ impl<'a> Symbol for GitCheckout<'a> {
|
||||||
Ok(fetch_head == head)
|
Ok(fetch_head == head)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&self) -> Result<(), Self::Error> {
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||||
if let Err(e) = metadata(self.target) {
|
if let Err(e) = metadata(self.target) {
|
||||||
return if e.kind() == io::ErrorKind::NotFound {
|
return if e.kind() == io::ErrorKind::NotFound {
|
||||||
try!(self.command_runner.run_with_args("git", &["clone", "-b", self.branch, self.source, self.target]));
|
try!(self.command_runner.run_with_args("git", &["clone", "-b", self.branch, self.source, self.target]));
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(e)
|
Err(Box::new(e))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
try!(self._run_in_target_repo(&["fetch", self.source, self.branch]));
|
try!(self._run_in_target_repo(&["fetch", self.source, self.branch]));
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
pub trait Symbol: Display {
|
pub trait Symbol: Display {
|
||||||
type Error: Error;
|
fn target_reached(&self) -> Result<bool, Box<Error>>;
|
||||||
fn target_reached(&self) -> Result<bool, Self::Error>;
|
fn execute(&self) -> Result<(), Box<Error>>;
|
||||||
fn execute(&self) -> Result<(), Self::Error>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod file;
|
pub mod file;
|
||||||
|
|
|
||||||
|
|
@ -86,8 +86,7 @@ impl<'a> NginxServer<'a, String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
|
impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
|
||||||
type Error = NginxServerError<<FileSymbol<C, Cow<'a, str>> as Symbol>::Error>;
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||||
fn target_reached(&self) -> Result<bool, Self::Error> {
|
|
||||||
if !try!(self.file.target_reached()) {
|
if !try!(self.file.target_reached()) {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
@ -95,7 +94,7 @@ impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&self) -> Result<(), Self::Error> {
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||||
try!(self.file.execute());
|
try!(self.file.execute());
|
||||||
try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"]));
|
try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"]));
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io;
|
|
||||||
|
|
||||||
use command_runner::CommandRunner;
|
use command_runner::CommandRunner;
|
||||||
use symbols::Symbol;
|
use symbols::Symbol;
|
||||||
|
|
@ -25,13 +25,12 @@ impl<'a> fmt::Display for NpmInstall<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Symbol for NpmInstall<'a> {
|
impl<'a> Symbol for NpmInstall<'a> {
|
||||||
type Error = io::Error;
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||||
fn target_reached(&self) -> Result<bool, Self::Error> {
|
|
||||||
let result = try!(self.command_runner.run_with_args("sh", &["-c", &format!("cd '{}' && npm ls", self.target)]));
|
let result = try!(self.command_runner.run_with_args("sh", &["-c", &format!("cd '{}' && npm ls", self.target)]));
|
||||||
Ok(result.status.success() && !String::from_utf8(result.stdout).unwrap().contains("(empty)"))
|
Ok(result.status.success() && !String::from_utf8(result.stdout).unwrap().contains("(empty)"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&self) -> Result<(), Self::Error> {
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||||
try!(self.command_runner.run_with_args("sh", &["-c", &format!("cd '{}' && npm install --production", self.target)]));
|
try!(self.command_runner.run_with_args("sh", &["-c", &format!("cd '{}' && npm install --production", self.target)]));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ WantedBy=default.target
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, P, C> NodeJsSystemdUserService<'a, P, C> where P: AsRef<str> + fmt::Display, C: Deref<Target=str> {
|
impl<'a, P, C> NodeJsSystemdUserService<'a, P, C> where P: AsRef<str> + fmt::Display, C: Deref<Target=str> {
|
||||||
fn check_if_service(&self) -> Result<bool, <Self as Symbol>::Error> {
|
fn check_if_service(&self) -> Result<bool, Box<Error>> {
|
||||||
loop {
|
loop {
|
||||||
// Check if service is registered
|
// Check if service is registered
|
||||||
let active_state = try!(self.command_runner.run_with_args("systemctl", &["--user", "show", "--property", "ActiveState", self.name]));
|
let active_state = try!(self.command_runner.run_with_args("systemctl", &["--user", "show", "--property", "ActiveState", self.name]));
|
||||||
|
|
@ -97,7 +97,7 @@ impl<'a, P, C> NodeJsSystemdUserService<'a, P, C> where P: AsRef<str> + fmt::Dis
|
||||||
match String::from_utf8(active_state.stdout).unwrap().trim_right() {
|
match String::from_utf8(active_state.stdout).unwrap().trim_right() {
|
||||||
"ActiveState=activating" => sleep(Duration::from_millis(500)),
|
"ActiveState=activating" => sleep(Duration::from_millis(500)),
|
||||||
"ActiveState=active" => return Ok(true),
|
"ActiveState=active" => return Ok(true),
|
||||||
"ActiveState=failed" => return Err(NodeJsSystemdUserServiceError::ActivationFailed(self.command_runner.run_with_args("journalctl", &["--user", &format!("--user-unit={}", self.name)]))),
|
"ActiveState=failed" => return Err(Box::new(NodeJsSystemdUserServiceError::ActivationFailed(self.command_runner.run_with_args("journalctl", &["--user", &format!("--user-unit={}", self.name)])) as NodeJsSystemdUserServiceError<io::Error>)),
|
||||||
_ => return Ok(false)
|
_ => return Ok(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -105,12 +105,11 @@ impl<'a, P, C> NodeJsSystemdUserService<'a, P, C> where P: AsRef<str> + fmt::Dis
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, P, C> Symbol for NodeJsSystemdUserService<'a, P, C> where P: AsRef<str> + fmt::Display, C: Deref<Target=str> {
|
impl<'a, P, C> Symbol for NodeJsSystemdUserService<'a, P, C> where P: AsRef<str> + fmt::Display, C: Deref<Target=str> {
|
||||||
type Error = NodeJsSystemdUserServiceError<io::Error>;
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||||
fn target_reached(&self) -> Result<bool, Self::Error> {
|
|
||||||
match self.file.target_reached() {
|
match self.file.target_reached() {
|
||||||
Ok(false) => return Ok(false),
|
Ok(false) => return Ok(false),
|
||||||
Ok(true) => {},
|
Ok(true) => {},
|
||||||
Err(e) => return Err(NodeJsSystemdUserServiceError::GenericError)
|
Err(e) => return Err(Box::new(NodeJsSystemdUserServiceError::GenericError as NodeJsSystemdUserServiceError<io::Error>))
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
if !(try!(self.file.target_reached())) {
|
if !(try!(self.file.target_reached())) {
|
||||||
|
|
@ -120,7 +119,7 @@ impl<'a, P, C> Symbol for NodeJsSystemdUserService<'a, P, C> where P: AsRef<str>
|
||||||
self.check_if_service()
|
self.check_if_service()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&self) -> Result<(), Self::Error> {
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||||
try!(self.command_runner.run_with_args("mkdir", &["-p", &format!("{}/var", self.home)]));
|
try!(self.command_runner.run_with_args("mkdir", &["-p", &format!("{}/var", self.home)]));
|
||||||
try!(self.command_runner.run_with_args("rm", &[&format!("{}/var/service.socket", self.home)]));
|
try!(self.command_runner.run_with_args("rm", &[&format!("{}/var/service.socket", self.home)]));
|
||||||
|
|
||||||
|
|
@ -129,12 +128,12 @@ impl<'a, P, C> Symbol for NodeJsSystemdUserService<'a, P, C> where P: AsRef<str>
|
||||||
// try!(create_dir_all(Path::new(&path).parent().unwrap()));
|
// try!(create_dir_all(Path::new(&path).parent().unwrap()));
|
||||||
match self.file.execute() {
|
match self.file.execute() {
|
||||||
Ok(_) => {},
|
Ok(_) => {},
|
||||||
Err(e) => return Err(NodeJsSystemdUserServiceError::GenericError)
|
Err(e) => return Err(Box::new(NodeJsSystemdUserServiceError::GenericError as NodeJsSystemdUserServiceError<io::Error>))
|
||||||
}
|
}
|
||||||
try!(self.command_runner.run_with_args("systemctl", &["--user", "enable", self.name]));
|
try!(self.command_runner.run_with_args("systemctl", &["--user", "enable", self.name]));
|
||||||
try!(self.command_runner.run_with_args("systemctl", &["--user", "start", self.name]));
|
try!(self.command_runner.run_with_args("systemctl", &["--user", "start", self.name]));
|
||||||
|
|
||||||
if !(try!(self.check_if_service())) { return Err(NodeJsSystemdUserServiceError::GenericError); }
|
if !(try!(self.check_if_service())) { return Err(Box::new(NodeJsSystemdUserServiceError::GenericError as NodeJsSystemdUserServiceError<io::Error>)); }
|
||||||
|
|
||||||
let file_name = format!("{}/var/service.socket", self.home);
|
let file_name = format!("{}/var/service.socket", self.home);
|
||||||
// try!(self.command_runner.run_with_args("chmod", &["666", &file_name]));
|
// try!(self.command_runner.run_with_args("chmod", &["666", &file_name]));
|
||||||
|
|
|
||||||
|
|
@ -49,21 +49,20 @@ impl<'a> SystemdUserSession<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Symbol for SystemdUserSession<'a> {
|
impl<'a> Symbol for SystemdUserSession<'a> {
|
||||||
type Error = SystemdUserSessionError<IoError>;
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||||
fn target_reached(&self) -> Result<bool, Self::Error> {
|
|
||||||
let mut path = PathBuf::from("/var/lib/systemd/linger");
|
let mut path = PathBuf::from("/var/lib/systemd/linger");
|
||||||
path.push(self.user_name);
|
path.push(self.user_name);
|
||||||
Ok(path.exists())
|
Ok(path.exists())
|
||||||
// Could also do `loginctl show-user ${self.user_name} | grep -F 'Linger=yes`
|
// Could also do `loginctl show-user ${self.user_name} | grep -F 'Linger=yes`
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&self) -> Result<(), Self::Error> {
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||||
match self.command_runner.run_with_args("loginctl", &["enable-linger", self.user_name]) {
|
match self.command_runner.run_with_args("loginctl", &["enable-linger", self.user_name]) {
|
||||||
Ok(output) => { println!("{:?} {:?}", from_utf8(&output.stdout).unwrap(), from_utf8(&output.stderr).unwrap() ); match output.status.code() {
|
Ok(output) => { println!("{:?} {:?}", from_utf8(&output.stdout).unwrap(), from_utf8(&output.stderr).unwrap() ); match output.status.code() {
|
||||||
Some(0) => Ok(()),
|
Some(0) => Ok(()),
|
||||||
_ => Err(SystemdUserSessionError::GenericError)
|
_ => Err(Box::new(SystemdUserSessionError::GenericError as SystemdUserSessionError<IoError>))
|
||||||
} },
|
} },
|
||||||
Err(e) => Err(SystemdUserSessionError::ExecError(e))
|
Err(e) => Err(Box::new(SystemdUserSessionError::ExecError(e)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,27 +43,24 @@ pub trait UserAdder {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum UserError<E: Error> {
|
pub enum UserError {
|
||||||
GenericError,
|
GenericError
|
||||||
ExecError(E)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Error> Error for UserError<E> {
|
impl Error for UserError {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
&UserError::GenericError => "Could not find out if user exists",
|
&UserError::GenericError => "Could not find out if user exists"
|
||||||
&UserError::ExecError(_) => "Error executing symbol"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn cause(&self) -> Option<&Error> {
|
fn cause(&self) -> Option<&Error> {
|
||||||
match self {
|
match self {
|
||||||
&UserError::ExecError(ref e) => Some(e),
|
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Error> fmt::Display for UserError<E> {
|
impl fmt::Display for UserError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self.cause() {
|
match self.cause() {
|
||||||
Some(e) => write!(f, "{} (cause: {})", self.description(), e),
|
Some(e) => write!(f, "{} (cause: {})", self.description(), e),
|
||||||
|
|
@ -94,22 +91,21 @@ impl<'a, E: Error, A: UserAdder<SubE=E>> fmt::Display for User<'a, E, A> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E: Error, A: UserAdder<SubE=E>> Symbol for User<'a, E, A> {
|
impl<'a, E: 'static + Error, A: UserAdder<SubE=E>> Symbol for User<'a, E, A> {
|
||||||
type Error = UserError<UserAdderError<E>>;
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||||
fn target_reached(&self) -> Result<bool, Self::Error> {
|
|
||||||
let output = self.command_runner.run_with_args("getent", &["passwd", self.user_name]);
|
let output = self.command_runner.run_with_args("getent", &["passwd", self.user_name]);
|
||||||
match output {
|
match output {
|
||||||
Ok(output) => match output.status.code() {
|
Ok(output) => match output.status.code() {
|
||||||
Some(2) => Ok(false),
|
Some(2) => Ok(false),
|
||||||
Some(0) => Ok(true),
|
Some(0) => Ok(true),
|
||||||
_ => Err(UserError::GenericError)
|
_ => Err(Box::new(UserError::GenericError))
|
||||||
},
|
},
|
||||||
Err(_) => Err(UserError::GenericError)
|
Err(e) => Err(Box::new(e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(&self) -> Result<(), Self::Error> {
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||||
self.user_adder.add_user(self.user_name).map_err(|e| UserError::ExecError(e))
|
self.user_adder.add_user(self.user_name).map_err(|e| Box::new(e) as Box<Error>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -192,12 +188,12 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_target_reached_nonexisting() {
|
fn test_target_reached_nonexisting() {
|
||||||
let symbol = User { user_name: "nonexisting", command_runner: &StdCommandRunner, user_adder: &DummyUserAdder };
|
let symbol = User { user_name: "nonexisting", command_runner: &StdCommandRunner, user_adder: &DummyUserAdder };
|
||||||
assert_eq!(symbol.target_reached(), Ok(false));
|
assert_eq!(symbol.target_reached().unwrap(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_target_reached_root() {
|
fn test_target_reached_root() {
|
||||||
let symbol = User { user_name: "root", command_runner: &StdCommandRunner, user_adder: &DummyUserAdder };
|
let symbol = User { user_name: "root", command_runner: &StdCommandRunner, user_adder: &DummyUserAdder };
|
||||||
assert_eq!(symbol.target_reached(), Ok(true));
|
assert_eq!(symbol.target_reached().unwrap(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue