From acda8b7d6e1797d677e81d123c66e9c3eb38900e Mon Sep 17 00:00:00 2001 From: RogerWork Date: Thu, 1 Feb 2024 14:02:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BF=AE=E6=94=B9=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=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/models.py | 1 + update/serializers.py | 2 +- update/utils/cmd_extcute.py | 40 ++++++++++++++++++++++++++++++++++++- update/views.py | 37 +++++++++++++++++++++++++++------- 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/update/models.py b/update/models.py index f200ced..f23700d 100644 --- a/update/models.py +++ b/update/models.py @@ -108,6 +108,7 @@ class UpdateCommand(BaseModels): is_checked = models.BooleanField(verbose_name='默认勾选', default=False, help_text='前台页面默认是否勾选') is_force = models.BooleanField(verbose_name='必选项', default=False, help_text='必选项') is_delete = models.BooleanField(verbose_name='是否有效', default=True, help_text='是否有效') + comment = models.TextField(verbose_name='页面显示提示', default='',help_text='页面显示提示') def __str__(self): return self.desc diff --git a/update/serializers.py b/update/serializers.py index a9b92bc..6150a60 100644 --- a/update/serializers.py +++ b/update/serializers.py @@ -41,4 +41,4 @@ class UpdateCommandSerializer(serializers.ModelSerializer): model = UpdateCommand fields = ( 'id', 'desc', 'command', 'ver_id', 'process', 'run_num', 'is_sys', 'is_checked', 'is_force', 'is_delete', - 'update_time') + 'comment', 'update_time') diff --git a/update/utils/cmd_extcute.py b/update/utils/cmd_extcute.py index af397d4..92f2cb8 100644 --- a/update/utils/cmd_extcute.py +++ b/update/utils/cmd_extcute.py @@ -1,7 +1,11 @@ +import json import time import paramiko -from update.models import UpdateCommand +import pymysql + +from update.models import UpdateCommand, Cinema +from update.serializers import UpdateCommandSerializer from django.db.models import Q from time import sleep @@ -84,3 +88,37 @@ class UpdateCommandUtil: else: exec_cmd_list.append(cmd) self.exec_cmd(cinema_ip, exec_cmd_list) + + +# 执行设置相关 +class UpdateConfigUtil: + def __init__(self, cinema_ip, checked_list): + self.ip = cinema_ip + self.checked_list = checked_list + + def exec_config(self): + cmd_list = self.get_all_exec_cmd() + db_config = Cinema.objects.filter(ip=self.ip).values()[0] + print(db_config) + print(db_config['db_user'], db_config['db_pwd']) + db_conn = pymysql.Connect(host=self.ip, port=3306, user=db_config['db_user'], passwd=db_config['db_pwd'], + database='cine') + db_cursor = db_conn.cursor() + for cmds in cmd_list: + for cmd in cmds: + db_cursor.execute(cmd) + db_conn.commit() + db_cursor.close() + db_conn.close() + + def get_all_exec_cmd(self): + all_config_obj = UpdateCommand.objects.filter(Q(process='config') & Q(is_delete=False)).values() + print(all_config_obj) + cmd_list = [] + for config_item in all_config_obj: + print(config_item) + if config_item['id'] in self.checked_list: + cmd_list.append(json.loads(config_item['command'])['checked']) + else: + cmd_list.append(json.loads(config_item['command'])['unchecked']) + return cmd_list diff --git a/update/views.py b/update/views.py index 97be231..46a5a94 100644 --- a/update/views.py +++ b/update/views.py @@ -18,7 +18,7 @@ from django.core.cache import cache from update.utils.get_version import GetVersion from rest_framework.decorators import action from update.utils.git_util import GitUtil, GitDbUtil -from update.utils.cmd_extcute import UpdateCommandUtil +from update.utils.cmd_extcute import UpdateCommandUtil, UpdateConfigUtil from update.utils.db_compare import DbCompare from update.utils.client_util import ClientUtil @@ -93,7 +93,15 @@ def get_git_version(request): def update_cine(request): req = request.GET.dict() cinema_ip = req.get('ip') - + # 处理命令格式 + cmd_dict = {} + cmd_obj = UpdateCommand.objects.filter(is_sys=0).values('id', 'process') + for cmd in cmd_obj: + if cmd['process'] in cmd_dict.keys(): + cmd_dict[cmd['process']].append(cmd['id']) + else: + cmd_dict[cmd['process']] = [cmd['id']] + print(cmd_dict) update_status = { 'git': {'result': 'success', 'msg': ''}, 'setup': {'result': 'success', 'msg': ''}, @@ -115,24 +123,39 @@ def update_cine(request): print('获取并写入cine.sql') git_util = GitUtil(short_release) git_result = git_util.handle_create_cine() + # 执行setup print('执行setup') + setup_list = list(set(cmd_list) & set(cmd_dict['setup'])) update_cmd = UpdateCommandUtil() - update_cmd.exec_cmd_by_type(cinema_ip, 'setup', cmd_list, short_release) + update_cmd.exec_cmd_by_type(cinema_ip, 'setup', setup_list, short_release) + # 数据库对比 - if 19 in cmd_list: + sql_list = list(set(cmd_list) & set(cmd_dict['sql'])) + if len(sql_list) > 0: print('数据库对比') db_compare = DbCompare(cinema_ip, short_release) db_compare.exec_diff_sql() + # 执行teardown - if 17 in cmd_list: + teardown_list = list(set(cmd_list) & set(cmd_dict['teardown'])) + if len(teardown_list) > 0: print('执行teardown') - update_cmd.exec_cmd_by_type(cinema_ip, 'teardown', cmd_list, short_release) + update_cmd.exec_cmd_by_type(cinema_ip, 'teardown', teardown_list, short_release) + + # 修改设置 + print('执行config_list') + config_list = list(set(cmd_list) & set(cmd_dict['config'])) + update_config = UpdateConfigUtil(cinema_ip, config_list) + update_config.exec_config() + # 传输客户端 - if 20 in cmd_list: + client_list = list(set(cmd_list) & set(cmd_dict['client'])) + if len(client_list) > 0: print('传输客户端') client_release = ClientUtil() client_release.client_process(cinema_ip, short_release) + return JsonResponse({'result': 'success', 'ip': cinema_ip})