Created
May 31, 2019 10:38
-
-
Save pylixm/9b8d9549ba686b5be93300af68ae0a12 to your computer and use it in GitHub Desktop.
SaltApi.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import urllib, urllib2 | |
| import time | |
| import ssl | |
| ssl._create_default_https_context = ssl._create_unverified_context | |
| try: | |
| import json | |
| except ImportError: | |
| import simplejson as json | |
| class SaltAPI(object): | |
| def __init__(self, url, username, password): | |
| self.__url = url.rstrip('/') | |
| self.__user = username | |
| self.__password = password | |
| self.__token_id = self.get_token() | |
| def get_token(self): | |
| ''' user login and get token id ''' | |
| params = { | |
| 'eauth': 'pam', | |
| 'username': self.__user, | |
| 'password': self.__password, | |
| } | |
| encode = urllib.urlencode(params) | |
| obj = urllib.unquote(encode) | |
| url = self.__url + '/login' | |
| req = urllib2.Request(url, obj) | |
| 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='/'): | |
| self.__token_id = self.get_token() | |
| 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) | |
| 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) | |
| 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) | |
| 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, | |
| } | |
| obj = urllib.urlencode(params) | |
| content = self.postRequest(obj) | |
| return content | |
| def remote_execution(self, tgt, fun, arg): | |
| ''' Command execution with parameters ''' | |
| params = { | |
| 'client': 'local', | |
| 'tgt': tgt, | |
| 'fun': fun, | |
| 'arg': arg, | |
| } | |
| obj = urllib.urlencode(params) | |
| content = self.postRequest(obj) | |
| return content['return'] | |
| # list to execute | |
| def list_remote_execution(self, tgt, fun, arg): | |
| ''' Command execution with parameters ''' | |
| """ | |
| 'w168-221-239.lq.autohome.cc', 'cmd.run', 'hostname' | |
| """ | |
| params = { | |
| 'client': 'local', | |
| 'tgt': tgt, | |
| 'fun': fun, | |
| 'arg': arg, | |
| 'expr_form': 'list', | |
| } | |
| obj = urllib.urlencode(params) | |
| content = self.postRequest(obj) | |
| return content | |
| def async_list_remote_execution(self, tgt, fun, arg): | |
| ''' Command execution with parameters ''' | |
| params = { | |
| 'client': 'local_async', | |
| 'tgt': tgt, | |
| 'fun': 'cmd.run', | |
| 'arg': arg, | |
| 'expr_form': 'list', | |
| } | |
| obj = urllib.urlencode(params) | |
| content = self.postRequest(obj) | |
| return content['return'] | |
| 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) | |
| 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) | |
| 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) | |
| 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) | |
| content = self.postRequest(obj) | |
| jid = content['return'][0]['jid'] | |
| return jid | |
| if __name__ == '__main__': | |
| sapi = SaltAPI(url='http://salt-api-host', username='salt', password='xxx') | |
| print sapi.list_remote_execution('salt-minion-id', 'cmd.run', 'hostname') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment