Salt-api templates

Source: Internet
Author: User
Tags sapi urlencode

Tag: Salt

# -*- coding: utf-8 -*-from django.http import httpresponsefrom  Django.http import httpresponseredirectfrom django.shortcuts import render_to_ responseimport urllib2,urllibimport timeimport retry:    import  jsonexcept 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 =&Nbsp;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 ': &nbSP; ' 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)     &nbsP;   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&nBSP; ""         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 jiddef 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 ()


This article is from the "Learn Linux" blog, so be sure to keep this source http://10265013.blog.51cto.com/10255013/1897079

Salt-api templates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.