From 8a8ed30e053417d3a8c9d50d1d4f74ce6de87115 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Fri, 12 Jan 2024 16:08:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AC=AC=E4=B8=80=E5=8F=AF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E5=8D=95=E6=9C=BA=E5=8D=87=E7=BA=A7=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- update/urls.py | 3 +- update/utils/client_util.py | 338 +++++++++++++++++++++++++++--------- update/utils/dingxin_sql.py | 7 + update/utils/git_util.py | 2 + update/views.py | 30 +++- 5 files changed, 295 insertions(+), 85 deletions(-) create mode 100644 update/utils/dingxin_sql.py diff --git a/update/urls.py b/update/urls.py index 263ae16..6cc5324 100644 --- a/update/urls.py +++ b/update/urls.py @@ -36,7 +36,8 @@ urlpatterns = [ path('test/get_cmd/', get_cmd), path('test/setup_cmd/', setup_cmd), path('test/teardown_cmd/', teardown_cmd), - path('test/get_client/', get_client) + path('test/get_client/', get_client), + path('test/update_cine/', update_cine) ] # &version=(?P[\S]*?) diff --git a/update/utils/client_util.py b/update/utils/client_util.py index 5e86dbf..5fe3d2d 100644 --- a/update/utils/client_util.py +++ b/update/utils/client_util.py @@ -1,10 +1,12 @@ import os import paramiko +from django.core.checks import database from smb.SMBConnection import SMBConnection from update.models import ClientRelease, Cinema import pymysql from pymysql.cursors import DictCursor from dingxin_toolbox_drf.settings import BASE_DIR +from update.utils.dingxin_sql import * class ClientUtil: @@ -12,104 +14,132 @@ class ClientUtil: 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.db_config = {'host': '172.16.3.139', 'port': 3306, 'user': 'clientdeploy', + 'password': 'clientdeploy123456', 'database': 'yhz_tool'} - def get_test_client_file(self): + def smb_connect(self): self.smb_conn.connect(self.smb_host, 445) - smb_files = self.smb_conn.listPath('data1', '/客户端/测试专用', pattern=r'client_v2.033*.7z') - for file in smb_files: - file_name = file.filename - print(file_name) - if ClientRelease.objects.filter(origin_name=file_name).first(): - continue - local_file = open(os.path.join(self.local_path, file_name), 'wb') - # 接收文件并写入本地文件 - self.smb_conn.retrieveFile('data1', rf'/客户端/测试专用/{file_name}', local_file) - # 关闭本地文件 - local_file.close() - self.write_db(file_name, 'test') + + def smb_disconnect(self): self.smb_conn.close() - # return smb_files - def get_prd_client_file(self): - self.smb_conn.connect(self.smb_host, 445) - prd_smb_files = self.smb_conn.listPath('data1', '/客户端/结测常用客户端', pattern=r'client_v2.033*.7z') - for file in prd_smb_files: - file_name = file.filename - print(file_name) - if ClientRelease.objects.filter(origin_name=file_name).first(): - continue - local_file = open(os.path.join(self.local_path, file_name), 'wb') - # 接收文件并写入本地文件 - self.smb_conn.retrieveFile('data1', rf'/客户端/结测常用客户端/{file_name}', local_file) - # 关闭本地文件 - local_file.close() - self.write_db(file_name, 'prd') - - history_smb_files = self.smb_conn.listPath('data1', '/客户端/结测常用客户端/历史客户端', - pattern=r'client_v2.033*.7z') - for file in history_smb_files: + def get_all_smb_client(self): + test_client_list = self.get_smb_file_list('data1', '/客户端/测试专用', r'client_v2.033*.7z') + prd_client_list = self.get_smb_file_list('data1', '/客户端/结测常用客户端', r'client_v2.033*.7z') + history_client_list = self.get_smb_file_list('data1', '/客户端/结测常用客户端/历史客户端', + r'client_v2.033*.7z', ) + return {'test': test_client_list, 'prd': prd_client_list, 'history': history_client_list} + + def get_smb_file_list(self, service_name, path, pattern): + smb_files = self.smb_conn.listPath(service_name, path, pattern=pattern) + file_list = [] + for file in smb_files: file_name = file.filename - print(file_name) - if ClientRelease.objects.filter(origin_name=file_name).first(): - continue - local_file = open(os.path.join(self.local_path, file_name), 'wb') - # 接收文件并写入本地文件 - self.smb_conn.retrieveFile('data1', rf'/客户端/结测常用客户端/历史客户端/{file_name}', local_file) - # 关闭本地文件 - local_file.close() - self.write_db(file_name, 'prd') - self.smb_conn.close() + file_list.append(file_name) + return file_list + + def handle_client_db(self): + client_data = self.get_all_smb_client() + ClientRelease.objects.update(is_delete=True) + self.write_db(client_data, 'test') + self.write_db(client_data, 'prd') + self.write_db(client_data, 'history') @staticmethod - def write_db(_file, _type): - # 写入数据库 - origin_name = _file - main_ver = _file[8:18] - sub_ver = _file[19:23] - upload_name = _file[:23] + '.7z' - client_ver = _file[8:23] - ver_id = int(_file[14:18]) - ClientRelease.objects.create(origin_name=origin_name, main_ver=main_ver, sub_ver=sub_ver, - upload_name=upload_name, client_type=_type, client_ver=client_ver, ver_id=ver_id) - - def update_client_file(self): - prd_clients = self.get_prd_client_file() - test_clients = self.get_test_client_file() - all_clients = prd_clients + test_clients - all_db_clients = ClientRelease.objects.filter(is_delete='0').all() - for db_client in all_db_clients: - if db_client['origin_name'] in all_clients: - continue + def write_db(_client_data, _type): + for client in _client_data[_type]: + file_name = client + if ClientRelease.objects.filter(origin_name=file_name).first(): + ClientRelease.objects.filter(origin_name=file_name).update(is_delete=False) else: - ClientRelease.objects.filter(pk=db_client['id']).update(is_delete='1') - path = f'../../dx/client/{db_client["origin_name"]}' - os.remove(path) + # 写入数据库 + origin_name = file_name + main_ver = file_name[8:18] + sub_ver = file_name[19:23] + upload_name = file_name[:23] + '.7z' + client_ver = file_name[8:23] + ver_id = int(file_name[14:18]) + ClientRelease.objects.create(origin_name=origin_name, main_ver=main_ver, sub_ver=sub_ver, + upload_name=upload_name, client_type=_type, client_ver=client_ver, + ver_id=ver_id) + + def clear_delete_file(self): + all_delete = ClientRelease.objects.filter(is_delete=True).all() + for delete in all_delete: + os.remove(os.path.join(self.local_path, delete.origin_name)) + + def get_client_info_from_deploy_db(self, short_ver): + client_ver = short_ver.replace('0.33', '033') + db_conn = pymysql.Connect(**self.db_config) + db_cursor = db_conn.cursor(cursor=DictCursor) + print(short_ver) + db_cursor.execute(GET_DEPLOY_CLIENT_INFO, (client_ver,)) + result = db_cursor.fetchone() + print(result) + # for version in result: + # if version['full_version'].startswith(short_ver): + # print(version['full_version']) + # return version['full_version'] + # return None + # # print(result) + if result is None: + return None + return result['full_version'] - def upload_client_file(self, cinema_ip, _release): + @staticmethod + def get_client_version_from_cine(cinema_ip): cine = Cinema.objects.filter(ip=cinema_ip).first() db_config = { 'host': cinema_ip, - 'username': cine['db_user'], - 'password': cine['db_pwd'], + 'user': cine.db_user, + 'password': cine.db_pwd, 'port': 3306, 'database': 'cine' } db_conn = pymysql.Connect(**db_config) db_cursor = db_conn.cursor(cursor=DictCursor) - # 获取客户端信息 - client = ClientRelease.objects.filter(main_ver=_release).order_by('-sub_ver').first() - print(client) - if client: - self.upload(cinema_ip, client['origin_name'], client['upload_name']) - db_cursor.execute('UPDATE cinema_version SET client_version = %s WHERE 1=1;', (client['client_ver'],)) + db_cursor.execute('SELECT client_version FROM cinema_version;') + client_ver = db_cursor.fetchone()['client_version'] + return client_ver + + def download_client_file(self, file_name, path): + local_file = open(os.path.join(self.local_path, file_name), 'wb') + # 接收文件并写入本地文件 + self.smb_conn.retrieveFile('data1', rf'{path}{file_name}', local_file) + # 关闭本地文件 + local_file.close() + + def get_client_version(self, cinema_ip, short_version): + print('get_client_version') + print(short_version) + client_version = self.get_client_info_from_deploy_db(short_version) + print(client_version) + if client_version is None: + client_version = self.get_client_version_from_cine(cinema_ip) + return client_version + + def get_client(self, cinema_ip, short_version): + client_version = self.get_client_version(cinema_ip, short_version) + # 查看本地是否有此版本客户端 + local_file_list = list(os.walk(self.local_path))[0][2] + for local_file in local_file_list: + if client_version in local_file: + print('找到本地文件') + return + # 如果本地没有则到服务器上拉取 + client = ClientRelease.objects.filter(client_ver=client_version).first() + print(client.upload_name) + if client.client_type == 'test': + self.download_client_file(client.origin_name, r'/客户端/测试专用/') + elif client.client_type == 'prd': + self.download_client_file(client.origin_name, r'/客户端/结测常用客户端/') + elif client.client_type == 'history': + self.download_client_file(client.origin_name, r'/客户端/结测常用客户端/历史客户端/') else: - db_cursor.execute('SELECT client_version FROM cinema_version;') - client_ver = db_cursor.fetchone()['client_version'] - client = ClientRelease.objects.filter(client_ver=client_ver).order_by('-sub_ver').first() - self.upload(cinema_ip, client['origin_name'], client['upload_name']) + print('检查下载客户端逻辑') - @staticmethod - def upload(cine_ip, origin, target): + # 传输客户端的方法 + def upload(self, cine_ip, origin, target): # 创建Transport客户端 trans = paramiko.Transport((cine_ip, 22)) # 使用密码连接服务器 @@ -118,7 +148,155 @@ class ClientUtil: sftp = paramiko.SFTPClient.from_transport(trans) # 上传文件 参数(本地文件路径, 远程文件路径) sftp.put( - f"../../dx/client/{origin}", + os.path.join(self.local_path, origin), f"/data0/cine/resource/upload/client/{target}") # 关闭客户端 trans.close() + + def upload_client(self, cinema_ip, short_version): + # 上传操作 + client_version = self.get_client_version(cinema_ip, short_version) + client = ClientRelease.objects.filter(client_ver=client_version).first() + self.upload(cinema_ip, client.origin_name, client.upload_name) + # 更新数据库 + cine = Cinema.objects.filter(ip=cinema_ip).first() + db_config = { + 'host': cinema_ip, + 'user': cine.db_user, + 'password': cine.db_pwd, + 'port': 3306, + 'database': 'cine' + } + db_conn = pymysql.Connect(**db_config) + db_cursor = db_conn.cursor(cursor=DictCursor) + db_cursor.execute('UPDATE cinema_version SET client_version = %s WHERE 1=1;', (client_version,)) + db_conn.commit() + db_cursor.close() + db_conn.close() + + def client_process(self, cinema_ip, short_release): + self.smb_connect() + short_version = short_release[:11] + print(short_version) + self.handle_client_db() + self.clear_delete_file() + self.get_client(cinema_ip, short_version) + self.upload_client(cinema_ip, short_version) + self.smb_disconnect() + +# +# class ClientUtil: +# 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') +# +# def get_test_client_file(self): +# self.smb_conn.connect(self.smb_host, 445) +# smb_files = self.smb_conn.listPath('data1', '/客户端/测试专用', pattern=r'client_v2.033*.7z') +# for file in smb_files: +# file_name = file.filename +# print(file_name) +# if ClientRelease.objects.filter(origin_name=file_name).first(): +# continue +# local_file = open(os.path.join(self.local_path, file_name), 'wb') +# # 接收文件并写入本地文件 +# self.smb_conn.retrieveFile('data1', rf'/客户端/测试专用/{file_name}', local_file) +# # 关闭本地文件 +# local_file.close() +# self.write_db(file_name, 'test') +# self.smb_conn.close() +# # return smb_files +# +# def get_prd_client_file(self): +# self.smb_conn.connect(self.smb_host, 445) +# prd_smb_files = self.smb_conn.listPath('data1', '/客户端/结测常用客户端', pattern=r'client_v2.033*.7z') +# for file in prd_smb_files: +# file_name = file.filename +# print(file_name) +# if ClientRelease.objects.filter(origin_name=file_name).first(): +# continue +# local_file = open(os.path.join(self.local_path, file_name), 'wb') +# # 接收文件并写入本地文件 +# self.smb_conn.retrieveFile('data1', rf'/客户端/结测常用客户端/{file_name}', local_file) +# # 关闭本地文件 +# local_file.close() +# self.write_db(file_name, 'prd') +# +# history_smb_files = self.smb_conn.listPath('data1', '/客户端/结测常用客户端/历史客户端', +# pattern=r'client_v2.033*.7z') +# for file in history_smb_files: +# file_name = file.filename +# print(file_name) +# if ClientRelease.objects.filter(origin_name=file_name).first(): +# continue +# local_file = open(os.path.join(self.local_path, file_name), 'wb') +# # 接收文件并写入本地文件 +# self.smb_conn.retrieveFile('data1', rf'/客户端/结测常用客户端/历史客户端/{file_name}', local_file) +# # 关闭本地文件 +# local_file.close() +# self.write_db(file_name, 'prd') +# self.smb_conn.close() +# +# @staticmethod +# def write_db(_file, _type): +# # 写入数据库 +# origin_name = _file +# main_ver = _file[8:18] +# sub_ver = _file[19:23] +# upload_name = _file[:23] + '.7z' +# client_ver = _file[8:23] +# ver_id = int(_file[14:18]) +# ClientRelease.objects.create(origin_name=origin_name, main_ver=main_ver, sub_ver=sub_ver, +# upload_name=upload_name, client_type=_type, client_ver=client_ver, ver_id=ver_id) +# +# def update_client_file(self): +# prd_clients = self.get_prd_client_file() +# test_clients = self.get_test_client_file() +# all_clients = prd_clients + test_clients +# all_db_clients = ClientRelease.objects.filter(is_delete='0').all() +# for db_client in all_db_clients: +# if db_client['origin_name'] in all_clients: +# continue +# else: +# ClientRelease.objects.filter(pk=db_client['id']).update(is_delete='1') +# path = f'../../dx/client/{db_client["origin_name"]}' +# os.remove(path) +# +# def upload_client_file(self, cinema_ip, _release): +# cine = Cinema.objects.filter(ip=cinema_ip).first() +# db_config = { +# 'host': cinema_ip, +# 'username': cine['db_user'], +# 'password': cine['db_pwd'], +# 'port': 3306, +# 'database': 'cine' +# } +# db_conn = pymysql.Connect(**db_config) +# db_cursor = db_conn.cursor(cursor=DictCursor) +# # 获取客户端信息 +# client = ClientRelease.objects.filter(main_ver=_release).order_by('-sub_ver').first() +# print(client) +# if client: +# self.upload(cinema_ip, client['origin_name'], client['upload_name']) +# db_cursor.execute('UPDATE cinema_version SET client_version = %s WHERE 1=1;', (client['client_ver'],)) +# else: +# db_cursor.execute('SELECT client_version FROM cinema_version;') +# client_ver = db_cursor.fetchone()['client_version'] +# client = ClientRelease.objects.filter(client_ver=client_ver).order_by('-sub_ver').first() +# self.upload(cinema_ip, client['origin_name'], client['upload_name']) +# +# @staticmethod +# def upload(cine_ip, origin, target): +# # 创建Transport客户端 +# trans = paramiko.Transport((cine_ip, 22)) +# # 使用密码连接服务器 +# trans.connect(username='root', password='cine123456') +# # 创建SFTP客户端 +# sftp = paramiko.SFTPClient.from_transport(trans) +# # 上传文件 参数(本地文件路径, 远程文件路径) +# sftp.put( +# f"../../dx/client/{origin}", +# f"/data0/cine/resource/upload/client/{target}") +# # 关闭客户端 +# trans.close() diff --git a/update/utils/dingxin_sql.py b/update/utils/dingxin_sql.py new file mode 100644 index 0000000..d638c37 --- /dev/null +++ b/update/utils/dingxin_sql.py @@ -0,0 +1,7 @@ +GET_DEPLOY_CLIENT_INFO = """SELECT cdl.version_full as full_version, + cdl.version_revise as sub_version +FROM client_deploy_log cdl +WHERE publish_type = '通用' + AND id > 162 + AND SUBSTRING(cdl.version_full, 1, 10) = %s +ORDER BY sub_version DESC LIMIT 1;""" \ No newline at end of file diff --git a/update/utils/git_util.py b/update/utils/git_util.py index a357ec5..ac658a2 100644 --- a/update/utils/git_util.py +++ b/update/utils/git_util.py @@ -81,9 +81,11 @@ class GitUtil: if os.path.exists(target_org_path): if self.get_md5(org_path) == self.get_md5(target_org_path): + print('md5相同') if os.path.exists(target_path): return False else: + print('md5不同') os.remove(target_org_path) os.remove(target_path) shutil.copyfile(org_path, target_org_path) diff --git a/update/views.py b/update/views.py index ed2d0bf..e7f4c96 100644 --- a/update/views.py +++ b/update/views.py @@ -93,9 +93,6 @@ def write_cine_sql(request): def compare_db_structor(request): db_compare = DbCompare('172.16.3.88', '2.0.33.0338_Release') - # db_compare.gen_diff_file() - # db_compare.remove_drop() - # db_compare.gen_create_file() db_compare.exec_diff_sql() return JsonResponse({'result': 'success'}) @@ -131,5 +128,30 @@ def teardown_cmd(request): def get_client(request): client_release = ClientUtil() - client_release.get_test_client_file() + client_release.client_process('172.16.3.88', '2.0.33.0338_Release') return JsonResponse({'result': 'success'}) + + +def update_cine(request): + req = request.GET.dict() + cinema_ip = req.get('cinema_ip') + short_release = req.get('short_release') + print(cinema_ip, short_release) + # cinema_ip = '172.16.3.88' + # short_release = '2.0.33.0338_Release' + cmd_list = [10, 11, 12, 13, 14, 15, 16, 17, 19, 20] + # 获取并写入cine.sql + git_util = GitUtil(short_release) + git_util.handle_create_cine() + # 数据库对比 + db_compare = DbCompare(cinema_ip, short_release) + db_compare.exec_diff_sql() + # 执行setup + update_cmd = UpdateCommandUtil() + update_cmd.exec_cmd_by_type(cinema_ip, 'setup', cmd_list, short_release) + # 执行teardown + update_cmd.exec_cmd_by_type(cinema_ip, 'teardown', cmd_list, short_release) + # 传输客户端 + client_release = ClientUtil() + client_release.client_process(cinema_ip, short_release) + return JsonResponse({'result': 'success'}) \ No newline at end of file