From 5b33b0f61f3756f646be6ffa64d5aaec004f31b8 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 7 Apr 2021 20:06:22 +0200 Subject: [PATCH] Fix HAProxy draining (#1993) (#2197) * Fix HAProxy draining by manually entering the 'MAINT' state Inspired by rldleblanc: https://github.com/ansible/ansible/issues/37591#issuecomment-610130611 Signed-off-by: Norman Ziegner * Add changelog fragment Signed-off-by: Norman Ziegner * Fix drain function docstring Signed-off-by: Norman Ziegner * Fix typos Signed-off-by: Norman Ziegner * Update changelog fragment Signed-off-by: Norman Ziegner (cherry picked from commit 7145204594a30bfd9dcb087aa72a019baaa22224) Co-authored-by: Norman Ziegner --- .../fragments/1993-haproxy-fix-draining.yml | 3 +++ plugins/modules/net_tools/haproxy.py | 15 ++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/1993-haproxy-fix-draining.yml diff --git a/changelogs/fragments/1993-haproxy-fix-draining.yml b/changelogs/fragments/1993-haproxy-fix-draining.yml new file mode 100644 index 0000000000..fd5c77f573 --- /dev/null +++ b/changelogs/fragments/1993-haproxy-fix-draining.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - haproxy - fix a bug preventing haproxy from properly entering ``DRAIN`` mode (https://github.com/ansible-collections/community.general/issues/1913). diff --git a/plugins/modules/net_tools/haproxy.py b/plugins/modules/net_tools/haproxy.py index 848cc1faeb..8efb59ed2e 100644 --- a/plugins/modules/net_tools/haproxy.py +++ b/plugins/modules/net_tools/haproxy.py @@ -367,10 +367,9 @@ class HAProxy(object): # We can assume there will only be 1 element in state because both svname and pxname are always set when we get here # When using track we get a status like this: MAINT (via pxname/svname) so we need to do substring matching if status in state[0]['status']: - if not self._drain or (state[0]['scur'] == '0' and 'MAINT' in state): + if not self._drain or state[0]['scur'] == '0': return True - else: - time.sleep(self.wait_interval) + time.sleep(self.wait_interval) self.module.fail_json(msg="server %s/%s not status '%s' after %d retries. Aborting." % (pxname, svname, status, self.wait_retries)) @@ -409,15 +408,17 @@ class HAProxy(object): def drain(self, host, backend, status='DRAIN'): """ Drain action, sets the server to DRAIN mode. - In this mode mode, the server will not accept any new connections + In this mode, the server will not accept any new connections other than those that are accepted via persistence. """ haproxy_version = self.discover_version() - # check if haproxy version suppots DRAIN state (starting with 1.5) + # check if haproxy version supports DRAIN state (starting with 1.5) if haproxy_version and (1, 5) <= haproxy_version: cmd = "set server $pxname/$svname state drain" - self.execute_for_backends(cmd, backend, host, status) + self.execute_for_backends(cmd, backend, host, "DRAIN") + if status == "MAINT": + self.disabled(host, backend, self.shutdown_sessions) def act(self): """ @@ -426,7 +427,7 @@ class HAProxy(object): # Get the state before the run self.command_results['state_before'] = self.get_state_for(self.backend, self.host) - # toggle enable/disbale server + # toggle enable/disable server if self.state == 'enabled': self.enabled(self.host, self.backend, self.weight) elif self.state == 'disabled' and self._drain: