Directory restructuration
This commit is contained in:
parent
eb10288717
commit
b196905320
23 changed files with 127 additions and 166 deletions
18
Dockerfile
18
Dockerfile
|
|
@ -1,23 +1,19 @@
|
||||||
FROM haproxy:1.9-alpine
|
FROM haproxy:1.9-alpine
|
||||||
|
|
||||||
WORKDIR /root
|
WORKDIR /scripts
|
||||||
|
|
||||||
RUN apk add --no-cache bash python3 py-yaml supervisor docker
|
RUN apk add --no-cache bash python3 py-yaml supervisor docker
|
||||||
|
|
||||||
COPY requirements.txt /root
|
COPY requirements.txt /scripts
|
||||||
RUN pip3 install --upgrade pip \
|
RUN pip3 install --upgrade pip \
|
||||||
&& pip install -r requirements.txt
|
&& pip install -r requirements.txt
|
||||||
|
|
||||||
|
COPY swarm.* /scripts/
|
||||||
|
COPY static.* /scripts/
|
||||||
|
COPY templates /scripts/templates/
|
||||||
|
COPY easymapping /scripts/easymapping/
|
||||||
|
|
||||||
COPY haproxy.cfg /etc/haproxy/haproxy.cfg
|
COPY assets /
|
||||||
COPY conf.d /etc/haproxy/conf.d
|
|
||||||
COPY assets/errors-custom/* /etc/haproxy/errors-custom/
|
|
||||||
COPY assets/supervisord.conf /etc/supervisord.conf
|
|
||||||
COPY assets/crontab /etc/crontabs/root
|
|
||||||
|
|
||||||
COPY scripts/exit-event-listener.py /exit-event-listener.py
|
|
||||||
COPY scripts/haproxy.sh /haproxy.sh
|
|
||||||
COPY scripts/haproxy-reload.sh /haproxy-reload.sh
|
|
||||||
|
|
||||||
|
|
||||||
#CMD ["haproxy", "-f", "/etc/haproxy/haproxy.cfg"]
|
#CMD ["haproxy", "-f", "/etc/haproxy/haproxy.cfg"]
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,5 @@
|
||||||
0 2 * * * run-parts /etc/periodic/daily
|
0 2 * * * run-parts /etc/periodic/daily
|
||||||
0 3 * * 6 run-parts /etc/periodic/weekly
|
0 3 * * 6 run-parts /etc/periodic/weekly
|
||||||
0 5 1 * * run-parts /etc/periodic/monthly
|
0 5 1 * * run-parts /etc/periodic/monthly
|
||||||
* * * * * /haproxy-reload.sh
|
* * * * * /scripts/haproxy-reload.sh
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
|
||||||
serverurl=unix:///dev/shm/supervisor.sock ; use a unix:// URL for a unix socket
|
serverurl=unix:///dev/shm/supervisor.sock ; use a unix:// URL for a unix socket
|
||||||
|
|
||||||
[program:haproxy]
|
[program:haproxy]
|
||||||
command = /haproxy.sh
|
command = /scripts/haproxy.sh
|
||||||
autostart=true
|
autostart=true
|
||||||
autorestart=false
|
autorestart=false
|
||||||
priority=5
|
priority=5
|
||||||
|
|
@ -41,5 +41,5 @@ stderr_logfile=/dev/stderr
|
||||||
stderr_logfile_maxbytes=0
|
stderr_logfile_maxbytes=0
|
||||||
|
|
||||||
[eventlistener:exit_on_any_fatal]
|
[eventlistener:exit_on_any_fatal]
|
||||||
command=/exit-event-listener.py
|
command=/scripts/exit-event-listener.py
|
||||||
events=PROCESS_STATE_FATAL,PROCESS_STATE_EXITED,PROCESS_STATE_STOPPED
|
events=PROCESS_STATE_FATAL,PROCESS_STATE_EXITED,PROCESS_STATE_STOPPED
|
||||||
12
easymapping/__init__.py
Normal file
12
easymapping/__init__.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
||||||
|
|
||||||
|
class HaproxyConfigGenerator:
|
||||||
|
def __init__(self, mapping):
|
||||||
|
self.mapping = mapping
|
||||||
|
|
||||||
|
def generate(self):
|
||||||
|
file_loader = FileSystemLoader('templates')
|
||||||
|
env = Environment(loader=file_loader)
|
||||||
|
template = env.get_template('haproxy.cfg.j2')
|
||||||
|
return template.render(data=self.mapping)
|
||||||
109
entrypoint.py
109
entrypoint.py
|
|
@ -1,109 +0,0 @@
|
||||||
import yaml
|
|
||||||
import sys
|
|
||||||
|
|
||||||
if len(sys.argv) != 2:
|
|
||||||
print("You need to pass the easyconfig.cfg path")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def defaults(custom):
|
|
||||||
result = """
|
|
||||||
defaults
|
|
||||||
log global
|
|
||||||
|
|
||||||
timeout connect 3s
|
|
||||||
timeout client 10s
|
|
||||||
timeout server 10m
|
|
||||||
"""
|
|
||||||
if custom:
|
|
||||||
result += """
|
|
||||||
errorfile 400 /etc/haproxy/errors-custom/400.http
|
|
||||||
errorfile 403 /etc/haproxy/errors-custom/403.http
|
|
||||||
errorfile 408 /etc/haproxy/errors-custom/408.http
|
|
||||||
errorfile 500 /etc/haproxy/errors-custom/500.http
|
|
||||||
errorfile 502 /etc/haproxy/errors-custom/502.http
|
|
||||||
errorfile 503 /etc/haproxy/errors-custom/503.http
|
|
||||||
errorfile 504 /etc/haproxy/errors-custom/504.http
|
|
||||||
"""
|
|
||||||
result += """
|
|
||||||
global
|
|
||||||
log /dev/log local0
|
|
||||||
maxconn 2000
|
|
||||||
tune.ssl.default-dh-param 2048
|
|
||||||
"""
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def stats(map):
|
|
||||||
return """
|
|
||||||
frontend stats
|
|
||||||
bind *:{2}
|
|
||||||
mode http
|
|
||||||
stats enable
|
|
||||||
stats hide-version
|
|
||||||
stats realm Haproxy\ Statistics
|
|
||||||
stats uri /
|
|
||||||
stats auth {0}:{1}
|
|
||||||
# acl is_proxystats hdr(host) -i some.host.com
|
|
||||||
# default_backend srv_stats
|
|
||||||
# use_backend srv_stats if is_proxystats
|
|
||||||
default_backend srv_stats
|
|
||||||
|
|
||||||
backend srv_stats
|
|
||||||
mode http
|
|
||||||
server Local 127.0.0.1:{2}
|
|
||||||
""".format(map["username"], map["password"], map["port"] if "port" in map else 1936)
|
|
||||||
|
|
||||||
|
|
||||||
def easymapping(o, salt):
|
|
||||||
port = o["port"]
|
|
||||||
ssl = " ssl crt " + o["ssl_cert"] if "ssl_cert" in o else ""
|
|
||||||
hosts = o["hosts"] if "hosts" in o else dict()
|
|
||||||
redir = o["redirect"] if "redirect" in o else dict()
|
|
||||||
|
|
||||||
result = """
|
|
||||||
frontend http_in_{0}_{1}
|
|
||||||
bind *:{0} {2}
|
|
||||||
mode http
|
|
||||||
|
|
||||||
""".format(port, salt, ssl)
|
|
||||||
|
|
||||||
for k in redir:
|
|
||||||
result += " redirect prefix " + redir[k] + " code 301 if { hdr(host) -i " + k + " }\n"
|
|
||||||
|
|
||||||
result += "\n"
|
|
||||||
for k in hosts:
|
|
||||||
host = k.replace(".", "_") + "_{0}_{1}".format(port, salt)
|
|
||||||
result += " acl is_rule_{0}_1 hdr(host) -i {1}\n".format(host, k)
|
|
||||||
result += " acl is_rule_{0}_2 hdr(host) -i {1}:{2}\n".format(host, k, port)
|
|
||||||
result += " use_backend srv_{0} if is_rule_{0}_1 OR is_rule_{0}_2\n\n".format(host)
|
|
||||||
|
|
||||||
for k in hosts:
|
|
||||||
host = k.replace(".", "_") + "_{0}_{1}".format(port, salt)
|
|
||||||
result += """
|
|
||||||
backend srv_{0}
|
|
||||||
balance roundrobin
|
|
||||||
mode http
|
|
||||||
option forwardfor
|
|
||||||
http-request set-header X-Forwarded-Port %[dst_port]""".format(host) + """
|
|
||||||
http-request add-header X-Forwarded-Proto https if { ssl_fc }""" + """
|
|
||||||
server srv {0} check weight 1
|
|
||||||
""".format(hosts[k])
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
with open(sys.argv[1], 'r') as content_file:
|
|
||||||
parsed = yaml.load(content_file.read())
|
|
||||||
|
|
||||||
n = 0
|
|
||||||
|
|
||||||
print(defaults(parsed["customerrors"] if "customerrors" in parsed else False))
|
|
||||||
if "stats" in parsed:
|
|
||||||
print(stats(parsed["stats"]))
|
|
||||||
if "easymapping" in parsed:
|
|
||||||
for k in parsed["easymapping"]:
|
|
||||||
n = n + 1
|
|
||||||
print(easymapping(k, n))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
echo " ______ _ _ _____ "
|
|
||||||
echo "| ____| | | | | /\ | __ \ "
|
|
||||||
echo "| |__ __ _ ___ _ _ | |__| | / \ | |__) | __ _____ ___ _ "
|
|
||||||
echo "| __| / _\` / __| | | | | __ | / /\ \ | ___/ '__/ _ \ \/ / | | |"
|
|
||||||
echo "| |___| (_| \__ \ |_| | | | | |/ ____ \| | | | | (_) > <| |_| |"
|
|
||||||
echo "|______\__,_|___/\__, | |_| |_/_/ \_\_| |_| \___/_/\_\\__, |"
|
|
||||||
echo " __/ | __/ |"
|
|
||||||
echo " |___/ |___/ "
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
HAPROXY_CFG="/etc/haproxy/haproxy.cfg"
|
|
||||||
EASYCONFIG_CFG="/etc/easyconfig/easyconfig.cfg"
|
|
||||||
|
|
||||||
if [ ! -f "$EASYCONFIG_CFG" ]
|
|
||||||
then
|
|
||||||
echo "File '$EASYCONFIG_CFG' does not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
python3 /entrypoint.py "$EASYCONFIG_CFG" > $HAPROXY_CFG
|
|
||||||
|
|
||||||
|
|
||||||
/sbin/syslogd -O /proc/1/fd/1
|
|
||||||
/docker-entrypoint.sh "$@"
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
pyyaml
|
pyyaml
|
||||||
docker
|
docker
|
||||||
|
jinja2
|
||||||
16
static.py
Normal file
16
static.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import yaml
|
||||||
|
import sys
|
||||||
|
from easymapping import HaproxyConfigGenerator
|
||||||
|
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print("You need to pass the easyconfig.cfg path")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
with open(sys.argv[1], 'r') as content_file:
|
||||||
|
parsed = yaml.load(content_file.read())
|
||||||
|
|
||||||
|
cfg = HaproxyConfigGenerator(parsed)
|
||||||
|
print(cfg.generate())
|
||||||
|
|
||||||
|
exit(0)
|
||||||
|
|
@ -1,11 +1,14 @@
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
from easymapping import HaproxyConfigGenerator
|
||||||
|
|
||||||
path = os.path.dirname(os.path.realpath(__file__))
|
path = os.path.dirname(os.path.realpath(__file__))
|
||||||
with open(path + "/.docker_data", 'r') as content_file:
|
with open(path + "/.docker_data", 'r') as content_file:
|
||||||
lineList = content_file.readlines()
|
lineList = content_file.readlines()
|
||||||
|
|
||||||
result = dict()
|
result = {
|
||||||
|
"easymapping": []
|
||||||
|
}
|
||||||
|
|
||||||
for line in lineList:
|
for line in lineList:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
|
|
@ -19,19 +22,25 @@ for line in lineList:
|
||||||
|
|
||||||
for definition in definitions:
|
for definition in definitions:
|
||||||
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"
|
||||||
if port not in result:
|
data = {
|
||||||
result[port] = dict()
|
"port": port,
|
||||||
result[port]["hosts"] = dict()
|
"hosts": dict(),
|
||||||
result[port]["redirect"] = dict()
|
"redirect": dict(),
|
||||||
result[port]["ssl_cert"] = None
|
# "ssl_cert": ""
|
||||||
|
}
|
||||||
|
|
||||||
result[port]["hosts"][d["com.byjg.easyhaproxy.host." + definition] if "com.byjg.easyhaproxy.host." + definition in d else ""] = container + ":" + (d["com.byjg.easyhaproxy.localport." + definition] if "com.byjg.easyhaproxy.localport." + definition in d else "80")
|
data["hosts"][d["com.byjg.easyhaproxy.host." + definition] if "com.byjg.easyhaproxy.host." + definition in d else ""] = container + ":" + (d["com.byjg.easyhaproxy.localport." + definition] if "com.byjg.easyhaproxy.localport." + definition in d else "80")
|
||||||
|
|
||||||
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("=>")
|
||||||
result[port]["redirect"][r_parts[0]] = r_parts[1]
|
data["redirect"][r_parts[0]] = r_parts[1]
|
||||||
|
|
||||||
|
result["easymapping"].append(data)
|
||||||
|
|
||||||
|
|
||||||
|
cfg = HaproxyConfigGenerator(result)
|
||||||
|
print(cfg.generate())
|
||||||
|
|
||||||
print(result)
|
print(result)
|
||||||
# print(jsonStr)
|
# print(jsonStr)
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
|
||||||
#docker run \
|
#docker run \
|
||||||
# -l com.byjg.easyhaproxy.definitions=http \
|
# -l com.byjg.easyhaproxy.definitions=http \
|
||||||
# -l com.byjg.easyhaproxy.port.http=80 \
|
# -l com.byjg.easyhaproxy.port.http=80 \
|
||||||
|
|
@ -22,9 +21,8 @@ done
|
||||||
|
|
||||||
|
|
||||||
if cmp -s ./.docker_data ./.docker_data_old ; then
|
if cmp -s ./.docker_data ./.docker_data_old ; then
|
||||||
echo "Nothing changed"
|
exit 0
|
||||||
else
|
|
||||||
echo "Something changed"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
python3 swarm.py
|
||||||
# byjg_site={"com.byjg.easyhaproxy.definition":"http","com.byjg.easyhaproxy.host.http":"byjg.com.br","com.byjg.easyhaproxy.port.http":"80","com.byjg.easyhaproxy.redirect.http":"byjg.com.br%http://www.byjg.com.br,byjg.com%http://www.byjg.com.br","maintainer":"Joao Gilberto Magalhaes"}
|
# byjg_site={"com.byjg.easyhaproxy.definition":"http","com.byjg.easyhaproxy.host.http":"byjg.com.br","com.byjg.easyhaproxy.port.http":"80","com.byjg.easyhaproxy.redirect.http":"byjg.com.br%http://www.byjg.com.br,byjg.com%http://www.byjg.com.br","maintainer":"Joao Gilberto Magalhaes"}
|
||||||
68
templates/haproxy.cfg.j2
Normal file
68
templates/haproxy.cfg.j2
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
defaults
|
||||||
|
log global
|
||||||
|
|
||||||
|
timeout connect 3s
|
||||||
|
timeout client 10s
|
||||||
|
timeout server 10m
|
||||||
|
{% if data["customerrors"] %}
|
||||||
|
errorfile 400 /etc/haproxy/errors-custom/400.http
|
||||||
|
errorfile 403 /etc/haproxy/errors-custom/403.http
|
||||||
|
errorfile 408 /etc/haproxy/errors-custom/408.http
|
||||||
|
errorfile 500 /etc/haproxy/errors-custom/500.http
|
||||||
|
errorfile 502 /etc/haproxy/errors-custom/502.http
|
||||||
|
errorfile 503 /etc/haproxy/errors-custom/503.http
|
||||||
|
errorfile 504 /etc/haproxy/errors-custom/504.http
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
global
|
||||||
|
log /dev/log local0
|
||||||
|
maxconn 2000
|
||||||
|
tune.ssl.default-dh-param 2048
|
||||||
|
|
||||||
|
{% if "stats" in data %}
|
||||||
|
frontend stats
|
||||||
|
bind *:{{ data["stats"]["port"] | default(1936) }}
|
||||||
|
mode http
|
||||||
|
stats enable
|
||||||
|
stats hide-version
|
||||||
|
stats realm Haproxy\ Statistics
|
||||||
|
stats uri /
|
||||||
|
stats auth {{ data["stats"]["username"] }}:{{ data["stats"]["password"] }}
|
||||||
|
# acl is_proxystats hdr(host) -i some.host.com
|
||||||
|
# default_backend srv_stats
|
||||||
|
# use_backend srv_stats if is_proxystats
|
||||||
|
default_backend srv_stats
|
||||||
|
|
||||||
|
backend srv_stats
|
||||||
|
mode http
|
||||||
|
server Local 127.0.0.1:{{ data["stats"]["port"] | default(1936) }}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% for o in data["easymapping"] %}
|
||||||
|
{% set salt = loop.index %}
|
||||||
|
frontend http_in_{{ o["port"] }}_{{ salt }}
|
||||||
|
bind *:{{ o["port"] }} {{ " ssl crt " + o["ssl_cert"] if "ssl_cert" in o else "" }}
|
||||||
|
mode http
|
||||||
|
|
||||||
|
{% for k in o["redirect"] -%}
|
||||||
|
redirect prefix {{ o["redirect"][k] }} code 301 if { hdr(host) -i {{ k }} }
|
||||||
|
{% endfor -%}
|
||||||
|
|
||||||
|
{% for k in o["hosts"] %}
|
||||||
|
{% set host = k.replace(".", "_") + "_{0}_{1}".format(o["port"], salt) %}
|
||||||
|
acl is_rule_{{ host }}_1 hdr(host) -i {{ k }}
|
||||||
|
acl is_rule_{{ host }}_2 hdr(host) -i {{ k }}:{{ o["port"] }}
|
||||||
|
use_backend srv_{{ host }} if is_rule_{{ host }}_1 OR is_rule_{{ host }}_2
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
{% for k in o["hosts"] %}
|
||||||
|
{% set host = k.replace(".", "_") + "_{0}_{1}".format(o["port"], salt) %}
|
||||||
|
backend srv_{{ host }}
|
||||||
|
balance roundrobin
|
||||||
|
mode http
|
||||||
|
option forwardfor
|
||||||
|
http-request set-header X-Forwarded-Port %[dst_port]
|
||||||
|
http-request add-header X-Forwarded-Proto https if { ssl_fc }
|
||||||
|
server srv {{ k }} check weight 1
|
||||||
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue