diff --git a/Cargo.toml b/Cargo.toml index ee3f6ee..4420078 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Adrian Heine "] [dependencies] users = "0.5.0" +regex = "0.2" [dev-dependencies] tempdir = "0.3" -regex = "0.2" diff --git a/src/command_runner.rs b/src/command_runner.rs index 0b1b20a..bfdd35e 100644 --- a/src/command_runner.rs +++ b/src/command_runner.rs @@ -1,9 +1,32 @@ +use std::error::Error; use std::io::Result as IoResult; use std::process::Command; use std::process::Output; pub trait CommandRunner { fn run_with_args(&self, program: &str, args: &[&str]) -> IoResult; + fn get_output(&self, program: &str, args: &[&str]) -> Result, Box> { + let output = try!(self.run_with_args(program, args)); + if !output.status.success() { + return Err(try!(String::from_utf8(output.stderr)).into()); + } + Ok(output.stdout) + } + fn get_stderr(&self, program: &str, args: &[&str]) -> Result, Box> { + let output = try!(self.run_with_args(program, args)); + if !output.status.success() { + return Err(try!(String::from_utf8(output.stderr)).into()); + } + Ok(output.stderr) + } + fn run_successfully(&self, program: &str, args: &[&str]) -> Result<(), Box> { + let output = try!(self.run_with_args(program, args)); + if output.status.success() { + Ok(()) + } else { + Err(try!(String::from_utf8(output.stderr)).into()) + } + } } #[derive(Debug)] diff --git a/src/lib.rs b/src/lib.rs index 2244e97..cd29069 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ missing_debug_implementations #![allow(box_pointers)] +extern crate regex; extern crate users; pub mod command_runner; diff --git a/src/symbols/acme/account_key.rs b/src/symbols/acme/account_key.rs index 0ff0a90..1e5da2f 100644 --- a/src/symbols/acme/account_key.rs +++ b/src/symbols/acme/account_key.rs @@ -1,6 +1,7 @@ -use std::borrow::Cow; +use std::borrow::{Borrow, Cow}; use std::error::Error; use std::fmt; +use std::path::Path; use command_runner::CommandRunner; use symbols::Symbol; @@ -18,8 +19,8 @@ impl<'a> AcmeAccountKey<'a> { } } - fn get_path(&self) -> String { - self.path.clone().into_owned() + fn get_path(&self) -> &str { + self.path.borrow() } fn get_bytes(&self) -> u32 { @@ -35,23 +36,15 @@ impl<'a> fmt::Display for AcmeAccountKey<'a> { impl<'a> Symbol for AcmeAccountKey<'a> { fn target_reached(&self) -> Result> { - let result = self.command_runner.run_with_args("openssl", &["rsa", "-in", &self.get_path(), "-noout", "-check", "-text"]); - match result { - Err(e) => Err(Box::new(e)), - Ok(output) => match output.status.code() { - Some(0) => Ok(output.stdout.starts_with(format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes())), - Some(_) => Ok(false), - _ => Err("Didn't work".to_string().into()) - } + if !Path::new(self.get_path()).exists() { + return Ok(false); } + let stdout = try!(self.command_runner.get_output("openssl", &["rsa", "-in", self.get_path(), "-noout", "-check", "-text"])); + Ok(stdout.starts_with(&format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes())) } fn execute(&self) -> Result<(), Box> { - let output = self.command_runner.run_with_args("openssl", &["genrsa", "-out", &self.get_path(), &self.get_bytes().to_string()]); - match output { - Err(e) => Err(Box::new(e)), - Ok(_) => Ok(()) - } + self.command_runner.run_successfully("openssl", &["genrsa", "-out", self.get_path(), &self.get_bytes().to_string()]) } } diff --git a/src/symbols/acme/cert.rs b/src/symbols/acme/cert.rs index 8e603f6..c1863f5 100644 --- a/src/symbols/acme/cert.rs +++ b/src/symbols/acme/cert.rs @@ -2,7 +2,8 @@ use std::borrow::Cow; use std::error::Error; use std::fmt; use std::fs::File as FsFile; -use std::io::{self, Write}; +use std::io::Write; +use std::path::Path; use command_runner::CommandRunner; use symbols::Symbol; @@ -40,35 +41,23 @@ const DAYS_IN_SECONDS: u32 = 24*60*60; impl<'a> Symbol for AcmeCert<'a> { fn target_reached(&self) -> Result> { - let file = FsFile::open(self.get_cert_path()); - // Check first if file exists to support dry-run mode where the acme user is not even created - if let Err(e) = file { - return if e.kind() == io::ErrorKind::NotFound { - Ok(false) - } else { - Err(Box::new(e)) - }; + if !Path::new(&self.get_cert_path()).exists() { + return Ok(false); } // FIXME: check who signed it - let result = self.command_runner.run_with_args("openssl", &["x509", "-in", &self.get_cert_path(), "-noout", "-subject", "-checkend", &(30*DAYS_IN_SECONDS).to_string()]); - match result { - Err(e) => Err(Box::new(e)), - Ok(output) => match output.status.code() { - Some(0) => if output.stdout == format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes() { - let result = try!(self.command_runner.run_with_args("openssl", &["verify", "--untrusted", "/home/acme/lets_encrypt_x3_cross_signed.pem", &self.get_cert_path()]).map_err(|e| Box::new(e))); - Ok(result.status.code() == Some(0)) - } else { Ok(false) }, - Some(_) => Ok(false), - _ => Err("Didn't work".to_string().into()) - } + let stdout = try!(self.command_runner.get_output("openssl", &["x509", "-in", &self.get_cert_path(), "-noout", "-subject", "-checkend", &(30*DAYS_IN_SECONDS).to_string()])); + if stdout != format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes() { + return Ok(false); } + try!(self.command_runner.run_successfully("openssl", &["verify", "--untrusted", "/home/acme/lets_encrypt_x3_cross_signed.pem", &self.get_cert_path()])); + Ok(true) } fn execute(&self) -> Result<(), Box> { - let output = try!(self.command_runner.run_with_args("acme-tiny", &["--account-key", "/home/acme/account.key", "--csr", &self.get_csr_path(), "--acme-dir", "/home/acme/challenges/"]).map_err(|e| Box::new(e))); + let output = try!(self.command_runner.get_output("acme-tiny", &["--account-key", "/home/acme/account.key", "--csr", &self.get_csr_path(), "--acme-dir", "/home/acme/challenges/"])); let mut file = try!(FsFile::create(self.get_cert_path())); - try!(file.write_all(&output.stdout)); + try!(file.write_all(&output)); Ok(()) } diff --git a/src/symbols/git/checkout.rs b/src/symbols/git/checkout.rs index 6fea070..ab6ad33 100644 --- a/src/symbols/git/checkout.rs +++ b/src/symbols/git/checkout.rs @@ -34,10 +34,10 @@ impl<'a> fmt::Display for GitCheckout<'a> { use std::fs::metadata; impl<'a> GitCheckout<'a> { - fn _run_in_target_repo(&self, args: &[&str]) -> Result, io::Error> { + fn _run_in_target_repo(&self, args: &[&str]) -> Result, Box> { let mut new_args = vec!["-C", self.target]; new_args.extend_from_slice(args); - self.command_runner.run_with_args("git", &new_args).map(|res| res.stdout) + self.command_runner.get_output("git", &new_args) } } @@ -57,13 +57,8 @@ impl<'a> Symbol for GitCheckout<'a> { } fn execute(&self) -> Result<(), Box> { - if let Err(e) = metadata(self.target) { - return if e.kind() == io::ErrorKind::NotFound { - try!(self.command_runner.run_with_args("git", &["clone", "--depth", "1", "-b", self.branch, self.source, self.target])); - Ok(()) - } else { - Err(Box::new(e)) - }; + if !Path::new(self.target).exists() { + return self.command_runner.run_successfully("git", &["clone", "--depth", "1", "-b", self.branch, self.source, self.target]); } try!(self._run_in_target_repo(&["fetch", self.source, self.branch])); try!(self._run_in_target_repo(&["merge", "FETCH_HEAD"])); diff --git a/src/symbols/git/submodules.rs b/src/symbols/git/submodules.rs index 99eba64..9a4cc37 100644 --- a/src/symbols/git/submodules.rs +++ b/src/symbols/git/submodules.rs @@ -1,6 +1,5 @@ use std::error::Error; use std::fmt; -use std::io; use command_runner::CommandRunner; use symbols::Symbol; @@ -26,17 +25,17 @@ impl<'a> fmt::Display for GitSubmodules<'a> { } impl<'a> GitSubmodules<'a> { - fn _run_in_target_repo(&self, args: &[&str]) -> Result, io::Error> { + fn _run_in_target_repo(&self, args: &[&str]) -> Result, Box> { let mut new_args = vec!["-C", self.target]; new_args.extend_from_slice(args); - self.command_runner.run_with_args("git", &new_args).map(|res| res.stdout) + self.command_runner.get_output("git", &new_args) } } impl<'a> Symbol for GitSubmodules<'a> { fn target_reached(&self) -> Result> { - let output = try!(self._run_in_target_repo(&["submodule", "status"])); - Ok(String::from_utf8(output).unwrap().lines().all(|line| line.len() == 0 || line.starts_with(' '))) + let output = try!(String::from_utf8(try!(self._run_in_target_repo(&["submodule", "status"])))); + Ok(output.lines().all(|line| line.len() == 0 || line.starts_with(' '))) } fn execute(&self) -> Result<(), Box> { diff --git a/src/symbols/mariadb/database.rs b/src/symbols/mariadb/database.rs index a6b7a3d..60951b2 100644 --- a/src/symbols/mariadb/database.rs +++ b/src/symbols/mariadb/database.rs @@ -21,11 +21,8 @@ impl<'a> MariaDBDatabase<'a> { } fn run_sql(&self, sql: &str) -> Result> { - let output = try!(self.command_runner.run_with_args("mariadb", &["--skip-column-names", "-B", "-e", sql])); - if output.status.code() != Some(0) { - return Err(try!(String::from_utf8(output.stderr)).into()); - } - Ok(try!(String::from_utf8(output.stdout))) + let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql])); + Ok(try!(String::from_utf8(b))) } } @@ -42,8 +39,7 @@ impl<'a> Symbol for MariaDBDatabase<'a> { fn execute(&self) -> Result<(), Box> { try!(self.run_sql(&format!("CREATE DATABASE {}", self.db_name))); - try!(self.command_runner.run_with_args("sh", &["-c", &format!("mariadb '{}' < {}", self.db_name, self.seed_file)])); - Ok(()) + self.command_runner.run_successfully("sh", &["-c", &format!("mariadb '{}' < {}", self.db_name, self.seed_file)]) } } diff --git a/src/symbols/mariadb/database_dump.rs b/src/symbols/mariadb/database_dump.rs index 4dc4fb0..a304b02 100644 --- a/src/symbols/mariadb/database_dump.rs +++ b/src/symbols/mariadb/database_dump.rs @@ -23,11 +23,8 @@ impl<'a, S> DatabaseDump<'a, S> where S: Storage { } fn run_sql(&self, sql: &str) -> Result> { - let output = try!(self.command_runner.run_with_args("mariadb", &["--skip-column-names", "-B", "-e", sql])); - if output.status.code() != Some(0) { - return Err(try!(String::from_utf8(output.stderr)).into()); - } - Ok(try!(String::from_utf8(output.stdout))) + let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql])); + Ok(try!(String::from_utf8(b))) } } @@ -45,8 +42,7 @@ impl<'a, S> Symbol for DatabaseDump<'a, S> where S: Storage { } fn execute(&self) -> Result<(), Box> { - try!(self.command_runner.run_with_args("sh", &["-c", &format!("mysqldump '{}' > {}", self.db_name, self.storage.write_filename())])); - Ok(()) + self.command_runner.run_successfully("sh", &["-c", &format!("mysqldump '{}' > {}", self.db_name, self.storage.write_filename())]) } } diff --git a/src/symbols/mariadb/user.rs b/src/symbols/mariadb/user.rs index a44c667..b4e53b1 100644 --- a/src/symbols/mariadb/user.rs +++ b/src/symbols/mariadb/user.rs @@ -20,11 +20,8 @@ impl<'a> MariaDBUser<'a> { } fn run_sql(&self, sql: &str) -> Result> { - let output = try!(self.command_runner.run_with_args("mariadb", &["--skip-column-names", "-e", sql])); - if output.status.code() != Some(0) { - return Err(try!(String::from_utf8(output.stderr)).into()); - } - Ok(try!(String::from_utf8(output.stdout))) + let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql])); + Ok(try!(String::from_utf8(b))) } } diff --git a/src/symbols/mod.rs b/src/symbols/mod.rs index aa57e2e..4f43cd0 100644 --- a/src/symbols/mod.rs +++ b/src/symbols/mod.rs @@ -22,10 +22,10 @@ pub mod if_already_present; pub mod list; pub mod mariadb; pub mod nginx; -pub mod not_a_symlink; pub mod npm; pub mod owner; pub mod stored_directory; pub mod systemd; pub mod tls; pub mod user; +pub mod wordpress; diff --git a/src/symbols/nginx/server.rs b/src/symbols/nginx/server.rs index 6406ed2..19a7f33 100644 --- a/src/symbols/nginx/server.rs +++ b/src/symbols/nginx/server.rs @@ -140,8 +140,7 @@ impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref { fn execute(&self) -> Result<(), Box> { try!(self.file.execute()); - try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"])); - Ok(()) + self.command_runner.run_successfully("systemctl", &["reload-or-restart", "nginx"]) } fn get_prerequisites(&self) -> Vec { diff --git a/src/symbols/not_a_symlink.rs b/src/symbols/not_a_symlink.rs deleted file mode 100644 index 771c69e..0000000 --- a/src/symbols/not_a_symlink.rs +++ /dev/null @@ -1,44 +0,0 @@ -use std::error::Error; -use std::fmt; -use std::fs; -use std::io; - -use symbols::Symbol; - -pub struct NotASymlink where D: AsRef + fmt::Display { - path: D -} - -impl NotASymlink where D: AsRef + fmt::Display { - pub fn new(path: D) -> Self { - NotASymlink { - path: path - } - } -} - -impl Symbol for NotASymlink where D: AsRef + fmt::Display { - fn target_reached(&self) -> Result> { - let metadata = fs::symlink_metadata(self.path.as_ref()); - // Check if file exists - if let Err(e) = metadata { - return if e.kind() == io::ErrorKind::NotFound { - Ok(true) - } else { - Err(Box::new(e)) - }; - } - Ok(!metadata.unwrap().file_type().is_symlink()) - } - - fn execute(&self) -> Result<(), Box> { - try!(fs::remove_file(self.path.as_ref())); - Ok(()) - } -} - -impl fmt::Display for NotASymlink where D: AsRef + fmt::Display { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ - write!(f, "NotASymlink {}", self.path) - } -} diff --git a/src/symbols/npm.rs b/src/symbols/npm.rs index e4bbe78..a36e000 100644 --- a/src/symbols/npm.rs +++ b/src/symbols/npm.rs @@ -1,5 +1,6 @@ use std::error::Error; use std::fmt; +use std::path::Path; use command_runner::CommandRunner; use symbols::Symbol; @@ -26,13 +27,15 @@ impl<'a> fmt::Display for NpmInstall<'a> { impl<'a> Symbol for NpmInstall<'a> { fn target_reached(&self) -> Result> { + if !Path::new(self.target).exists() { + return Ok(false); + } 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<(), Box> { - try!(self.command_runner.run_with_args("sh", &["-c", &format!("cd '{}' && npm install --production --unsafe-perm", self.target)])); - Ok(()) + self.command_runner.run_successfully("sh", &["-c", &format!("cd '{}' && npm install --production --unsafe-perm", self.target)]) } } diff --git a/src/symbols/owner.rs b/src/symbols/owner.rs index 5424e05..5a8d75a 100644 --- a/src/symbols/owner.rs +++ b/src/symbols/owner.rs @@ -30,8 +30,7 @@ impl<'a, D> Symbol for Owner<'a, D> where D: AsRef + fmt::Display { } fn execute(&self) -> Result<(), Box> { - try!(self.command_runner.run_with_args("chown", &[self.user_name.borrow(), self.path.as_ref()])); - Ok(()) + self.command_runner.run_successfully("chown", &[self.user_name.borrow(), self.path.as_ref()]) } fn get_prerequisites(&self) -> Vec { diff --git a/src/symbols/stored_directory.rs b/src/symbols/stored_directory.rs index 18141d1..fb6575e 100644 --- a/src/symbols/stored_directory.rs +++ b/src/symbols/stored_directory.rs @@ -12,7 +12,7 @@ use symbols::Symbol; use storage::Storage; #[derive(Debug, PartialEq)] -pub enum StorageDirection { load, save } +pub enum StorageDirection { Load, Save } pub struct StoredDirectory<'a, S> where S: Storage { path: Cow<'a, str>, @@ -44,7 +44,7 @@ impl<'a, S> Symbol for StoredDirectory<'a, S> where S: Storage { // Check if dir exists if let Err(e) = metadata { return if e.kind() == io::ErrorKind::NotFound { - Ok(self.dir == StorageDirection::save) + Ok(self.dir == StorageDirection::Save) } else { Err(Box::new(e)) }; @@ -54,25 +54,21 @@ impl<'a, S> Symbol for StoredDirectory<'a, S> where S: Storage { } let dump_date = try!(self.storage.recent_date()); - let output = try!(self.command_runner.run_with_args("sh", &["-c", &format!("find {} -printf '%T@\\n' | sort -r | head -n1 | grep '^[0-9]\\+' -o", self.path)])); - if output.status.code() != Some(0) { - return Err(try!(String::from_utf8(output.stderr)).into()); - } - let modified_date = try!(u64::from_str(try!(String::from_utf8(output.stdout)).trim_right())); - Ok(if self.dir == StorageDirection::save { modified_date <= dump_date } else { dump_date <= modified_date }) + let output = try!(self.command_runner.get_output("sh", &["-c", &format!("find {} -printf '%T@\\n' | sort -r | head -n1 | grep '^[0-9]\\+' -o", self.path)])); + let modified_date = try!(u64::from_str(try!(String::from_utf8(output)).trim_right())); + Ok(if self.dir == StorageDirection::Save { modified_date <= dump_date } else { dump_date <= modified_date }) } fn execute(&self) -> Result<(), Box> { - if self.dir == StorageDirection::load { - try!(self.command_runner.run_with_args("cp", &["-a", &try!(self.storage.read_filename()), self.path.borrow()])); + if self.dir == StorageDirection::Load { + self.command_runner.run_successfully("cp", &["-a", &try!(self.storage.read_filename()), self.path.borrow()]) } else { - try!(self.command_runner.run_with_args("cp", &["-a", self.path.borrow(), &self.storage.write_filename()])); + self.command_runner.run_successfully("cp", &["-a", self.path.borrow(), &self.storage.write_filename()]) } - Ok(()) } fn get_prerequisites(&self) -> Vec { - if self.dir == StorageDirection::save { return vec![]; } + if self.dir == StorageDirection::Save { return vec![]; } if let Some(parent) = Path::new(self.path.as_ref()).parent() { vec![ Resource::new("dir", parent.to_string_lossy()) ] } else { @@ -81,7 +77,7 @@ impl<'a, S> Symbol for StoredDirectory<'a, S> where S: Storage { } fn provides(&self) -> Option> { - if self.dir == StorageDirection::load { + if self.dir == StorageDirection::Load { Some(vec![ Resource::new("dir", self.path.to_string()) ]) } else { None diff --git a/src/symbols/systemd/node_js_user_service.rs b/src/symbols/systemd/node_js_user_service.rs index a0b7526..c1a94d4 100644 --- a/src/symbols/systemd/node_js_user_service.rs +++ b/src/symbols/systemd/node_js_user_service.rs @@ -159,7 +159,7 @@ impl<'a, C, R> Symbol for NodeJsSystemdUserService<'a, C, R> where C: Deref Result<(), Box> { try!(self.file.execute()); try!(self.systemctl_wait_for_dbus(&["--user", "enable", self.service_name])); - try!(self.systemctl_wait_for_dbus(&["--user", "start", self.service_name])); + try!(self.systemctl_wait_for_dbus(&["--user", "restart", self.service_name])); if !(try!(self.check_if_service())) { return Err(Box::new(NodeJsSystemdUserServiceError::GenericError as NodeJsSystemdUserServiceError)); diff --git a/src/symbols/systemd/reload.rs b/src/symbols/systemd/reload.rs index 30a18e9..71abe4d 100644 --- a/src/symbols/systemd/reload.rs +++ b/src/symbols/systemd/reload.rs @@ -24,8 +24,7 @@ impl<'a> Symbol for ReloadService<'a> { } fn execute(&self) -> Result<(), Box> { - try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", self.service])); - Ok(()) + self.command_runner.run_successfully("systemctl", &["reload-or-restart", self.service]) } } diff --git a/src/symbols/systemd/user_session.rs b/src/symbols/systemd/user_session.rs index 1c6966b..a56232b 100644 --- a/src/symbols/systemd/user_session.rs +++ b/src/symbols/systemd/user_session.rs @@ -1,9 +1,7 @@ use std::borrow::{ Borrow, Cow }; use std::error::Error; use std::fmt; -use std::io::Error as IoError; use std::path::PathBuf; -use std::str::from_utf8; use command_runner::CommandRunner; use symbols::Symbol; @@ -58,13 +56,7 @@ impl<'a> Symbol for SystemdUserSession<'a> { } fn execute(&self) -> Result<(), Box> { - match self.command_runner.run_with_args("loginctl", &["enable-linger", self.user_name.borrow()]) { - Ok(output) => { println!("{:?} {:?}", from_utf8(&output.stdout).unwrap(), from_utf8(&output.stderr).unwrap() ); match output.status.code() { - Some(0) => Ok(()), - _ => Err(Box::new(SystemdUserSessionError::GenericError as SystemdUserSessionError)) - } }, - Err(e) => Err(Box::new(SystemdUserSessionError::ExecError(e))) - } + self.command_runner.run_successfully("loginctl", &["enable-linger", self.user_name.borrow()]) } } diff --git a/src/symbols/tls/csr.rs b/src/symbols/tls/csr.rs index a9a692b..b4f7dc7 100644 --- a/src/symbols/tls/csr.rs +++ b/src/symbols/tls/csr.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use std::error::Error; use std::fmt; +use std::path::Path; use command_runner::CommandRunner; use resources::Resource; @@ -36,19 +37,16 @@ impl<'a> fmt::Display for TlsCsr<'a> { impl<'a> Symbol for TlsCsr<'a> { fn target_reached(&self) -> Result> { - let result = self.command_runner.run_with_args("openssl", &["req", "-in", &self.get_csr_path(), "-noout", "-verify"]); - match result { - Err(e) => Err(Box::new(e)), - Ok(output) => match output.status.code() { - Some(0) => Ok(output.stderr == "verify OK\n".as_bytes()), - Some(_) => Ok(false), - _ => Err("Didn't work".to_string().into()) - } + if !Path::new(&self.get_csr_path()).exists() { + return Ok(false); } + + let output = try!(self.command_runner.get_stderr("openssl", &["req", "-in", &self.get_csr_path(), "-noout", "-verify"])); + Ok(output == b"verify OK\n") } fn execute(&self) -> Result<(), Box> { - let output = try!(self.command_runner.run_with_args("openssl", &["req", "-new", "-sha256", "-key", &self.get_key_path(), "-out", &self.get_csr_path(), "-subj", &format!("/CN={}", self.domain)]).map_err(|e| Box::new(e))); + try!(self.command_runner.run_successfully("openssl", &["req", "-new", "-sha256", "-key", &self.get_key_path(), "-out", &self.get_csr_path(), "-subj", &format!("/CN={}", self.domain)])); Ok(()) } diff --git a/src/symbols/tls/key.rs b/src/symbols/tls/key.rs index 534c317..329d72a 100644 --- a/src/symbols/tls/key.rs +++ b/src/symbols/tls/key.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use std::error::Error; use std::fmt; +use std::path::Path; use command_runner::CommandRunner; use symbols::Symbol; @@ -35,23 +36,16 @@ impl<'a> fmt::Display for TlsKey<'a> { impl<'a> Symbol for TlsKey<'a> { fn target_reached(&self) -> Result> { - let result = self.command_runner.run_with_args("openssl", &["rsa", "-in", &self.get_path(), "-noout", "-check", "-text"]); - match result { - Err(e) => Err(Box::new(e)), - Ok(output) => match output.status.code() { - Some(0) => Ok(output.stdout.starts_with(format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes())), - Some(_) => Ok(false), - _ => Err("Didn't work".to_string().into()) - } + if !Path::new(&self.get_path()).exists() { + return Ok(false); } + + let output = try!(self.command_runner.get_output("openssl", &["rsa", "-in", &self.get_path(), "-noout", "-check", "-text"])); + Ok(output.starts_with(&format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes())) } fn execute(&self) -> Result<(), Box> { - let output = self.command_runner.run_with_args("openssl", &["genrsa", "-out", &self.get_path(), &self.get_bytes().to_string()]); - match output { - Err(e) => Err(Box::new(e)), - Ok(_) => Ok(()) - } + self.command_runner.run_successfully("openssl", &["genrsa", "-out", &self.get_path(), &self.get_bytes().to_string()]) } } diff --git a/src/symbols/tls/self_signed_cert.rs b/src/symbols/tls/self_signed_cert.rs index a385335..21d3ca4 100644 --- a/src/symbols/tls/self_signed_cert.rs +++ b/src/symbols/tls/self_signed_cert.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use std::error::Error; use std::fmt; +use std::path::Path; use command_runner::CommandRunner; use resources::Resource; @@ -38,23 +39,15 @@ const DAYS_IN_SECONDS: u32 = 24*60*60; impl<'a> Symbol for SelfSignedTlsCert<'a> { fn target_reached(&self) -> Result> { - let result = self.command_runner.run_with_args("openssl", &["x509", "-in", &self.get_cert_path(), "-noout", "-subject", "-checkend", &(30*DAYS_IN_SECONDS).to_string()]); - match result { - Err(e) => Err(Box::new(e)), - Ok(output) => match output.status.code() { - Some(0) => Ok(output.stdout == format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes()), - Some(_) => Ok(false), - _ => Err("Didn't work".to_string().into()) - } + if !Path::new(&self.get_cert_path()).exists() { + return Ok(false); } + let output = try!(self.command_runner.get_output("openssl", &["x509", "-in", &self.get_cert_path(), "-noout", "-subject", "-checkend", &(30*DAYS_IN_SECONDS).to_string()])); + Ok(output == format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes()) } fn execute(&self) -> Result<(), Box> { - let output = self.command_runner.run_with_args("openssl", &["req", "-x509", "-sha256", "-days", "90", "-key", &self.get_key_path(), "-out", &self.get_cert_path(), "-subj", &format!("/CN={}", self.domain)]); - match output { - Err(e) => Err(Box::new(e)), - Ok(_) => Ok(()) - } + self.command_runner.run_successfully("openssl", &["req", "-x509", "-sha256", "-days", "90", "-key", &self.get_key_path(), "-out", &self.get_cert_path(), "-subj", &format!("/CN={}", self.domain)]) } fn get_prerequisites(&self) -> Vec { diff --git a/src/symbols/user.rs b/src/symbols/user.rs index 7342d77..33425eb 100644 --- a/src/symbols/user.rs +++ b/src/symbols/user.rs @@ -95,14 +95,11 @@ impl<'a, E: Error, A: UserAdder> fmt::Display for User<'a, E, A> { impl<'a, E: 'static + Error, A: UserAdder> Symbol for User<'a, E, A> { fn target_reached(&self) -> Result> { - let output = self.command_runner.run_with_args("getent", &["passwd", &*self.user_name]); - match output { - Ok(output) => match output.status.code() { - Some(2) => Ok(false), - Some(0) => Ok(true), - _ => Err(Box::new(UserError::GenericError)) - }, - Err(e) => Err(Box::new(e)) + let output = try!(self.command_runner.run_with_args("getent", &["passwd", &*self.user_name])); + match output.status.code() { + Some(2) => Ok(false), + Some(0) => Ok(true), + _ => Err(Box::new(UserError::GenericError)) } } diff --git a/src/symbols/wordpress/translation.rs b/src/symbols/wordpress/translation.rs index 4ddb244..baf63c8 100644 --- a/src/symbols/wordpress/translation.rs +++ b/src/symbols/wordpress/translation.rs @@ -75,7 +75,7 @@ impl<'a, C, D, R> Symbol for WordpressTranslation<'a, C, D, R> where C: Deref Result<(), Box> {