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.

109 lines
2.9 KiB

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. #[derive(Debug)]
  9. pub enum NginxServerError<E: Error> {
  10. ExecError(E),
  11. GenericError
  12. }
  13. impl From<io::Error> for NginxServerError<io::Error> {
  14. fn from(err: io::Error) -> NginxServerError<io::Error> {
  15. NginxServerError::ExecError(err)
  16. }
  17. }
  18. impl<E: Error> Error for NginxServerError<E> {
  19. fn description(&self) -> &str {
  20. match self {
  21. &NginxServerError::ExecError(ref e) => e.description(),
  22. &NginxServerError::GenericError => "Generic error"
  23. }
  24. }
  25. fn cause(&self) -> Option<&Error> {
  26. match self {
  27. &NginxServerError::ExecError(ref e) => Some(e),
  28. _ => None
  29. }
  30. }
  31. }
  32. impl<E: Error> fmt::Display for NginxServerError<E> {
  33. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
  34. write!(f, "{}", self.description())
  35. }
  36. }
  37. pub struct NginxServer<'a, C> where C: Deref<Target=str> {
  38. command_runner: &'a CommandRunner,
  39. file: FileSymbol<C, Cow<'a, str>>,
  40. }
  41. use std::borrow::Cow;
  42. impl<'a> NginxServer<'a, String> {
  43. pub fn new(home: &'a str, domain: &'a str, static_path: &'a str, redir_domains: &[&'a str], command_runner: &'a CommandRunner) -> Self {
  44. let file_path: Cow<str> = Cow::from(String::from("/etc/nginx/sites-enabled/") + domain);
  45. let redir_content = redir_domains.iter().map(|redir_domain| format!("server {{
  46. listen 80;
  47. server_name {};
  48. return 302 $scheme://{}$request_uri;
  49. }}
  50. ", redir_domain, domain)).fold(String::new(), |s, v| s + &v);
  51. let content = String::from(redir_content) + &format!("server {{
  52. listen 80;
  53. # listen 443 ssl;
  54. # ssl_certificate /etc/ssl/.crt;
  55. # ssl_certificate_key /etc/ssl/.key;
  56. server_name {};
  57. root {};
  58. location / {{
  59. try_files $uri @proxy;
  60. }}
  61. location @proxy {{
  62. include fastcgi_params;
  63. proxy_pass http://unix:{}/var/service.socket:;
  64. proxy_redirect off;
  65. }}
  66. }}", domain, static_path, home);
  67. NginxServer {
  68. command_runner: command_runner,
  69. file: FileSymbol::new(file_path, content)
  70. }
  71. }
  72. }
  73. impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
  74. type Error = NginxServerError<<FileSymbol<C, Cow<'a, str>> as Symbol>::Error>;
  75. fn target_reached(&self) -> Result<bool, Self::Error> {
  76. if !try!(self.file.target_reached()) {
  77. return Ok(false);
  78. }
  79. // TODO: Could try to find out if the server is in the live config
  80. Ok(true)
  81. }
  82. fn execute(&self) -> Result<(), Self::Error> {
  83. try!(self.file.execute());
  84. try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"]));
  85. Ok(())
  86. }
  87. }
  88. impl<'a, C> fmt::Display for NginxServer<'a, C> where C: Deref<Target=str> {
  89. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
  90. write!(f, "Nginx server config")
  91. }
  92. }