整理线上配置

main
RogerWork 9 months ago
parent 40b7d979db
commit 36b9d9a369
  1. 18
      update/consumers.py
  2. 115
      update/views.py

@ -1,6 +1,7 @@
import json import json
from update.views import get_cinema_update_status from update.views import get_cinema_update_status
from channels.generic.websocket import WebsocketConsumer from channels.generic.websocket import WebsocketConsumer
from django_redis import get_redis_connection
class UpdateConsumer(WebsocketConsumer): class UpdateConsumer(WebsocketConsumer):
@ -9,22 +10,29 @@ class UpdateConsumer(WebsocketConsumer):
self.send('你已经连接成功') self.send('你已经连接成功')
def receive(self, text_data=None, bytes_data=None): def receive(self, text_data=None, bytes_data=None):
redis_conn = get_redis_connection()
if text_data is None: if text_data is None:
self.send('你发了啥') self.send('你发了啥')
else: else:
data = json.loads(text_data) data = json.loads(text_data)
cinema_update_status = get_cinema_update_status() # cinema_update_status = get_cinema_update_status()
if data.get('msg', False): if data.get('msg', False):
if data['msg'] == 'ping': if data['msg'] == 'ping':
self.send(json.dumps({'msg': 'pong'})) self.send(json.dumps({'msg': 'pong'}))
if data.get('finish', False): if data.get('finish', False):
ip = data['finish'] ip = data['finish']
if cinema_update_status.get(ip): redis_key = f'cinema_update_status_{ip}'
del cinema_update_status[ip] if redis_conn.exists(redis_key):
redis_conn.delete(redis_key)
print(cinema_update_status.get(ip, None)) print(cinema_update_status.get(ip, None))
if data.get('ip', False): if data.get('ip', False):
ip = data['ip'] ip = data['ip']
print('ws发送消息前', cinema_update_status) redis_key = f'cinema_update_status_{ip}'
self.send(json.dumps({'status': {'ip': ip, 'result': cinema_update_status.get(ip, None)}})) # print('ws发送消息前', cinema_update_status)
data = json.loads(redis_conn.get(redis_key))
self.send(json.dumps({'status': {'ip': ip, 'result': data}}))
else: else:
self.send(text_data) self.send(text_data)

