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.

116 lines
3.2 KiB

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