Browse Source

Add generic systemd user service

master
Adrian Heine 5 years ago
parent
commit
f4f738c898
  1. 2
      src/symbols/systemd/mod.rs
  2. 76
      src/symbols/systemd/user_service.rs

2
src/symbols/systemd/mod.rs

@ -1,3 +1,3 @@
pub mod node_js_user_service;
pub mod reload;
pub mod user_service;
pub mod user_session;

76
src/symbols/systemd/node_js_user_service.rs → src/symbols/systemd/user_service.rs

@ -13,45 +13,45 @@ use symbols::file::File as FileSymbol;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
#[derive(Debug)]
pub enum NodeJsSystemdUserServiceError<E: Error> {
pub enum UserServiceError<E: Error> {
ActivationFailed(io::Result<Output>),
ExecError(E),
GenericError,
}
impl From<io::Error> for NodeJsSystemdUserServiceError<io::Error> {
fn from(err: io::Error) -> NodeJsSystemdUserServiceError<io::Error> {
NodeJsSystemdUserServiceError::ExecError(err)
impl From<io::Error> for UserServiceError<io::Error> {
fn from(err: io::Error) -> UserServiceError<io::Error> {
UserServiceError::ExecError(err)
}
}
impl<E: Error> Error for NodeJsSystemdUserServiceError<E> {
impl<E: Error> Error for UserServiceError<E> {
fn description(&self) -> &str {
match self {
NodeJsSystemdUserServiceError::ExecError(ref e) => e.description(),
NodeJsSystemdUserServiceError::GenericError => "Generic error",
NodeJsSystemdUserServiceError::ActivationFailed(_) => "Activation of service failed",
UserServiceError::ExecError(ref e) => e.description(),
UserServiceError::GenericError => "Generic error",
UserServiceError::ActivationFailed(_) => "Activation of service failed",
}
}
fn cause(&self) -> Option<&dyn Error> {
match self {
NodeJsSystemdUserServiceError::ExecError(ref e) => Some(e),
UserServiceError::ExecError(ref e) => Some(e),
_ => None,
}
}
}
impl<E: Error> fmt::Display for NodeJsSystemdUserServiceError<E> {
impl<E: Error> fmt::Display for UserServiceError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
try!(write!(f, "{}", self.description()));
if let NodeJsSystemdUserServiceError::ActivationFailed(Ok(ref log)) = self {
if let UserServiceError::ActivationFailed(Ok(ref log)) = self {
try!(write!(f, ": {:?}", log));
};
Ok(())
}
}
pub struct NodeJsSystemdUserService<'a, C, R>
pub struct UserService<'a, C, R>
where
C: Deref<Target = str>,
R: CommandRunner,
@ -62,23 +62,17 @@ where
file: FileSymbol<C, String>,
}
impl<'a, R> NodeJsSystemdUserService<'a, String, SetuidCommandRunner<'a, R>>
impl<'a, R> UserService<'a, String, SetuidCommandRunner<'a, R>>
where
R: CommandRunner,
{
pub fn new(
pub fn new_nodejs(
home: &'a str,
user_name: &'a str,
service_name: &'a str,
path: &'a str,
command_runner: &'a R,
) -> Self {
let file_path = format!(
"{}/.config/systemd/user/{}.service",
home.trim_end(),
service_name
);
let port = format!("/var/tmp/{}-{}.socket", user_name, service_name);
let content = format!(
"[Service]
@ -90,17 +84,32 @@ ExecStartPost=/bin/sh -c 'sleep 1 && chmod 666 {1}'
# FIXME: This only works if the nodejs path is a directory
WorkingDirectory={0}
Restart=always
#RuntimeDirectory=service
#RuntimeDirectoryMode=766
Restart=always
[Install]
WantedBy=default.target
",
path, port
);
UserService::new(home, user_name, service_name, command_runner, content)
}
pub fn new(
home: &'a str,
user_name: &'a str,
service_name: &'a str,
command_runner: &'a R,
content: String,
) -> Self {
let file_path = format!(
"{}/.config/systemd/user/{}.service",
home.trim_end(),
service_name
);
NodeJsSystemdUserService {
UserService {
service_name,
user_name,
command_runner: SetuidCommandRunner::new(user_name, command_runner),
@ -109,7 +118,7 @@ WantedBy=default.target
}
}
impl<'a, C, R> NodeJsSystemdUserService<'a, C, R>
impl<'a, C, R> UserService<'a, C, R>
where
C: Deref<Target = str>,
R: CommandRunner,
@ -152,13 +161,12 @@ where
"ActiveState=activating" => sleep(Duration::from_millis(500)),
"ActiveState=active" => return Ok(true),
"ActiveState=failed" => {
return Err(Box::new(NodeJsSystemdUserServiceError::ActivationFailed(
self.command_runner.run_with_args(
return Err(Box::new(
UserServiceError::ActivationFailed(self.command_runner.run_with_args(
"journalctl",
&["--user", &format!("--user-unit={}", self.service_name)],
),
)
as NodeJsSystemdUserServiceError<io::Error>))
)) as UserServiceError<io::Error>,
))
}
_ => return Ok(false),
}
@ -166,7 +174,7 @@ where
}
}
impl<'a, C, R> Symbol for NodeJsSystemdUserService<'a, C, R>
impl<'a, C, R> Symbol for UserService<'a, C, R>
where
C: Deref<Target = str>,
R: CommandRunner,
@ -185,7 +193,7 @@ where
if !(try!(self.check_if_service())) {
return Err(Box::new(
NodeJsSystemdUserServiceError::GenericError as NodeJsSystemdUserServiceError<io::Error>,
UserServiceError::GenericError as UserServiceError<io::Error>,
));
}
@ -216,16 +224,12 @@ where
}
}
impl<'a, C, R> fmt::Display for NodeJsSystemdUserService<'a, C, R>
impl<'a, C, R> fmt::Display for UserService<'a, C, R>
where
C: Deref<Target = str>,
R: CommandRunner,
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
f,
"Systemd Node.js user service unit for {}",
self.service_name
)
write!(f, "Systemd user service unit for {}", self.service_name)
}
}
Loading…
Cancel
Save