Merge "Add wsrep.galera_check module"
diff --git a/_modules/wsrep.py b/_modules/wsrep.py
new file mode 100644
index 0000000..2f7cc2f
--- /dev/null
+++ b/_modules/wsrep.py
@@ -0,0 +1,46 @@
+import logging
+import time
+from salt.exceptions import CommandExecutionError
+
+__copyright__ = "Copyright 2019, Mirantis Inc."
+__license__ = "Apache 2.0"
+
+try:
+    # Trying to import MySQLdb
+    import MySQLdb
+except ImportError:
+    try:
+        # MySQLdb import failed, try to import PyMySQL
+        import pymysql
+        pymysql.install_as_MySQLdb()
+        import MySQLdb
+    except ImportError:
+        MySQLdb = None
+
+def __virtual__():
+    '''
+    Confirm that a python mysql client is installed.
+    '''
+    return bool(MySQLdb), 'No python mysql client installed.' if MySQLdb is None else ''
+
+log = logging.getLogger(__name__)
+
+def wait_for_galera_services(cluster_size=3, retries=18, timeout=10):
+    """
+    Ensure that number of specified nodes with mysql services are alive, otherwise will fail with exception.
+
+    :param timeout:              number of seconds to wait before retries
+    :param retries:              number of retries
+    """
+
+    for i in range(retries):
+        out =  __salt__['mysql.status']()
+
+        if out and int(out['wsrep_cluster_size']) == int(cluster_size) and out['wsrep_evs_state'] == 'OPERATIONAL':
+            break
+        time.sleep(timeout)
+    else:
+        raise CommandExecutionError('Galera cluster is not consistent',
+                                    info={'wsrep_cluster_size': out['wsrep_cluster_size'],
+                                    'wsrep_evs_state': out['wsrep_evs_state']})
+    return True