1
0
Fork 0

Group the bind to the same port.

This commit is contained in:
Joao Gilberto Magalhaes 2019-07-21 00:18:13 -05:00
parent cbcd59d6f7
commit 21686e1605

View file

@ -2,6 +2,7 @@ import os
import json import json
import time import time
import base64 import base64
import hashlib
from easymapping import HaproxyConfigGenerator from easymapping import HaproxyConfigGenerator
# path = os.path.dirname(os.path.realpath(__file__)) # path = os.path.dirname(os.path.realpath(__file__))
@ -12,6 +13,8 @@ result = {
"easymapping": [], "easymapping": [],
"customerrors": True if os.getenv("HAPROXY_CUSTOMERRORS") == "true" else False "customerrors": True if os.getenv("HAPROXY_CUSTOMERRORS") == "true" else False
} }
easymapping = dict()
if os.getenv("HAPROXY_PASSWORD"): if os.getenv("HAPROXY_PASSWORD"):
result["stats"] = { result["stats"] = {
"username": os.getenv("HAPROXY_USERNAME") if os.getenv("HAPROXY_USERNAME") else "admin", "username": os.getenv("HAPROXY_USERNAME") if os.getenv("HAPROXY_USERNAME") else "admin",
@ -34,18 +37,23 @@ for line in lineList:
continue continue
port = d["com.byjg.easyhaproxy.port." + definition] if "com.byjg.easyhaproxy.port." + definition in d else "80" port = d["com.byjg.easyhaproxy.port." + definition] if "com.byjg.easyhaproxy.port." + definition in d else "80"
data = { hash = hashlib.md5(d["com.byjg.easyhaproxy.sslcert." + definition].encode('utf-8')).hexdigest() if "com.byjg.easyhaproxy.sslcert." + definition in d else ""
"port": port,
"hosts": dict(),
"redirect": dict(),
# "ssl_cert": ""
}
data["hosts"][d["com.byjg.easyhaproxy.host." + definition]] = container + ":" + (d["com.byjg.easyhaproxy.localport." + definition] if "com.byjg.easyhaproxy.localport." + definition in d else "80") key = port+hash
if key not in easymapping:
easymapping[key] = {
"port": port,
"hosts": dict(),
"redirect": dict(),
# "ssl_cert": ""
}
easymapping[key]["hosts"][d["com.byjg.easyhaproxy.host." + definition]] = container + ":" + (d["com.byjg.easyhaproxy.localport." + definition] if "com.byjg.easyhaproxy.localport." + definition in d else "80")
if "com.byjg.easyhaproxy.sslcert." + definition in d: if "com.byjg.easyhaproxy.sslcert." + definition in d:
filename = '/etc/haproxy/certs/' + d["com.byjg.easyhaproxy.host." + definition] + "." + str(time.time()) + ".pem" filename = '/etc/haproxy/certs/' + d["com.byjg.easyhaproxy.host." + definition] + "." + str(time.time()) + ".pem"
data["ssl_cert"] = filename easymapping[key]["ssl_cert"] = filename
with open(filename, 'wb') as file: with open(filename, 'wb') as file:
file.write(base64.b64decode(d["com.byjg.easyhaproxy.sslcert." + definition])) file.write(base64.b64decode(d["com.byjg.easyhaproxy.sslcert." + definition]))
@ -53,9 +61,9 @@ for line in lineList:
redirect = d["com.byjg.easyhaproxy.redirect." + definition] if "com.byjg.easyhaproxy.redirect." + definition in d else "" redirect = d["com.byjg.easyhaproxy.redirect." + definition] if "com.byjg.easyhaproxy.redirect." + definition in d else ""
for r in redirect.split(","): for r in redirect.split(","):
r_parts = r.split("--") r_parts = r.split("--")
data["redirect"][r_parts[0]] = r_parts[1] easymapping[key]["redirect"][r_parts[0]] = r_parts[1]
result["easymapping"].append(data) result["easymapping"] = easymapping.values()
cfg = HaproxyConfigGenerator(result) cfg = HaproxyConfigGenerator(result)