Redis
/usr/lib/python2.6/site-packages/salt/returners/redis_return.py
Requires Python driver for minion-side installation of Redis
#-*-Coding:utf-8-*-' Return data to a Redis Serverto enable this returner the Minion would need the Python client for Redisinstalled and the following values configured in the Minion or Masterconfig, these is the defaults:redis.db: ' 0 ' Redis.host: ' Salt ' redis.port:6379 to use the Redis returner, append '--return redis ' to the salt command. Ex:salt ' * ' test.ping--return redis '
# import Python libsimport json# import Salt libsimport salt.utils# import Third party libstry:import Redis has_red is = trueexcept Importerror:has_redis = false# Define The module ' s virtual name__virtualname__ = ' REDIS ' def __virtual_ _ (): If not Has_redis:return False return __virtualname__
Def _get_serv (): " return a redis server object ' if ' config.option ' in __salt__: return redis. Redis ( host=__ salt__[' config.option ' (' redis.host '), port=__salt__[' config.option ' (' Redis.port '), db=__salt__[' config.option ' (' redis.db ')) else: cfg = __opts__ return redis. Redis ( host= Cfg.get (' Redis.hosT ', none), port=cfg.get (' Redis.port ', none), db=cfg.get (' redis.db ', none))
Get Redis configuration information
def returner (ret): ' Return data to a Redis data store ' serv = _get_serv () serv.set (' {0}:{1} '. Format (r et[' id '], ret[' Jid ']), Json.dumps (ret)) Serv.lpush (' {0}:{1} '. Format (ret[' id '], ret[' fun '), ret[' Jid ']) serv.sadd (' m Inions ', ret[' id ']) serv.sadd (' Jids ', ret[' Jid ')
def save_load (Jid, load): "Save the load to the specified Jid" "serv = _get_serv () serv.set (Jid, JSON.) Dumps (load)) Serv.sadd (' Jids ', Jid)
Def get_load (Jid): " return the load data that marks a specified jid ' serv = _get_serv () data = serv.get (Jid) if Data: return json.loads (data) Return {}def get_jid (Jid): " Return the information returned when the specified job id was executed " serv = _get_serv () ret = {} for minion in serv.smembers (' Minions '): data = serv.get (' {0}:{1} '. Format (Minion, jid)) if data: ret[minion] = Json.loads (data) return retdef get_fun (fun): " Return a dict of the last function called for all minions " serv = _get_serv () ret = {} for minion in serv.smembers (' Minions '): ind_str = ' {0}:{1} '. Format (minion, fun) try: jid = serv.lindex (ind_str, 0) except Exception: continue &Nbsp;data = serv.get (' {0}:{1} '. Format (Minion, jid)) if data: ret[minion] = json.loads (data) return retdef get_jids (): " Return a list of all job ids " serv = _get_serv () return list (Serv.smembers (' Jids ') def get_minions (): " Return a list of minions " serv = _get_serv () return list (Serv.smembers (' Minions ')) Def prep_jid (Nocache, passed_jid=none): # pylint: disable=unused-argument ' Do Any work necessary to prepare a jid, including sending a custom id ' return passed_jid if passed_jid is not None else salt.utils.gen_jid ()
This article is from the Linux SA John blog, so be sure to keep this source http://john88wang.blog.51cto.com/2165294/1660833
Saltstack source code Analysis of Redis returner