完成自定义升级版本的基础代码部分

main
RogerWork 1 week ago
parent 5bb153030c
commit 6feae5bd81
  1. 7
      dingxin_toolbox_drf/settings.py
  2. 18
      update/migrations/0030_cinema_is_check.py
  3. 23
      update/migrations/0031_clientrelease_deploy_id_clientrelease_is_cached.py
  4. 28
      update/migrations/0032_clientrelease_file_name_clientrelease_md5_and_more.py
  5. 18
      update/migrations/0033_alter_clientrelease_md5.py
  6. 8
      update/models.py
  7. 122
      update/utils/client_util_custom.py
  8. 7
      update/utils/dingxin_sql.py
  9. 3
      update/views.py

@ -62,6 +62,13 @@ CONFIG = {
"PASSWORD": "cine123456", "PASSWORD": "cine123456",
"NAME": "dingxin_toolbox", "NAME": "dingxin_toolbox",
}, },
# 'DB': {
# "HOST": '172.16.1.63',
# "PORT": 3306,
# "USER": "root",
# "PASSWORD": "Cine123456",
# "NAME": "dingxin_toolbox",
# },
'REDIS_IP': '127.0.0.1', 'REDIS_IP': '127.0.0.1',
'ECARD_DB': { 'ECARD_DB': {
"HOST": '10.10.0.61', "HOST": '10.10.0.61',

@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2026-01-06 07:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('update', '0029_cinema_comments_cinema_is_online'),
]
operations = [
migrations.AddField(
model_name='cinema',
name='is_check',
field=models.BooleanField(default=False, help_text='是否需要拉取版本', verbose_name='是否在线'),
),
]

@ -0,0 +1,23 @@
# Generated by Django 4.2.7 on 2026-02-14 06:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('update', '0030_cinema_is_check'),
]
operations = [
migrations.AddField(
model_name='clientrelease',
name='deploy_id',
field=models.IntegerField(default=0, help_text='打包服务器版本ID', null=True, verbose_name='打包服务器版本ID'),
),
migrations.AddField(
model_name='clientrelease',
name='is_cached',
field=models.BooleanField(default=False, help_text='是否本地缓存', verbose_name='是否本地缓存'),
),
]

@ -0,0 +1,28 @@
# Generated by Django 4.2.7 on 2026-02-14 09:09
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('update', '0031_clientrelease_deploy_id_clientrelease_is_cached'),
]
operations = [
migrations.AddField(
model_name='clientrelease',
name='file_name',
field=models.CharField(help_text='smb上的名称', max_length=500, null=True, verbose_name='文件名称'),
),
migrations.AddField(
model_name='clientrelease',
name='md5',
field=models.CharField(default='', help_text='md5', max_length=30, null=True, verbose_name='md5'),
),
migrations.AlterField(
model_name='clientrelease',
name='origin_name',
field=models.CharField(help_text='deploy上的名称', max_length=500, verbose_name='原始名称'),
),
]

@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2026-02-14 09:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('update', '0032_clientrelease_file_name_clientrelease_md5_and_more'),
]
operations = [
migrations.AlterField(
model_name='clientrelease',
name='md5',
field=models.CharField(default='', help_text='md5', max_length=50, null=True, verbose_name='md5'),
),
]

