From f538e33dcff20da9adc37cfe175ac3c09394d862 Mon Sep 17 00:00:00 2001 From: Mildred Ki'Lya Date: Wed, 24 Jan 2018 00:55:43 +0100 Subject: [PATCH] Parametrize hosts Allows to use mailu without docker-compose when hostnames are not set up by docker itself but provided via a separate resolver. Use case: use mailu using nomad scheduler and consul resolver instead of docker-compose. Other servers are provided by the DNS resolver that resolves names like admin.service.consul or webmail.service.consul. These names needs to be configurable. --- core/admin/mailu/__init__.py | 5 +++++ core/admin/mailu/internal/nginx.py | 16 +++++++++++----- core/nginx/conf/nginx.conf | 8 ++++---- core/nginx/config.py | 8 ++++++++ docs/configuration.rst | 18 ++++++++++++++++++ 5 files changed, 46 insertions(+), 9 deletions(-) diff --git a/core/admin/mailu/__init__.py b/core/admin/mailu/__init__.py index c0b0992c..ef98cf9e 100644 --- a/core/admin/mailu/__init__.py +++ b/core/admin/mailu/__init__.py @@ -54,6 +54,11 @@ default_config = { 'WEB_WEBMAIL': '/webmail', # Advanced settings 'PASSWORD_SCHEME': 'SHA512-CRYPT', + # Host settings + 'HOST_IMAP': 'imap', + 'HOST_POP3': 'imap', + 'HOST_SMTP': 'smtp', + 'HOST_AUTHSMTP': os.environ.get('HOST_SMTP', 'smtp'), } # Load configuration from the environment if available diff --git a/core/admin/mailu/internal/nginx.py b/core/admin/mailu/internal/nginx.py index 3ad51de6..84ee8596 100644 --- a/core/admin/mailu/internal/nginx.py +++ b/core/admin/mailu/internal/nginx.py @@ -1,5 +1,6 @@ -from mailu import db, models +from mailu import db, models, app +import re import socket import urllib @@ -73,14 +74,19 @@ def get_status(protocol, status): status, codes = STATUSES[status] return status, codes[protocol] +def extract_host_port(host_and_port, default_port): + host, _, port = re.match('^(.*)(:([0-9]*))?$', host_and_port).groups() + return host, int(port) if port else default_port def get_server(protocol, authenticated=False): if protocol == "imap": - hostname, port = "imap", 143 + hostname, port = extract_host_port(app.config['HOST_IMAP'], 143) elif protocol == "pop3": - hostname, port = "imap", 110 + hostname, port = extract_host_port(app.config['HOST_POP3'], 110) elif protocol == "smtp": - hostname = "smtp" - port = 10025 if authenticated else 25 + if authenticated: + hostname, port = extract_host_port(app.config['HOST_AUTHSMTP'], 10025) + else: + hostname, port = extract_host_port(app.config['HOST_SMTP'], 25) address = socket.gethostbyname(hostname) return address, port diff --git a/core/nginx/conf/nginx.conf b/core/nginx/conf/nginx.conf index 1bb89442..0ed9aa72 100644 --- a/core/nginx/conf/nginx.conf +++ b/core/nginx/conf/nginx.conf @@ -37,10 +37,10 @@ http { # Main HTTP server server { # Variables for proxifying - set $admin admin; - set $antispam antispam:11334; - set $webmail webmail; - set $webdav webdav:5232; + set $admin {{ HOST_ADMIN }}; + set $antispam {{ HOST_ANTISPAM }}; + set $webmail {{ HOST_WEBMAIL }}; + set $webdav {{ HOST_WEBDAV }}; # Always listen over HTTP listen 80; diff --git a/core/nginx/config.py b/core/nginx/config.py index 5d9f73c3..aca27380 100755 --- a/core/nginx/config.py +++ b/core/nginx/config.py @@ -12,6 +12,14 @@ with open("/etc/resolv.conf") as handle: content = handle.read().split() args["RESOLVER"] = content[content.index("nameserver") + 1] +if "HOST_WEBMAIL" not in args: + args["HOST_WEBMAIL"] = "webmail" +if "HOST_ADMIN" not in args: + args["HOST_ADMIN"] = "admin" +if "HOST_WEBDAV" not in args: + args["HOST_WEBDAV"] = "webdav:5232" +if "HOST_ANTISPAM" not in args: + args["HOST_ANTISPAM"] = "antispam:11334" # TLS configuration args["TLS"] = { diff --git a/docs/configuration.rst b/docs/configuration.rst index 5e192653..a2e05bf0 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -69,3 +69,21 @@ Advanced settings The ``PASSWORD_SCHEME`` is the password encryption scheme. You should use the default value, unless you are importing password from a separate system and want to keep using the old password encryption scheme. + +Infrastructure settings +----------------------- + +Various environment variables ``HOST_*`` can be used to run Mailu containers +separately from a supported orchestrator. It is used by the various components +to find the location of the other containers it depends on. They can contain an +optional port number. Those variables are: + +- ``HOST_IMAP``: the container that is running the IMAP server (default: ``imap``, port 143) +- ``HOST_POP3``: the container that is running the POP3 server (default: ``imap``, port 110) +- ``HOST_SMTP``: the container that is running the SMTP server (default: ``smtp``, port 25) +- ``HOST_AUTHSMTP``: the container that is running the authenticated SMTP server for the webnmail (default: ``smtp``, port 10025) +- ``HOST_ADMIN``: the container that is running the admin interface (default: ``admin``) +- ``HOST_ANTISPAM``: the container that is running the antispam service (default: ``antispam:11334``) +- ``HOST_WEBMAIL``: the container that is running the webmail (default: ``webmail``) +- ``HOST_WEBDAV``: the container that is running the webdav server (default: ``webdav:5232``) +