You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
5.4 KiB
124 lines
5.4 KiB
import os |
|
import paramiko |
|
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 |
|
|
|
|
|
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()
|
|
|