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.

190 lines
5.3 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
7 years ago
6 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
5 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
6 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 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
7 years ago
7 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use std::io;
  4. use std::ops::Deref;
  5. use command_runner::CommandRunner;
  6. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  7. use symbols::file::File as FileSymbol;
  8. use resources::Resource;
  9. #[derive(Debug)]
  10. pub enum NginxServerError<E: Error> {
  11. ExecError(E),
  12. GenericError
  13. }
  14. impl From<io::Error> for NginxServerError<io::Error> {
  15. fn from(err: io::Error) -> NginxServerError<io::Error> {
  16. NginxServerError::ExecError(err)
  17. }
  18. }
  19. impl<E: Error> Error for NginxServerError<E> {
  20. fn description(&self) -> &str {
  21. match self {
  22. NginxServerError::ExecError(ref e) => e.description(),
  23. NginxServerError::GenericError => "Generic error"
  24. }
  25. }
  26. fn cause(&self) -> Option<&dyn Error> {
  27. match self {
  28. NginxServerError::ExecError(ref e) => Some(e),
  29. _ => None
  30. }
  31. }
  32. }
  33. impl<E: Error> fmt::Display for NginxServerError<E> {
  34. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  35. write!(f, "{}", self.description())
  36. }
  37. }
  38. pub struct NginxServer<'a, C: 'a + CommandRunner, T> where T: Deref<Target=str> {
  39. command_runner: &'a C,
  40. file: FileSymbol<T, String>,
  41. }
  42. use std::borrow::Cow;
  43. pub fn server_config(domain: &str, content: &str) -> String {
  44. format!("server {{
  45. listen 443 ssl http2;
  46. server_name {0};
  47. include \"snippets/acme-challenge.conf\";
  48. ssl_certificate /etc/ssl/local_certs/{0}.chained.crt;
  49. ssl_certificate_key /etc/ssl/private/{0}.key;
  50. add_header Strict-Transport-Security \"max-age=31536000\";
  51. {1}
  52. }}
  53. # Redirect all HTTP links to the matching HTTPS page
  54. server {{
  55. listen 80;
  56. server_name {0};
  57. include \"snippets/acme-challenge.conf\";
  58. location / {{
  59. return 301 https://$host$request_uri;
  60. }}
  61. }}
  62. ", domain, content)
  63. }
  64. pub fn php_server_config_snippet<'a>(socket_path: Cow<'a, str>, static_path: Cow<'a, str>) -> String {
  65. format!("
  66. root {};
  67. index index.html index.php;
  68. location ~ [^/]\\.php(/|$) {{
  69. fastcgi_pass unix:{};
  70. include \"snippets/fastcgi-php.conf\";
  71. }}", static_path, socket_path)
  72. }
  73. pub trait SocketSpec {
  74. fn to_nginx(&self) -> String;
  75. }
  76. impl SocketSpec for &str {
  77. fn to_nginx(&self) -> String {
  78. format!("unix:{}:", self)
  79. }
  80. }
  81. pub struct LocalTcpSocket(usize);
  82. impl LocalTcpSocket {
  83. pub fn new(x: usize) -> Self {
  84. LocalTcpSocket(x)
  85. }
  86. }
  87. impl SocketSpec for LocalTcpSocket {
  88. fn to_nginx(&self) -> String {
  89. format!("localhost:{}", self.0)
  90. }
  91. }
  92. impl<'a, C: CommandRunner> NginxServer<'a, C, String> {
  93. pub fn new_redir(domain: &'a str, target: &'a str, command_runner: &'a C) -> Self {
  94. let content = server_config(domain, &format!("location / {{
  95. return 301 $scheme://{}$request_uri;
  96. }}", target));
  97. NginxServer::new(domain, content, command_runner)
  98. }
  99. pub fn new_proxy<S: SocketSpec>(domain: &'a str, socket_path: S, static_path: &'a str, command_runner: &'a C) -> Self {
  100. let proxy_content = format!("location / {{
  101. try_files $uri @proxy;
  102. }}
  103. location @proxy {{
  104. include fastcgi_params;
  105. proxy_pass http://{};
  106. proxy_redirect off;
  107. }}", socket_path.to_nginx());
  108. let content = server_config(domain, &format!("
  109. root {};
  110. {}
  111. ", static_path, proxy_content));
  112. NginxServer::new(domain, content, command_runner)
  113. }
  114. pub fn new_php(domain: &'a str, socket_path: Cow<'a, str>, static_path: Cow<'a, str>, command_runner: &'a C) -> Self {
  115. let content = server_config(domain, &php_server_config_snippet(socket_path, static_path));
  116. NginxServer::new(domain, content, command_runner)
  117. }
  118. pub fn new_static(domain: &'a str, static_path: &'a str, command_runner: &'a C) -> Self {
  119. let content = server_config(domain, &format!("
  120. root {};
  121. try_files $uri $uri/ $uri.html =404;
  122. ", static_path));
  123. NginxServer::new(domain, content, command_runner)
  124. }
  125. pub fn new(domain: &'a str, content: String, command_runner: &'a C) -> Self {
  126. let file_path = String::from("/etc/nginx/sites-enabled/") + domain;
  127. NginxServer {
  128. command_runner,
  129. file: FileSymbol::new(file_path, content)
  130. }
  131. }
  132. }
  133. impl<'a, C: CommandRunner, T> Symbol for NginxServer<'a, C, T> where T: Deref<Target=str> {
  134. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  135. if !try!(self.file.target_reached()) {
  136. return Ok(false);
  137. }
  138. // TODO: Could try to find out if the server is in the live config
  139. Ok(true)
  140. }
  141. fn execute(&self) -> Result<(), Box<dyn Error>> {
  142. try!(self.file.execute());
  143. self.command_runner.run_successfully("systemctl", &["reload-or-restart", "nginx"])
  144. }
  145. fn get_prerequisites(&self) -> Vec<Resource> {
  146. self.file.get_prerequisites()
  147. }
  148. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  149. Box::new(SymbolAction::new(runner, self))
  150. }
  151. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> where Self: 'b {
  152. Box::new(OwnedSymbolAction::new(runner, *self))
  153. }
  154. }
  155. impl<'a, C: CommandRunner, T> fmt::Display for NginxServer<'a, C, T> where T: Deref<Target=str> {
  156. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
  157. write!(f, "Nginx server config")
  158. }
  159. }