From 54c694fc0fc11450449de9f1540d4fac9dc9c7b6 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Wed, 20 Mar 2024 12:32:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=B3=E8=81=94=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...mmand_combo_cmd_alter_updatecommand_run.py | 23 ++++++++++++++++ update/models.py | 3 ++- update/serializers.py | 2 +- update/utils/cmd_extcute.py | 26 +++++++++++++------ update/views.py | 4 +-- 5 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 update/migrations/0027_updatecommand_combo_cmd_alter_updatecommand_run.py diff --git a/update/migrations/0027_updatecommand_combo_cmd_alter_updatecommand_run.py b/update/migrations/0027_updatecommand_combo_cmd_alter_updatecommand_run.py new file mode 100644 index 0000000..1f1600b --- /dev/null +++ b/update/migrations/0027_updatecommand_combo_cmd_alter_updatecommand_run.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.7 on 2024-03-20 03:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('update', '0026_updatelog'), + ] + + operations = [ + migrations.AddField( + model_name='updatecommand', + name='combo_cmd', + field=models.IntegerField(default=0, help_text='需要关联执行的命令', verbose_name='需要关联执行的命令'), + ), + migrations.AlterField( + model_name='updatecommand', + name='run', + field=models.BooleanField(default=False, help_text='在teardown前运行还是之后运行', verbose_name='在teardown前运行还是之后运行 true(0)为之前,false(1)为之后'), + ), + ] diff --git a/update/models.py b/update/models.py index 47b09df..9ece1e8 100644 --- a/update/models.py +++ b/update/models.py @@ -112,8 +112,9 @@ class UpdateCommand(BaseModels): 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='页面显示提示') - run = models.BooleanField(verbose_name='在teardown前运行还是之后运行 true为之前,false为之后', default=False, + run = models.BooleanField(verbose_name='在teardown前运行还是之后运行 true(0)为之前,false(1)为之后', default=False, help_text='在teardown前运行还是之后运行') + combo_cmd = models.IntegerField(verbose_name='需要关联执行的命令', default=0, help_text='需要关联执行的命令') def __str__(self): return self.desc diff --git a/update/serializers.py b/update/serializers.py index f28934c..5bcb8de 100644 --- a/update/serializers.py +++ b/update/serializers.py @@ -41,7 +41,7 @@ class UpdateCommandSerializer(serializers.ModelSerializer): model = UpdateCommand fields = ( 'id', 'desc', 'command', 'ver_id', 'process', 'run_num', 'is_sys', 'is_checked', 'is_force', 'is_delete', - 'comment', 'update_time') + 'comment', 'run', 'combo_cmd', 'update_time') class UpdateAlterSerializer(serializers.ModelSerializer): diff --git a/update/utils/cmd_extcute.py b/update/utils/cmd_extcute.py index 8a63d3c..0535231 100644 --- a/update/utils/cmd_extcute.py +++ b/update/utils/cmd_extcute.py @@ -47,12 +47,12 @@ class UpdateCommandUtil: return self.model.objects.filter(Q(is_delete=False) & Q(is_sys=False) & Q(process='client')).order_by( 'id').all() - def get_checked_cmd(self, cmd_list): + def get_checked_cmd(self, cmd_list, is_delete='0'): sys_result = self.get_sys_cmd() checked_setup_result = self.model.objects.filter( - Q(is_delete='0') & Q(id__in=cmd_list) & Q(process='setup')).order_by('run_num').all() + Q(is_delete=is_delete) & Q(id__in=cmd_list) & Q(process='setup')).order_by('run_num').all() checked_teardown_result = self.model.objects.filter( - Q(is_delete='0') & Q(id__in=cmd_list) & Q(process='teardown')).order_by('run_num').all() + Q(is_delete=is_delete) & Q(id__in=cmd_list) & Q(process='teardown')).order_by('run_num').all() sys_cmd = [{'desc': sys.desc, 'cmd': sys.command} for sys in sys_result] checked_setup_cmd = [{'desc': setup.desc, 'cmd': setup.command} for setup in checked_setup_result] checked_teardown_cmd = [{'desc': teardown.desc, 'cmd': teardown.command} for teardown in @@ -94,10 +94,10 @@ class UpdateCommandUtil: print('输出结果:', exec_output) return True, '
'.join(exec_output) - def exec_cmd_by_type(self, cinema_ip, _type, cmd_list, short_release): + def exec_cmd_by_type(self, cinema_ip, _type, cmd_list, short_release, is_delete='0'): print('需要执行的命令列表:', cmd_list) exec_cmd_list = [] - exec_cmd_data = self.get_checked_cmd(cmd_list) + exec_cmd_data = self.get_checked_cmd(cmd_list, is_delete) if _type == 'setup': cmds = exec_cmd_data['sys'] + exec_cmd_data['setup'] elif _type == 'teardown': @@ -124,7 +124,7 @@ class UpdateConfigUtil: self.checked_list = checked_list self.run_before_teardown = run_before_teardown - def exec_config(self): + def exec_config(self, short_release): cmd_list = self.get_all_exec_cmd() db_config = Cinema.objects.filter(ip=self.ip).values()[0] db_conn = pymysql.Connect(host=self.ip, port=3306, user=db_config['db_user'], passwd=db_config['db_pwd'], @@ -138,6 +138,13 @@ class UpdateConfigUtil: print("执行子命令: ", cmds['desc'], cmd, '结果:', '执行失败(设置无需修改)' if r == 0 else '执行成功') result_list.append(cmds['desc'] + (':执行失败(设置无需修改)' if r == 0 else ':执行成功')) db_conn.commit() + + if cmds['combo_cmd'] != 0: + update_cmd = UpdateCommandUtil() + result, setup_output = update_cmd.exec_cmd_by_type(self.ip, 'setup', [cmds['combo_cmd']], + short_release, is_delete='1') + print('执行关联命令:', setup_output) + db_cursor.close() db_conn.close() return '
'.join(result_list) @@ -146,9 +153,12 @@ class UpdateConfigUtil: all_config_obj = UpdateCommand.objects.filter( Q(process='config') & Q(run=self.run_before_teardown) & Q(is_delete=False)).values() cmd_list = [] + print(all_config_obj) for config_item in all_config_obj: if config_item['id'] in self.checked_list: - cmd_list.append({"desc": config_item['desc'], "cmd": json.loads(config_item['command'])['checked']}) + cmd_list.append({"desc": config_item['desc'], "cmd": json.loads(config_item['command'])['checked'], + "combo_cmd": config_item['combo_cmd']}) else: - cmd_list.append({"desc": config_item['desc'], "cmd": json.loads(config_item['command'])['unchecked']}) + cmd_list.append({"desc": config_item['desc'], "cmd": json.loads(config_item['command'])['unchecked'], + "combo_cmd": config_item['combo_cmd']}) return cmd_list diff --git a/update/views.py b/update/views.py index 2a7fba2..40d694c 100644 --- a/update/views.py +++ b/update/views.py @@ -250,7 +250,7 @@ def update_cine(request): redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) 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() + config_before_output = update_config.exec_config(short_release) update_status['config']['result'] = 'success' update_status['config']['msg'] = config_before_output redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) @@ -301,7 +301,7 @@ def update_cine(request): redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) 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() + config_after_output = update_config.exec_config(short_release) update_status['config']['result'] = 'success' update_status['config']['msg'] = config_before_output + '
' + config_after_output redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)