Add generic systemd user service
This commit is contained in:
parent
7479ffd244
commit
f4f738c898
2 changed files with 41 additions and 37 deletions
|
|
@ -1,3 +1,3 @@
|
||||||
pub mod node_js_user_service;
|
|
||||||
pub mod reload;
|
pub mod reload;
|
||||||
|
pub mod user_service;
|
||||||
pub mod user_session;
|
pub mod user_session;
|
||||||
|
|
|
||||||
|
|
@ -13,45 +13,45 @@ use symbols::file::File as FileSymbol;
|
||||||
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum NodeJsSystemdUserServiceError<E: Error> {
|
pub enum UserServiceError<E: Error> {
|
||||||
ActivationFailed(io::Result<Output>),
|
ActivationFailed(io::Result<Output>),
|
||||||
ExecError(E),
|
ExecError(E),
|
||||||
GenericError,
|
GenericError,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<io::Error> for NodeJsSystemdUserServiceError<io::Error> {
|
impl From<io::Error> for UserServiceError<io::Error> {
|
||||||
fn from(err: io::Error) -> NodeJsSystemdUserServiceError<io::Error> {
|
fn from(err: io::Error) -> UserServiceError<io::Error> {
|
||||||
NodeJsSystemdUserServiceError::ExecError(err)
|
UserServiceError::ExecError(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Error> Error for NodeJsSystemdUserServiceError<E> {
|
impl<E: Error> Error for UserServiceError<E> {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
NodeJsSystemdUserServiceError::ExecError(ref e) => e.description(),
|
UserServiceError::ExecError(ref e) => e.description(),
|
||||||
NodeJsSystemdUserServiceError::GenericError => "Generic error",
|
UserServiceError::GenericError => "Generic error",
|
||||||
NodeJsSystemdUserServiceError::ActivationFailed(_) => "Activation of service failed",
|
UserServiceError::ActivationFailed(_) => "Activation of service failed",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn cause(&self) -> Option<&dyn Error> {
|
fn cause(&self) -> Option<&dyn Error> {
|
||||||
match self {
|
match self {
|
||||||
NodeJsSystemdUserServiceError::ExecError(ref e) => Some(e),
|
UserServiceError::ExecError(ref e) => Some(e),
|
||||||
_ => None,
|
_ => 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> {
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
try!(write!(f, "{}", self.description()));
|
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));
|
try!(write!(f, ": {:?}", log));
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NodeJsSystemdUserService<'a, C, R>
|
pub struct UserService<'a, C, R>
|
||||||
where
|
where
|
||||||
C: Deref<Target = str>,
|
C: Deref<Target = str>,
|
||||||
R: CommandRunner,
|
R: CommandRunner,
|
||||||
|
|
@ -62,23 +62,17 @@ where
|
||||||
file: FileSymbol<C, String>,
|
file: FileSymbol<C, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, R> NodeJsSystemdUserService<'a, String, SetuidCommandRunner<'a, R>>
|
impl<'a, R> UserService<'a, String, SetuidCommandRunner<'a, R>>
|
||||||
where
|
where
|
||||||
R: CommandRunner,
|
R: CommandRunner,
|
||||||
{
|
{
|
||||||
pub fn new(
|
pub fn new_nodejs(
|
||||||
home: &'a str,
|
home: &'a str,
|
||||||
user_name: &'a str,
|
user_name: &'a str,
|
||||||
service_name: &'a str,
|
service_name: &'a str,
|
||||||
path: &'a str,
|
path: &'a str,
|
||||||
command_runner: &'a R,
|
command_runner: &'a R,
|
||||||
) -> Self {
|
) -> 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 port = format!("/var/tmp/{}-{}.socket", user_name, service_name);
|
||||||
let content = format!(
|
let content = format!(
|
||||||
"[Service]
|
"[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
|
# FIXME: This only works if the nodejs path is a directory
|
||||||
WorkingDirectory={0}
|
WorkingDirectory={0}
|
||||||
Restart=always
|
|
||||||
#RuntimeDirectory=service
|
#RuntimeDirectory=service
|
||||||
#RuntimeDirectoryMode=766
|
#RuntimeDirectoryMode=766
|
||||||
|
Restart=always
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=default.target
|
WantedBy=default.target
|
||||||
",
|
",
|
||||||
path, port
|
path, port
|
||||||
);
|
);
|
||||||
|
UserService::new(home, user_name, service_name, command_runner, content)
|
||||||
|
}
|
||||||
|
|
||||||
NodeJsSystemdUserService {
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
UserService {
|
||||||
service_name,
|
service_name,
|
||||||
user_name,
|
user_name,
|
||||||
command_runner: SetuidCommandRunner::new(user_name, command_runner),
|
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
|
where
|
||||||
C: Deref<Target = str>,
|
C: Deref<Target = str>,
|
||||||
R: CommandRunner,
|
R: CommandRunner,
|
||||||
|
|
@ -152,13 +161,12 @@ where
|
||||||
"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" => {
|
"ActiveState=failed" => {
|
||||||
return Err(Box::new(NodeJsSystemdUserServiceError::ActivationFailed(
|
return Err(Box::new(
|
||||||
self.command_runner.run_with_args(
|
UserServiceError::ActivationFailed(self.command_runner.run_with_args(
|
||||||
"journalctl",
|
"journalctl",
|
||||||
&["--user", &format!("--user-unit={}", self.service_name)],
|
&["--user", &format!("--user-unit={}", self.service_name)],
|
||||||
),
|
)) as UserServiceError<io::Error>,
|
||||||
)
|
))
|
||||||
as NodeJsSystemdUserServiceError<io::Error>))
|
|
||||||
}
|
}
|
||||||
_ => return Ok(false),
|
_ => 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
|
where
|
||||||
C: Deref<Target = str>,
|
C: Deref<Target = str>,
|
||||||
R: CommandRunner,
|
R: CommandRunner,
|
||||||
|
|
@ -185,7 +193,7 @@ where
|
||||||
|
|
||||||
if !(try!(self.check_if_service())) {
|
if !(try!(self.check_if_service())) {
|
||||||
return Err(Box::new(
|
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
|
where
|
||||||
C: Deref<Target = str>,
|
C: Deref<Target = str>,
|
||||||
R: CommandRunner,
|
R: CommandRunner,
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
write!(
|
write!(f, "Systemd user service unit for {}", self.service_name)
|
||||||
f,
|
|
||||||
"Systemd Node.js user service unit for {}",
|
|
||||||
self.service_name
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue