Added wait_state function in haproxy module

This commit is contained in:
Per-Gustaf Stenberg 2017-04-16 23:13:33 +02:00
parent 0120e32cfb
commit bcd88b0fb2
2 changed files with 45 additions and 0 deletions

View File

@ -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):
'''

View File

@ -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))