diff --git a/update/migrations/0023_cinema_ver_id.py b/update/migrations/0023_cinema_ver_id.py new file mode 100644 index 0000000..43f9d54 --- /dev/null +++ b/update/migrations/0023_cinema_ver_id.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.7 on 2024-02-05 06:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('update', '0022_updatecommand_run'), + ] + + operations = [ + migrations.AddField( + model_name='cinema', + name='ver_id', + field=models.IntegerField(default=0, help_text='版本id', verbose_name='版本id'), + ), + ] diff --git a/update/migrations/0024_updatealter.py b/update/migrations/0024_updatealter.py new file mode 100644 index 0000000..daf847e --- /dev/null +++ b/update/migrations/0024_updatealter.py @@ -0,0 +1,27 @@ +# Generated by Django 4.2.7 on 2024-02-06 00:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('update', '0023_cinema_ver_id'), + ] + + operations = [ + migrations.CreateModel( + name='UpdateAlter', + fields=[ + ('update_time', models.DateTimeField(auto_now=True, help_text='最后更新时间', verbose_name='最后更新时间')), + ('id', models.AutoField(primary_key=True, serialize=False)), + ('ver_id', models.IntegerField(default=0, help_text='版本id', verbose_name='版本id')), + ('alter', models.CharField(help_text='命令作用描述', max_length=200, verbose_name='命令作用描述')), + ], + options={ + 'verbose_name': 'alter', + 'verbose_name_plural': 'alter', + 'db_table': 'update_alter', + }, + ), + ] diff --git a/update/migrations/0025_alter_updatealter_alter.py b/update/migrations/0025_alter_updatealter_alter.py new file mode 100644 index 0000000..c9a129c --- /dev/null +++ b/update/migrations/0025_alter_updatealter_alter.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.7 on 2024-02-06 00:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('update', '0024_updatealter'), + ] + + operations = [ + migrations.AlterField( + model_name='updatealter', + name='alter', + field=models.CharField(help_text='命令作用描述', max_length=2000, verbose_name='命令作用描述'), + ), + ] diff --git a/update/models.py b/update/models.py index 76cb853..471e178 100644 --- a/update/models.py +++ b/update/models.py @@ -20,6 +20,7 @@ class Cinema(BaseModels): help_text='是否是云版本') remote_label = models.CharField(verbose_name='远程办公id', max_length=20, null=True, default='', help_text='远程办公id') + ver_id = models.IntegerField(verbose_name='版本id', null=False, default=0, help_text='版本id') def __str__(self): return self.ip @@ -121,3 +122,20 @@ class UpdateCommand(BaseModels): verbose_name = '升级脚本' verbose_name_plural = '升级脚本' db_table = 'update_command' + + +class UpdateAlter(BaseModels): + """ + 更新提示 + """ + id = models.AutoField(primary_key=True) + ver_id = models.IntegerField(verbose_name='版本id', null=False, default=0, help_text='版本id') + alter = models.CharField(verbose_name='命令作用描述', max_length=2000, null=False, help_text='命令作用描述') + + def __str__(self): + return self.alter + + class Meta: + verbose_name = 'alter' + verbose_name_plural = 'alter' + db_table = 'update_alter' diff --git a/update/serializers.py b/update/serializers.py index 6150a60..6866ea4 100644 --- a/update/serializers.py +++ b/update/serializers.py @@ -9,7 +9,7 @@ class CinemaSerializer(serializers.ModelSerializer): model = Cinema fields = ( 'id', 'name', 'ip', 'zz_num', 'inner_id', 'db_user', 'db_pwd', 'sys_ver', 'client_ver', 'user', - 'update_time', 'is_cloud', 'remote_label') + 'update_time', 'is_cloud', 'remote_label', 'ver_id') class CinemaUserSerializer(serializers.ModelSerializer): @@ -42,3 +42,11 @@ class UpdateCommandSerializer(serializers.ModelSerializer): fields = ( 'id', 'desc', 'command', 'ver_id', 'process', 'run_num', 'is_sys', 'is_checked', 'is_force', 'is_delete', 'comment', 'update_time') + + +class UpdateAlterSerializer(serializers.ModelSerializer): + update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S') + + class Meta: + model = UpdateAlter + fields = ('id', 'ver_id', 'alter', 'update_time') diff --git a/update/urls.py b/update/urls.py index 3d2fc4e..cdf5181 100644 --- a/update/urls.py +++ b/update/urls.py @@ -22,6 +22,7 @@ from update.views import * router = DefaultRouter() router.register('cinema', views.CinemaViewSet, 'cinema') router.register('user', views.CinemaUserViewSet, 'cinema_user') +router.register('update_alter', views.UpdateAlterViewSet, 'update_alter') urlpatterns = [ # path('cinema/search/', views.CinemaSearchAPIView.as_view()), diff --git a/update/utils/cmd_extcute.py b/update/utils/cmd_extcute.py index 95c483f..5f3885a 100644 --- a/update/utils/cmd_extcute.py +++ b/update/utils/cmd_extcute.py @@ -31,7 +31,7 @@ class UpdateCommandUtil: return self.model.objects.filter(Q(is_delete=False) & Q(is_sys=True)).order_by('id').all() def get_no_sys_cmd(self): - return self.model.objects.filter(Q(is_delete=False) & Q(is_sys=False)).order_by('id').all() + return self.model.objects.filter(Q(is_delete=False) & Q(is_sys=False)).order_by('run_num').all() def get_no_sys_setup_cmd(self): return self.model.objects.filter(Q(is_delete=False) & Q(is_sys=False) & Q(process='setup')).order_by('id').all() @@ -62,22 +62,32 @@ class UpdateCommandUtil: def exec_cmd(self, cinema_ip, _exec_cmd_list): self.connect(cinema_ip) exec_output = [] + exec_list = [] for exec_cmd in _exec_cmd_list: print(exec_cmd) stdin, stdout, stderr = self.client.exec_command(exec_cmd['cmd']) out = stdout.read().decode('utf-8') err = stderr.read().decode('utf-8') print('out', out, 'err', err) + out_format = out.replace('\n', '
').strip() + err_format = err.replace('\n', '
').strip() if err == '': if out == '': - exec_output.append(exec_cmd['desc'] + ':执行成功') + if exec_cmd['desc'] not in exec_list: + exec_output.append(exec_cmd['desc'] + ':执行成功') + exec_list.append(exec_cmd['desc']) else: - exec_output.append(exec_cmd['desc'] + ':执行成功 ' + out.strip()) + exec_output.append(exec_cmd['desc'] + ':执行成功
' + out_format) else: - if exec_cmd['desc'] in ['检出版本', '拉取代码', '执行升级脚本']: + if exec_cmd['desc'] in ['检出版本', '拉取代码']: + exec_output.append( + exec_cmd['desc'] + ':执行成功
' + out_format + '
' + err_format) + continue + if exec_cmd['desc'] in ['执行升级脚本']: exec_output.append(exec_cmd['desc'] + ':执行成功') + continue else: - exec_output.append(exec_cmd['desc'] + ':执行失败 ' + err.strip()) + exec_output.append(exec_cmd['desc'] + ':执行失败 ' + err.replace('\n', '
').strip()) return False, exec_output self.disconnect() print(exec_output) @@ -100,7 +110,7 @@ class UpdateCommandUtil: elif cmd['cmd'].startswith(''): cmd['cmd'] = cmd['cmd'].replace('', '') for c in cmd['cmd'].split('|||'): - exec_cmd_list.append({'desc': cmd['desc'], 'cmd':c}) + exec_cmd_list.append({'desc': cmd['desc'], 'cmd': c}) else: exec_cmd_list.append(cmd) return self.exec_cmd(cinema_ip, exec_cmd_list) diff --git a/update/utils/get_version.py b/update/utils/get_version.py index 72b568e..a65c870 100644 --- a/update/utils/get_version.py +++ b/update/utils/get_version.py @@ -73,7 +73,8 @@ class GetVersion(object): # 'update_time': datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S') 'update_time': timezone.now(), 'is_cloud': '云版本' if res.get('dx_sys_version', '--') == 'CLOUD' else '--', - 'remote_label': res.get('cinema_label', '--') if res.get('cinema_label', '--') != '' else '--' + 'remote_label': res.get('cinema_label', '--') if res.get('cinema_label', '--') != '' else '--', + 'ver_id': int(res['server_version'][7:11]) } print(ip, update_data, db_config) cinema_obj = Cinema.objects.filter(ip=ip).update(**update_data) diff --git a/update/views.py b/update/views.py index b0445b1..d40b355 100644 --- a/update/views.py +++ b/update/views.py @@ -59,6 +59,10 @@ class CinemaUserViewSet(CacheResponseMixin, viewsets.ModelViewSet): serializer_class = CinemaUserSerializer +class UpdateAlterViewSet(CacheResponseMixin, viewsets.ModelViewSet): + queryset = UpdateAlter.objects.all() + serializer_class = UpdateAlterSerializer + # class CinemaSearchAPIView(APIView, CacheResponseMixin): # @method_decorator(cache_page(60 * 5)) # def get(self, request, *args, **kwargs):