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.

66 lines
2.1 KiB

  1. use std::borrow::{Borrow, Cow};
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::ops::Deref;
  5. use std::path::PathBuf;
  6. use resources::Resource;
  7. use command_runner::CommandRunner;
  8. use symbols::{Action, OwnedSymbolAction, SymbolAction, SymbolRunner, Symbol};
  9. use symbols::acme::AcmeAccountKey;
  10. use symbols::dir::Dir;
  11. use symbols::file::File;
  12. use symbols::hook::Hook;
  13. use symbols::list::List;
  14. use symbols::owner::Owner;
  15. pub struct AcmeUser<'a>(Cow<'a, str>);
  16. impl<'a> AcmeUser<'a> {
  17. pub fn new<S: Into<Cow<'a, str>>>(user_name: S) -> Self {
  18. AcmeUser(user_name.into())
  19. }
  20. }
  21. impl<'a> Symbol for AcmeUser<'a> {
  22. fn target_reached(&self) -> Result<bool, Box<Error>> {
  23. Ok(false)
  24. }
  25. fn execute(&self) -> Result<(), Box<Error>> {
  26. Ok(())
  27. }
  28. fn get_prerequisites(&self) -> Vec<Resource> {
  29. vec![]
  30. }
  31. fn provides(&self) -> Option<Vec<Resource>> {
  32. None
  33. }
  34. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  35. Box::new(SymbolAction::new(runner, self))
  36. }
  37. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  38. Box::new(OwnedSymbolAction::new(runner, *self))
  39. }
  40. }
  41. impl<'a> fmt::Display for AcmeUser<'a> {
  42. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  43. write!(f, "AcmeUser {}", self.0)
  44. }
  45. }
  46. pub fn new<'a, S: Into<Cow<'a, str>>, C: CommandRunner, P: 'a + Deref<Target=str>>(command_runner: &'a C, cert: P, user_name: S) -> Box<Symbol + 'a> { // impl trait
  47. let user_name_cow = user_name.into();
  48. let account_key_file: PathBuf = ["home", user_name_cow.borrow(), "account.key"].iter().collect();
  49. Box::new(List::new(vec![
  50. Box::new(AcmeAccountKey::new(account_key_file.clone().into(), command_runner)),
  51. Box::new(Owner::new(account_key_file.to_string_lossy().into_owned(), user_name_cow.clone(), command_runner)),
  52. Box::new(Dir::new("/home/acme/challenges")),
  53. Box::new(Owner::new("/home/acme/challenges", user_name_cow.clone(), command_runner)),
  54. Box::new(Dir::new("/etc/ssl/local_certs")),
  55. Box::new(Owner::new("/etc/ssl/local_certs", user_name_cow, command_runner)),
  56. Box::new(File::new("/home/acme/lets_encrypt_x3_cross_signed.pem", cert))
  57. ]))
  58. }