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.

49 lines
1.3 KiB

7 years ago
  1. use std::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use command_runner::CommandRunner;
  5. use symbols::Symbol;
  6. pub struct MariaDBUser<'a> {
  7. user_name: Cow<'a, str>,
  8. command_runner: &'a CommandRunner
  9. }
  10. impl<'a> MariaDBUser<'a> {
  11. pub fn new(user_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> MariaDBUser<'a> {
  12. MariaDBUser {
  13. user_name: user_name,
  14. command_runner: command_runner
  15. }
  16. }
  17. fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
  18. let output = try!(self.command_runner.run_with_args("mariadb", &["--skip-column-names", "-e", sql]));
  19. if output.status.code() != Some(0) {
  20. return Err(try!(String::from_utf8(output.stderr)).into());
  21. }
  22. Ok(try!(String::from_utf8(output.stdout)))
  23. }
  24. }
  25. impl<'a> fmt::Display for MariaDBUser<'a> {
  26. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  27. write!(f, "MariaDB User {}", self.user_name)
  28. }
  29. }
  30. impl<'a> Symbol for MariaDBUser<'a> {
  31. fn target_reached(&self) -> Result<bool, Box<Error>> {
  32. 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)
  33. }
  34. fn execute(&self) -> Result<(), Box<Error>> {
  35. try!(self.run_sql(&format!("GRANT ALL ON {0}.* TO {0} IDENTIFIED VIA unix_socket", self.user_name)));
  36. Ok(())
  37. }
  38. }
  39. #[cfg(test)]
  40. mod test {
  41. }