@ -79,7 +79,7 @@ class ClientRelease(BaseModels):
鼎新客户端版本 鼎新客户端版本
""" """
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
origin_name = models.CharField(verbose_name='原始名称', max_length=500, null=False, help_text='smb上的名称') origin_name = models.CharField(verbose_name='原始名称', max_length=500, null=False, help_text='deploy上的名称')
client_ver = models.CharField(verbose_name='客户端版本', max_length=100, null=False, default='', client_ver = models.CharField(verbose_name='客户端版本', max_length=100, null=False, default='',
help_text='客户端版本') help_text='客户端版本')
main_ver = models.CharField(verbose_name='主版本', max_length=100, null=False, default='', help_text='主版本') main_ver = models.CharField(verbose_name='主版本', max_length=100, null=False, default='', help_text='主版本')
@ -89,8 +89,10 @@ class ClientRelease(BaseModels):
client_type = models.CharField(verbose_name='类型', max_length=10, default='test', client_type = models.CharField(verbose_name='类型', max_length=10, default='test',
help_text='类型 test 测试 prd 线上') help_text='类型 test 测试 prd 线上')
is_delete = models.BooleanField(verbose_name='是否有效', default=False, help_text='是否有效') is_delete = models.BooleanField(verbose_name='是否有效', default=False, help_text='是否有效')
# dev_id = models.IntegerField(verbose_name='打包服务器版本ID', null=True, default=0, help_text='打包服务器版本ID') deploy_id = models.IntegerField(verbose_name='打包服务器版本ID', null=True, default=0, help_text='打包服务器版本ID')
# is_cached = models.BooleanField(verbose_name='是否本地缓存', default=False, help_text='是否本地缓存') is_cached = models.BooleanField(verbose_name='是否本地缓存', default=False, help_text='是否本地缓存')
file_name = models.CharField(verbose_name='文件名称', max_length=500, null=True, help_text='smb上的名称')
md5 = models.CharField(verbose_name='md5', max_length=50, null=True, default='', help_text='md5')
def __str__(self): def __str__(self):
return self.upload_name return self.upload_name

@ -0,0 +1,122 @@
import os
import json
import paramiko
from smb.SMBConnection import SMBConnection
from update.models import ClientRelease, Cinema
import pymysql
from pymysql.cursors import DictCursor
from django.db.models import Q, Max
from dingxin_toolbox_drf.settings import BASE_DIR
from update.utils.dingxin_sql import *
# 定义客户端读取路径
TEST_CLIENT_PATH = r'/客户端/测试专用/'
TEST_HISTORY_CLIENT_PATH = r'/客户端/测试专用/历史测试测客户端/'
PRD_CLIENT_PATH = r'/客户端/结测常用客户端/'
PRD_HISTORY_CLIENT_PATH = r'/客户端/结测常用客户端/历史客户端——通用/'
class ClientUtilCustom:
def __init__(self):
self.smb_conn = SMBConnection('admin', 'admin', '', '', use_ntlm_v2=True)
self.smb_host = '172.16.3.68'
self.local_path = os.path.join(BASE_DIR, 'dx', 'client')
self.deploy_db_config = {'host': '10.10.0.80', 'port': 3306, 'user': 'clientdeploy',
'password': 'clientdeploy123456', 'database': 'yhz_tool'}
self.deploy_id = 0
self.client_data = []
self.client_file_list = []
def smb_connect(self):
self.smb_conn.connect(self.smb_host, 445)
def smb_disconnect(self):
self.smb_conn.close()
def sync_client_db(self):
# 获取本地数据库最大的同步ID
self.deploy_id = ClientRelease.objects.all().aggregate(Max('deploy_id'))['deploy_id__max']
print('deploy_id', self.deploy_id)
# 连接到deploy库获取大于deploy_id的数据
db_conn = pymysql.connect(**self.deploy_db_config)
cursor = db_conn.cursor(cursor=DictCursor)
cursor.execute(GET_NEW_DEPLOY_CLIENT_INFO, (self.deploy_id,))
cursor.close()
# 获取未同步的客户端信息
client_info_list = cursor.fetchall()
print(client_info_list)
# 将客户端信息转化为本地数据库格式
self.handle_client_data_format(client_info_list)
# 启动smb服务
self.smb_connect()
# 获取smb服务器上的文件列表
self.get_client_file_list()
# 将文件写入本地服务器
self.get_client_file()
# 将数据信息写人本地数据库
self.update_client_db()
# 关闭smb服务
self.smb_disconnect()
# 关闭数据库
db_conn.close()
def handle_client_data_format(self, client_data_list):
print('handle_client_data_format')
for data in client_data_list:
print(data)
main_ver = data['version_full'][:-5]
upload_name = data['zip_full_name'][0:23] + '.7z'
ver_id = int(data['version_full'][7:10])
self.client_data.append(
{'origin_name': data['zip_full_name'],
'main_ver': main_ver,
'sub_ver': data['version_revise'],
'upload_name': upload_name,
'client_ver': data['version_full'],
'ver_id': ver_id,
'deploy_id': data['id'],
'is_cached': '0',
'file_name': '',
'md5': data['zip_md5'],
}
)
print(self.client_data)
def update_client_db(self):
for d in self.client_data:
if not ClientRelease.objects.filter(deploy_id=d['deploy_id']).exists():
ClientRelease.objects.create(**d)
def get_client_file_list(self):
print('get_client_file_list')
path_list = [TEST_CLIENT_PATH, TEST_HISTORY_CLIENT_PATH, PRD_CLIENT_PATH, PRD_HISTORY_CLIENT_PATH]
for path in path_list:
smb_files = self.smb_conn.listPath('data1', path, pattern=r'client_v*.7z')
for file in smb_files:
self.client_file_list.append({'path': path, 'name': file.filename})
print(json.dumps(self.client_file_list))
def get_client_file(self):
print('get_client_file')
local_file_list = list(os.walk(self.local_path))[0][2]
for client in self.client_data:
for file in self.client_file_list:
if client['origin_name'][:23] == file['name'][:23]:
# 查看本地是否有此版本客户端
if file['name'] in local_file_list:
print('找到本地文件:', file['name'])
continue
# 下载文件到本地
with open(os.path.join(self.local_path, file['name']), 'wb') as local_file:
# 接收文件并写入本地文件
print('从共享下载客户端文件到服务器 共享路径:', rf"{file['path']}{file['name']}", '服务器路径:',
os.path.join(self.local_path, file['name']))
self.smb_conn.retrieveFile('data1', rf"{file['path']}{file['name']}", local_file)
# 更新数据
client['is_cached'] = '1'
client['file_name'] = file['name']
# 关闭本地文件
local_file.close()
print(self.client_data)

