增加降级提示的逻辑

main
RogerWork 9 months ago
parent a88e8afd48
commit 23e295dcdb
  1. 18
      update/migrations/0023_cinema_ver_id.py
  2. 27
      update/migrations/0024_updatealter.py
  3. 18
      update/migrations/0025_alter_updatealter_alter.py
  4. 18
      update/models.py
  5. 10
      update/serializers.py
  6. 1
      update/urls.py
  7. 20
      update/utils/cmd_extcute.py
  8. 3
      update/utils/get_version.py
  9. 4
      update/views.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'),
),
]

@ -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',
},
),
]

@ -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='命令作用描述'),
),
]

@ -20,6 +20,7 @@ class Cinema(BaseModels):
help_text='是否是云版本') help_text='是否是云版本')
remote_label = models.CharField(verbose_name='远程办公id', max_length=20, null=True, default='', remote_label = models.CharField(verbose_name='远程办公id', max_length=20, null=True, default='',
help_text='远程办公id') help_text='远程办公id')
ver_id = models.IntegerField(verbose_name='版本id', null=False, default=0, help_text='版本id')
def __str__(self): def __str__(self):
return self.ip return self.ip
@ -121,3 +122,20 @@ class UpdateCommand(BaseModels):
verbose_name = '升级脚本' verbose_name = '升级脚本'
verbose_name_plural = '升级脚本' verbose_name_plural = '升级脚本'
db_table = 'update_command' 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'

@ -9,7 +9,7 @@ class CinemaSerializer(serializers.ModelSerializer):
model = Cinema model = Cinema
fields = ( fields = (
'id', 'name', 'ip', 'zz_num', 'inner_id', 'db_user', 'db_pwd', 'sys_ver', 'client_ver', 'user', '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): class CinemaUserSerializer(serializers.ModelSerializer):
@ -42,3 +42,11 @@ class UpdateCommandSerializer(serializers.ModelSerializer):
fields = ( fields = (
'id', 'desc', 'command', 'ver_id', 'process', 'run_num', 'is_sys', 'is_checked', 'is_force', 'is_delete', 'id', 'desc', 'command', 'ver_id', 'process', 'run_num', 'is_sys', 'is_checked', 'is_force', 'is_delete',
'comment', 'update_time') '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')

@ -22,6 +22,7 @@ from update.views import *
router = DefaultRouter() router = DefaultRouter()
router.register('cinema', views.CinemaViewSet, 'cinema') router.register('cinema', views.CinemaViewSet, 'cinema')
router.register('user', views.CinemaUserViewSet, 'cinema_user') router.register('user', views.CinemaUserViewSet, 'cinema_user')
router.register('update_alter', views.UpdateAlterViewSet, 'update_alter')
urlpatterns = [ urlpatterns = [
# path('cinema/search/', views.CinemaSearchAPIView.as_view()), # path('cinema/search/', views.CinemaSearchAPIView.as_view()),

@ -31,7 +31,7 @@ class UpdateCommandUtil:
return self.model.objects.filter(Q(is_delete=False) & Q(is_sys=True)).order_by('id').all() return self.model.objects.filter(Q(is_delete=False) & Q(is_sys=True)).order_by('id').all()
def get_no_sys_cmd(self): 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): 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() 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): def exec_cmd(self, cinema_ip, _exec_cmd_list):
self.connect(cinema_ip) self.connect(cinema_ip)
exec_output = [] exec_output = []
exec_list = []
for exec_cmd in _exec_cmd_list: for exec_cmd in _exec_cmd_list:
print(exec_cmd) print(exec_cmd)
stdin, stdout, stderr = self.client.exec_command(exec_cmd['cmd']) stdin, stdout, stderr = self.client.exec_command(exec_cmd['cmd'])
out = stdout.read().decode('utf-8') out = stdout.read().decode('utf-8')
err = stderr.read().decode('utf-8') err = stderr.read().decode('utf-8')
print('out', out, 'err', err) print('out', out, 'err', err)
out_format = out.replace('\n', '<br/>').strip()
err_format = err.replace('\n', '<br/>').strip()
if err == '': if err == '':
if out == '': if out == '':
if exec_cmd['desc'] not in exec_list:
exec_output.append(exec_cmd['desc'] + ':执行成功') exec_output.append(exec_cmd['desc'] + ':执行成功')
exec_list.append(exec_cmd['desc'])
else: else:
exec_output.append(exec_cmd['desc'] + ':执行成功 ' + out.strip()) exec_output.append(exec_cmd['desc'] + ':执行成功 <br/>' + out_format)
else: else:
if exec_cmd['desc'] in ['检出版本', '拉取代码', '执行升级脚本']: if exec_cmd['desc'] in ['检出版本', '拉取代码']:
exec_output.append(
exec_cmd['desc'] + ':执行成功 <br/>' + out_format + '<br/>' + err_format)
continue
if exec_cmd['desc'] in ['执行升级脚本']:
exec_output.append(exec_cmd['desc'] + ':执行成功') exec_output.append(exec_cmd['desc'] + ':执行成功')
continue
else: else:
exec_output.append(exec_cmd['desc'] + ':执行失败 ' + err.strip()) exec_output.append(exec_cmd['desc'] + ':执行失败 ' + err.replace('\n', '<br/>').strip())
return False, exec_output return False, exec_output
self.disconnect() self.disconnect()
print(exec_output) print(exec_output)
@ -100,7 +110,7 @@ class UpdateCommandUtil:
elif cmd['cmd'].startswith('<multi>'): elif cmd['cmd'].startswith('<multi>'):
cmd['cmd'] = cmd['cmd'].replace('<multi>', '') cmd['cmd'] = cmd['cmd'].replace('<multi>', '')
for c in cmd['cmd'].split('|||'): 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: else:
exec_cmd_list.append(cmd) exec_cmd_list.append(cmd)
return self.exec_cmd(cinema_ip, exec_cmd_list) return self.exec_cmd(cinema_ip, exec_cmd_list)

@ -73,7 +73,8 @@ class GetVersion(object):
# 'update_time': datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S') # 'update_time': datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
'update_time': timezone.now(), 'update_time': timezone.now(),
'is_cloud': '云版本' if res.get('dx_sys_version', '--') == 'CLOUD' else '--', '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) print(ip, update_data, db_config)
cinema_obj = Cinema.objects.filter(ip=ip).update(**update_data) cinema_obj = Cinema.objects.filter(ip=ip).update(**update_data)

@ -59,6 +59,10 @@ class CinemaUserViewSet(CacheResponseMixin, viewsets.ModelViewSet):
serializer_class = CinemaUserSerializer serializer_class = CinemaUserSerializer
class UpdateAlterViewSet(CacheResponseMixin, viewsets.ModelViewSet):
queryset = UpdateAlter.objects.all()
serializer_class = UpdateAlterSerializer
# class CinemaSearchAPIView(APIView, CacheResponseMixin): # class CinemaSearchAPIView(APIView, CacheResponseMixin):
# @method_decorator(cache_page(60 * 5)) # @method_decorator(cache_page(60 * 5))
# def get(self, request, *args, **kwargs): # def get(self, request, *args, **kwargs):

Loading…
Cancel
Save