From 3ae8bb6c4a1f6bec3490115c2a8dd7b948f9b067 Mon Sep 17 00:00:00 2001 From: yongpeng1 Date: Sat, 15 Jun 2019 11:36:39 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E5=8D=87?= =?UTF-8?q?=E7=BA=A7SQLAlchemy=E5=88=B01.3.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index cd64cba..39a92ce 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 From b32f3154a41f39cd5d912c1b8d0c0aa99bb14f4b Mon Sep 17 00:00:00 2001 From: yongpeng1 Date: Sat, 15 Jun 2019 11:37:28 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E6=94=AF?= =?UTF-8?q?=E6=8C=812019=E4=BB=A5=E5=90=8E=E7=9A=84salt=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/saltstack_api.py | 100 +++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/common/saltstack_api.py b/common/saltstack_api.py index ee6e1d7..69c46dd 100644 --- a/common/saltstack_api.py +++ b/common/saltstack_api.py @@ -45,8 +45,7 @@ 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} @@ -54,8 +53,7 @@ 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} @@ -63,8 +61,7 @@ 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} @@ -72,57 +69,72 @@ 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} @@ -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} @@ -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: From 4d7bb290c51641c38b44e69a881bf1e8ca8db0a4 Mon Sep 17 00:00:00 2001 From: yongpeng1 Date: Sat, 15 Jun 2019 11:38:23 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=89=A7=E8=A1=8Cminion=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/execute.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/resources/execute.py b/resources/execute.py index d31f711..33f4596 100644 --- a/resources/execute.py +++ b/resources/execute.py @@ -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) @@ -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) From b54ea21bfe00343832adef313cabaf77ffd133de Mon Sep 17 00:00:00 2001 From: yongpeng1 Date: Sat, 15 Jun 2019 14:05:27 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9Awindows=20grains?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/db.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/common/db.py b/common/db.py index 4196a40..f6ae44e 100644 --- a/common/db.py +++ b/common/db.py @@ -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) @@ -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)