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.

193 lines
7.3 KiB

6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
5 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
5 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
  1. use std::borrow::Cow;
  2. use std::ops::Deref;
  3. use std::path::Path;
  4. use command_runner::{CommandRunner, SetuidCommandRunner};
  5. use storage::{SimpleStorage, Storage};
  6. use symbols::{Action, Symbol, SymbolRunner};
  7. use symbols::acme::{AcmeCert, AcmeCertChain};
  8. use symbols::file::File;
  9. use symbols::git::checkout::GitCheckout;
  10. use symbols::hook::Hook;
  11. use symbols::list::ListAction;
  12. use symbols::mariadb::{DatabaseDump, MariaDBDatabase, MariaDBUser};
  13. use symbols::nginx::server::{NginxServer, server_config};
  14. use symbols::owner::Owner;
  15. use symbols::stored_directory::{StoredDirectory, StorageDirection};
  16. use symbols::systemd::reload::ReloadService;
  17. use symbols::tls::SelfSignedTlsCert;
  18. pub trait Policy {
  19. fn user_name_for_host(&self, host_name: &'static str) -> String;
  20. }
  21. pub struct DefaultPolicy;
  22. impl Policy for DefaultPolicy {
  23. fn user_name_for_host(&self, host_name: &'static str) -> String {
  24. host_name.split('.').rev().fold(String::new(), |result, part| if result.is_empty() { result } else { result + "_" } + part)
  25. }
  26. }
  27. pub struct SymbolFactory<'a, C: 'a + CommandRunner, R: 'a + SymbolRunner, P: 'a + Policy>{
  28. command_runner: &'a C,
  29. acme_command_runner: SetuidCommandRunner<'a, C>,
  30. symbol_runner: &'a R,
  31. policy: &'a P
  32. }
  33. impl<'b, C: 'b + CommandRunner, R: 'b + SymbolRunner, P: 'b + Policy> SymbolFactory<'b, C, R, P> {
  34. pub fn new(command_runner: &'b C, symbol_runner: &'b R, policy: &'b P) -> Self {
  35. let acme_user = "acme"; // FIXME: CONFIG
  36. let acme_command_runner = SetuidCommandRunner::new(acme_user, command_runner);
  37. SymbolFactory { command_runner, acme_command_runner, symbol_runner, policy }
  38. }
  39. pub fn get_nginx_acme_server<'a, 'c: 'a, S: 'a + Symbol>(&'c self, host: &'static str, nginx_server_symbol: S) -> Box<Action + 'a> {
  40. Box::new(ListAction::new(vec![
  41. Box::new(SelfSignedTlsCert::new(
  42. host.into(),
  43. self.command_runner
  44. )).into_action(self.symbol_runner),
  45. Box::new(Hook::new(
  46. nginx_server_symbol,
  47. ReloadService::new("nginx", self.command_runner)
  48. )).into_action(self.symbol_runner),
  49. Box::new(AcmeCert::new(
  50. host.into(),
  51. &self.acme_command_runner
  52. )).into_action(self.symbol_runner),
  53. Box::new(Hook::new(
  54. AcmeCertChain::new(
  55. host.into(),
  56. &self.acme_command_runner
  57. ),
  58. ReloadService::new("nginx", self.command_runner)
  59. )).into_action(self.symbol_runner)
  60. ]))
  61. }
  62. pub fn get_nginx_acme_challenge_config<'a>(&'a self) -> Box<Action + 'a> {
  63. Box::new(File::new(
  64. "/etc/nginx/snippets/acme-challenge.conf", "location ^~ /.well-known/acme-challenge/ {
  65. alias /home/acme/challenges/;
  66. try_files $uri =404;
  67. }"
  68. )).into_action(self.symbol_runner)
  69. }
  70. fn get_php_fpm_pool_socket_path<'a>(&'a self, user_name: &str) -> String {
  71. format!("/run/php/{}.sock", user_name)
  72. }
  73. fn get_php_fpm_pool<'a>(&'a self, user_name: &str) -> Box<Action + 'a> {
  74. let socket = self.get_php_fpm_pool_socket_path(user_name);
  75. Box::new(Hook::new(
  76. File::new(
  77. format!("/etc/php/7.0/fpm/pool.d/{}.conf", user_name),
  78. format!(
  79. "[{0}]
  80. user = {0}
  81. group = www-data
  82. listen = {1}
  83. listen.owner = www-data
  84. pm = ondemand
  85. pm.max_children = 10"
  86. , user_name, socket)),
  87. ReloadService::new("php7.0-fpm", self.command_runner)
  88. )).into_action(self.symbol_runner)
  89. }
  90. pub fn serve_php<'a>(&'a self, host_name: &'static str, root_dir: Cow<'a, str>) -> Box<Action + 'a> {
  91. let user_name = self.policy.user_name_for_host(host_name);
  92. let socket = self.get_php_fpm_pool_socket_path(&user_name);
  93. Box::new(ListAction::new(vec![
  94. self.get_php_fpm_pool(&user_name),
  95. self.get_nginx_acme_server(host_name,
  96. NginxServer::new_php(
  97. host_name,
  98. socket.into(),
  99. root_dir,
  100. self.command_runner
  101. )
  102. )
  103. ]))
  104. }
  105. pub fn serve_dokuwiki<'a>(&'a self, host_name: &'static str, root_dir: &'static str) -> Box<Action + 'a> {
  106. let user_name = self.policy.user_name_for_host(host_name);
  107. let socket = self.get_php_fpm_pool_socket_path(&user_name);
  108. Box::new(ListAction::new(vec![
  109. self.get_php_fpm_pool(&user_name),
  110. self.get_nginx_acme_server(host_name,
  111. NginxServer::new(
  112. host_name,
  113. server_config("hostname", &format!("
  114. root {};
  115. index doku.php;
  116. location ~ [^/]\\.php(/|$) {{
  117. fastcgi_pass unix:{};
  118. include \"snippets/fastcgi-php.conf\";
  119. }}
  120. location ~ /(data/|conf/|bin/|inc/|install.php) {{ deny all; }}
  121. location / {{ try_files $uri $uri/ @dokuwiki; }}
  122. location @dokuwiki {{
  123. # rewrites \"doku.php/\" out of the URLs if you set the userewrite setting to .htaccess in dokuwiki config page
  124. rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
  125. rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
  126. rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
  127. rewrite ^/(.*) /doku.php?id=$1&$args last;
  128. }}
  129. ",
  130. root_dir,
  131. socket)),
  132. self.command_runner
  133. ))
  134. ]))
  135. }
  136. pub fn serve_redir<'a>(&'a self, host_name: &'static str, target: &'static str) -> Box<Action + 'a> {
  137. self.get_nginx_acme_server(host_name, NginxServer::new_redir(host_name, target, self.command_runner))
  138. }
  139. pub fn serve_static<'a>(&'a self, host_name: &'static str, dir: &'a str) -> Box<Action + 'a> {
  140. self.get_nginx_acme_server(host_name, NginxServer::new_static(host_name, dir, self.command_runner))
  141. }
  142. pub fn get_stored_directory<'a, T: Into<String>>(&'a self, storage_name: &'static str, target: T) -> (Box<Action + 'a>, Box<Action + 'a>) {
  143. let data = SimpleStorage::new("/root/data".to_string(), storage_name.to_string());
  144. let string_target = target.into();
  145. (
  146. Box::new(StoredDirectory::new(string_target.clone().into(), data.clone(), StorageDirection::Save, self.command_runner)).into_action(self.symbol_runner),
  147. Box::new(StoredDirectory::new(string_target.into(), data.clone(), StorageDirection::Load, self.command_runner)).into_action(self.symbol_runner)
  148. )
  149. }
  150. pub fn get_mariadb_database<'a>(&'a self, name: &'static str) -> Box<Action + 'a> {
  151. let db_dump = SimpleStorage::new("/root/data".to_string(), format!("{}.sql", name));
  152. Box::new(ListAction::new(vec![
  153. Box::new(MariaDBDatabase::new(name.into(), db_dump.read_filename().unwrap().into(), self.command_runner)).into_action(self.symbol_runner),
  154. Box::new(DatabaseDump::new(name, db_dump, self.command_runner)).into_action(self.symbol_runner)
  155. ]))
  156. }
  157. pub fn get_mariadb_user<'a>(&'a self, user_name: &'static str) -> Box<Action + 'a> {
  158. Box::new(MariaDBUser::new(user_name.into(), self.command_runner)).into_action(self.symbol_runner)
  159. }
  160. pub fn get_git_checkout<'a, T: 'a + AsRef<str>>(&'a self, target: T, source: &'a str, branch: &'a str) -> Box<Action + 'a> {
  161. Box::new(GitCheckout::new(target, source, branch, self.command_runner)).into_action(self.symbol_runner)
  162. }
  163. pub fn get_owner<'a, F: 'a + AsRef<str>>(&'a self, file: F, user: &'a str) -> Box<Action + 'a> {
  164. Box::new(Owner::new(file, user.into(), self.command_runner)).into_action(self.symbol_runner)
  165. }
  166. pub fn get_file<'a, F: 'a + Deref<Target=str>, Q: 'a + AsRef<Path>>(&'a self, path: Q, content: F) -> Box<Action + 'a> {
  167. Box::new(File::new(path, content)).into_action(self.symbol_runner)
  168. }
  169. }