整理线上配置

main
RogerWork 8 months ago
parent 77023d8538
commit a3ee2ee85e
  1. 49
      dingxin_toolbox_drf/settings.py
  2. 1
      env.py
  3. 7
      mock/views.py
  4. 27
      update/utils/client_util.py
  5. 35
      update/views.py

@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/4.2/ref/settings/
from pathlib import Path from pathlib import Path
import os import os
from env import ENV
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
@ -19,11 +20,35 @@ BASE_DIR = Path(__file__).resolve().parent.parent
print(BASE_DIR) print(BASE_DIR)
# 服务器IP # 服务器IP
# SERVER_IP = '172.16.1.63' # # SERVER_IP = '172.16.1.63'
# REDIS_IP = '172.16.1.63' # # REDIS_IP = '172.16.1.63'
SERVER_IP = '172.16.1.222' # SERVER_IP = '172.16.1.222'
REDIS_IP = '172.16.1.222' # REDIS_IP = '172.16.1.222'
# SERVER_IP = '' # # 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 # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
@ -98,11 +123,11 @@ DATABASES = {
"default": { "default": {
"ENGINE": "django.db.backends.mysql", "ENGINE": "django.db.backends.mysql",
# "HOST": "home.rogersun.cn", # "HOST": "home.rogersun.cn",
"HOST": SERVER_IP, "HOST": CONFIG[ENV]['DB']['HOST'],
"PORT": 3306, "PORT": CONFIG[ENV]['DB']['PORT'],
"USER": "dingxin", "USER": CONFIG[ENV]['DB']['USER'],
"PASSWORD": "cine123456", "PASSWORD": CONFIG[ENV]['DB']['PASSWORD'],
"NAME": "dingxin_toolbox", "NAME": CONFIG[ENV]['DB']['NAME'],
} }
} }
@ -110,7 +135,7 @@ CACHES = {
"default": { "default": {
'BACKEND': 'django_redis.cache.RedisCache', 'BACKEND': 'django_redis.cache.RedisCache',
# "BACKEND": "django.core.cache.backends.redis.RedisCache", # "BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": f"redis://{REDIS_IP}:6379", "LOCATION": f"redis://{CONFIG[ENV]['REDIS_IP']}:6379",
"OPTIONS": { "OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient", "CLIENT_CLASS": "django_redis.client.DefaultClient",
@ -230,7 +255,7 @@ CHANNEL_LAYERS = {
"default": { "default": {
"BACKEND": "channels_redis.core.RedisChannelLayer", "BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": { "CONFIG": {
"hosts": [(REDIS_IP, 6379)], "hosts": [(CONFIG[ENV]['REDIS_IP'], 6379)],
# "hosts": [os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379/1')] # "hosts": [os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379/1')]
} }
} }

@ -0,0 +1 @@
ENV = 'dev'

@ -14,7 +14,8 @@ from django.core.management.commands.runserver import Command as runserver
import socket import socket
from mock.mock_templates import * from mock.mock_templates import *
from mock.cinema_sql 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' ZZ_URL = 'https://zzcs.yinghezhong.com'
mock_config = dict() mock_config = dict()
@ -213,8 +214,8 @@ def get_response(request):
# 获取本机 IP 地址: # 获取本机 IP 地址:
def get_local_ip(): def get_local_ip():
ip = SERVER_IP ip = CONFIG[ENV]['SERVER_IP']
if SERVER_IP == '': if CONFIG[ENV]['SERVER_IP'] == '':
r = runserver() r = runserver()
ip = socket.gethostbyname(socket.gethostname()) ip = socket.gethostbyname(socket.gethostname())
print(ip) print(ip)

@ -5,6 +5,7 @@ from smb.SMBConnection import SMBConnection
from update.models import ClientRelease, Cinema from update.models import ClientRelease, Cinema
import pymysql import pymysql
from pymysql.cursors import DictCursor from pymysql.cursors import DictCursor
from django.db.models import Q
from dingxin_toolbox_drf.settings import BASE_DIR from dingxin_toolbox_drf.settings import BASE_DIR
from update.utils.dingxin_sql import * from update.utils.dingxin_sql import *
@ -38,6 +39,13 @@ class ClientUtil:
file_list.append(file_name) file_list.append(file_name)
return file_list 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): def handle_client_db(self):
client_data = self.get_all_smb_client() client_data = self.get_all_smb_client()
ClientRelease.objects.update(is_delete=True) ClientRelease.objects.update(is_delete=True)
@ -66,16 +74,16 @@ class ClientUtil:
def clear_delete_file(self): def clear_delete_file(self):
all_delete = ClientRelease.objects.filter(is_delete=True).all() all_delete = ClientRelease.objects.filter(is_delete=True).all()
for delete in all_delete: 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): def get_client_info_from_deploy_db(self, short_ver):
client_ver = short_ver.replace('0.33', '033') client_ver = short_ver.replace('0.33', '033')
db_conn = pymysql.Connect(**self.db_config) db_conn = pymysql.Connect(**self.db_config)
db_cursor = db_conn.cursor(cursor=DictCursor) db_cursor = db_conn.cursor(cursor=DictCursor)
print(short_ver)
db_cursor.execute(GET_DEPLOY_CLIENT_INFO, (client_ver,)) db_cursor.execute(GET_DEPLOY_CLIENT_INFO, (client_ver,))
result = db_cursor.fetchone() result = db_cursor.fetchone()
print(result)
if result is None: if result is None:
return None return None
return result['full_version'] return result['full_version']
@ -104,10 +112,8 @@ class ClientUtil:
local_file.close() local_file.close()
def get_client_version(self, cinema_ip, short_version): 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) 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上没有 # 客户端打包服务器上有,smb上没有
is_exist = False is_exist = False
for test_client in test_client_list: for test_client in test_client_list:
@ -116,12 +122,9 @@ class ClientUtil:
if is_exist is False: if is_exist is False:
client = ClientRelease.objects.filter(ver_id=int(short_version[7:])).order_by('-sub_ver').first() client = ClientRelease.objects.filter(ver_id=int(short_version[7:])).order_by('-sub_ver').first()
client_version = client.client_ver client_version = client.client_ver
print(is_exist)
print(client_version)
# 新版本还没有对应的包 # 新版本还没有对应的包
if client_version is None: if client_version is None:
client_version = self.get_client_version_from_cine(cinema_ip) client_version = self.get_client_version_from_cine(cinema_ip)
print(client_version)
return client_version return client_version
def get_client(self, cinema_ip, short_version): def get_client(self, cinema_ip, short_version):
@ -133,8 +136,7 @@ class ClientUtil:
print('找到本地文件') print('找到本地文件')
return return
# 如果本地没有则到服务器上拉取 # 如果本地没有则到服务器上拉取
client = ClientRelease.objects.filter(client_ver=client_version).first() client = ClientRelease.objects.filter(Q(client_ver=client_version) & Q(is_delete=False)).first()
print(client.upload_name)
if client.client_type == 'test': if client.client_type == 'test':
self.download_client_file(client.origin_name, r'/客户端/测试专用/') self.download_client_file(client.origin_name, r'/客户端/测试专用/')
elif client.client_type == 'prd': elif client.client_type == 'prd':
@ -162,7 +164,7 @@ class ClientUtil:
def upload_client(self, cinema_ip, short_version): def upload_client(self, cinema_ip, short_version):
# 上传操作 # 上传操作
client_version = self.get_client_version(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) self.upload(cinema_ip, client.origin_name, client.upload_name)
# 更新数据库 # 更新数据库
cine = Cinema.objects.filter(ip=cinema_ip).first() cine = Cinema.objects.filter(ip=cinema_ip).first()
@ -183,7 +185,6 @@ class ClientUtil:
def client_process(self, cinema_ip, short_release): def client_process(self, cinema_ip, short_release):
self.smb_connect() self.smb_connect()
short_version = short_release[:11] short_version = short_release[:11]
print(short_version)
self.handle_client_db() self.handle_client_db()
self.clear_delete_file() self.clear_delete_file()
self.get_client(cinema_ip, short_version) self.get_client(cinema_ip, short_version)

@ -117,6 +117,7 @@ def update_cine(request):
# 增加状态 # 增加状态
update_status = { update_status = {
'result': '',
'git': {'result': '', 'msg': ''}, 'git': {'result': '', 'msg': ''},
'setup': {'result': '', 'msg': ''}, 'setup': {'result': '', 'msg': ''},
'sql': {'result': '', 'msg': ''}, 'sql': {'result': '', 'msg': ''},
@ -133,10 +134,14 @@ def update_cine(request):
if redis_conn.exists(redis_key): if redis_conn.exists(redis_key):
# update_status = copy.deepcopy(json.loads(redis_conn.get(redis_key))) # update_status = copy.deepcopy(json.loads(redis_conn.get(redis_key)))
print('redis存在key', redis_key) val = json.loads(redis_conn.get(redis_key))
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': '该影院正在升级中,请稍后再试!'}) print(val)
else: if val['result'] == 'running':
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) 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) print('update_status', update_status)
@ -151,6 +156,9 @@ def update_cine(request):
# short_release = '2.0.33.0338_Release' # short_release = '2.0.33.0338_Release'
# cmd_list = [10, 11, 12, 13, 14, 15, 16, 17, 19, 20] # 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 # 获取并写入cine.sql
print('获取并写入cine.sql') print('获取并写入cine.sql')
try: try:
@ -163,8 +171,10 @@ def update_cine(request):
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e: except Exception as e:
print('git', e) print('git', e)
update_status['result'] = 'fail'
update_status['git']['result'] = 'fail' update_status['git']['result'] = 'fail'
update_status['git']['msg'] = str(e) update_status['git']['msg'] = str(e)
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key) # redis_conn.delete(redis_key)
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)})
@ -177,14 +187,17 @@ def update_cine(request):
update_cmd = UpdateCommandUtil() update_cmd = UpdateCommandUtil()
result, setup_output = update_cmd.exec_cmd_by_type(cinema_ip, 'setup', setup_list, short_release) result, setup_output = update_cmd.exec_cmd_by_type(cinema_ip, 'setup', setup_list, short_release)
if result is False: if result is False:
print('执行setup特殊处理')
raise Exception(setup_output) raise Exception(setup_output)
update_status['setup']['result'] = 'success' update_status['setup']['result'] = 'success'
update_status['setup']['msg'] = setup_output update_status['setup']['msg'] = setup_output
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e: except Exception as e:
print('setup', e) print('setup', e)
update_status['result'] = 'fail'
update_status['setup']['result'] = 'fail' update_status['setup']['result'] = 'fail'
update_status['setup']['msg'] = str(e) update_status['setup']['msg'] = str(e)
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key) # redis_conn.delete(redis_key)
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) 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) redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e: except Exception as e:
print('sql', e) print('sql', e)
update_status['result'] = 'fail'
update_status['sql']['result'] = 'fail' update_status['sql']['result'] = 'fail'
update_status['sql']['msg'] = str(e) update_status['sql']['msg'] = str(e)
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key) # redis_conn.delete(redis_key)
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) 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) redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e: except Exception as e:
print('config', e) print('config', e)
update_status['result'] = 'fail'
update_status['config']['result'] = 'fail' update_status['config']['result'] = 'fail'
update_status['config']['msg'] = str(e) update_status['config']['msg'] = str(e)
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key) # redis_conn.delete(redis_key)
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) 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) redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e: except Exception as e:
print('teardown', e) print('teardown', e)
update_status['result'] = 'fail'
update_status['teardown']['result'] = 'fail' update_status['teardown']['result'] = 'fail'
update_status['teardown']['msg'] = str(e) update_status['teardown']['msg'] = str(e)
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key) # redis_conn.delete(redis_key)
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) 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) redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e: except Exception as e:
print('config', e) print('config', e)
update_status['result'] = 'fail'
update_status['config']['result'] = 'fail' update_status['config']['result'] = 'fail'
update_status['config']['msg'] = config_before_output + '<br/>' + str(e) update_status['config']['msg'] = config_before_output + '<br/>' + str(e)
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key) # redis_conn.delete(redis_key)
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)})
@ -272,17 +293,23 @@ def update_cine(request):
update_status['client']['result'] = 'running' update_status['client']['result'] = 'running'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
client_release = ClientUtil() client_release = ClientUtil()
print('client_release.client_process')
client_release.client_process(cinema_ip, short_release) client_release.client_process(cinema_ip, short_release)
update_status['client']['result'] = 'success' update_status['client']['result'] = 'success'
update_status['client']['msg'] = '客户端上传成功' update_status['client']['msg'] = '客户端上传成功'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60) redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e: except Exception as e:
print('client', e) print('client', e)
update_status['result'] = 'fail'
update_status['client']['result'] = 'fail' update_status['client']['result'] = 'fail'
update_status['client']['msg'] = str(e) update_status['client']['msg'] = str(e)
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key) # redis_conn.delete(redis_key)
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)}) 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('update_status', update_status)
print('redis_get', redis_conn.get(redis_key)) print('redis_get', redis_conn.get(redis_key))
return JsonResponse({'result': 'success', 'ip': cinema_ip, 'msg': ''}) return JsonResponse({'result': 'success', 'ip': cinema_ip, 'msg': ''})

Loading…
Cancel
Save