2018-10-31 19:17:23 +02:00
|
|
|
#!/usr/bin/python3
|
2017-09-24 17:50:10 +02:00
|
|
|
|
|
|
|
import jinja2
|
|
|
|
import os
|
2019-01-08 00:38:06 +02:00
|
|
|
import logging as log
|
|
|
|
import sys
|
2017-09-24 17:50:10 +02:00
|
|
|
|
|
|
|
args = os.environ.copy()
|
|
|
|
|
2019-01-08 05:16:05 +02:00
|
|
|
log.basicConfig(stream=sys.stderr, level=args.get("LOG_LEVEL", "WARNING"))
|
2019-01-08 00:38:06 +02:00
|
|
|
|
|
|
|
def convert(src, dst, args):
|
|
|
|
logger = log.getLogger("convert()")
|
|
|
|
logger.debug("Source: %s, Destination: %s", src, dst)
|
|
|
|
open(dst, "w").write(jinja2.Template(open(src).read()).render(**args))
|
|
|
|
|
2017-11-13 22:38:04 +02:00
|
|
|
# Get the first DNS server
|
|
|
|
with open("/etc/resolv.conf") as handle:
|
|
|
|
content = handle.read().split()
|
|
|
|
args["RESOLVER"] = content[content.index("nameserver") + 1]
|
|
|
|
|
2018-01-24 01:55:43 +02:00
|
|
|
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"
|
2017-11-13 22:38:04 +02:00
|
|
|
|
|
|
|
# TLS configuration
|
2018-05-01 14:04:18 +02:00
|
|
|
cert_name = os.getenv("TLS_CERT_FILENAME", default="cert.pem")
|
|
|
|
keypair_name = os.getenv("TLS_KEYPAIR_FILENAME", default="key.pem")
|
2017-09-24 17:50:10 +02:00
|
|
|
args["TLS"] = {
|
2018-05-01 14:04:18 +02:00
|
|
|
"cert": ("/certs/%s" % cert_name, "/certs/%s" % keypair_name),
|
2017-09-24 17:50:10 +02:00
|
|
|
"letsencrypt": ("/certs/letsencrypt/live/mailu/fullchain.pem",
|
|
|
|
"/certs/letsencrypt/live/mailu/privkey.pem"),
|
2018-05-01 14:04:18 +02:00
|
|
|
"mail": ("/certs/%s" % cert_name, "/certs/%s" % keypair_name),
|
2017-12-05 01:21:58 +02:00
|
|
|
"mail-letsencrypt": ("/certs/letsencrypt/live/mailu/fullchain.pem",
|
|
|
|
"/certs/letsencrypt/live/mailu/privkey.pem"),
|
2017-09-24 17:50:10 +02:00
|
|
|
"notls": None
|
|
|
|
}[args["TLS_FLAVOR"]]
|
|
|
|
|
|
|
|
if args["TLS"] and not all(os.path.exists(file_path) for file_path in args["TLS"]):
|
|
|
|
print("Missing cert or key file, disabling TLS")
|
|
|
|
args["TLS_ERROR"] = "yes"
|
|
|
|
|
2017-11-13 22:38:04 +02:00
|
|
|
# Build final configuration paths
|
2017-09-24 18:43:14 +02:00
|
|
|
convert("/conf/tls.conf", "/etc/nginx/tls.conf", args)
|
2017-12-04 22:16:08 +02:00
|
|
|
convert("/conf/proxy.conf", "/etc/nginx/proxy.conf", args)
|
2017-09-24 17:50:10 +02:00
|
|
|
convert("/conf/nginx.conf", "/etc/nginx/nginx.conf", args)
|
2018-06-02 10:23:33 +02:00
|
|
|
if os.path.exists("/var/run/nginx.pid"):
|
2017-12-03 12:28:26 +02:00
|
|
|
os.system("nginx -s reload")
|