parent
5bb153030c
commit
6feae5bd81
9 changed files with 232 additions and 4 deletions
@ -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'), |
||||
), |
||||
] |
||||
@ -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) |
||||
Loading…
Reference in new issue