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.

73 lines
1.8 KiB

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