@ -26,7 +26,7 @@ from update.utils.client_util import ClientUtil
# from consumers import UpdateConsumer # from consumers import UpdateConsumer
cinema_update_status = dict() # cinema_update_status = dict()
# CacheResponseMixin 一定要放第一位 # CacheResponseMixin 一定要放第一位
@ -114,9 +114,6 @@ def update_cine(request):
cmd_dict[cmd['process']] = [cmd['id']] cmd_dict[cmd['process']] = [cmd['id']]
print(cmd_dict) print(cmd_dict)
# 初始化redis
redis_conn = get_redis_connection()
# 增加状态 # 增加状态
update_status = { update_status = {
'git': {'result': '', 'msg': ''}, 'git': {'result': '', 'msg': ''},
@ -127,10 +124,23 @@ def update_cine(request):
'client': {'result': '', 'msg': ''}, 'client': {'result': '', 'msg': ''},
} }
cinema_update_status[cinema_ip] = copy.deepcopy(update_status) # cinema_update_status[cinema_ip] = copy.deepcopy(update_status)
# 初始化redis
redis_conn = get_redis_connection()
redis_key = f'cinema_update_status_{cinema_ip}'
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), 10*60)
print('update_status', update_status)
# redis_conn.set(f'cinema_update_status_{cinema_ip}', json.dumps(update_status), 10*60) # redis_conn.set(f'cinema_update_status_{cinema_ip}', json.dumps(update_status), 10*60)
print("ws调试", cinema_update_status) # print("ws调试", cinema_update_status)
short_release = req.get('version') short_release = req.get('version')
cmd_list = json.loads(req.get('cmd')) cmd_list = json.loads(req.get('cmd'))
@ -143,32 +153,36 @@ def update_cine(request):
# 获取并写入cine.sql # 获取并写入cine.sql
print('获取并写入cine.sql') print('获取并写入cine.sql')
try: try:
cinema_update_status[cinema_ip]['git']['result'] = 'running' update_status['git']['result'] = 'running'
git_util = GitUtil(short_release) git_util = GitUtil(short_release)
git_output = git_util.handle_create_cine() git_output = git_util.handle_create_cine()
cinema_update_status[cinema_ip]['git']['result'] = 'success' update_status['git']['result'] = 'success'
cinema_update_status[cinema_ip]['git']['msg'] = git_output update_status['git']['msg'] = git_output
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)
cinema_update_status[cinema_ip]['git']['result'] = 'fail' update_status['git']['result'] = 'fail'
cinema_update_status[cinema_ip]['git']['msg'] = str(e) update_status['git']['msg'] = str(e)
# 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)})
# 执行setup # 执行setup
print('执行setup') print('执行setup')
try: try:
cinema_update_status[cinema_ip]['setup']['result'] = 'running' update_status['setup']['result'] = 'running'
setup_list = list(set(cmd_list) & set(cmd_dict['setup'])) setup_list = list(set(cmd_list) & set(cmd_dict['setup']))
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:
raise Exception(setup_output) raise Exception(setup_output)
cinema_update_status[cinema_ip]['setup']['result'] = 'success' update_status['setup']['result'] = 'success'
cinema_update_status[cinema_ip]['setup']['msg'] = setup_output update_status['setup']['msg'] = setup_output
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)
cinema_update_status[cinema_ip]['setup']['result'] = 'fail' update_status['setup']['result'] = 'fail'
cinema_update_status[cinema_ip]['setup']['msg'] = str(e) update_status['setup']['msg'] = str(e)
# 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)})
# 数据库对比 # 数据库对比
@ -176,30 +190,34 @@ def update_cine(request):
if len(sql_list) > 0: if len(sql_list) > 0:
print('数据库对比') print('数据库对比')
try: try:
cinema_update_status[cinema_ip]['sql']['result'] = 'running' update_status['sql']['result'] = 'running'
db_compare = DbCompare(cinema_ip, short_release) db_compare = DbCompare(cinema_ip, short_release)
sql_output = db_compare.exec_diff_sql() sql_output = db_compare.exec_diff_sql()
cinema_update_status[cinema_ip]['sql']['result'] = 'success' update_status['sql']['result'] = 'success'
cinema_update_status[cinema_ip]['sql']['msg'] = sql_output update_status['sql']['msg'] = sql_output
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)
cinema_update_status[cinema_ip]['sql']['result'] = 'fail' update_status['sql']['result'] = 'fail'
cinema_update_status[cinema_ip]['sql']['msg'] = str(e) update_status['sql']['msg'] = str(e)
# 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)})
# 修改设置 # 修改设置
print('执行config(teardown前)') print('执行config(teardown前)')
try: try:
cinema_update_status[cinema_ip]['config']['result'] = 'running' update_status['config']['result'] = 'running'
config_list = list(set(cmd_list) & set(cmd_dict['config'])) config_list = list(set(cmd_list) & set(cmd_dict['config']))
update_config = UpdateConfigUtil(cinema_ip, config_list, True) update_config = UpdateConfigUtil(cinema_ip, config_list, True)
config_before_output = update_config.exec_config() config_before_output = update_config.exec_config()
cinema_update_status[cinema_ip]['config']['result'] = 'success' update_status['config']['result'] = 'success'
cinema_update_status[cinema_ip]['config']['msg'] = config_before_output update_status['config']['msg'] = config_before_output
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)
cinema_update_status[cinema_ip]['config']['result'] = 'fail' update_status['config']['result'] = 'fail'
cinema_update_status[cinema_ip]['config']['msg'] = str(e) update_status['config']['msg'] = str(e)
# 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)})
# 执行teardown # 执行teardown
@ -207,35 +225,38 @@ def update_cine(request):
if len(teardown_list) > 0: if len(teardown_list) > 0:
print('执行teardown') print('执行teardown')
try: try:
cinema_update_status[cinema_ip]['teardown']['result'] = 'running' update_status['teardown']['result'] = 'running'
update_cmd = UpdateCommandUtil() update_cmd = UpdateCommandUtil()
result, teardown_output = update_cmd.exec_cmd_by_type(cinema_ip, 'teardown', teardown_list, short_release) result, teardown_output = update_cmd.exec_cmd_by_type(cinema_ip, 'teardown', teardown_list, short_release)
if result is False: if result is False:
raise Exception(teardown_output) raise Exception(teardown_output)
cinema_update_status[cinema_ip]['teardown']['result'] = 'success' update_status['teardown']['result'] = 'success'
cinema_update_status[cinema_ip]['teardown']['msg'] = '执行升级脚本:执行成功' update_status['teardown']['msg'] = '执行升级脚本:执行成功'
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)
cinema_update_status[cinema_ip]['teardown']['result'] = 'fail' update_status['teardown']['result'] = 'fail'
cinema_update_status[cinema_ip]['teardown']['msg'] = str(e) update_status['teardown']['msg'] = str(e)
# 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)})
# 修改设置 # 修改设置
print('执行config(teardown后)') print('执行config(teardown后)')
try: try:
cinema_update_status[cinema_ip]['config']['result'] = 'running' update_status['config']['result'] = 'running'
config_list = list(set(cmd_list) & set(cmd_dict['config'])) config_list = list(set(cmd_list) & set(cmd_dict['config']))
update_config = UpdateConfigUtil(cinema_ip, config_list, False) update_config = UpdateConfigUtil(cinema_ip, config_list, False)
config_after_output = update_config.exec_config() config_after_output = update_config.exec_config()
cinema_update_status[cinema_ip]['config']['result'] = 'success' update_status['config']['result'] = 'success'
cinema_update_status[cinema_ip]['config']['msg'] = config_before_output + '<br/>' + config_after_output update_status['config']['msg'] = config_before_output + '<br/>' + config_after_output
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)
cinema_update_status[cinema_ip]['config']['result'] = 'fail' update_status['config']['result'] = 'fail'
cinema_update_status[cinema_ip]['config']['msg'] = config_before_output + '<br/>' + str(e) update_status['config']['msg'] = config_before_output + '<br/>' + str(e)
# 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)})
d = redis_conn.get(f'cinema_update_status_{cinema_ip}')
print("redis", d)
# 传输客户端 # 传输客户端
client_list = list(set(cmd_list) & set(cmd_dict['client'])) client_list = list(set(cmd_list) & set(cmd_dict['client']))
if len(client_list) > 0: if len(client_list) > 0:
@ -243,15 +264,18 @@ def update_cine(request):
try: try:
client_release = ClientUtil() client_release = ClientUtil()
client_release.client_process(cinema_ip, short_release) client_release.client_process(cinema_ip, short_release)
cinema_update_status[cinema_ip]['client']['result'] = 'success' update_status['client']['result'] = 'success'
cinema_update_status[cinema_ip]['client']['msg'] = '客户端上传成功' update_status['client']['msg'] = '客户端上传成功'
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)
cinema_update_status[cinema_ip]['client']['result'] = 'fail' update_status['client']['result'] = 'fail'
cinema_update_status[cinema_ip]['client']['msg'] = str(e) update_status['client']['msg'] = str(e)
# 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)})
print(cinema_update_status[cinema_ip]) print('update_status', update_status)
print('redis_get', redis_conn.get(redis_key))
return JsonResponse({'result': 'success', 'ip': cinema_ip, 'msg': ''}) return JsonResponse({'result': 'success', 'ip': cinema_ip, 'msg': ''})
@ -261,11 +285,6 @@ def write_git_version_to_db(request):
git_util.set_release_to_db() git_util.set_release_to_db()
return JsonResponse({'result': 'success'}) return JsonResponse({'result': 'success'})
def get_cinema_update_status():
print('return', cinema_update_status)
return cinema_update_status
# #
# #
# def write_cine_sql(request): # def write_cine_sql(request):

Loading…
Cancel
Save