From 22b7de819cd4a603eb44b78bce7f54c84eeb127b Mon Sep 17 00:00:00 2001 From: William Wernert Date: Tue, 10 Nov 2020 10:00:21 -0500 Subject: [PATCH] [fix] Put mysql import in try,catch in case it hasn't been installed --- salt/_modules/so.py | 57 ++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/salt/_modules/so.py b/salt/_modules/so.py index 43ffac250..de337c43f 100644 --- a/salt/_modules/so.py +++ b/salt/_modules/so.py @@ -3,36 +3,41 @@ import logging def status(): - return __salt__['cmd.run']('/usr/sbin/so-status') + return __salt__['cmd.run']('/usr/sbin/so-status') def mysql_conn(retry): - from MySQLdb import _mysql - import time + log = logging.getLogger(__name__) - log = logging.getLogger(__name__) - mainint = __salt__['pillar.get']('sensor:mainint', __salt__['pillar.get']('manager:mainint')) - mainip = __salt__['grains.get']('ip_interfaces').get(mainint)[0] + try: + from MySQLdb import _mysql + except ImportError as e: + log.error(e) + return False + from time import sleep - 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) + mainint = __salt__['pillar.get']('sensor:mainint', __salt__['pillar.get']('manager:mainint')) + mainip = __salt__['grains.get']('ip_interfaces').get(mainint)[0] - if not mysql_up: - log.error(f'Could not connect to MySQL server on {mainip} after {retry} attempts.') + 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 + sleep(1) - return mysql_up \ No newline at end of file + if not mysql_up: + log.error(f'Could not connect to MySQL server on {mainip} after {retry} attempts.') + + return mysql_up \ No newline at end of file