Browse Source

Update

master
Adrian Heine 8 years ago
parent
commit
8e848fc104
  1. 2
      src/symbols/git/checkout.rs
  2. 52
      src/symbols/mariadb/database.rs
  3. 5
      src/symbols/mariadb/mod.rs
  4. 49
      src/symbols/mariadb/user.rs
  5. 1
      src/symbols/mod.rs
  6. 1
      src/symbols/nginx/mod.rs
  7. 12
      src/symbols/nginx/server.rs
  8. 3
      src/symbols/systemd/mod.rs
  9. 20
      src/symbols/systemd/reload.rs

2
src/symbols/git/checkout.rs

@ -57,7 +57,7 @@ impl<'a> Symbol for GitCheckout<'a> {
fn execute(&self) -> Result<(), Box<Error>> { fn execute(&self) -> Result<(), Box<Error>> {
if let Err(e) = metadata(self.target) { if let Err(e) = metadata(self.target) {
return if e.kind() == io::ErrorKind::NotFound { return if e.kind() == io::ErrorKind::NotFound {
try!(self.command_runner.run_with_args("git", &["clone", "-b", self.branch, self.source, self.target]));
try!(self.command_runner.run_with_args("git", &["clone", "--depth", "1", "-b", self.branch, self.source, self.target]));
Ok(()) Ok(())
} else { } else {
Err(Box::new(e)) Err(Box::new(e))

52
src/symbols/mariadb/database.rs

@ -0,0 +1,52 @@
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use command_runner::CommandRunner;
use symbols::Symbol;
pub struct MariaDBDatabase<'a> {
db_name: Cow<'a, str>,
seed_file: &'a str,
command_runner: &'a CommandRunner
}
impl<'a> MariaDBDatabase<'a> {
pub fn new(db_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> MariaDBDatabase<'a> {
MariaDBDatabase {
db_name: db_name,
seed_file: "/root/seedfile.sql",
command_runner: command_runner
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
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)))
}
}
impl<'a> fmt::Display for MariaDBDatabase<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MariaDB Database {}", self.db_name)
}
}
impl<'a> Symbol for MariaDBDatabase<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
Ok(try!(self.run_sql(&format!("SHOW DATABASES LIKE '{}'", self.db_name))).trim_right() == self.db_name)
}
fn execute(&self) -> Result<(), Box<Error>> {
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(())
}
}
#[cfg(test)]
mod test {
}

5
src/symbols/mariadb/mod.rs

@ -0,0 +1,5 @@
mod database;
mod user;
pub use self::database::MariaDBDatabase;
pub use self::user::MariaDBUser;

49
src/symbols/mariadb/user.rs

@ -0,0 +1,49 @@
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use command_runner::CommandRunner;
use symbols::Symbol;
pub struct MariaDBUser<'a> {
user_name: Cow<'a, str>,
command_runner: &'a CommandRunner
}
impl<'a> MariaDBUser<'a> {
pub fn new(user_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> MariaDBUser<'a> {
MariaDBUser {
user_name: user_name,
command_runner: command_runner
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
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)))
}
}
impl<'a> fmt::Display for MariaDBUser<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MariaDB User {}", self.user_name)
}
}
impl<'a> Symbol for MariaDBUser<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
Ok(try!(self.run_sql(&format!("SELECT User FROM mysql.user WHERE User = '{}' AND plugin = 'unix_socket'", self.user_name))).trim_right() == self.user_name)
}
fn execute(&self) -> Result<(), Box<Error>> {
try!(self.run_sql(&format!("GRANT ALL ON {0}.* TO {0} IDENTIFIED VIA unix_socket", self.user_name)));
Ok(())
}
}
#[cfg(test)]
mod test {
}

1
src/symbols/mod.rs

@ -16,6 +16,7 @@ pub mod file;
pub mod git; pub mod git;
pub mod hook; pub mod hook;
pub mod list; pub mod list;
pub mod mariadb;
pub mod nginx; pub mod nginx;
pub mod not_a_symlink; pub mod not_a_symlink;
pub mod npm; pub mod npm;

1
src/symbols/nginx/mod.rs

@ -1,2 +1 @@
pub mod reload;
pub mod server; pub mod server;

12
src/symbols/nginx/server.rs

@ -100,6 +100,18 @@ location @proxy {{
NginxServer::new(domain, content, command_runner) NginxServer::new(domain, content, command_runner)
} }
pub fn new_php(domain: &'a str, socket_path: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
let content = NginxServer::server_config(domain, &format!("
root {};
index index.html index.php;
location ~ [^/]\\.php(/|$) {{
fastcgi_pass unix:{};
include \"snippets/fastcgi-php.conf\";
}}
", static_path, socket_path));
NginxServer::new(domain, content, command_runner)
}
pub fn new_static(domain: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self { pub fn new_static(domain: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
let content = NginxServer::server_config(domain, &format!(" let content = NginxServer::server_config(domain, &format!("
root {}; root {};

3
src/symbols/systemd/mod.rs

@ -1,2 +1,3 @@
pub mod user_session;
pub mod node_js_user_service; pub mod node_js_user_service;
pub mod reload;
pub mod user_session;

20
src/symbols/nginx/reload.rs → src/symbols/systemd/reload.rs

@ -4,31 +4,33 @@ use std::fmt;
use command_runner::CommandRunner; use command_runner::CommandRunner;
use symbols::Symbol; use symbols::Symbol;
pub struct NginxReload<'a> {
command_runner: &'a CommandRunner,
pub struct ReloadService<'a> {
service: &'a str,
command_runner: &'a CommandRunner
} }
impl<'a> NginxReload<'a> {
pub fn new(command_runner: &'a CommandRunner) -> Self {
NginxReload {
impl<'a> ReloadService<'a> {
pub fn new(service: &'a str, command_runner: &'a CommandRunner) -> Self {
ReloadService {
service: service,
command_runner: command_runner command_runner: command_runner
} }
} }
} }
impl<'a> Symbol for NginxReload<'a> {
impl<'a> Symbol for ReloadService<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> { fn target_reached(&self) -> Result<bool, Box<Error>> {
Ok(true) Ok(true)
} }
fn execute(&self) -> Result<(), Box<Error>> { fn execute(&self) -> Result<(), Box<Error>> {
try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"]));
try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", self.service]));
Ok(()) Ok(())
} }
} }
impl<'a> fmt::Display for NginxReload<'a> {
impl<'a> fmt::Display for ReloadService<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
write!(f, "Reload nginx server")
write!(f, "Reload service {}", self.service)
} }
} }
Loading…
Cancel
Save