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.

89 lines
3.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. use command_runner::{CommandRunner, SetuidCommandRunner};
  2. use symbols::{Action, Symbol, SymbolRunner};
  3. use symbols::acme::{AcmeCert, AcmeCertChain};
  4. use symbols::file::File;
  5. use symbols::hook::Hook;
  6. use symbols::list::ListAction;
  7. use symbols::nginx::server::NginxServer;
  8. use symbols::systemd::reload::ReloadService;
  9. use symbols::tls::SelfSignedTlsCert;
  10. pub struct SymbolFactory<'a, C: 'a + CommandRunner, R: 'a + SymbolRunner>{
  11. command_runner: &'a C,
  12. acme_command_runner: SetuidCommandRunner<'a, C>,
  13. symbol_runner: &'a R
  14. }
  15. impl<'b, C: 'b + CommandRunner, R: 'b + SymbolRunner> SymbolFactory<'b, C, R> {
  16. pub fn new(command_runner: &'b C, symbol_runner: &'b R) -> Self {
  17. let acme_user = "acme"; // FIXME: CONFIG
  18. let acme_command_runner = SetuidCommandRunner::new(acme_user, command_runner);
  19. SymbolFactory { command_runner: command_runner, acme_command_runner: acme_command_runner, symbol_runner: symbol_runner }
  20. }
  21. pub fn get_nginx_acme_server<'a, 'c: 'a, S: 'a + Symbol>(&'c self, host: &'static str, nginx_server_symbol: S) -> Box<Action + 'a> {
  22. Box::new(ListAction::new(vec![
  23. Box::new(SelfSignedTlsCert::new(
  24. host.into(),
  25. self.command_runner
  26. )).into_action(self.symbol_runner),
  27. Box::new(Hook::new(
  28. nginx_server_symbol,
  29. ReloadService::new("nginx", self.command_runner)
  30. )).into_action(self.symbol_runner),
  31. Box::new(AcmeCert::new(
  32. host.into(),
  33. &self.acme_command_runner
  34. )).into_action(self.symbol_runner),
  35. Box::new(Hook::new(
  36. AcmeCertChain::new(
  37. host.into(),
  38. &self.acme_command_runner
  39. ),
  40. ReloadService::new("nginx", self.command_runner)
  41. )).into_action(self.symbol_runner)
  42. ]))
  43. }
  44. pub fn get_nginx_acme_challenge_config<'a>(&'a self) -> Box<Action + 'a> {
  45. Box::new(File::new(
  46. "/etc/nginx/snippets/acme-challenge.conf", "location ^~ /.well-known/acme-challenge/ {
  47. alias /home/acme/challenges/;
  48. try_files $uri =404;
  49. }"
  50. )).into_action(self.symbol_runner)
  51. }
  52. fn get_php_fpm_pool_socket_path<'a>(&'a self, user_name: &'static str) -> String {
  53. format!("/run/php/{}.sock", user_name)
  54. }
  55. pub fn get_php_fpm_pool<'a>(&'a self, user_name: &'static str) -> Box<Action + 'a> {
  56. let socket = self.get_php_fpm_pool_socket_path(user_name);
  57. Box::new(Hook::new(
  58. File::new(
  59. format!("/etc/php/7.0/fpm/pool.d/{}.conf", user_name),
  60. format!(
  61. "[{0}]
  62. user = {0}
  63. group = www-data
  64. listen = {1}
  65. listen.owner = www-data
  66. pm = ondemand
  67. pm.max_children = 10"
  68. , user_name, socket)),
  69. ReloadService::new("php7.0-fpm", self.command_runner)
  70. )).into_action(self.symbol_runner)
  71. }
  72. pub fn get_nginx_php_server<'a>(&'a self, host_name: &'static str, user_name: &'static str, root_dir: &'static str) -> NginxServer<'a, C, String> {
  73. let socket = self.get_php_fpm_pool_socket_path(user_name);
  74. NginxServer::new_php(
  75. host_name,
  76. socket.into(),
  77. root_dir,
  78. self.command_runner
  79. )
  80. }
  81. }