[root@test81 _returners]# cat mysql_return.py
from contextlib import contextmanager
import sys
import json
import logging
try:
import MySQLdb
HAS_MYSQL = True
except ImportError:
HAS_MYSQL = False
log = logging.getLogger(__name__)
def __virtual__():
if not HAS_MYSQL:
return False
return 'test_mysql'
def _get_options():
'''
Returns options used for the MySQL connection.
'''
defaults = {'host': '192.168.2.100',
'user': 'salt',
'pass': 'salt',
'db': 'salt',
'port': 3306}
_options = {}
for attr in defaults:
_attr = __salt__['config.option']('mysql.{0}'.format(attr))
if not _attr:
log.debug('Using default for MySQL {0}'.format(attr))
_options[attr] = defaults[attr]
continue
_options[attr] = _attr
return _options
def returner(ret):
'''
Return data to a mysql server
'''
with _get_serv(commit=True) as cur:
sql = '''INSERT INTO `salt_returns`
(`fun`, `jid`, `return`, `id`, `success`, `full_ret` )
VALUES (%s, %s, %s, %s, %s, %s)'''
cur.execute(sql, (ret['fun'], ret['jid'],
str(ret['return']), ret['id'],
ret['success'], json.dumps(ret)))