You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.0 KiB
124 lines
4.0 KiB
use std::error::Error;
|
|
use std::fmt;
|
|
use std::io;
|
|
use std::path::Path;
|
|
use std::thread::sleep;
|
|
use std::time::Duration;
|
|
use std::ops::Deref;
|
|
|
|
use command_runner::CommandRunner;
|
|
use symbols::Symbol;
|
|
use symbols::file::File as FileSymbol;
|
|
|
|
#[derive(Debug)]
|
|
pub enum NodeJsSystemdUserServiceError<E: Error> {
|
|
ExecError(E),
|
|
GenericError
|
|
}
|
|
|
|
impl From<io::Error> for NodeJsSystemdUserServiceError<io::Error> {
|
|
fn from(err: io::Error) -> NodeJsSystemdUserServiceError<io::Error> {
|
|
NodeJsSystemdUserServiceError::ExecError(err)
|
|
}
|
|
}
|
|
|
|
impl<E: Error> Error for NodeJsSystemdUserServiceError<E> {
|
|
fn description(&self) -> &str {
|
|
match self {
|
|
&NodeJsSystemdUserServiceError::ExecError(ref e) => e.description(),
|
|
&NodeJsSystemdUserServiceError::GenericError => "Generic error"
|
|
}
|
|
}
|
|
fn cause(&self) -> Option<&Error> {
|
|
match self {
|
|
&NodeJsSystemdUserServiceError::ExecError(ref e) => Some(e),
|
|
_ => None
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E: Error> fmt::Display for NodeJsSystemdUserServiceError<E> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
write!(f, "{}", self.description())
|
|
}
|
|
}
|
|
|
|
pub struct NodeJsSystemdUserService<'a, P, C> where P: AsRef<str> + fmt::Display, C: Deref<Target=str> {
|
|
name: &'a str,
|
|
path: P,
|
|
command_runner: &'a CommandRunner,
|
|
file: FileSymbol<C, Cow<'a, str>>
|
|
}
|
|
|
|
use std::borrow::Cow;
|
|
|
|
impl<'a> NodeJsSystemdUserService<'a, Cow<'a, str>, String> {
|
|
pub fn new(name: &'a str, path: &'a str, command_runner: &'a CommandRunner) -> Self {
|
|
let home = String::from_utf8(command_runner.run_with_args("sh", &["-c", "echo \"$HOME\""]).unwrap().stdout).unwrap();
|
|
let file_path: Cow<str> = Cow::from(String::from(home.trim_right()) + "/.config/systemd/user/" + name + ".service");
|
|
|
|
let content = format!("[Service]
|
|
ExecStart=/usr/bin/nodejs {}
|
|
Restart=always
|
|
Environment=NODE_ENV=production
|
|
Environment=PORT={}/var/service.socket
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
", path, home);
|
|
NodeJsSystemdUserService {
|
|
name: name,
|
|
path: file_path.clone(),
|
|
command_runner: command_runner,
|
|
file: FileSymbol::new(file_path, content)
|
|
}
|
|
}
|
|
}
|
|
|
|
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, Self::Error> {
|
|
match self.file.target_reached() {
|
|
Ok(false) => return Ok(false),
|
|
Ok(true) => {},
|
|
Err(e) => return Err(NodeJsSystemdUserServiceError::GenericError)
|
|
}
|
|
/*
|
|
if !(try!(self.file.target_reached())) {
|
|
return Ok(false)
|
|
}
|
|
*/
|
|
loop {
|
|
// Check if service is registered
|
|
let active_state = try!(self.command_runner.run_with_args("systemctl", &["--user", "show", "--property", "ActiveState", self.name]));
|
|
if !active_state.status.success() {
|
|
return Ok(false);
|
|
}
|
|
// Check if service is running
|
|
match String::from_utf8(active_state.stdout).unwrap().trim_right() {
|
|
"ActiveState=activating" => sleep(Duration::from_millis(500)),
|
|
"ActiveState=active" => return Ok(true),
|
|
_ => return Ok(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Self::Error> {
|
|
try!(self.command_runner.run_with_args("mkdir", &["-p", Path::new(self.path.as_ref()).parent().unwrap().to_str().unwrap()]));
|
|
// FIXME: Permissions
|
|
// try!(create_dir_all(Path::new(&path).parent().unwrap()));
|
|
match self.file.execute() {
|
|
Ok(_) => {},
|
|
Err(e) => return Err(NodeJsSystemdUserServiceError::GenericError)
|
|
}
|
|
try!(self.command_runner.run_with_args("systemctl", &["--user", "enable", self.name]));
|
|
try!(self.command_runner.run_with_args("systemctl", &["--user", "start", self.name]));
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<'a, P, C> fmt::Display for NodeJsSystemdUserService<'a, P, C> where P: AsRef<str> + fmt::Display, C: Deref<Target=str> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
|
|
write!(f, "Systemd Node.js user service unit for {}", self.path)
|
|
}
|
|
}
|