bootstrap.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import subprocess
  3. from jinja2 import Environment, FileSystemLoader
  4. def includes_conf(env, template_vars):
  5. server_name = "server_name.active"
  6. listen_plain = "listen_plain.active"
  7. listen_ssl = "listen_ssl.active"
  8. server_name_config = f"server_name {template_vars['MAILCOW_HOSTNAME']} autodiscover.* autoconfig.* {' '.join(template_vars['ADDITIONAL_SERVER_NAMES'])};"
  9. listen_plain_config = f"listen {template_vars['HTTP_PORT']};"
  10. listen_ssl_config = f"listen {template_vars['HTTPS_PORT']};"
  11. if not template_vars['DISABLE_IPv6']:
  12. listen_plain_config += f"\nlisten [::]:{template_vars['HTTP_PORT']};"
  13. listen_ssl_config += f"\nlisten [::]:{template_vars['HTTPS_PORT']} ssl;"
  14. listen_ssl_config += "\nhttp2 on;"
  15. with open(f"/etc/nginx/conf.d/{server_name}", "w") as f:
  16. f.write(server_name_config)
  17. with open(f"/etc/nginx/conf.d/{listen_plain}", "w") as f:
  18. f.write(listen_plain_config)
  19. with open(f"/etc/nginx/conf.d/{listen_ssl}", "w") as f:
  20. f.write(listen_ssl_config)
  21. def sites_default_conf(env, template_vars):
  22. config_name = "sites-default.conf"
  23. template = env.get_template(f"{config_name}.j2")
  24. config = template.render(template_vars)
  25. with open(f"/etc/nginx/includes/{config_name}", "w") as f:
  26. f.write(config)
  27. def nginx_conf(env, template_vars):
  28. config_name = "nginx.conf"
  29. template = env.get_template(f"{config_name}.j2")
  30. config = template.render(template_vars)
  31. with open(f"/etc/nginx/{config_name}", "w") as f:
  32. f.write(config)
  33. def prepare_template_vars():
  34. ipv4_network = os.getenv("IPV4_NETWORK", "172.22.1")
  35. additional_server_names = os.getenv("ADDITIONAL_SERVER_NAMES", "")
  36. trusted_proxies = os.getenv("TRUSTED_PROXIES", "")
  37. template_vars = {
  38. 'IPV4_NETWORK': ipv4_network,
  39. 'TRUSTED_PROXIES': [item.strip() for item in trusted_proxies.split(",") if item.strip()],
  40. 'SKIP_RSPAMD': os.getenv("SKIP_RSPAMD", "n").lower() in ("y", "yes"),
  41. 'SKIP_SOGO': os.getenv("SKIP_SOGO", "n").lower() in ("y", "yes"),
  42. 'NGINX_USE_PROXY_PROTOCOL': os.getenv("NGINX_USE_PROXY_PROTOCOL", "n").lower() in ("y", "yes"),
  43. 'MAILCOW_HOSTNAME': os.getenv("MAILCOW_HOSTNAME", ""),
  44. 'ADDITIONAL_SERVER_NAMES': [item.strip() for item in additional_server_names.split(",") if item.strip()],
  45. 'HTTP_PORT': os.getenv("HTTP_PORT", "80"),
  46. 'HTTPS_PORT': os.getenv("HTTPS_PORT", "443"),
  47. 'SOGOHOST': os.getenv("SOGOHOST", ipv4_network + ".248"),
  48. 'RSPAMDHOST': os.getenv("RSPAMDHOST", "rspamd-mailcow"),
  49. 'PHPFPMHOST': os.getenv("PHPFPMHOST", "php-fpm-mailcow"),
  50. 'DISABLE_IPv6': os.getenv("DISABLE_IPv6", "n").lower() in ("y", "yes"),
  51. 'HTTP_REDIRECT': os.getenv("HTTP_REDIRECT", "n").lower() in ("y", "yes"),
  52. }
  53. ssl_dir = '/etc/ssl/mail/'
  54. template_vars['valid_cert_dirs'] = []
  55. for d in os.listdir(ssl_dir):
  56. full_path = os.path.join(ssl_dir, d)
  57. if not os.path.isdir(full_path):
  58. continue
  59. cert_path = os.path.join(full_path, 'cert.pem')
  60. key_path = os.path.join(full_path, 'key.pem')
  61. domains_path = os.path.join(full_path, 'domains')
  62. if os.path.isfile(cert_path) and os.path.isfile(key_path) and os.path.isfile(domains_path):
  63. with open(domains_path, 'r') as file:
  64. domains = file.read().strip()
  65. domains_list = domains.split()
  66. if domains_list and template_vars["MAILCOW_HOSTNAME"] not in domains_list:
  67. template_vars['valid_cert_dirs'].append({
  68. 'cert_path': full_path + '/',
  69. 'domains': domains
  70. })
  71. return template_vars
  72. def main():
  73. env = Environment(loader=FileSystemLoader('./etc/nginx/conf.d/templates'))
  74. # Render config
  75. print("Render config")
  76. template_vars = prepare_template_vars()
  77. sites_default_conf(env, template_vars)
  78. nginx_conf(env, template_vars)
  79. includes_conf(env, template_vars)
  80. if __name__ == "__main__":
  81. main()