Adrian Heine
8 years ago
9 changed files with 133 additions and 12 deletions
-
2src/symbols/git/checkout.rs
-
52src/symbols/mariadb/database.rs
-
5src/symbols/mariadb/mod.rs
-
49src/symbols/mariadb/user.rs
-
1src/symbols/mod.rs
-
1src/symbols/nginx/mod.rs
-
12src/symbols/nginx/server.rs
-
3src/symbols/systemd/mod.rs
-
20src/symbols/systemd/reload.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 {
|
|||
}
|
@ -0,0 +1,5 @@ |
|||
mod database;
|
|||
mod user;
|
|||
|
|||
pub use self::database::MariaDBDatabase;
|
|||
pub use self::user::MariaDBUser;
|
@ -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,2 +1 @@ |
|||
pub mod reload;
|
|||
pub mod server;
|
@ -1,2 +1,3 @@ |
|||
pub mod user_session;
|
|||
pub mod node_js_user_service;
|
|||
pub mod reload;
|
|||
pub mod user_session;
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue