Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
yongpeng1 committed Jun 15, 2019
2 parents eadad2c + b54ea21 commit 3e192b8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 50 deletions.
7 changes: 4 additions & 3 deletions common/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def delete_by_id(self, table, id):

def update_by_id(self, table, data, id):
sql = "UPDATE %s SET data='%s' WHERE data -> '$.id'='%s'" % (table, data.replace("'", r"\'").
replace(r"\n", r'\\n').replace(r'\"', r''), id)
replace(r"\n", r'\\n').replace(r'\"', r'').
replace(r'\\', r'/'), id)
logger.info(sql)
try:
self.cursor.execute(sql)
Expand All @@ -115,9 +116,9 @@ def update_by_id_kv(self, table, k, v, id):
return False, str(e)

def insert(self, table, data):
# 转义'
# 转义
sql = "INSERT INTO %s(data) VALUES('%s') " % (table, data.replace("'", r"\'").replace(r"\n", r'\\n').
replace(r'\"', r''))
replace(r'\"', r'').replace(r'\\', r'/'))
logger.info(sql)
try:
self.cursor.execute(sql)
Expand Down
100 changes: 58 additions & 42 deletions common/saltstack_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,84 +45,96 @@ def list_all_key(self):
params = {'client': 'wheel', 'fun': 'key.list_all'}
content = self.post_request(params)
if isinstance(content, dict):
minions = content['return'][0]['data']['return']
return minions
return content['return'][0]['data']['return']
else:
return {"status": False, "message": "Salt API Error : " + content}

def delete_key(self, node_name):
params = {'client': 'wheel', 'fun': 'key.delete', 'match': node_name}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0]['data']['success']
return ret
return content['return'][0]['data']['success']
else:
return {"status": False, "message": "salt api error : " + content}

def accept_key(self, node_name):
params = {'client': 'wheel', 'fun': 'key.accept', 'match': node_name}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0]['data']['success']
return ret
return content['return'][0]['data']['success']
else:
return {"status": False, "message": "Salt API Error : " + content}

def reject_key(self, node_name):
params = {'client': 'wheel', 'fun': 'key.reject', 'match': node_name}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0]['data']['success']
return ret
return content['return'][0]['data']['success']
else:
return {"status": False, "message": "Salt API Error : " + content}

def remote_noarg_execution(self, tgt, fun):
def remote_noarg_execution(self, tgt, fun, types="tgt_type"):
# Execute commands without parameters
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'expr_form': 'list'}
params = {'client': 'local', 'tgt': tgt, 'fun': fun, types: 'list'}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0][tgt]
# 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
if not ret and types != "expr_form": # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
return self.remote_noarg_execution(tgt, fun, types="expr_form")
return ret
else:
return {"status": False, "message": "Salt API Error : " + content}

def remote_noarg_execution_notgt(self, tgt, fun):
def remote_noarg_execution_notgt(self, tgt, fun, types="tgt_type"):
# Execute commands without parameters
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'expr_form': 'list'}
params = {'client': 'local', 'tgt': tgt, 'fun': fun, types: 'list'}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0]
# 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
if not ret and types != "expr_form": # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
return self.remote_noarg_execution_notgt(tgt, fun, types="expr_form")
return ret
else:
return {"status": False, "message": "Salt API Error : " + content}

def remote_execution(self, tgt, fun, arg):
def remote_execution(self, tgt, fun, arg, types="tgt_type"):
# Command execution with parameters
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'arg': arg, 'expr_form': 'list'}
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'arg': arg, types: 'list'}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0][tgt]
# 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
if not ret and types != "expr_form": # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
return self.remote_execution(tgt, fun, arg, types="expr_form")
return ret
else:
return {"status": False, "message": "Salt API Error : " + content}

def remote_execution_notgt(self, tgt, fun, arg):
def remote_execution_notgt(self, tgt, fun, arg, types="tgt_type"):
# Command execution with parameters
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'arg': arg, 'expr_form': 'list'}
params = {'client': 'local', 'tgt': tgt, 'fun': fun, 'arg': arg, types: 'list'}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0]
# 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
if not ret and types != "expr_form": # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
return self.remote_execution_notgt(tgt, fun, arg, types="expr_form")
return ret
else:
return {"status": False, "message": "Salt API Error : " + content}

def shell_remote_execution(self, tgt, arg):
def shell_remote_execution(self, tgt, arg, types="tgt_type"):
# Shell command execution with parameters
params = {'client': 'local', 'tgt': tgt, 'fun': 'cmd.run', 'arg': arg, 'expr_form': 'list'}
# Changed in version 2017.7.0: Renamed from expr_form to tgt_type
params = {'client': 'local', 'tgt': tgt, 'fun': 'cmd.run', 'arg': arg, types: 'list'}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0]
# 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
if not ret and types != "expr_form": # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
return self.shell_remote_execution(tgt, arg, types="expr_form")
return ret
else:
return {"status": False, "message": "Salt API Error : " + content}
Expand All @@ -132,8 +144,7 @@ def grain(self, tgt, arg):
params = {'client': 'local', 'tgt': tgt, 'fun': 'grains.item', 'arg': arg}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0]
return ret
return content['return'][0]
else:
return {"status": False, "message": "Salt API Error : " + content}

Expand All @@ -142,59 +153,64 @@ def grains(self, tgt):
params = {'client': 'local', 'tgt': tgt, 'fun': 'grains.items'}
content = self.post_request(params)
if isinstance(content, dict):
ret = content['return'][0]
return {"status": True, "message": "", "data": ret}
return {"status": True, "message": "", "data": content['return'][0]}
else:
return {"status": False, "message": "Salt API Error : " + content}

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'}
content = self.post_request(params)
if isinstance(content, dict):
jid = content['return'][0]['jid']
return jid
else:
return {"status": False, "message": "Salt API Error : " + content}
# 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'}
# content = self.post_request(params)
# if isinstance(content, dict):
# jid = content['return'][0]['jid']
# return jid
# else:
# return {"status": False, "message": "Salt API Error : " + content}

def deploy(self, tgt, arg):
# Module deployment
params = {'client': 'local', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg}
content = self.post_request(params)
return content
return self.post_request(params)

def async_deploy(self, tgt, arg):
# Asynchronously send a command to connected minions
params = {'client': 'local_async', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg}
content = self.post_request(params)
if isinstance(content, dict):
jid = content['return'][0]['jid']
return jid
return content['return'][0]['jid']
else:
return {"status": False, "message": "salt api error : " + content}

def target_deploy(self, tgt, arg):
def target_deploy(self, tgt, arg, types="tgt_type"):
# Based on the list forms deployment
params = {'client': 'local', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg, 'expr_form': 'list'}
params = {'client': 'local', 'tgt': tgt, 'fun': 'state.sls', 'arg': arg, types: 'list'}
content = self.post_request(params)
if isinstance(content, dict):
try:
return content.get("return")[0]
ret = content.get("return")[0]
# 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
if not ret and types != "expr_form": # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
return self.target_deploy(tgt, arg, types="expr_form")
return ret
except Exception as e:
return {"status": False, "message": str(e)}
else:
return {"status": False, "message": "Salt API Error : " + content}

def pillar_items(self, tgt, arg=[]):
def pillar_items(self, tgt, arg=[], types="tgt_type"):
# Get pillar item
if arg:
params = {'client': 'local', 'tgt': tgt, 'fun': 'pillar.item', 'arg': arg, 'expr_form': 'list'}
params = {'client': 'local', 'tgt': tgt, 'fun': 'pillar.item', 'arg': arg, types: 'list'}
else:
params = {'client': 'local', 'tgt': tgt, 'fun': 'pillar.items', 'arg': arg, 'expr_form': 'list'}
params = {'client': 'local', 'tgt': tgt, 'fun': 'pillar.items', 'arg': arg, types: 'list'}
content = self.post_request(params)
if isinstance(content, dict):
try:
return content.get("return")[0]
ret = content.get("return")[0]
# 如果返回结果是空,说明salt可能版本小于2017.7.0的版本,使用次方法处理
if not ret and types != "expr_form": # types != "expr_form" 很重要确保最多执行两次,否则肯能发送死循环
return self.pillar_items(tgt, arg=[], types="expr_form")
return ret
except Exception as e:
return {"status": False, "message": str(e)}
else:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ gevent==1.3.4
gunicorn==19.8.1
Flask-Celery-Helper==1.1.0
Flask-APScheduler==1.8.0
SQLAlchemy==1.2.9
SQLAlchemy==1.3.0
pycrypto==2.6.1
16 changes: 12 additions & 4 deletions resources/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ def post(self):
audit_log(user_info["username"], minion_id, args["product_id"], "minion", "shell")

minion_count = str(len(minion_id))
cmd_succeed = str(len(result))
cmd_failure = str(len(minion_id) - len(result))
result_len = len(result)
for k, v in result.items():
if not v:
result_len -= 1
cmd_succeed = str(result_len)
cmd_failure = str(len(minion_id) - result_len)
succeed_minion = []
for i in result:
succeed_minion.append(i)
Expand Down Expand Up @@ -110,8 +114,12 @@ def post(self):
audit_log(user_info["username"], minion_id, args["product_id"], "minion", "sls")

minion_count = str(len(minion_id))
cmd_succeed = str(len(result))
cmd_failure = str(len(minion_id) - len(result))
result_len = len(result)
for k, v in result.items():
if not v:
result_len -= 1
cmd_succeed = str(result_len)
cmd_failure = str(len(minion_id) - result_len)
succeed_minion = []
for i in result:
succeed_minion.append(i)
Expand Down

0 comments on commit 3e192b8

Please sign in to comment.