Adrian Heine
6 years ago
8 changed files with 279 additions and 25 deletions
-
2src/build.rs
-
87src/symbols/factory.rs
-
2src/symbols/mod.rs
-
71src/symbols/nginx/server.rs
-
29src/symbols/noop.rs
-
53src/symbols/postgresql/database.rs
-
55src/symbols/postgresql/database_dump.rs
-
5src/symbols/postgresql/mod.rs
@ -0,0 +1,29 @@ |
|||||
|
use std::error::Error;
|
||||
|
use std::fmt::{self, Display};
|
||||
|
|
||||
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
||||
|
|
||||
|
pub struct NoopSymbol;
|
||||
|
|
||||
|
impl Symbol for NoopSymbol {
|
||||
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||
|
Ok(true)
|
||||
|
}
|
||||
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||
|
Ok(())
|
||||
|
}
|
||||
|
fn as_action<'a>(&'a self, runner: &'a SymbolRunner) -> Box<Action + 'a> {
|
||||
|
Box::new(SymbolAction::new(runner, self))
|
||||
|
}
|
||||
|
|
||||
|
fn into_action<'a>(self: Box<Self>, runner: &'a SymbolRunner) -> Box<Action + 'a> where Self: 'a {
|
||||
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
impl Display for NoopSymbol {
|
||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
|
||||
|
write!(f, "Noop")
|
||||
|
}
|
||||
|
}
|
||||
|
|
@ -0,0 +1,53 @@ |
|||||
|
use std::borrow::Cow;
|
||||
|
use std::error::Error;
|
||||
|
use std::fmt;
|
||||
|
|
||||
|
use command_runner::CommandRunner;
|
||||
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
||||
|
|
||||
|
pub struct PostgreSQLDatabase<'a, C: 'a + CommandRunner> {
|
||||
|
name: Cow<'a, str>,
|
||||
|
seed_file: Cow<'a, str>,
|
||||
|
command_runner: &'a C
|
||||
|
}
|
||||
|
|
||||
|
impl<'a, C: CommandRunner> PostgreSQLDatabase<'a, C> {
|
||||
|
pub fn new(name: Cow<'a, str>, seed_file: Cow<'a, str>, command_runner: &'a C) -> Self {
|
||||
|
PostgreSQLDatabase { name, seed_file, command_runner }
|
||||
|
}
|
||||
|
|
||||
|
fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
|
||||
|
let b = try!(self.command_runner.get_output("su", &["-", "postgres", "-c", &format!("psql -t -c \"{}\"", sql)]));
|
||||
|
Ok(try!(String::from_utf8(b)))
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
impl<'a, C: CommandRunner> fmt::Display for PostgreSQLDatabase<'a, C> {
|
||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
write!(f, "PostgreSQL Database {}", self.name)
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
impl<'a, C: CommandRunner> Symbol for PostgreSQLDatabase<'a, C> {
|
||||
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||
|
Ok(try!(self.run_sql(&format!("SELECT datname FROM pg_database WHERE datname LIKE '{}'", self.name))).trim() == self.name)
|
||||
|
}
|
||||
|
|
||||
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||
|
self.command_runner.run_successfully("su", &["-", "postgres", "-c", &format!("createuser {}", self.name)])?;
|
||||
|
self.command_runner.run_successfully("su", &["-", "postgres", "-c", &format!("createdb -E UTF8 -T template0 -O {} {0}", self.name)])?;
|
||||
|
self.command_runner.run_successfully("su", &["-", "postgres", "-c", &format!("psql '{}' < {}", self.name, self.seed_file)])
|
||||
|
}
|
||||
|
|
||||
|
fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
|
||||
|
Box::new(SymbolAction::new(runner, self))
|
||||
|
}
|
||||
|
|
||||
|
fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
|
||||
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
#[cfg(test)]
|
||||
|
mod test {
|
||||
|
}
|
@ -0,0 +1,55 @@ |
|||||
|
use std::error::Error;
|
||||
|
use std::fmt;
|
||||
|
use std::str::FromStr;
|
||||
|
|
||||
|
use command_runner::CommandRunner;
|
||||
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
||||
|
use storage::Storage;
|
||||
|
|
||||
|
pub struct DatabaseDump<'a, N, C, S> where N: 'a + AsRef<str>, C: 'a + CommandRunner, S: Storage {
|
||||
|
db_name: N,
|
||||
|
storage: S,
|
||||
|
command_runner: &'a C
|
||||
|
}
|
||||
|
|
||||
|
impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> DatabaseDump<'a, N, C, S> {
|
||||
|
pub fn new(db_name: N, storage: S, command_runner: &'a C) -> Self {
|
||||
|
DatabaseDump { db_name, storage, command_runner }
|
||||
|
}
|
||||
|
|
||||
|
fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
|
||||
|
let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql]));
|
||||
|
Ok(try!(String::from_utf8(b)))
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> fmt::Display for DatabaseDump<'a, N, C, S> {
|
||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
write!(f, "Dump MariaDB Database {}", self.db_name.as_ref())
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
impl<'a, N: AsRef<str>, C: CommandRunner, S: Storage> Symbol for DatabaseDump<'a, N, C, S> {
|
||||
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||
|
let dump_date = try!(self.storage.recent_date());
|
||||
|
let modified_date = try!(self.run_sql(&format!("select UNIX_TIMESTAMP(MAX(UPDATE_TIME)) from information_schema.tables WHERE table_schema = '{}'", self.db_name.as_ref())));
|
||||
|
if modified_date.trim_right() == "NULL" { return Ok(false); }
|
||||
|
Ok(try!(u64::from_str(modified_date.trim_right())) <= dump_date)
|
||||
|
}
|
||||
|
|
||||
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||
|
self.command_runner.run_successfully("sh", &["-c", &format!("mysqldump '{}' > {}", self.db_name.as_ref(), self.storage.write_filename())])
|
||||
|
}
|
||||
|
|
||||
|
fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
|
||||
|
Box::new(SymbolAction::new(runner, self))
|
||||
|
}
|
||||
|
|
||||
|
fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
|
||||
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
#[cfg(test)]
|
||||
|
mod test {
|
||||
|
}
|
@ -0,0 +1,5 @@ |
|||||
|
mod database;
|
||||
|
mod database_dump;
|
||||
|
|
||||
|
pub use self::database::PostgreSQLDatabase;
|
||||
|
//pub use self::database_dump::PostgreSQLDump;
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue