Oleksandr Bryndzii | 3a7b50d | 2019-06-13 11:59:00 +0300 | [diff] [blame] | 1 | import logging |
| 2 | import time |
| 3 | from salt.exceptions import CommandExecutionError |
| 4 | |
| 5 | __copyright__ = "Copyright 2019, Mirantis Inc." |
| 6 | __license__ = "Apache 2.0" |
| 7 | |
| 8 | try: |
| 9 | # Trying to import MySQLdb |
| 10 | import MySQLdb |
| 11 | except 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 | |
| 20 | def __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 | |
| 26 | log = logging.getLogger(__name__) |
| 27 | |
| 28 | def 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 |