forked from d2iq-archive/marathon-lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaproxy_wrapper.py
executable file
·70 lines (58 loc) · 1.84 KB
/
haproxy_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import os
import sys
import time
import errno
import logging
logger = logging.getLogger('haproxy_wrapper')
logger.setLevel(getattr(logging, 'DEBUG'))
formatter = logging.Formatter("%(asctime)-15s %(name)s: %(message)s")
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(formatter)
logger.addHandler(consoleHandler)
def create_haproxy_pipe():
logger.debug("create_haproxy_pipe called")
pipefd = os.pipe()
os.set_inheritable(pipefd[0], True)
os.set_inheritable(pipefd[1], True)
logger.debug("create_haproxy_pipe done")
return pipefd
def close_and_swallow(fd):
logger.debug("close_and_swallow called")
try:
os.close(fd)
logger.debug("close_and_swallow successful")
except OSError as e:
# swallow
logger.debug("close_and_swallow swallow OSError: %s", e)
pass
def wait_on_haproxy_pipe(pipefd):
logger.debug("wait_on_haproxy_pipe called")
try:
ret = os.read(pipefd[0], 1)
if len(ret) == 0:
close_and_swallow(pipefd[0])
close_and_swallow(pipefd[1])
logger.debug("wait_on_haproxy_pipe done (False)")
return False
except OSError as e:
logger.debug("wait_on_haproxy_pipe OSError: %s", e)
if e.args[0] != errno.EINTR:
close_and_swallow(pipefd[0])
close_and_swallow(pipefd[1])
logger.debug("wait_on_haproxy_pipe done (False)")
return False
logger.debug("wait_on_haproxy_pipe done (True)")
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.005)
sys.exit(0)