diff --git a/dingxin_toolbox_drf/settings.py b/dingxin_toolbox_drf/settings.py index 09ecd52..e6384eb 100644 --- a/dingxin_toolbox_drf/settings.py +++ b/dingxin_toolbox_drf/settings.py @@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/4.2/ref/settings/ from pathlib import Path import os +from env import ENV # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -19,11 +20,35 @@ BASE_DIR = Path(__file__).resolve().parent.parent print(BASE_DIR) # 服务器IP -# SERVER_IP = '172.16.1.63' -# REDIS_IP = '172.16.1.63' -SERVER_IP = '172.16.1.222' -REDIS_IP = '172.16.1.222' -# SERVER_IP = '' +# # SERVER_IP = '172.16.1.63' +# # REDIS_IP = '172.16.1.63' +# SERVER_IP = '172.16.1.222' +# REDIS_IP = '172.16.1.222' +# # SERVER_IP = '' +CONFIG = { + 'dev': { + 'SERVER_IP': '172.16.1.63', + 'DB': { + "HOST": 'home.rogersun.cn', + "PORT": 3306, + "USER": "dingxin", + "PASSWORD": "cine123456", + "NAME": "dingxin_toolbox", + }, + 'REDIS_IP': '172.16.1.63' + }, + 'prd': { + 'SERVER_IP': '172.16.1.222', + 'DB': { + "HOST": '172.16.1.222', + "PORT": 3306, + "USER": "dingxin", + "PASSWORD": "cine123456", + "NAME": "dingxin_toolbox", + }, + 'REDIS_IP': '172.16.1.222' + } +} # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ @@ -98,11 +123,11 @@ DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", # "HOST": "home.rogersun.cn", - "HOST": SERVER_IP, - "PORT": 3306, - "USER": "dingxin", - "PASSWORD": "cine123456", - "NAME": "dingxin_toolbox", + "HOST": CONFIG[ENV]['DB']['HOST'], + "PORT": CONFIG[ENV]['DB']['PORT'], + "USER": CONFIG[ENV]['DB']['USER'], + "PASSWORD": CONFIG[ENV]['DB']['PASSWORD'], + "NAME": CONFIG[ENV]['DB']['NAME'], } } @@ -110,7 +135,7 @@ CACHES = { "default": { 'BACKEND': 'django_redis.cache.RedisCache', # "BACKEND": "django.core.cache.backends.redis.RedisCache", - "LOCATION": f"redis://{REDIS_IP}:6379", + "LOCATION": f"redis://{CONFIG[ENV]['REDIS_IP']}:6379", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", @@ -230,7 +255,7 @@ CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { - "hosts": [(REDIS_IP, 6379)], + "hosts": [(CONFIG[ENV]['REDIS_IP'], 6379)], # "hosts": [os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379/1')] } } diff --git a/env.py b/env.py new file mode 100644 index 0000000..380ee2a --- /dev/null +++ b/env.py @@ -0,0 +1 @@ +ENV = 'dev' diff --git a/mock/views.py b/mock/views.py index 3de0523..8ae81b8 100644 --- a/mock/views.py +++ b/mock/views.py @@ -14,7 +14,8 @@ from django.core.management.commands.runserver import Command as runserver import socket from mock.mock_templates import * from mock.cinema_sql import * -from dingxin_toolbox_drf.settings import SERVER_IP +from dingxin_toolbox_drf.settings import CONFIG +from env import ENV ZZ_URL = 'https://zzcs.yinghezhong.com' mock_config = dict() @@ -213,8 +214,8 @@ def get_response(request): # 获取本机 IP 地址: def get_local_ip(): - ip = SERVER_IP - if SERVER_IP == '': + ip = CONFIG[ENV]['SERVER_IP'] + if CONFIG[ENV]['SERVER_IP'] == '': r = runserver() ip = socket.gethostbyname(socket.gethostname()) print(ip) diff --git a/update/utils/client_util.py b/update/utils/client_util.py index 9412deb..968cb46 100644 --- a/update/utils/client_util.py +++ b/update/utils/client_util.py @@ -5,6 +5,7 @@ from smb.SMBConnection import SMBConnection from update.models import ClientRelease, Cinema import pymysql from pymysql.cursors import DictCursor +from django.db.models import Q from dingxin_toolbox_drf.settings import BASE_DIR from update.utils.dingxin_sql import * @@ -38,6 +39,13 @@ class ClientUtil: file_list.append(file_name) return file_list + def get_all_smb_file_list(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_client_list + prd_client_list + history_client_list + def handle_client_db(self): client_data = self.get_all_smb_client() ClientRelease.objects.update(is_delete=True) @@ -66,16 +74,16 @@ class ClientUtil: 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)) + file = os.path.join(self.local_path, delete.origin_name) + if os.path.exists(file): + os.remove(file) 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) if result is None: return None return result['full_version'] @@ -104,10 +112,8 @@ class ClientUtil: 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) - test_client_list = self.get_smb_file_list('data1', '/客户端/测试专用', r'client_v2.033*.7z') + test_client_list = self.get_all_smb_file_list() # 客户端打包服务器上有,smb上没有 is_exist = False for test_client in test_client_list: @@ -116,12 +122,9 @@ class ClientUtil: if is_exist is False: client = ClientRelease.objects.filter(ver_id=int(short_version[7:])).order_by('-sub_ver').first() client_version = client.client_ver - print(is_exist) - print(client_version) # 新版本还没有对应的包 if client_version is None: client_version = self.get_client_version_from_cine(cinema_ip) - print(client_version) return client_version def get_client(self, cinema_ip, short_version): @@ -133,8 +136,7 @@ class ClientUtil: print('找到本地文件') return # 如果本地没有则到服务器上拉取 - client = ClientRelease.objects.filter(client_ver=client_version).first() - print(client.upload_name) + client = ClientRelease.objects.filter(Q(client_ver=client_version) & Q(is_delete=False)).first() if client.client_type == 'test': self.download_client_file(client.origin_name, r'/客户端/测试专用/') elif client.client_type == 'prd': @@ -162,7 +164,7 @@ class ClientUtil: 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() + client = ClientRelease.objects.filter(Q(client_ver=client_version) & Q(is_delete=False)).first() self.upload(cinema_ip, client.origin_name, client.upload_name) # 更新数据库 cine = Cinema.objects.filter(ip=cinema_ip).first() @@ -183,7 +185,6 @@ class ClientUtil: 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) diff --git a/update/views.py b/update/views.py index f01436a..3e4b41b 100644 --- a/update/views.py +++ b/update/views.py @@ -117,6 +117,7 @@ def update_cine(request): # 增加状态 update_status = { + 'result': '', 'git': {'result': '', 'msg': ''}, 'setup': {'result': '', 'msg': ''}, 'sql': {'result': '', 'msg': ''}, @@ -133,10 +134,14 @@ def update_cine(request): if redis_conn.exists(redis_key): # update_status = copy.deepcopy(json.loads(redis_conn.get(redis_key))) - print('redis存在key', redis_key) - return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': '该影院正在升级中,请稍后再试!'}) - else: - redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) + val = json.loads(redis_conn.get(redis_key)) + print(val) + if val['result'] == 'running': + print('redis存在key', redis_key) + return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': '该影院正在升级中,请稍后再试!'}) + # 如果存在对应的键值,且结果为running,则提示升级中 + + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) print('update_status', update_status) @@ -151,6 +156,9 @@ def update_cine(request): # short_release = '2.0.33.0338_Release' # cmd_list = [10, 11, 12, 13, 14, 15, 16, 17, 19, 20] + update_status['result'] = 'running' + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) + # 获取并写入cine.sql print('获取并写入cine.sql') try: @@ -163,8 +171,10 @@ def update_cine(request): redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) except Exception as e: print('git', e) + update_status['result'] = 'fail' update_status['git']['result'] = 'fail' update_status['git']['msg'] = str(e) + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) # redis_conn.delete(redis_key) return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) @@ -177,14 +187,17 @@ def update_cine(request): update_cmd = UpdateCommandUtil() result, setup_output = update_cmd.exec_cmd_by_type(cinema_ip, 'setup', setup_list, short_release) if result is False: + print('执行setup特殊处理') raise Exception(setup_output) update_status['setup']['result'] = 'success' update_status['setup']['msg'] = setup_output redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) except Exception as e: print('setup', e) + update_status['result'] = 'fail' update_status['setup']['result'] = 'fail' update_status['setup']['msg'] = str(e) + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) # redis_conn.delete(redis_key) return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) @@ -202,8 +215,10 @@ def update_cine(request): redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) except Exception as e: print('sql', e) + update_status['result'] = 'fail' update_status['sql']['result'] = 'fail' update_status['sql']['msg'] = str(e) + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) # redis_conn.delete(redis_key) return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) @@ -220,8 +235,10 @@ def update_cine(request): redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) except Exception as e: print('config', e) + update_status['result'] = 'fail' update_status['config']['result'] = 'fail' update_status['config']['msg'] = str(e) + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) # redis_conn.delete(redis_key) return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) @@ -241,8 +258,10 @@ def update_cine(request): redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) except Exception as e: print('teardown', e) + update_status['result'] = 'fail' update_status['teardown']['result'] = 'fail' update_status['teardown']['msg'] = str(e) + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) # redis_conn.delete(redis_key) return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) @@ -259,8 +278,10 @@ def update_cine(request): redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) except Exception as e: print('config', e) + update_status['result'] = 'fail' update_status['config']['result'] = 'fail' update_status['config']['msg'] = config_before_output + '
' + str(e) + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) # redis_conn.delete(redis_key) return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) @@ -272,17 +293,23 @@ def update_cine(request): update_status['client']['result'] = 'running' redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) client_release = ClientUtil() + print('client_release.client_process') client_release.client_process(cinema_ip, short_release) update_status['client']['result'] = 'success' update_status['client']['msg'] = '客户端上传成功' redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) except Exception as e: print('client', e) + update_status['result'] = 'fail' update_status['client']['result'] = 'fail' update_status['client']['msg'] = str(e) + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) # redis_conn.delete(redis_key) return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) + update_status['result'] = 'success' + redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) + print('update_status', update_status) print('redis_get', redis_conn.get(redis_key)) return JsonResponse({'result': 'success', 'ip': cinema_ip, 'msg': ''})