Skip to content
This repository was archived by the owner on Jan 24, 2020. It is now read-only.

Commit 59e5402

Browse files
committed
Avoid server hang on restarts
When restarting, the entrypoint was appending to existing configuration always, without checking if it was already filled. As such, since the 1st restart, frontend and backend names got duplicated and the server stopped working. In an automated restart environment, the file was only growing constantly. Now we generate the full config and overwrite it always.
1 parent 9ba554d commit 59e5402

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ CMD ["haproxy", "-f", "/usr/local/etc/haproxy/haproxy.cfg"]
66
RUN apk add --no-cache python3 &&\
77
pip3 install --no-cache-dir dnspython
88

9-
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
109
COPY magic-entrypoint.py /magic-entrypoint
1110

1211
ENV NAMESERVERS="208.67.222.222 8.8.8.8 208.67.220.220 8.8.4.4" \

haproxy.cfg

Lines changed: 0 additions & 9 deletions
This file was deleted.

magic-entrypoint.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717
TEMPLATE = """
1818
backend talk_{index}
1919
server stupid_{index} {talk}
20-
2120
frontend listen_{index}
2221
bind {listen}
2322
default_backend talk_{index}
2423
"""
24+
config = """
25+
global
26+
log /dev/log local0
27+
defaults
28+
log global
29+
mode tcp
30+
timeout client 10m
31+
timeout connect 10s
32+
timeout server 10m
33+
"""
2534

2635
if len(LISTENS) != len(TALKS):
2736
sys.exit("Set the same amount of servers in $LISTEN and $TALK")
@@ -47,15 +56,15 @@
4756
logging.info("Resolved %s to %s", server, ip)
4857

4958
# Render template
50-
result = TEMPLATE.format(
59+
config += TEMPLATE.format(
5160
index=index,
5261
listen=listen,
5362
talk=f"{ip}:{port}",
5463
)
5564

56-
# Write template to haproxy's cfg file
57-
with open("/usr/local/etc/haproxy/haproxy.cfg", "a") as cfg:
58-
cfg.write(result)
65+
# Write template to haproxy's cfg file
66+
with open("/usr/local/etc/haproxy/haproxy.cfg", "w") as cfg:
67+
cfg.write(config)
5968

6069
logging.info("Magic ready, executing now: %s", " ".join(sys.argv[1:]))
6170
os.execv(sys.argv[1], sys.argv[1:])

0 commit comments

Comments
 (0)