From 22456e57374ca67808438ca660c56b5c6304c654 Mon Sep 17 00:00:00 2001 From: tifayuki Date: Mon, 12 Jun 2017 10:49:56 -0700 Subject: [PATCH 1/6] add the missing import module --- haproxy/helper/update_helper.py | 1 + 1 file changed, 1 insertion(+) diff --git a/haproxy/helper/update_helper.py b/haproxy/helper/update_helper.py index 83a3d14..085a6c0 100644 --- a/haproxy/helper/update_helper.py +++ b/haproxy/helper/update_helper.py @@ -2,6 +2,7 @@ import subprocess import threading import time +import errno from haproxy.config import HAPROXY_RUN_COMMAND, RELOAD_TIMEOUT From aa153a12c49db26bc36c85bd64a6e028ddfc7f02 Mon Sep 17 00:00:00 2001 From: Mark van Rossum Date: Tue, 13 Jun 2017 12:23:47 +0100 Subject: [PATCH 2/6] Updated alpine to 3.6 (this is haproxy 1.7.5) --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c85f368..84960f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:3.4 +FROM alpine:3.6 MAINTAINER Feng Honglin COPY . /haproxy-src From 1a77f7ceab8de1f7128e030d1330ab31a448749c Mon Sep 17 00:00:00 2001 From: Mark van Rossum Date: Tue, 13 Jun 2017 12:24:21 +0100 Subject: [PATCH 3/6] Add config check before reload Fix tests --- haproxy/config.py | 1 + haproxy/helper/update_helper.py | 12 +++++++++++- tests/unit/helper/test_update_helper.py | 10 +++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/haproxy/config.py b/haproxy/config.py index 67af435..6986997 100644 --- a/haproxy/config.py +++ b/haproxy/config.py @@ -129,6 +129,7 @@ def parse_additional_backend_settings(envvars): CACERT_DIR = "/cacerts/" HAPROXY_CONFIG_FILE = "/haproxy.cfg" HAPROXY_RUN_COMMAND = ['/usr/sbin/haproxy', '-f', HAPROXY_CONFIG_FILE, '-db', '-q'] +HAPROXY_CONFIG_CHECK_COMMAND = ['/usr/sbin/haproxy', '-c', '-f', HAPROXY_CONFIG_FILE] API_RETRY = 10 # seconds PID_FILE = "/tmp/dockercloud-haproxy.pid" SERVICE_PORTS_ENVVAR_NAME = "SERVICE_PORTS" diff --git a/haproxy/helper/update_helper.py b/haproxy/helper/update_helper.py index 085a6c0..655dcbc 100644 --- a/haproxy/helper/update_helper.py +++ b/haproxy/helper/update_helper.py @@ -4,7 +4,7 @@ import time import errno -from haproxy.config import HAPROXY_RUN_COMMAND, RELOAD_TIMEOUT +from haproxy.config import HAPROXY_RUN_COMMAND, RELOAD_TIMEOUT, HAPROXY_CONFIG_CHECK_COMMAND logger = logging.getLogger("haproxy") @@ -22,6 +22,16 @@ # def run_reload(old_process, timeout=int(RELOAD_TIMEOUT)): if old_process: + p = subprocess.Popen(HAPROXY_CONFIG_CHECK_COMMAND, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output, err = p.communicate() + return_code = p.returncode + if (return_code != 0): + logger.error("Config check failed. NOT reloading haproxy") + logger.error(output) + logger.error(err) + return + else: + logger.info("Config check passed") # Reload haproxy logger.info("Reloading HAProxy") if timeout == -1: diff --git a/tests/unit/helper/test_update_helper.py b/tests/unit/helper/test_update_helper.py index 5360229..a3924a8 100644 --- a/tests/unit/helper/test_update_helper.py +++ b/tests/unit/helper/test_update_helper.py @@ -24,11 +24,19 @@ def poll(self): def terminate(self): self.terminated = True + + def communicate(self): + return ("", "") pass class Object(object): - pass + returncode = 0 + + def communicate(self): + return ("", "") + + pass @mock.patch("haproxy.helper.update_helper.subprocess.Popen") def test_run_graceful_reload_within_timeout(self, mock_popen): From 12bb35a504defec074e68a91e70cb7de865abacf Mon Sep 17 00:00:00 2001 From: tifayuki Date: Tue, 13 Jun 2017 10:42:39 -0700 Subject: [PATCH 4/6] reformat the code --- haproxy/helper/update_helper.py | 15 ++++++++------- tests/unit/helper/test_update_helper.py | 14 +++++++------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/haproxy/helper/update_helper.py b/haproxy/helper/update_helper.py index 655dcbc..f27cc3c 100644 --- a/haproxy/helper/update_helper.py +++ b/haproxy/helper/update_helper.py @@ -1,8 +1,8 @@ +import errno import logging import subprocess import threading import time -import errno from haproxy.config import HAPROXY_RUN_COMMAND, RELOAD_TIMEOUT, HAPROXY_CONFIG_CHECK_COMMAND @@ -22,16 +22,17 @@ # def run_reload(old_process, timeout=int(RELOAD_TIMEOUT)): if old_process: - p = subprocess.Popen(HAPROXY_CONFIG_CHECK_COMMAND, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = subprocess.Popen(HAPROXY_CONFIG_CHECK_COMMAND, stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) output, err = p.communicate() return_code = p.returncode if (return_code != 0): - logger.error("Config check failed. NOT reloading haproxy") - logger.error(output) - logger.error(err) - return + logger.error("Config check failed. NOT reloading haproxy") + logger.error(output) + logger.error(err) + return else: - logger.info("Config check passed") + logger.info("Config check passed") # Reload haproxy logger.info("Reloading HAProxy") if timeout == -1: diff --git a/tests/unit/helper/test_update_helper.py b/tests/unit/helper/test_update_helper.py index a3924a8..39578e1 100644 --- a/tests/unit/helper/test_update_helper.py +++ b/tests/unit/helper/test_update_helper.py @@ -24,19 +24,19 @@ def poll(self): def terminate(self): self.terminated = True - + def communicate(self): return ("", "") pass class Object(object): - returncode = 0 - - def communicate(self): - return ("", "") - - pass + returncode = 0 + + def communicate(self): + return ("", "") + + pass @mock.patch("haproxy.helper.update_helper.subprocess.Popen") def test_run_graceful_reload_within_timeout(self, mock_popen): From 828e049905666fad0d91d3f7dac26cec9e870453 Mon Sep 17 00:00:00 2001 From: tifayuki Date: Tue, 13 Jun 2017 11:33:51 -0700 Subject: [PATCH 5/6] bump ver --- haproxy/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/haproxy/__init__.py b/haproxy/__init__.py index 3b4d1c1..64206c4 100644 --- a/haproxy/__init__.py +++ b/haproxy/__init__.py @@ -1 +1 @@ -__version__ = "1.6.6" +__version__ = "1.6.7" From 9eb0dbdd9a23557e3110c9d5a28e24ca13103398 Mon Sep 17 00:00:00 2001 From: tifayuki Date: Tue, 13 Jun 2017 17:33:48 -0700 Subject: [PATCH 6/6] when conf check failed, run_haproxy should return the old pid instead of empty string --- haproxy/helper/update_helper.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/haproxy/helper/update_helper.py b/haproxy/helper/update_helper.py index f27cc3c..348a7ab 100644 --- a/haproxy/helper/update_helper.py +++ b/haproxy/helper/update_helper.py @@ -22,18 +22,17 @@ # def run_reload(old_process, timeout=int(RELOAD_TIMEOUT)): if old_process: + # Config check p = subprocess.Popen(HAPROXY_CONFIG_CHECK_COMMAND, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, err = p.communicate() - return_code = p.returncode - if (return_code != 0): - logger.error("Config check failed. NOT reloading haproxy") - logger.error(output) - logger.error(err) - return + if p.returncode != 0: + logger.error("Config check failed. NOT reloading haproxy: %s - %s" % (err, output)) + return old_process else: logger.info("Config check passed") - # Reload haproxy + + # Reload Haproxy logger.info("Reloading HAProxy") if timeout == -1: flag = "-st"