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.

117 lines
3.1 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
  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 new(socket_path: Option<&'a str>, domain: &'a str, static_path: &'a str, redir_domains: &[&'a str], command_runner: &'a CommandRunner) -> Self {
  45. let file_path: Cow<str> = Cow::from(String::from("/etc/nginx/sites-enabled/") + domain);
  46. let redir_content = redir_domains.iter().map(|redir_domain| format!("server {{
  47. listen 80;
  48. server_name {};
  49. return 302 $scheme://{}$request_uri;
  50. }}
  51. ", redir_domain, domain)).fold(String::new(), |s, v| s + &v);
  52. let proxy_content = if let Some(socket) = socket_path {
  53. format!("location / {{
  54. try_files $uri @proxy;
  55. }}
  56. location @proxy {{
  57. include fastcgi_params;
  58. proxy_pass http://unix:{}:;
  59. proxy_redirect off;
  60. }}", socket)
  61. } else { "".to_string() };
  62. let content = String::from(redir_content) + &format!("server {{
  63. listen 80;
  64. # listen 443 ssl;
  65. # ssl_certificate /etc/ssl/.crt;
  66. # ssl_certificate_key /etc/ssl/.key;
  67. server_name {};
  68. root {};
  69. {}
  70. }}", domain, static_path, proxy_content);
  71. NginxServer {
  72. command_runner: command_runner,
  73. file: FileSymbol::new(file_path, content)
  74. }
  75. }
  76. }
  77. impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
  78. fn target_reached(&self) -> Result<bool, Box<Error>> {
  79. if !try!(self.file.target_reached()) {
  80. return Ok(false);
  81. }
  82. // TODO: Could try to find out if the server is in the live config
  83. Ok(true)
  84. }
  85. fn execute(&self) -> Result<(), Box<Error>> {
  86. try!(self.file.execute());
  87. try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"]));
  88. Ok(())
  89. }
  90. fn get_prerequisites(&self) -> Vec<Box<Resource>> {
  91. self.file.get_prerequisites()
  92. }
  93. }
  94. impl<'a, C> fmt::Display for NginxServer<'a, C> where C: Deref<Target=str> {
  95. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
  96. write!(f, "Nginx server config")
  97. }
  98. }