Skip to content

Commit

Permalink
Group the bind to the same port.
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Jul 21, 2019
1 parent cbcd59d commit 21686e1
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import time
import base64
import hashlib
from easymapping import HaproxyConfigGenerator

# path = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -12,6 +13,8 @@
"easymapping": [],
"customerrors": True if os.getenv("HAPROXY_CUSTOMERRORS") == "true" else False
}
easymapping = dict()

if os.getenv("HAPROXY_PASSWORD"):
result["stats"] = {
"username": os.getenv("HAPROXY_USERNAME") if os.getenv("HAPROXY_USERNAME") else "admin",
Expand All @@ -34,28 +37,33 @@
continue

port = d["com.byjg.easyhaproxy.port." + definition] if "com.byjg.easyhaproxy.port." + definition in d else "80"
data = {
"port": port,
"hosts": dict(),
"redirect": dict(),
# "ssl_cert": ""
}
hash = hashlib.md5(d["com.byjg.easyhaproxy.sslcert." + definition].encode('utf-8')).hexdigest() if "com.byjg.easyhaproxy.sslcert." + definition in d else ""

key = port+hash

if key not in easymapping:
easymapping[key] = {
"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")
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:
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:
file.write(base64.b64decode(d["com.byjg.easyhaproxy.sslcert." + definition]))

if "com.byjg.easyhaproxy.redirect." + definition in d:
redirect = d["com.byjg.easyhaproxy.redirect." + definition] if "com.byjg.easyhaproxy.redirect." + definition in d else ""
for r in redirect.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)
Expand Down

0 comments on commit 21686e1

Please sign in to comment.