标签:salt-api、django、cmd.run
一、介绍
凡是使用saltstack都是在master的处事器上直接命令操纵,这个对付运维人员来说不是什么大事,但是也会有堕落的时候,而一旦堕落,就会有不成挽回的后果。
二、框架
这里使用django框架,通过对salt-api的封装,传入命令,执行api,将功效返回到页面上显示。注意:为了防备误操纵,我们对传入的命令进行了查抄,所有被界说的危险命令将不会被执行。(我这里为了简单,所以界说了可以被执行的命令。),前端使用了jquery+ajax的方法来不刷新页面就将功效显示在页面上的方法。
三、salt-api的安置
网上教程很多,我这里就不再空话了。
四、django代码
1)、整体布局
2)、salt_api.py(这里参照了github上dzhops的代码)
# -*- coding: utf-8 -*- import urllib2, urllib, json import requests import json import ssl ssl._create_default_https_context = ssl._create_unverified_context class SaltAPI(object): def __init__(self, url, username, password): self.__url = url.rstrip(‘/‘) self.__user = username self.__password = password self.__token_id = self.saltLogin() def saltLogin(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 masterToMinionContent(self, tgt, fun, arg): ‘‘‘ Master控制Minion,返回的功效是内容,,不是jid; 方针参数tgt是一个如下格局的字符串:‘*‘ 或 ‘zhaogb-201‘ ‘‘‘ if tgt == ‘*‘: params = {‘client‘: ‘local‘, ‘tgt‘: tgt, ‘fun‘: fun, ‘arg‘: arg} else: params = {‘client‘: ‘local‘, ‘tgt‘: tgt, ‘fun‘: fun, ‘arg‘: arg, ‘expr_form‘: ‘list‘} obj = urllib.urlencode(params) content = self.postRequest(obj) result = content[‘return‘][0] return result def allMinionKeys(self): ‘‘‘ 返回所有Minion keys; 分袂为 已接受、待接受、已拒绝; :return: [u‘local‘, u‘minions_rejected‘, u‘minions_denied‘, u‘minions_pre‘, u‘minions‘] ‘‘‘ 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‘] minions_rej = content[‘return‘][0][‘data‘][‘return‘][‘minions_rejected‘] # return minions, minions_pre, minions_rej return minions def actionKyes(self, keystrings, action): ‘‘‘ 对Minion keys 进行指定措置惩罚惩罚; :param keystrings: 将要措置惩罚惩罚的minion id字符串; :param action: 将要进行的措置惩罚惩罚,如接受、拒绝、删除; :return: {"return": [{"tag": "salt/wheel/20160322171740805129", "data": {"jid": "20160322171740805129", "return": {}, "success": true, "_stamp": "2016-03-22T09:17:40.899757", "tag": "salt/wheel/20160322171740805129", "user": "zhaogb", "fun": "wheel.key.delete"}}]} ‘‘‘ func = ‘key.‘ + action params = {‘client‘: ‘wheel‘, ‘fun‘: func, ‘match‘: keystrings} obj = urllib.urlencode(params) content = self.postRequest(obj) ret = content[‘return‘][0][‘data‘][‘success‘] return ret def acceptKeys(self, keystrings): ‘‘‘ 接受Minion发过来的key; :return: ‘‘‘ params = {‘client‘: ‘wheel‘, ‘fun‘: ‘key.accept‘, ‘match‘: keystrings} obj = urllib.urlencode(params) content = self.postRequest(obj) ret = content[‘return‘][0][‘data‘][‘success‘] return ret def deleteKeys(self, keystrings): ‘‘‘ 删除Minion keys; :param node_name: :return: ‘‘‘ params = {‘client‘: ‘wheel‘, ‘fun‘: ‘key.delete‘, ‘match‘: keystrings} obj = urllib.urlencode(params) content = self.postRequest(obj) ret = content[‘return‘][0][‘data‘][‘success‘] return ret