[feat] Add salt module to check if mysql is accepting db connections

This commit is contained in:
William Wernert
2020-11-09 12:05:37 -05:00
parent 059c4e03e1
commit 9960cf0592
2 changed files with 40 additions and 0 deletions

35
salt/_modules/mysql.py Normal file
View File

@@ -0,0 +1,35 @@
#!py
from MySQLdb import _mysql
import logging
import time
log = logging.getLogger(__name__)
def status(retry):
mainint = __salt__['pillar.get']('sensor:mainint', __salt__['pillar.get']('manager:mainint'))
mainip = __salt__['grains.get']('ip_interfaces').get(mainint)[0]
mysql_up = False
for i in range(0, retry):
log.debug(f'Connection attempt {i+1}')
try:
_mysql.connect(
host=mainip,
user="root",
passwd=__salt__['pillar.get']('secrets:mysql')
)
mysql_up = True
break
except _mysql.OperationalError as e:
log.debug(e)
except Exception as e:
log.error(e)
break
time.sleep(1)
if not mysql_up:
log.error(f'Could not connect to MySQL server on {mainip} after {retry} attempts.')
return mysql_up

View File

@@ -97,6 +97,11 @@ so-mysql:
- timeout: 900
- onchanges:
- docker_container: so-mysql
module.run:
- mysql.status:
- retry: 900
- onchanges:
- cmd: so-mysql
{% endif %}
{% else %}