@ -5,3 +5,10 @@ WHERE publish_type = '通用'
AND id > 162 AND id > 162
AND SUBSTRING(cdl.version_full, 1, 10) = %s AND SUBSTRING(cdl.version_full, 1, 10) = %s
ORDER BY sub_version DESC LIMIT 1;""" ORDER BY sub_version DESC LIMIT 1;"""
GET_NEW_DEPLOY_CLIENT_INFO = """
SELECT id, version_full, version_revise, zip_full_name, zip_md5
FROM client_deploy_log
WHERE publish_type = '通用'
AND id > %s; \
"""

@ -23,6 +23,7 @@ from update.utils.git_util import GitUtil, GitDbUtil
from update.utils.cmd_extcute import UpdateCommandUtil, UpdateConfigUtil from update.utils.cmd_extcute import UpdateCommandUtil, UpdateConfigUtil
from update.utils.db_compare import DbCompare from update.utils.db_compare import DbCompare
from update.utils.client_util import ClientUtil from update.utils.client_util import ClientUtil
from update.utils.client_util_custom import ClientUtilCustom
from django.utils import timezone from django.utils import timezone
@ -52,6 +53,8 @@ class CinemaViewSet(CacheResponseMixin, viewsets.ModelViewSet):
# filter_fields = ('ip',) # filter_fields = ('ip',)
filterset_fields = ('ip',) filterset_fields = ('ip',)
GetVersion().main_process() # 修改数据model时需要注释调 GetVersion().main_process() # 修改数据model时需要注释调
client_custom = ClientUtilCustom()
client_custom.sync_client_db()
@action(methods=['get'], detail=False) @action(methods=['get'], detail=False)
@method_decorator(cache_page(60 * 1)) @method_decorator(cache_page(60 * 1))

Loading…
Cancel
Save