blob: 2f7cc2f50af19a5117f63a330e16b6eb890dbf31 [file] [log] [blame]
Oleksandr Bryndzii3a7b50d2019-06-13 11:59:00 +03001import logging
2import time
3from salt.exceptions import CommandExecutionError
4
5__copyright__ = "Copyright 2019, Mirantis Inc."
6__license__ = "Apache 2.0"
7
8try:
9 # Trying to import MySQLdb
10 import MySQLdb
11except ImportError:
12 try:
13 # MySQLdb import failed, try to import PyMySQL
14 import pymysql
15 pymysql.install_as_MySQLdb()
16 import MySQLdb
17 except ImportError:
18 MySQLdb = None
19
20def __virtual__():
21 '''
22 Confirm that a python mysql client is installed.
23 '''
24 return bool(MySQLdb), 'No python mysql client installed.' if MySQLdb is None else ''
25
26log = logging.getLogger(__name__)
27
28def wait_for_galera_services(cluster_size=3, retries=18, timeout=10):
29 """
30 Ensure that number of specified nodes with mysql services are alive, otherwise will fail with exception.
31
32 :param timeout: number of seconds to wait before retries
33 :param retries: number of retries
34 """
35
36 for i in range(retries):
37 out = __salt__['mysql.status']()
38
39 if out and int(out['wsrep_cluster_size']) == int(cluster_size) and out['wsrep_evs_state'] == 'OPERATIONAL':
40 break
41 time.sleep(timeout)
42 else:
43 raise CommandExecutionError('Galera cluster is not consistent',
44 info={'wsrep_cluster_size': out['wsrep_cluster_size'],
45 'wsrep_evs_state': out['wsrep_evs_state']})
46 return True