码迷,mamicode.com
首页 > Windows程序 > 详细

salt-api 模板

时间:2017-02-13 00:37:14      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:salt

# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
import urllib2,urllib
import time
import re

try:
    import json
except ImportError:
    import simplejson as json

#class SaltAPI(object):
#    __token_id = ‘‘
#    def __init__(self,url,username,password):
#        self.__url = url.rstrip(‘/‘)
#        self.__user = username
#        self.__password = password
#
#    def token_id(self):
#        ‘‘‘ user login and get token id ‘‘‘
#        params = {‘eauth‘: ‘pam‘, ‘username‘: self.__user, ‘password‘: self.__password}
#        encode = urllib.urlencode(params)
#        obj = urllib.unquote(encode)
#        content = self.postRequest(obj,prefix=‘/login‘)
#	try:
#            self.__token_id = content[‘return‘][0][‘token‘]
#        except KeyError:
#            raise KeyError
#tgt = ‘computer‘
class SaltAPI:
    def __init__(self,url,username,password):
        self.__url = url.rstrip(‘/‘)
        self.__user =  username
        self.__password = password
        self.__token_id = self.token_id()
    def token_id(self):
        params = {‘eauth‘: ‘pam‘, ‘username‘: self.__user, ‘password‘: self.__password}
        encode = urllib.urlencode(params)
        obj = urllib.unquote(encode)
        headers = {‘X-Auth-Token‘:‘‘}
        url = self.__url + ‘/login‘
        req = urllib2.Request(url, obj, headers)
        opener = urllib2.urlopen(req)
        content = json.loads(opener.read())
        try:
            token = content[‘return‘][0][‘token‘]
            return token
        except KeyError:
            raise KeyError

    def postRequest(self,obj,prefix=‘/‘):
        url = self.__url + prefix
        headers = {‘X-Auth-Token‘   : self.__token_id}
        req = urllib2.Request(url, obj, headers)
        opener = urllib2.urlopen(req)
        content = json.loads(opener.read())
        return content

    def list_all_key(self):
        params = {‘client‘: ‘wheel‘, ‘fun‘: ‘key.list_all‘}
        obj = urllib.urlencode(params)
        self.token_id()
        content = self.postRequest(obj)
        minions = content[‘return‘][0][‘data‘][‘return‘][‘minions‘]
        minions_pre = content[‘return‘][0][‘data‘][‘return‘][‘minions_pre‘]
        return minions,minions_pre

    def delete_key(self,node_name):
        params = {‘client‘: ‘wheel‘, ‘fun‘: ‘key.delete‘, ‘match‘: node_name}
        obj = urllib.urlencode(params)
        self.token_id()
        content = self.postRequest(obj)
        ret = content[‘return‘][0][‘data‘][‘success‘]
        return ret

    def accept_key(self,node_name):
        params = {‘client‘: ‘wheel‘, ‘fun‘: ‘key.accept‘, ‘match‘: node_name}
        obj = urllib.urlencode(params)
        self.token_id()
        content = self.postRequest(obj)
        ret = content[‘return‘][0][‘data‘][‘success‘]
        return ret

    def remote_noarg_execution(self,tgt,fun):
        ‘‘‘ Execute commands without parameters ‘‘‘
        params = {‘client‘: ‘local‘, ‘tgt‘: tgt, ‘fun‘: fun}
    #    return HttpResponse(params)
        obj = urllib.urlencode(params)
        self.token_id()
        content = self.postRequest(obj)
        ret = content[‘return‘][0][tgt]
        return ret

    def remote_execution(self,tgt,fun,arg):
        ‘‘‘ Command execution with parameters ‘‘‘        
        params = {‘client‘: ‘local‘, ‘tgt‘: tgt, ‘fun‘: fun, ‘arg‘: arg}
        obj = urllib.urlencode(params)
        self.token_id()
        content = self.postRequest(obj)
        ret = content[‘return‘][0][tgt]
        return ret

    def target_remote_execution(self,tgt,fun,arg):
        ‘‘‘ Use targeting for remote execution ‘‘‘
        params = {‘client‘: ‘local‘, ‘tgt‘: tgt, ‘fun‘: fun, ‘arg‘: arg, ‘expr_form‘: ‘nodegroup‘}
        obj = urllib.urlencode(params)
        self.token_id()
        content = self.postRequest(obj)
        jid = content[‘return‘][0][‘jid‘]
        return jid

    def deploy(self,tgt,arg):
        ‘‘‘ Module deployment ‘‘‘
        params = {‘client‘: ‘local‘, ‘tgt‘: tgt, ‘fun‘: ‘state.sls‘, ‘arg‘: arg}
        obj = urllib.urlencode(params)
        self.token_id()
        content = self.postRequest(obj)
        return content

    def async_deploy(self,tgt,arg):
        ‘‘‘ Asynchronously send a command to connected minions ‘‘‘
        params = {‘client‘: ‘local_async‘, ‘tgt‘: tgt, ‘fun‘: ‘state.sls‘, ‘arg‘: arg}
        obj = urllib.urlencode(params)
        self.token_id()
        content = self.postRequest(obj)
        jid = content[‘return‘][0][‘jid‘]
        return jid

    def target_deploy(self,tgt,arg):
        ‘‘‘ Based on the node group forms deployment ‘‘‘
        params = {‘client‘: ‘local_async‘, ‘tgt‘: tgt, ‘fun‘: ‘state.sls‘, ‘arg‘: arg, ‘expr_form‘: ‘nodegroup‘}
        obj = urllib.urlencode(params)
        self.token_id()
        content = self.postRequest(obj)
        jid = content[‘return‘][0][‘jid‘]
        return jid

def main():
    sapi = SaltAPI(url=‘https://113.31.43.140:8888‘,username=‘saltapi‘,password=‘yao‘)
    ad = sapi.remote_noarg_execution(‘computer‘,‘grains.items‘)
    ipv4 =  ad["ipv4"]
    i = len(ipv4)
    for a in range(0, i):
        if ipv4[a][0:3] == "113":
           print ipv4[a]
    #sapi.token_id()
    #print sapi.list_all_key()
    #sapi.delete_key(‘test-01‘)
#    print sapi.accept_key()
    #sapi.deploy(‘test-01‘,‘nginx‘)
    #print sapi.remote_noarg_execution(‘test-01‘,‘grains.items‘)

if __name__ == ‘__main__‘:
    main()


本文出自 “学习linux” 博客,请务必保留此出处http://10265013.blog.51cto.com/10255013/1897079

salt-api 模板

标签:salt

原文地址:http://10265013.blog.51cto.com/10255013/1897079

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!