6 changed files with 212 additions and 21 deletions
			
			
		- 
					6src/symbols/file.rs
 - 
					2src/symbols/mod.rs
 - 
					1src/symbols/nginx/mod.rs
 - 
					116src/symbols/nginx/server.rs
 - 
					42src/symbols/npm.rs
 - 
					66src/symbols/systemd/node_js_user_service.rs
 
@ -0,0 +1 @@ | 
			
		|||||
 | 
				pub mod server;
 | 
			
		||||
@ -0,0 +1,116 @@ | 
			
		|||||
 | 
				use std::error::Error;
 | 
			
		||||
 | 
				use std::fmt;
 | 
			
		||||
 | 
				use std::io;
 | 
			
		||||
 | 
				use std::ops::Deref;
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				use command_runner::CommandRunner;
 | 
			
		||||
 | 
				use symbols::Symbol;
 | 
			
		||||
 | 
				use symbols::file::File as FileSymbol;
 | 
			
		||||
 | 
				use symbols::file::FileError;
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				#[derive(Debug)]
 | 
			
		||||
 | 
				pub enum NginxServerError<E: Error> {
 | 
			
		||||
 | 
				  ExecError(E),
 | 
			
		||||
 | 
				  GenericError
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<E: Error> From<FileError<E>> for NginxServerError<FileError<E>> {
 | 
			
		||||
 | 
				  fn from(err: FileError<E>) -> NginxServerError<FileError<E>> {
 | 
			
		||||
 | 
				    NginxServerError::ExecError(err)
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<E: Error> From<io::Error> for NginxServerError<FileError<E>> {
 | 
			
		||||
 | 
				  fn from(err: io::Error) -> NginxServerError<FileError<E>> {
 | 
			
		||||
 | 
				    NginxServerError::GenericError
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<E: Error> Error for NginxServerError<E> {
 | 
			
		||||
 | 
				  fn description(&self) -> &str {
 | 
			
		||||
 | 
				    match self {
 | 
			
		||||
 | 
				      &NginxServerError::ExecError(ref e) => e.description(),
 | 
			
		||||
 | 
				      &NginxServerError::GenericError => "Generic error"
 | 
			
		||||
 | 
				    }
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				  fn cause(&self) -> Option<&Error> {
 | 
			
		||||
 | 
				    match self {
 | 
			
		||||
 | 
				      &NginxServerError::ExecError(ref e) => Some(e),
 | 
			
		||||
 | 
				      _ => None
 | 
			
		||||
 | 
				    }
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<E: Error> fmt::Display for NginxServerError<E> {
 | 
			
		||||
 | 
				  fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
 | 
			
		||||
 | 
				    write!(f, "{}", self.description())
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				pub struct NginxServer<'a, C> where C: Deref<Target=str> {
 | 
			
		||||
 | 
				  command_runner: &'a CommandRunner,
 | 
			
		||||
 | 
				  file: FileSymbol<C, Cow<'a, str>>,
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				use std::borrow::Cow;
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<'a> NginxServer<'a, String> {
 | 
			
		||||
 | 
				  pub fn new(home: &'a str, domain: &'a str, static_path: &'a str, redir_domains: &[&'a str], command_runner: &'a CommandRunner) -> Self {
 | 
			
		||||
 | 
				    let file_path: Cow<str> = Cow::from(String::from("/etc/nginx/sites-enabled/") + domain);
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				    let redir_content = redir_domains.iter().map(|redir_domain| format!("server {{
 | 
			
		||||
 | 
				    listen 80;
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				    server_name {};
 | 
			
		||||
 | 
				    return 302 $scheme://{}$request_uri;
 | 
			
		||||
 | 
				  }}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				  ", redir_domain, domain)).fold(String::new(), |s, v| s + &v);
 | 
			
		||||
 | 
				      let content = String::from(redir_content) + &format!("server {{
 | 
			
		||||
 | 
				    listen                  80;
 | 
			
		||||
 | 
				  #  listen                  443 ssl;
 | 
			
		||||
 | 
				  #  ssl_certificate         /etc/ssl/.crt;
 | 
			
		||||
 | 
				  #  ssl_certificate_key     /etc/ssl/.key;
 | 
			
		||||
 | 
				    server_name             {};
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				    root                    {};
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				  location / {{
 | 
			
		||||
 | 
				    try_files             $uri @proxy;
 | 
			
		||||
 | 
				  }}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				  location @proxy {{
 | 
			
		||||
 | 
				    include               fastcgi_params;
 | 
			
		||||
 | 
				    proxy_pass            http://unix:{}/var/service.socket:;
 | 
			
		||||
 | 
				    proxy_redirect        off;
 | 
			
		||||
 | 
				  }}
 | 
			
		||||
 | 
				}}", domain, static_path, home);
 | 
			
		||||
 | 
				    NginxServer {
 | 
			
		||||
 | 
				      command_runner: command_runner,
 | 
			
		||||
 | 
				      file: FileSymbol::new(file_path, content)
 | 
			
		||||
 | 
				    }
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				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, Self::Error> {
 | 
			
		||||
 | 
				    if !try!(self.file.target_reached()) {
 | 
			
		||||
 | 
				      return Ok(false);
 | 
			
		||||
 | 
				    }
 | 
			
		||||
 | 
				    // TODO: Could try to find out if the server is in the live config
 | 
			
		||||
 | 
				    Ok(true)
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				  fn execute(&self) -> Result<(), Self::Error> {
 | 
			
		||||
 | 
				    try!(self.file.execute());
 | 
			
		||||
 | 
				    try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"]));
 | 
			
		||||
 | 
				    Ok(())
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<'a, C> fmt::Display for NginxServer<'a, C> where C: Deref<Target=str> {
 | 
			
		||||
 | 
				  fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
 | 
			
		||||
 | 
				    write!(f, "Nginx server config")
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
@ -0,0 +1,42 @@ | 
			
		|||||
 | 
				use std::fmt;
 | 
			
		||||
 | 
				use std::io;
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				use command_runner::CommandRunner;
 | 
			
		||||
 | 
				use symbols::Symbol;
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				pub struct NpmInstall<'a> {
 | 
			
		||||
 | 
				  target: &'a str,
 | 
			
		||||
 | 
				  command_runner: &'a CommandRunner
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<'a> NpmInstall<'a> {
 | 
			
		||||
 | 
				  pub fn new(target: &'a str, command_runner: &'a CommandRunner) -> NpmInstall<'a> {
 | 
			
		||||
 | 
				    NpmInstall {
 | 
			
		||||
 | 
				      target: target,
 | 
			
		||||
 | 
				      command_runner: command_runner
 | 
			
		||||
 | 
				    }
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<'a> fmt::Display for NpmInstall<'a> {
 | 
			
		||||
 | 
				  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 | 
			
		||||
 | 
				    write!(f, "npm install in {}", self.target)
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<'a> Symbol for NpmInstall<'a> {
 | 
			
		||||
 | 
				  type Error = io::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)]));
 | 
			
		||||
 | 
				    Ok(result.status.success() && !String::from_utf8(result.stdout).unwrap().contains("(empty)"))
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				  fn execute(&self) -> Result<(), Self::Error> {
 | 
			
		||||
 | 
				    try!(self.command_runner.run_with_args("sh", &["-c", &format!("cd '{}' && npm install --production", self.target)]));
 | 
			
		||||
 | 
				    Ok(())
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				#[cfg(test)]
 | 
			
		||||
 | 
				mod test {
 | 
			
		||||
 | 
				}
 | 
			
		||||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue