From 737c50127de0aa075eb4c71b1a6fe1c2cda0a8d6 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Thu, 14 Mar 2024 18:20:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=B0=E5=BD=95=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=98=E5=8C=96=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- update/models.py | 22 ++++++++++++++++++++++ update/serializers.py | 8 ++++++++ update/utils/get_version.py | 17 +++++++++++++++++ update/views.py | 6 +++++- 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/update/models.py b/update/models.py index 471e178..47b09df 100644 --- a/update/models.py +++ b/update/models.py @@ -139,3 +139,25 @@ class UpdateAlter(BaseModels): verbose_name = 'alter' verbose_name_plural = 'alter' db_table = 'update_alter' + + +class UpdateLog(BaseModels): + """ + 记录升级日志 + """ + id = models.AutoField(primary_key=True) + ip = models.CharField(verbose_name='ip地址', max_length=20, null=False, help_text='ip地址') + type = models.CharField(verbose_name='触发类型', max_length=20, null=False, help_text='触发类型') + origin_ver = models.CharField(verbose_name='升级前版本', max_length=200, null=False, help_text='升级前版本') + target_ver = models.CharField(verbose_name='升级后版本', max_length=200, null=False, help_text='升级后版本') + time = models.DateTimeField(verbose_name='操作时间', help_text='操作时间') + result = models.CharField(verbose_name='升级结果', max_length=20, null=False, help_text='升级结果') + result_detail = models.TextField(verbose_name='升级结果详情', null=False, help_text='升级结果详情') + + def __str__(self): + return self.ip + + class Meta: + verbose_name = '升级日志' + verbose_name_plural = '升级日志' + db_table = 'update_log' diff --git a/update/serializers.py b/update/serializers.py index 6866ea4..f28934c 100644 --- a/update/serializers.py +++ b/update/serializers.py @@ -50,3 +50,11 @@ class UpdateAlterSerializer(serializers.ModelSerializer): class Meta: model = UpdateAlter fields = ('id', 'ver_id', 'alter', 'update_time') + + +class UpdateLogSerializer(serializers.ModelSerializer): + time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S') + + class Meta: + model = UpdateLog + fields = ('id', 'ip', 'type', 'origin_ver', 'target_ver', 'time', 'result', 'result_detail') diff --git a/update/utils/get_version.py b/update/utils/get_version.py index a65c870..7d6c5f4 100644 --- a/update/utils/get_version.py +++ b/update/utils/get_version.py @@ -80,6 +80,23 @@ class GetVersion(object): cinema_obj = Cinema.objects.filter(ip=ip).update(**update_data) +def get_cinema_ver_by_ip(ip): + sql_str = "SELECT cv.server_version FROM cinema_version cv ON 1=1;" + db_config = { + 'host': ip, + 'user': Cinema.objects.get(ip=ip).db_user, + 'password': Cinema.objects.get(ip=ip).db_pwd, + 'database': 'cine', + 'connect_timeout': 5, + } + print(db_config) + db_conn = pymysql.Connect(**db_config) + db_cursor = db_conn.cursor(cursor=DictCursor) + db_cursor.execute(sql_str) + res = db_cursor.fetchone() + return res.get('server_version') + + if __name__ == '__main__': v = GetVersion() v.get_all_cinema() diff --git a/update/views.py b/update/views.py index df67c7f..829d1b1 100644 --- a/update/views.py +++ b/update/views.py @@ -16,7 +16,7 @@ from django.views.decorators.cache import cache_page from django.views.decorators.csrf import csrf_exempt from django_filters.rest_framework import DjangoFilterBackend from django.core.cache import cache -from update.utils.get_version import GetVersion +from update.utils.get_version import GetVersion, get_cinema_ver_by_ip from rest_framework.decorators import action from update.utils.git_util import GitUtil, GitDbUtil from update.utils.cmd_extcute import UpdateCommandUtil, UpdateConfigUtil @@ -105,6 +105,10 @@ def get_git_version(request): def update_cine(request): req = request.GET.dict() cinema_ip = req.get('ip') + + # 获取当前版本 + org_ver = get_cinema_ver_by_ip(ip=cinema_ip) + # 处理命令格式 cmd_dict = {} cmd_obj = UpdateCommand.objects.filter(is_sys=0).values('id', 'process')