Adrian Heine
8 years ago
8 changed files with 288 additions and 15 deletions
-
29src/command_runner.rs
-
7src/schema.rs
-
94src/symbols/file.rs
-
8src/symbols/mod.rs
-
1src/symbols/systemd/mod.rs
-
124src/symbols/systemd/node_js_user_service.rs
-
16src/symbols/systemd/user_session.rs
-
24src/symbols/user.rs
@ -0,0 +1,94 @@ |
|||
use std::error::Error;
|
|||
use std::fmt;
|
|||
use std::io;
|
|||
use std::ops::Deref;
|
|||
|
|||
use symbols::Symbol;
|
|||
|
|||
#[derive(Debug)]
|
|||
pub enum FileError<E: Error> {
|
|||
ExecError(E),
|
|||
GenericError
|
|||
}
|
|||
|
|||
impl From<io::Error> for FileError<io::Error> {
|
|||
fn from(err: io::Error) -> FileError<io::Error> {
|
|||
FileError::ExecError(err)
|
|||
}
|
|||
}
|
|||
|
|||
impl<E: Error> Error for FileError<E> {
|
|||
fn description(&self) -> &str {
|
|||
match self {
|
|||
&FileError::ExecError(ref e) => e.description(),
|
|||
&FileError::GenericError => "Generic error"
|
|||
}
|
|||
}
|
|||
fn cause(&self) -> Option<&Error> {
|
|||
match self {
|
|||
&FileError::ExecError(ref e) => Some(e),
|
|||
_ => None
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
impl<E: Error> fmt::Display for FileError<E> {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|||
write!(f, "{}", self.description())
|
|||
}
|
|||
}
|
|||
|
|||
use std::convert::AsRef;
|
|||
|
|||
pub struct File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
|
|||
path: D,
|
|||
content: C
|
|||
}
|
|||
|
|||
impl<C, D> File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
|
|||
pub fn new(path: D, content: C) -> Self {
|
|||
File {
|
|||
path: path,
|
|||
content: content
|
|||
}
|
|||
}
|
|||
}
|
|||
|
|||
use std::fs::File as FsFile;
|
|||
use std::io::{Read, Write};
|
|||
|
|||
impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
|
|||
type Error = FileError<io::Error>;
|
|||
fn target_reached(&self) -> Result<bool, Self::Error> {
|
|||
let file = FsFile::open(self.path.as_ref());
|
|||
// Check if file exists
|
|||
if let Err(e) = file {
|
|||
return if e.kind() == io::ErrorKind::NotFound {
|
|||
Ok(false)
|
|||
} else {
|
|||
Err(e.into())
|
|||
};
|
|||
}
|
|||
// Check if content is the same
|
|||
let file_content = file.unwrap().bytes();
|
|||
let content_equal = try!(self.content.bytes().zip(file_content).fold(
|
|||
Ok(true),
|
|||
|state, (target_byte, file_byte_option)| state.and_then(|s| file_byte_option.map(|file_byte| s && file_byte == target_byte))
|
|||
));
|
|||
return Ok(content_equal)
|
|||
}
|
|||
|
|||
fn execute(&self) -> Result<(), Self::Error> {
|
|||
//try!(self.command_runner.run_with_args("mkdir", &["-p", Path::new(&path).parent().unwrap().to_str().unwrap()]));
|
|||
// FIXME: Permissions
|
|||
// try!(create_dir_all(Path::new(&path).parent().unwrap()));
|
|||
try!(try!(FsFile::create(self.path.as_ref())).write_all(self.content.as_bytes()));
|
|||
Ok(())
|
|||
}
|
|||
}
|
|||
|
|||
impl<C, D> fmt::Display for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
|
|||
write!(f, "File {}", self.path)
|
|||
}
|
|||
}
|
@ -1,9 +1,13 @@ |
|||
pub trait Symbol {
|
|||
type Error;
|
|||
use std::error::Error;
|
|||
use std::fmt::Display;
|
|||
|
|||
pub trait Symbol: Display {
|
|||
type Error: Error;
|
|||
fn target_reached(&self) -> Result<bool, Self::Error>;
|
|||
fn execute(&self) -> Result<(), Self::Error>;
|
|||
}
|
|||
|
|||
pub mod file;
|
|||
pub mod git;
|
|||
pub mod user;
|
|||
pub mod systemd;
|
@ -1 +1,2 @@ |
|||
pub mod user_session;
|
|||
pub mod node_js_user_service;
|
@ -0,0 +1,124 @@ |
|||
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)
|
|||
}
|
|||
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue