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