From e8b8122973c5dced6226806b76e4c5ac76486486 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Sat, 24 Jun 2017 11:04:59 +0200 Subject: [PATCH] Update --- src/symbols/acme/cert.rs | 3 +- src/symbols/git/submodules.rs | 4 ++ src/symbols/stored_directory.rs | 10 ++- src/symbols/systemd/node_js_user_service.rs | 2 +- src/symbols/tls/self_signed_cert.rs | 14 +++- src/symbols/wordpress/mod.rs | 1 + src/symbols/wordpress/plugin.rs | 76 +++++++++++++++++++++ 7 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 src/symbols/wordpress/plugin.rs diff --git a/src/symbols/acme/cert.rs b/src/symbols/acme/cert.rs index 6e1e257..6ccb44f 100644 --- a/src/symbols/acme/cert.rs +++ b/src/symbols/acme/cert.rs @@ -49,8 +49,7 @@ impl<'a> Symbol for AcmeCert<'a> { 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) + Ok(self.command_runner.run_successfully("openssl", &["verify", "--untrusted", "/home/acme/lets_encrypt_x3_cross_signed.pem", &self.get_cert_path()]).is_ok()) } fn execute(&self) -> Result<(), Box> { diff --git a/src/symbols/git/submodules.rs b/src/symbols/git/submodules.rs index 9a4cc37..d929833 100644 --- a/src/symbols/git/submodules.rs +++ b/src/symbols/git/submodules.rs @@ -1,5 +1,6 @@ use std::error::Error; use std::fmt; +use std::path::Path; use command_runner::CommandRunner; use symbols::Symbol; @@ -34,6 +35,9 @@ impl<'a> GitSubmodules<'a> { impl<'a> Symbol for GitSubmodules<'a> { fn target_reached(&self) -> Result> { + if !Path::new(self.target).exists() { + return Ok(false); + } 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(' '))) } diff --git a/src/symbols/stored_directory.rs b/src/symbols/stored_directory.rs index fb6575e..9ce4d14 100644 --- a/src/symbols/stored_directory.rs +++ b/src/symbols/stored_directory.rs @@ -56,11 +56,19 @@ 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.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 }) + if if self.dir == StorageDirection::Save { modified_date > dump_date } else { dump_date > modified_date } { + let output = try!(self.command_runner.run_with_args("diff", &["-rq", &try!(self.storage.read_filename()), self.path.borrow()])); + match output.status.code() { + Some(0) => Ok(true), + Some(1) => Ok(false), + _ => Err(try!(String::from_utf8(output.stderr)).into()) + } + } else { Ok(true) } } fn execute(&self) -> Result<(), Box> { if self.dir == StorageDirection::Load { + try!(self.command_runner.run_successfully("rm", &["-rf", self.path.borrow()])); self.command_runner.run_successfully("cp", &["-a", &try!(self.storage.read_filename()), self.path.borrow()]) } else { self.command_runner.run_successfully("cp", &["-a", self.path.borrow(), &self.storage.write_filename()]) diff --git a/src/symbols/systemd/node_js_user_service.rs b/src/symbols/systemd/node_js_user_service.rs index c1a94d4..ff8a176 100644 --- a/src/symbols/systemd/node_js_user_service.rs +++ b/src/symbols/systemd/node_js_user_service.rs @@ -64,7 +64,7 @@ impl<'a, R> NodeJsSystemdUserService<'a, String, SetuidCommandRunner<'a, R>> whe let file_path = format!("{}/.config/systemd/user/{}.service", home.trim_right(), name); let content = format!("[Service] -ExecStartPre=rm /var/tmp/{1}-{2}.socket +ExecStartPre=/bin/rm -f /var/tmp/{1}-{2}.socket # This only works if the path is a directory WorkingDirectory={0} ExecStart=/usr/bin/nodejs {0} diff --git a/src/symbols/tls/self_signed_cert.rs b/src/symbols/tls/self_signed_cert.rs index 21d3ca4..e91797a 100644 --- a/src/symbols/tls/self_signed_cert.rs +++ b/src/symbols/tls/self_signed_cert.rs @@ -42,8 +42,18 @@ impl<'a> Symbol for SelfSignedTlsCert<'a> { 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()) + let output = try!(self.command_runner.run_with_args("openssl", &["x509", "-in", &self.get_cert_path(), "-noout", "-subject", "-checkend", &(30*DAYS_IN_SECONDS).to_string()])); +println!("{}", output.status.code().unwrap()); + match output.status.code() { + Some(0) => Ok(output.stdout == format!("subject=CN = {}\nCertificate will not expire\n", self.domain).as_bytes()), + Some(_) => if output.stdout == format!("subject=CN = {}\nCertificate will expire\n", self.domain).as_bytes() { + Ok(false) + } else { + Err("Exit code non-zero, but wrong stdout".to_string().into()) + }, + _ => Err("Apparently killed by signal".to_string().into()) + } + } fn execute(&self) -> Result<(), Box> { diff --git a/src/symbols/wordpress/mod.rs b/src/symbols/wordpress/mod.rs index f5648e8..e489e42 100644 --- a/src/symbols/wordpress/mod.rs +++ b/src/symbols/wordpress/mod.rs @@ -1 +1,2 @@ +pub mod plugin; pub mod translation; diff --git a/src/symbols/wordpress/plugin.rs b/src/symbols/wordpress/plugin.rs new file mode 100644 index 0000000..a406e3e --- /dev/null +++ b/src/symbols/wordpress/plugin.rs @@ -0,0 +1,76 @@ +use regex::Regex; +use std::error::Error; +use std::fmt; +use std::fs::File as FsFile; +use std::io::Read; +use std::ops::Deref; +use std::path::{Path, PathBuf}; + +use command_runner::CommandRunner; +use symbols::Symbol; +use resources::Resource; + +pub struct WordpressPlugin<'a, C, R> where C: Deref + fmt::Display, R: 'a + CommandRunner { + base: C, + name: C, + command_runner: &'a R +} + +impl<'a, C, R> WordpressPlugin<'a, C, R> where C: Deref + fmt::Display, R: CommandRunner { + pub fn new(base: C, name: C, command_runner: &'a R) -> Self { + WordpressPlugin { + base: base, + name: name, + command_runner: command_runner + } + } + + fn get_path(&self) -> PathBuf { + Path::new(&*self.base).join("wp-content/plugins").join(&*self.name) + } +} + +impl<'a, C, R> Symbol for WordpressPlugin<'a, C, R> where C: Deref + fmt::Display, R: CommandRunner { + fn target_reached(&self) -> Result> { + if !self.get_path().exists() { + return Ok(false); + } + let path = self.get_path().join(self.name.to_string() + ".php"); + let mut file = try!(FsFile::open(path)); + let mut file_content = String::new(); + try!(file.read_to_string(&mut file_content)); + let mut version = String::new(); + let mut plugin_uri = String::new(); + let regex = try!(Regex::new("(?m)^(Plugin URI|Version): (.+)$")); + for matches in regex.captures_iter(&file_content) { + if &matches[1] == "Plugin URI" { + plugin_uri = matches[2].to_string(); + } else { + version = matches[2].to_string(); + } + } + let upstream = try!(self.command_runner.get_output("curl", &["--form", &format!(r###"plugins={{"plugins":{{"{0}/{0}.php":{{"Version":"{1}", "PluginURI":"{2}"}}}}}}"###, self.name, version, plugin_uri), "https://api.wordpress.org/plugins/update-check/1.1/"])); + Ok(try!(String::from_utf8(upstream)).contains(r###""plugins":[]"###)) + } + + fn execute(&self) -> Result<(), Box> { + let zip = format!("/tmp/{}.zip", self.name); + try!(self.command_runner.run_successfully("curl", &[&format!("https://downloads.wordpress.org/plugin/{}.zip", self.name), "-o", &zip])); + try!(self.command_runner.run_successfully("rm", &["-rf", &self.get_path().to_string_lossy()])); + self.command_runner.run_successfully("unzip", &[&zip, "-d", &Path::new(&*self.base).join("wp-content/plugins").to_string_lossy()]) + } + + fn get_prerequisites(&self) -> Vec { + match self.get_path().parent() { + Some(p) => vec![ Resource::new("dir", p.to_string_lossy()) ], + None => vec![] + } + } +} + +impl<'a, C, R> fmt::Display for WordpressPlugin<'a, C, R> where C: Deref + fmt::Display, R: CommandRunner { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ + write!(f, "WordpressPlugin {}", self.name) + } +} +