SaltStack源碼分析之Redis Returner

來源:互聯網
上載者:User

標籤:redis   returner   

Redis

/usr/lib/python2.6/site-packages/salt/returners/redis_return.py



需要Minion端安裝Redis的python驅動



# -*- coding: utf-8 -*-‘‘‘Return data to a redis serverTo enable this returner the minion will need the python client for redisinstalled and the following values configured in the minion or masterconfig, these are 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_REDIS = 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))

擷取Redis配置資訊



def returner(ret):    ‘‘‘    Return data to a redis data store    ‘‘‘    serv = _get_serv()    serv.set(‘{0}:{1}‘.format(ret[‘id‘], ret[‘jid‘]), json.dumps(ret))    serv.lpush(‘{0}:{1}‘.format(ret[‘id‘], ret[‘fun‘]), ret[‘jid‘])    serv.sadd(‘minions‘, 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        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()




本文出自 “Linux SA John” 部落格,請務必保留此出處http://john88wang.blog.51cto.com/2165294/1660833

SaltStack源碼分析之Redis Returner

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.