From 15ed79245c79898a425cb976351af0b3167dab5e Mon Sep 17 00:00:00 2001 From: RogerWork Date: Thu, 1 Feb 2024 17:47:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90websocket=E5=8F=91=E9=80=81?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- update/consumers.py | 16 +++++----- update/views.py | 71 +++++++++++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/update/consumers.py b/update/consumers.py index d5c18b6..590be04 100644 --- a/update/consumers.py +++ b/update/consumers.py @@ -1,5 +1,6 @@ -from channels.generic.websocket import WebsocketConsumer import json +from update.views import cinema_update_status +from channels.generic.websocket import WebsocketConsumer class UpdateConsumer(WebsocketConsumer): @@ -10,12 +11,9 @@ class UpdateConsumer(WebsocketConsumer): def receive(self, text_data=None, bytes_data=None): if text_data is None: self.send('你发了啥') - if text_data == 'update': - self.send(json.dumps({'code': '111'})) - if text_data == 'close': - self.send('白白') - self.close() - self.disconnect('500') else: - self.send(f'你收到的消息是{text_data}') - + if text_data.startswith('ip:'): + ip = text_data.replace('ip:', '') + self.send(json.dumps(cinema_update_status.get(ip, None))) + else: + self.send(text_data) diff --git a/update/views.py b/update/views.py index 6bccd12..3d782a1 100644 --- a/update/views.py +++ b/update/views.py @@ -1,4 +1,5 @@ import json +import copy from django.http import JsonResponse from django.db.models import Q @@ -21,6 +22,7 @@ from update.utils.git_util import GitUtil, GitDbUtil from update.utils.cmd_extcute import UpdateCommandUtil, UpdateConfigUtil from update.utils.db_compare import DbCompare from update.utils.client_util import ClientUtil +# from consumers import UpdateConsumer # CacheResponseMixin 一定要放第一位 @@ -90,6 +92,9 @@ def get_git_version(request): return JsonResponse(serializer.data, safe=False) +cinema_update_status = dict() + + def update_cine(request): req = request.GET.dict() cinema_ip = req.get('ip') @@ -113,6 +118,8 @@ def update_cine(request): 'client': {'result': '', 'msg': ''}, } + cinema_update_status[cinema_ip] = copy.deepcopy(update_status) + short_release = req.get('version') cmd_list = json.loads(req.get('cmd')) print(cinema_ip, short_release, cmd_list) @@ -124,81 +131,87 @@ def update_cine(request): # 获取并写入cine.sql print('获取并写入cine.sql') try: + cinema_update_status[cinema_ip]['git']['result'] = 'running' git_util = GitUtil(short_release) git_output = git_util.handle_create_cine() - update_status['git']['result'] = 'success' - update_status['git']['msg'] = git_output + cinema_update_status[cinema_ip]['git']['result'] = 'success' + cinema_update_status[cinema_ip]['git']['msg'] = git_output except Exception as e: print('git', e) - update_status['git']['result'] = 'fail' - update_status['git']['msg'] = str(e) + cinema_update_status[cinema_ip]['git']['result'] = 'fail' + cinema_update_status[cinema_ip]['git']['msg'] = str(e) # 执行setup print('执行setup') try: + cinema_update_status[cinema_ip]['setup']['result'] = 'running' setup_list = list(set(cmd_list) & set(cmd_dict['setup'])) update_cmd = UpdateCommandUtil() setup_output = update_cmd.exec_cmd_by_type(cinema_ip, 'setup', setup_list, short_release) - update_status['setup']['result'] = 'success' - update_status['setup']['msg'] = setup_output + cinema_update_status[cinema_ip]['setup']['result'] = 'success' + cinema_update_status[cinema_ip]['setup']['msg'] = setup_output except Exception as e: print('setup', e) - update_status['setup']['result'] = 'fail' - update_status['setup']['msg'] = str(e) + cinema_update_status[cinema_ip]['setup']['result'] = 'fail' + cinema_update_status[cinema_ip]['setup']['msg'] = str(e) # 数据库对比 sql_list = list(set(cmd_list) & set(cmd_dict['sql'])) if len(sql_list) > 0: print('数据库对比') try: + cinema_update_status[cinema_ip]['sql']['result'] = 'running' db_compare = DbCompare(cinema_ip, short_release) sql_output = db_compare.exec_diff_sql() - update_status['sql']['result'] = 'success' - update_status['sql']['msg'] = sql_output + cinema_update_status[cinema_ip]['sql']['result'] = 'success' + cinema_update_status[cinema_ip]['sql']['msg'] = sql_output except Exception as e: print('sql', e) - update_status['sql']['result'] = 'fail' - update_status['sql']['msg'] = str(e) + cinema_update_status[cinema_ip]['sql']['result'] = 'fail' + cinema_update_status[cinema_ip]['sql']['msg'] = str(e) # 修改设置 print('执行config(teardown前)') try: + cinema_update_status[cinema_ip]['config']['result'] = 'running' config_list = list(set(cmd_list) & set(cmd_dict['config'])) update_config = UpdateConfigUtil(cinema_ip, config_list, True) config_before_output = update_config.exec_config() - update_status['config']['result'] = 'success' - update_status['config']['msg'] = config_before_output + cinema_update_status[cinema_ip]['config']['result'] = 'success' + cinema_update_status[cinema_ip]['config']['msg'] = config_before_output except Exception as e: print('config', e) - update_status['config']['result'] = 'fail' - update_status['config']['msg'] = str(e) + cinema_update_status[cinema_ip]['config']['result'] = 'fail' + cinema_update_status[cinema_ip]['config']['msg'] = str(e) # 执行teardown teardown_list = list(set(cmd_list) & set(cmd_dict['teardown'])) if len(teardown_list) > 0: print('执行teardown') try: + cinema_update_status[cinema_ip]['teardown']['result'] = 'running' update_cmd = UpdateCommandUtil() teardown_output = update_cmd.exec_cmd_by_type(cinema_ip, 'teardown', teardown_list, short_release) - update_status['teardown']['result'] = 'success' - update_status['teardown']['msg'] = '执行升级脚本:执行成功' + cinema_update_status[cinema_ip]['teardown']['result'] = 'success' + cinema_update_status[cinema_ip]['teardown']['msg'] = '执行升级脚本:执行成功' except Exception as e: print('teardown', e) - update_status['teardown']['result'] = 'fail' - update_status['teardown']['msg'] = str(e) + cinema_update_status[cinema_ip]['teardown']['result'] = 'fail' + cinema_update_status[cinema_ip]['teardown']['msg'] = str(e) # 修改设置 print('执行config(teardown后)') try: + cinema_update_status[cinema_ip]['config']['result'] = 'running' config_list = list(set(cmd_list) & set(cmd_dict['config'])) update_config = UpdateConfigUtil(cinema_ip, config_list, False) config_after_output = update_config.exec_config() - update_status['config']['result'] = 'success' - update_status['config']['msg'] = update_status['config']['msg'] + '\n' + config_after_output + cinema_update_status[cinema_ip]['config']['result'] = 'success' + cinema_update_status[cinema_ip]['config']['msg'] = update_status['config']['msg'] + '\n' + config_after_output except Exception as e: print('config', e) - update_status['config']['result'] = 'fail' - update_status['config']['msg'] = update_status['config']['msg'] + '\n' + str(e) + cinema_update_status[cinema_ip]['config']['result'] = 'fail' + cinema_update_status[cinema_ip]['config']['msg'] = update_status['config']['msg'] + '\n' + str(e) # 传输客户端 client_list = list(set(cmd_list) & set(cmd_dict['client'])) @@ -207,14 +220,14 @@ def update_cine(request): try: client_release = ClientUtil() client_release.client_process(cinema_ip, short_release) - update_status['client']['result'] = 'success' - update_status['client']['msg'] = '客户端上传成功' + cinema_update_status[cinema_ip]['client']['result'] = 'success' + cinema_update_status[cinema_ip]['client']['msg'] = '客户端上传成功' except Exception as e: print('client', e) - update_status['client']['result'] = 'fail' - update_status['client']['msg'] = str(e) + cinema_update_status[cinema_ip]['client']['result'] = 'fail' + cinema_update_status[cinema_ip]['client']['msg'] = str(e) - print(update_status) + print(cinema_update_status[cinema_ip]) return JsonResponse({'result': 'success', 'ip': cinema_ip})