From 16c9ab6f49f6c2426c41b474da4061eb735ef915 Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Fri, 23 Dec 2016 10:31:35 -0500 Subject: [PATCH] Emulate behaviour of `haproxy-systemd-wrapper`. For reference, see: http://git.haproxy.org/?p=haproxy.git;a=blob;f=src/haproxy-systemd-wrapper.c;h=f6a9c852b277b5f193d6a952119a149b4cfe3e8b;hb=HEAD --- haproxy_wrapper.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ service/haproxy/run | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100755 haproxy_wrapper.py diff --git a/haproxy_wrapper.py b/haproxy_wrapper.py new file mode 100755 index 00000000..b6eee76a --- /dev/null +++ b/haproxy_wrapper.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +import os +import sys +import time + + +def create_haproxy_pipe(): + pipefd = os.pipe() + return pipefd + + +def close_and_swallow(fd): + try: + os.close(fd) + except OSError: + # swallow + pass + + +def wait_on_haproxy_pipe(pipefd): + try: + ret = os.read(pipefd[0], 1) + if len(ret) == 0: + close_and_swallow(pipefd[0]) + close_and_swallow(pipefd[1]) + return False + except OSError as e: + if e.args[0] != errno.EINTR: + close_and_swallow(pipefd[0]) + close_and_swallow(pipefd[1]) + return False + return True + +pipefd = create_haproxy_pipe() + +pid = os.fork() + +if not pid: + os.environ["HAPROXY_WRAPPER_FD"] = str(pipefd[1]) + # Close the read side + os.close(pipefd[0]) + os.execv(sys.argv[1], sys.argv[1:]) + +# Close the write side +os.close(pipefd[1]) +while wait_on_haproxy_pipe(pipefd): + time.sleep(0.01) diff --git a/service/haproxy/run b/service/haproxy/run index aca2290a..b009a681 100755 --- a/service/haproxy/run +++ b/service/haproxy/run @@ -33,7 +33,7 @@ reload() { # Trigger reload LATEST_HAPROXY_PID=$(cat $PIDFILE) - haproxy -p $PIDFILE -f /marathon-lb/haproxy.cfg -D -sf $LATEST_HAPROXY_PID 200>&- + /marathon-lb/haproxy_wrapper.py `which haproxy` -p $PIDFILE -f /marathon-lb/haproxy.cfg -D -sf $LATEST_HAPROXY_PID 200>&- if [ -n "${HAPROXY_RELOAD_SIGTERM_DELAY-}" ]; then sleep $HAPROXY_RELOAD_SIGTERM_DELAY && kill $LATEST_HAPROXY_PID 200>&- 2>/dev/null & fi