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.

279 lines
10 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
5 years ago
6 years ago
7 years ago
6 years ago
5 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
5 years ago
6 years ago
5 years ago
6 years ago
5 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, php_server_config_snippet};
  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. fn home_for_user(&self, user_name: &str) -> String {
  21. format!("/home/{}", user_name)
  22. }
  23. }
  24. pub struct DefaultPolicy;
  25. impl Policy for DefaultPolicy {
  26. fn user_name_for_host(&self, host_name: &'static str) -> String {
  27. host_name.split('.').rev().fold(String::new(), |result, part| if result.is_empty() { result } else { result + "_" } + part)
  28. }
  29. }
  30. pub struct SymbolFactory<'a, C: 'a + CommandRunner, R: 'a + SymbolRunner, P: 'a + Policy>{
  31. command_runner: &'a C,
  32. acme_command_runner: SetuidCommandRunner<'a, C>,
  33. symbol_runner: &'a R,
  34. policy: &'a P
  35. }
  36. impl<'b, C: 'b + CommandRunner, R: 'b + SymbolRunner, P: 'b + Policy> SymbolFactory<'b, C, R, P> {
  37. pub fn new(command_runner: &'b C, symbol_runner: &'b R, policy: &'b P) -> Self {
  38. let acme_user = "acme"; // FIXME: CONFIG
  39. let acme_command_runner = SetuidCommandRunner::new(acme_user, command_runner);
  40. SymbolFactory { command_runner, acme_command_runner, symbol_runner, policy }
  41. }
  42. pub fn get_nginx_acme_server<'a, 'c: 'a, S: 'a + Symbol>(&'c self, host: &'static str, nginx_server_symbol: S) -> Box<Action + 'a> {
  43. Box::new(ListAction::new(vec![
  44. Box::new(SelfSignedTlsCert::new(
  45. host.into(),
  46. self.command_runner
  47. )).into_action(self.symbol_runner),
  48. Box::new(Hook::new(
  49. nginx_server_symbol,
  50. ReloadService::new("nginx", self.command_runner)
  51. )).into_action(self.symbol_runner),
  52. Box::new(AcmeCert::new(
  53. host.into(),
  54. &self.acme_command_runner
  55. )).into_action(self.symbol_runner),
  56. Box::new(Hook::new(
  57. AcmeCertChain::new(
  58. host.into(),
  59. &self.acme_command_runner
  60. ),
  61. ReloadService::new("nginx", self.command_runner)
  62. )).into_action(self.symbol_runner)
  63. ]))
  64. }
  65. pub fn get_nginx_acme_challenge_config<'a>(&'a self) -> Box<Action + 'a> {
  66. Box::new(File::new(
  67. "/etc/nginx/snippets/acme-challenge.conf", "location ^~ /.well-known/acme-challenge/ {
  68. alias /home/acme/challenges/;
  69. try_files $uri =404;
  70. }"
  71. )).into_action(self.symbol_runner)
  72. }
  73. fn get_php_fpm_pool_socket_path<'a>(&'a self, user_name: &str) -> String {
  74. format!("/run/php/{}.sock", user_name)
  75. }
  76. fn get_php_fpm_pool<'a>(&'a self, user_name: &str) -> Box<Action + 'a> {
  77. let socket = self.get_php_fpm_pool_socket_path(user_name);
  78. Box::new(Hook::new(
  79. File::new(
  80. format!("/etc/php/7.0/fpm/pool.d/{}.conf", user_name),
  81. format!(
  82. "[{0}]
  83. user = {0}
  84. group = www-data
  85. listen = {1}
  86. listen.owner = www-data
  87. pm = ondemand
  88. pm.max_children = 10
  89. catch_workers_output = yes
  90. env[PATH] = /usr/local/bin:/usr/bin:/bin
  91. "
  92. , user_name, socket)),
  93. ReloadService::new("php7.0-fpm", self.command_runner)
  94. )).into_action(self.symbol_runner)
  95. }
  96. pub fn serve_php<'a>(&'a self, host_name: &'static str, root_dir: Cow<'a, str>) -> Box<Action + 'a> {
  97. let user_name = self.policy.user_name_for_host(host_name);
  98. let socket = self.get_php_fpm_pool_socket_path(&user_name);
  99. Box::new(ListAction::new(vec![
  100. self.get_php_fpm_pool(&user_name),
  101. self.get_nginx_acme_server(host_name,
  102. NginxServer::new_php(
  103. host_name,
  104. socket.into(),
  105. root_dir,
  106. self.command_runner
  107. )
  108. )
  109. ]))
  110. }
  111. pub fn serve_wordpress<'a>(&'a self, host_name: &'static str, root_dir: Cow<'a, str>) -> Box<Action + 'a> {
  112. let user_name = self.policy.user_name_for_host(host_name);
  113. let socket = self.get_php_fpm_pool_socket_path(&user_name);
  114. Box::new(ListAction::new(vec![
  115. self.get_php_fpm_pool(&user_name),
  116. self.get_nginx_acme_server(host_name,
  117. NginxServer::new(
  118. host_name,
  119. server_config(host_name, &format!("{}
  120. location / {{
  121. try_files $uri $uri/ /index.php?$args;
  122. }}
  123. ", php_server_config_snippet(socket.into(), root_dir))),
  124. self.command_runner
  125. ))
  126. ]))
  127. }
  128. pub fn serve_dokuwiki<'a>(&'a self, host_name: &'static str, root_dir: &'static str) -> Box<Action + 'a> {
  129. let user_name = self.policy.user_name_for_host(host_name);
  130. let socket = self.get_php_fpm_pool_socket_path(&user_name);
  131. Box::new(ListAction::new(vec![
  132. self.get_php_fpm_pool(&user_name),
  133. self.get_nginx_acme_server(host_name,
  134. NginxServer::new(
  135. host_name,
  136. server_config(host_name, &format!("
  137. root {};
  138. index doku.php;
  139. location ~ [^/]\\.php(/|$) {{
  140. fastcgi_pass unix:{};
  141. include \"snippets/fastcgi-php.conf\";
  142. }}
  143. location ~ /(data/|conf/|bin/|inc/|install.php) {{ deny all; }}
  144. location / {{ try_files $uri $uri/ @dokuwiki; }}
  145. location @dokuwiki {{
  146. # rewrites \"doku.php/\" out of the URLs if you set the userewrite setting to .htaccess in dokuwiki config page
  147. rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
  148. rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
  149. rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
  150. rewrite ^/(.*) /doku.php?id=$1&$args last;
  151. }}
  152. ",
  153. root_dir,
  154. socket)),
  155. self.command_runner
  156. ))
  157. ]))
  158. }
  159. pub fn serve_nextcloud<'a>(&'a self, host_name: &'static str, root_dir: Cow<'a, str>) -> Box<Action + 'a> {
  160. let user_name = self.policy.user_name_for_host(host_name);
  161. let socket = self.get_php_fpm_pool_socket_path(&user_name);
  162. Box::new(ListAction::new(vec![
  163. self.get_php_fpm_pool(&user_name),
  164. self.get_nginx_acme_server(host_name,
  165. NginxServer::new(
  166. host_name,
  167. server_config(host_name, &format!("{}
  168. client_max_body_size 500M;
  169. # Disable gzip to avoid the removal of the ETag header
  170. gzip off;
  171. rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
  172. rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
  173. rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
  174. error_page 403 /core/templates/403.php;
  175. error_page 404 /core/templates/404.php;
  176. location = /robots.txt {{
  177. allow all;
  178. log_not_found off;
  179. access_log off;
  180. }}
  181. location ~ ^/(?:\\.htaccess|data|config|db_structure\\.xml|README) {{
  182. deny all;
  183. }}
  184. location / {{
  185. # The following 2 rules are only needed with webfinger
  186. rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
  187. rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
  188. rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
  189. rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
  190. rewrite ^(/core/doc/[^\\/]+/)$ $1/index.html;
  191. try_files $uri $uri/ /index.php;
  192. }}
  193. # Adding the cache control header for js and css files
  194. # Make sure it is BELOW the location ~ \\.php(?:$|/) {{ block
  195. location ~* \\.(?:css|js)$ {{
  196. add_header Cache-Control \"public, max-age=7200\";
  197. # Optional: Don't log access to assets
  198. access_log off;
  199. }}
  200. # Optional: Don't log access to other assets
  201. location ~* \\.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {{
  202. access_log off;
  203. }}
  204. ", php_server_config_snippet(socket.into(), root_dir))),
  205. self.command_runner
  206. ))
  207. ]))
  208. }
  209. pub fn serve_redir<'a>(&'a self, host_name: &'static str, target: &'static str) -> Box<Action + 'a> {
  210. self.get_nginx_acme_server(host_name, NginxServer::new_redir(host_name, target, self.command_runner))
  211. }
  212. pub fn serve_static<'a>(&'a self, host_name: &'static str, dir: &'a str) -> Box<Action + 'a> {
  213. self.get_nginx_acme_server(host_name, NginxServer::new_static(host_name, dir, self.command_runner))
  214. }
  215. pub fn get_stored_directory<'a, T: Into<String>>(&'a self, storage_name: &'static str, target: T) -> (Box<Action + 'a>, Box<Action + 'a>) {
  216. let data = SimpleStorage::new("/root/data".to_string(), storage_name.to_string());
  217. let string_target = target.into();
  218. (
  219. Box::new(StoredDirectory::new(string_target.clone().into(), data.clone(), StorageDirection::Save, self.command_runner)).into_action(self.symbol_runner),
  220. Box::new(StoredDirectory::new(string_target.into(), data.clone(), StorageDirection::Load, self.command_runner)).into_action(self.symbol_runner)
  221. )
  222. }
  223. pub fn get_mariadb_database<'a>(&'a self, name: &'static str) -> Box<Action + 'a> {
  224. let db_dump = SimpleStorage::new("/root/data".to_string(), format!("{}.sql", name));
  225. Box::new(ListAction::new(vec![
  226. Box::new(MariaDBDatabase::new(name.into(), db_dump.read_filename().unwrap().into(), self.command_runner)).into_action(self.symbol_runner),
  227. Box::new(DatabaseDump::new(name, db_dump, self.command_runner)).into_action(self.symbol_runner)
  228. ]))
  229. }
  230. pub fn get_mariadb_user<'a>(&'a self, user_name: &'static str) -> Box<Action + 'a> {
  231. Box::new(MariaDBUser::new(user_name.into(), self.command_runner)).into_action(self.symbol_runner)
  232. }
  233. pub fn get_git_checkout<'a, T: 'a + AsRef<str>>(&'a self, target: T, source: &'a str, branch: &'a str) -> Box<Action + 'a> {
  234. Box::new(GitCheckout::new(target, source, branch, self.command_runner)).into_action(self.symbol_runner)
  235. }
  236. pub fn get_owner<'a, F: 'a + AsRef<str>>(&'a self, file: F, user: &'a str) -> Box<Action + 'a> {
  237. Box::new(Owner::new(file, user.into(), self.command_runner)).into_action(self.symbol_runner)
  238. }
  239. pub fn get_file<'a, F: 'a + Deref<Target=str>, Q: 'a + AsRef<Path>>(&'a self, path: Q, content: F) -> Box<Action + 'a> {
  240. Box::new(File::new(path, content)).into_action(self.symbol_runner)
  241. }
  242. }