diff --git a/salt/modules/haproxyconn.py b/salt/modules/haproxyconn.py index 5ac6e12920..29d68e1229 100644 --- a/salt/modules/haproxyconn.py +++ b/salt/modules/haproxyconn.py @@ -12,6 +12,7 @@ from __future__ import absolute_import import stat import os import logging +import time try: import haproxy.cmds @@ -73,6 +74,38 @@ def list_servers(backend, socket=DEFAULT_SOCKET_URL, objectify=False): return ha_conn.sendCmd(ha_cmd, objectify=objectify) +def wait_state(backend, server, value='up', timeout=60*5, socket=DEFAULT_SOCKET_URL): + ''' + + Wait for a specific server state + + backend + haproxy backend + + server + targeted server + + value + state value + + timeout + timeout before giving up state value, default 5 min + + socket + haproxy stats socket + + CLI Example: + + .. code-block:: bash + + salt '*' haproxy.wait_state mysql server01 up 60 + ''' + t = time.time() + timeout + while time.time() < t: + if get_backend(backend=backend, socket=socket)[server]["status"].lower() == value.lower(): + return True + return False + def get_backend(backend, socket=DEFAULT_SOCKET_URL): ''' diff --git a/tests/unit/modules/test_haproxyconn.py b/tests/unit/modules/test_haproxyconn.py index 724549691e..61a0ade9b8 100644 --- a/tests/unit/modules/test_haproxyconn.py +++ b/tests/unit/modules/test_haproxyconn.py @@ -203,3 +203,15 @@ class HaproxyConnTestCase(TestCase, LoaderModuleMockMixin): } } self.assertDictEqual(haproxyconn.get_backend('test'),expected_data) + + def test_wait_state_true(self,mock): + ''' + Test a successful wait for state + ''' + self.assertTrue(haproxyconn.wait_state('test','server01')) + + def test_wait_state_false(self,mock): + ''' + Test a failed wait for state, with a timeout of 0 + ''' + self.assertFalse(haproxyconn.wait_state('test','server02','up', 0))