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.

132 lines
3.7 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
  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::Symbol;
  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. listen 443 ssl;
  48. ssl_certificate /etc/ssl/local_certs/{0}.crt;
  49. ssl_certificate_key /etc/ssl/private/{0}.key;
  50. server_name {0};
  51. include \"snippets/acme-challenge.conf\";
  52. {1}
  53. }}
  54. ", domain, content)
  55. }
  56. pub fn new_redir(domain: &'a str, target: &'a str, command_runner: &'a CommandRunner) -> Self {
  57. let content = NginxServer::server_config(domain, &format!("location / {{
  58. return 301 $scheme://{}$request_uri;
  59. }}", target));
  60. NginxServer::new(domain, content, command_runner)
  61. }
  62. pub fn new_proxy(domain: &'a str, socket_path: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
  63. let proxy_content = format!("location / {{
  64. try_files $uri @proxy;
  65. }}
  66. location @proxy {{
  67. include fastcgi_params;
  68. proxy_pass http://unix:{}:;
  69. proxy_redirect off;
  70. }}", socket_path);
  71. let content = NginxServer::server_config(domain, &format!("
  72. root {};
  73. {}
  74. ", static_path, proxy_content));
  75. NginxServer::new(domain, content, command_runner)
  76. }
  77. pub fn new_static(domain: &'a str, static_path: &'a str, command_runner: &'a CommandRunner) -> Self {
  78. let content = NginxServer::server_config(domain, &format!("
  79. root {};
  80. try_files $uri $uri/ $uri.html =404;
  81. ", static_path));
  82. NginxServer::new(domain, content, command_runner)
  83. }
  84. pub fn new(domain: &'a str, content: String, command_runner: &'a CommandRunner) -> Self {
  85. let file_path: Cow<str> = Cow::from(String::from("/etc/nginx/sites-enabled/") + domain);
  86. NginxServer {
  87. command_runner: command_runner,
  88. file: FileSymbol::new(file_path, content)
  89. }
  90. }
  91. }
  92. impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
  93. fn target_reached(&self) -> Result<bool, Box<Error>> {
  94. if !try!(self.file.target_reached()) {
  95. return Ok(false);
  96. }
  97. // TODO: Could try to find out if the server is in the live config
  98. Ok(true)
  99. }
  100. fn execute(&self) -> Result<(), Box<Error>> {
  101. try!(self.file.execute());
  102. try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"]));
  103. Ok(())
  104. }
  105. fn get_prerequisites(&self) -> Vec<Box<Resource>> {
  106. self.file.get_prerequisites()
  107. }
  108. }
  109. impl<'a, C> fmt::Display for NginxServer<'a, C> where C: Deref<Target=str> {
  110. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
  111. write!(f, "Nginx server config")
  112. }
  113. }