A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. use std::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use command_runner::CommandRunner;
  5. use resources::Resource;
  6. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  7. pub struct MariaDBUser<'a> {
  8. user_name: Cow<'a, str>,
  9. command_runner: &'a CommandRunner
  10. }
  11. impl<'a> MariaDBUser<'a> {
  12. pub fn new(user_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> MariaDBUser<'a> {
  13. MariaDBUser {
  14. user_name: user_name,
  15. command_runner: command_runner
  16. }
  17. }
  18. fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
  19. let b = try!(self.command_runner.get_output("mariadb", &["--skip-column-names", "-B", "-e", sql]));
  20. Ok(try!(String::from_utf8(b)))
  21. }
  22. }
  23. impl<'a> fmt::Display for MariaDBUser<'a> {
  24. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  25. write!(f, "MariaDB User {}", self.user_name)
  26. }
  27. }
  28. impl<'a> Symbol for MariaDBUser<'a> {
  29. fn target_reached(&self) -> Result<bool, Box<Error>> {
  30. 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)
  31. }
  32. fn execute(&self) -> Result<(), Box<Error>> {
  33. try!(self.run_sql(&format!("GRANT ALL ON {0}.* TO {0} IDENTIFIED VIA unix_socket", self.user_name)));
  34. Ok(())
  35. }
  36. fn get_prerequisites(&self) -> Vec<Resource> {
  37. vec![ Resource::new("user", self.user_name.to_string()) ]
  38. }
  39. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  40. Box::new(SymbolAction::new(runner, self))
  41. }
  42. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  43. Box::new(OwnedSymbolAction::new(runner, *self))
  44. }
  45. }
  46. #[cfg(test)]
  47. mod test {
  48. }