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.

163 lines
4.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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<&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> where C: Deref<Target=str> {
  39. command_runner: &'a CommandRunner,
  40. file: FileSymbol<C, Cow<'a, str>>,
  41. }
  42. use std::borrow::Cow;
  43. impl<'a> NginxServer<'a, String> {
  44. pub fn server_config(domain: &str, content: &str) -> String {
  45. format!("server {{
  46. listen 80;
  47. server_name {0};
  48. include \"snippets/acme-challenge.conf\";
  49. location / {{
  50. # Redirect all HTTP links to the matching HTTPS page
  51. return 301 https://$host$request_uri;
  52. }}
  53. }}
  54. server {{
  55. listen 443 ssl http2;
  56. server_name {0};
  57. include \"snippets/acme-challenge.conf\";
  58. ssl_certificate /etc/ssl/local_certs/{0}.chained.crt;
  59. ssl_certificate_key /etc/ssl/private/{0}.key;
  60. add_header Strict-Transport-Security \"max-age=31536000\";
  61. {1}
  62. }}
  63. ", domain, content)
  64. }
  65. pub fn new_redir(domain: &'a str, target: &'a str, command_runner: &'a CommandRunner) -> Self {
  66. let content = NginxServer::server_config(domain, &format!("location / {{
  67. return 301 $scheme://{}$request_uri;
  68. }}", target));
  69. NginxServer::new(domain, content, command_runner)
  70. }
  71. pub fn new_proxy(domain: &'a str, socket_path: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
  72. let proxy_content = format!("location / {{
  73. try_files $uri @proxy;
  74. }}
  75. location @proxy {{
  76. include fastcgi_params;
  77. proxy_pass http://unix:{}:;
  78. proxy_redirect off;
  79. }}", socket_path);
  80. let content = NginxServer::server_config(domain, &format!("
  81. root {};
  82. {}
  83. ", static_path, proxy_content));
  84. NginxServer::new(domain, content, command_runner)
  85. }
  86. pub fn new_php(domain: &'a str, socket_path: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
  87. let content = NginxServer::server_config(domain, &format!("
  88. root {};
  89. index index.html index.php;
  90. location ~ [^/]\\.php(/|$) {{
  91. fastcgi_pass unix:{};
  92. include \"snippets/fastcgi-php.conf\";
  93. }}
  94. ", static_path, socket_path));
  95. NginxServer::new(domain, content, command_runner)
  96. }
  97. pub fn new_static(domain: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
  98. let content = NginxServer::server_config(domain, &format!("
  99. root {};
  100. try_files $uri $uri/ $uri.html =404;
  101. ", static_path));
  102. NginxServer::new(domain, content, command_runner)
  103. }
  104. pub fn new(domain: &'a str, content: String, command_runner: &'a CommandRunner) -> Self {
  105. let file_path: Cow<str> = Cow::from(String::from("/etc/nginx/sites-enabled/") + domain);
  106. NginxServer {
  107. command_runner: command_runner,
  108. file: FileSymbol::new(file_path, content)
  109. }
  110. }
  111. }
  112. impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
  113. fn target_reached(&self) -> Result<bool, Box<Error>> {
  114. if !try!(self.file.target_reached()) {
  115. return Ok(false);
  116. }
  117. // TODO: Could try to find out if the server is in the live config
  118. Ok(true)
  119. }
  120. fn execute(&self) -> Result<(), Box<Error>> {
  121. try!(self.file.execute());
  122. self.command_runner.run_successfully("systemctl", &["reload-or-restart", "nginx"])
  123. }
  124. fn get_prerequisites(&self) -> Vec<Resource> {
  125. self.file.get_prerequisites()
  126. }
  127. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  128. Box::new(SymbolAction::new(runner, self))
  129. }
  130. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  131. Box::new(OwnedSymbolAction::new(runner, *self))
  132. }
  133. }
  134. impl<'a, C> fmt::Display for NginxServer<'a, C> where C: Deref<Target=str> {
  135. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
  136. write!(f, "Nginx server config")
  137. }
  138. }