优化日志打印

main
RogerWork 8 months ago
parent 3411007afb
commit 1eef9f1b94
  1. 4197
      logs/dingxin.log
  2. 4
      update/consumers.py
  3. 5
      update/utils/client_util.py
  4. 12
      update/utils/cmd_extcute.py
  5. 65
      update/utils/db_compare.py
  6. 37
      update/utils/git_util.py
  7. 54
      update/views.py

File diff suppressed because it is too large Load Diff

@ -27,14 +27,14 @@ class UpdateConsumer(WebsocketConsumer):
if redis_conn.exists(redis_key):
redis_conn.delete(redis_key)
data = json.loads(redis_conn.get(redis_key))
print('ws任务完成消息', data)
# print('ws任务完成消息', data)
if data.get('ip', False):
ip = data['ip']
redis_key = f'cinema_update_status_{ip}'
# print('ws发送消息前', cinema_update_status)
data = json.loads(redis_conn.get(redis_key))
print('ws发送消息前', data)
# print('ws发送消息前', data)
self.send(json.dumps({'status': {'ip': ip, 'result': data}}))
else:
self.send(text_data)

@ -107,6 +107,7 @@ class ClientUtil:
def download_client_file(self, file_name, path):
local_file = open(os.path.join(self.local_path, file_name), 'wb')
# 接收文件并写入本地文件
print('从共享下载客户端文件到服务器 共享路径:', rf'{path}{file_name}', '服务器路径:', local_file)
self.smb_conn.retrieveFile('data1', rf'{path}{file_name}', local_file)
# 关闭本地文件
local_file.close()
@ -133,7 +134,7 @@ class ClientUtil:
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('找到本地文件')
print('找到本地文件', client_version)
return
# 如果本地没有则到服务器上拉取
client = ClientRelease.objects.filter(Q(client_ver=client_version) & Q(is_delete=False)).first()
@ -159,11 +160,13 @@ class ClientUtil:
os.path.join(self.local_path, origin),
f"/data0/cine/resource/upload/client/{target}")
# 关闭客户端
print(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)
print(f'开始上传操作, 目标主机 {cinema_ip}, 客户端版本 {client_version}')
client = ClientRelease.objects.filter(Q(client_ver=client_version) & Q(is_delete=False)).first()
self.upload(cinema_ip, client.origin_name, client.upload_name)
# 更新数据库

@ -64,11 +64,12 @@ class UpdateCommandUtil:
exec_output = []
exec_list = []
for exec_cmd in _exec_cmd_list:
print(exec_cmd)
print('执行命令对象:', exec_cmd)
stdin, stdout, stderr = self.client.exec_command(exec_cmd['cmd'])
out = stdout.read().decode('utf-8')
err = stderr.read().decode('utf-8')
print('out', out, 'err', err)
print('正确输出:', out)
print('错误输出:', err)
out_format = out.replace('\n', '<br/>').strip()
err_format = err.replace('\n', '<br/>').strip()
if err == '':
@ -90,11 +91,11 @@ class UpdateCommandUtil:
exec_output.append(exec_cmd['desc'] + ':执行失败 ' + err.replace('\n', '<br/>').strip())
return False, exec_output
self.disconnect()
print(exec_output)
print('输出结果:', exec_output)
return True, '<br/>'.join(exec_output)
def exec_cmd_by_type(self, cinema_ip, _type, cmd_list, short_release):
print(cinema_ip, _type, cmd_list, short_release)
print('需要执行的命令列表:', cmd_list)
exec_cmd_list = []
exec_cmd_data = self.get_checked_cmd(cmd_list)
if _type == 'setup':
@ -131,9 +132,10 @@ class UpdateConfigUtil:
db_cursor = db_conn.cursor()
result_list = []
for cmds in cmd_list:
print('执行命令: ', cmds)
for cmd in cmds['cmd']:
r = db_cursor.execute(cmd)
print("exec_config", cmds['desc'], r)
print("执行子命令: ", cmds['desc'], cmd, '结果:', '执行失败(设置无需修改)' if r == 0 else '执行成功')
result_list.append(cmds['desc'] + (':执行失败(设置无需修改)' if r == 0 else ':执行成功'))
db_conn.commit()
db_cursor.close()

@ -20,10 +20,13 @@ class DbCompare:
# 生成对比文件
def gen_diff_file(self):
print('开始生成差异文件')
if os.path.exists(self.diff_sql_path):
print('remove diff_sql_path')
print(f'删除之前生成的差异文件 {self.diff_sql_path}')
os.remove(self.diff_sql_path)
print(os.path.exists(self.diff_sql_path))
if os.path.exists(self.diff_sql_path):
print('删除失败')
print('删除成功')
# serv_host = '127.0.0.1'
# serv_user = 'dingxin'
@ -42,20 +45,28 @@ class DbCompare:
db_name = git_util.get_db_name()
cmd = f'mysqldiff {server_1} {server_2} --changes-for=server2 --difftype=sql --force --quiet --skip-table-options {db_name}:cine > {self.diff_sql_path}'
print(cmd)
print('执行命令', cmd)
# result = os.system(cmd)
# print('执行返回结果:', result >> 8)
result = subprocess.getstatusoutput(cmd)
if result[0] != 0:
print('执行失败:', result[1])
print('执行成功:', result[1])
# result = subprocess.run(cmd)
result = os.system(cmd)
print('gen_diff_file', result)
return self.diff_sql_path
# 针对降级的情况,需要屏蔽掉语句中的drop语句,此处可能有错误的风险,待验证
def remove_drop(self):
# 如果清理后的文件存在则删除
print('开始处理差异文件,清除Drop命令等')
if os.path.exists(self.clear_diff_sql_path):
print('remove clear_diff_sql_path')
print(f'清理之前生成的文件 {self.clear_diff_sql_path}')
os.remove(self.clear_diff_sql_path)
print(os.path.exists(self.clear_diff_sql_path))
if os.path.exists(self.clear_diff_sql_path):
print('清理失败')
print('清理成功')
# 打开生成的差异sql,如果遇到ALTER语句则进入alter模式,将ALTER下面的语句写入temp中,如果是DROP就跳过,当遇到换行时就结束alter模式,把temp写入主列表
with open(self.diff_sql_path, 'r', encoding='utf-8') as f:
sql_list = []
@ -66,10 +77,10 @@ class DbCompare:
if line.strip().startswith('ALTER TABLE'):
temp.append(line)
alter = 1
elif line.strip().startswith('DROP INDEX'):
pass
elif line.strip().startswith('DROP COLUMN'):
pass
# elif line.strip().startswith('DROP INDEX'):
# pass
# elif line.strip().startswith('DROP COLUMN'):
# pass
elif line.strip().startswith('[PASS]'):
pass
elif line.strip().startswith('+'):
@ -98,13 +109,19 @@ class DbCompare:
# 将清洗过的sql列表写入文件
with open(self.clear_diff_sql_path, 'w', encoding='utf-8') as f:
f.writelines(sql_list)
print('生成新的对比文件:', self.clear_diff_sql_path)
f.close()
# 生成新表sql
def gen_create_file(self):
# 如果有已生成的文件则删除
print('开始生成创建文件')
if os.path.exists(self.create_sql_path):
print(f'清理之前生成的文件 {self.create_sql_path}')
os.remove(self.create_sql_path)
if os.path.exists(self.create_sql_path):
print('清除失败')
print('清除成功')
# 在生成的差异文件中查找原始版本没有,目标版本中存在的表明
create_sql_list = []
with open(self.diff_sql_path, 'r', encoding='utf-8') as f:
@ -124,6 +141,7 @@ class DbCompare:
# 将上步中的sql语句列表些入create.sql文件中
with open(self.create_sql_path, 'w', encoding='utf-8') as f:
f.writelines(create_table_sql_list)
print('生成新的创建文件:', self.create_sql_path)
f.close()
return self.create_sql_path
@ -134,8 +152,21 @@ class DbCompare:
self.gen_create_file()
diff_cmd = f'mysql -h{self.target_server} -P{self.target_port} -u{self.target_user} -p{self.target_pwd} < {self.clear_diff_sql_path}'
create_cmd = f'mysql -h{self.target_server} -P{self.target_port} -u{self.target_user} -p{self.target_pwd} < {self.create_sql_path}'
diff_result = os.system(diff_cmd)
print('diff_result', diff_result)
create_result = os.system(create_cmd)
print('create_result', create_result)
return '数据库对比成功' if diff_result == 0 and create_result == 0 else '数据库对比失败'
print('执行命令:', diff_cmd)
# diff_result = os.system(diff_cmd)
# print('diff_result', diff_result)
diff_result = subprocess.getstatusoutput(diff_cmd)
if diff_result[0] != 0:
print('执行失败:', diff_result[1])
print('执行成功:', diff_result[1])
print('执行命令:', create_cmd)
# create_result = os.system(create_cmd)
# print('create_result', create_result)
create_result = subprocess.getstatusoutput(create_cmd)
if create_result[0] != 0:
print('执行失败:', create_result[1])
print('执行成功:', create_result[1])
return '数据库对比成功' if diff_result[0] == 0 and create_result[0] == 0 else '数据库对比失败' + diff_result[
1] + create_result[1]

@ -38,11 +38,12 @@ class GitUtil:
# 获取指定版本的最新代码
def checkout_release(self):
print('操作git获取指定版本的cine.sql')
local_repo = Repo(self.local_code_path)
checkout_result = local_repo.git.checkout(self.short_release)
print("checkout", checkout_result)
print("checkout: ", checkout_result)
pull_result = local_repo.git.pull()
print("pull", pull_result)
print("pull: ", pull_result)
return pull_result
# 复制cine.sql到本地路径cine_xxx_org.sql,然后生成新数据库名称的cine_xxx.sql
@ -56,17 +57,27 @@ class GitUtil:
target_path = os.path.join(self.cine_sql_path, f'{self.db_name}.sql')
if os.path.exists(target_org_path):
print(f'存在同名文件 {target_org_path}')
if self.get_md5(org_path) == self.get_md5(target_org_path):
print('md5相同')
print('新文件的md5与之前的相同,不需要处理')
if os.path.exists(target_path):
return False
else:
print('md5不同')
print('新文件的md5与之前的不同,开始处理流程')
os.remove(target_org_path)
if os.path.exists(target_org_path):
print(f'删除失败{target_org_path}')
print(f'删除成功{target_org_path}')
os.remove(target_path)
shutil.copyfile(org_path, target_org_path)
if os.path.exists(target_path):
print(f'删除失败{target_path}')
print(f'删除成功{target_path}')
r = shutil.copyfile(org_path, target_org_path)
print('拷贝到目标路径成功', r)
else:
shutil.copyfile(org_path, target_org_path)
print(f'没有同名文件 {target_org_path}')
r = shutil.copyfile(org_path, target_org_path)
print('拷贝到目标路径成功', r)
with open(target_path, 'w', encoding='utf-8') as target_file:
with open(org_path, 'r', encoding='utf') as f:
@ -74,27 +85,29 @@ class GitUtil:
target_lines = []
for line in lines:
if line.startswith('CREATE DATABASE'):
print('CREATE DATABASE')
print('修改 DROP DATABASE 和 CREATE DATABASE 语句为对应版本语句')
target_lines.append(f'DROP DATABASE IF EXISTS `{self.db_name}`;\r\n')
target_lines.append(
f'CREATE DATABASE /*!32312 IF NOT EXISTS*/`{self.db_name}` /*!40100 DEFAULT CHARACTER SET utf8 */;\r\n')
elif line.startswith('USE `cine`'):
print('USE `cine`')
print('修改 USE 语句为对应版本语句')
target_lines.append(f'USE `{self.db_name}`;\r\n')
else:
target_lines.append(line)
f.close()
target_file.writelines(target_lines)
target_file.close()
print('create_cine_sql', output)
# print('create_cine_sql', output)
return True
# 将生成的cine.sql写入数据库
def write_cine_sql_by_mysql(self):
print('开始将cine.sql写入数据库')
if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.db_name}.sql')):
cmd = f'mysql -h{self.db_config["host"]} -P{self.db_config["port"]} -u{self.db_config["user"]} -p{self.db_config["password"]} < {sql_path}'
print('执行命令', cmd)
r = os.system(cmd)
print('write_cine_sql_by_mysql', r)
print('cine.sql写入结果', r)
# 获取数据库名称
def get_db_name(self, _release=''):
@ -102,9 +115,9 @@ class GitUtil:
release = self.short_release
else:
release = _release
print(release)
# print(release)
rel = Release.objects.filter(short_release=release).first()
print(rel)
# print(rel)
return 'cine_' + str(rel.ver_id)
# 写入cine.sql

@ -107,10 +107,13 @@ def get_git_version(request):
def update_cine(request):
req = request.GET.dict()
cinema_ip = req.get('ip')
print('-'*100)
print(f'开始更新 {cinema_ip}')
# 获取当前版本
org_ver = get_cinema_ver_by_ip(ip=cinema_ip)
update_time = timezone.now()
print(f'原始版本 {org_ver}')
# 处理命令格式
cmd_dict = {}
@ -120,7 +123,7 @@ def update_cine(request):
cmd_dict[cmd['process']].append(cmd['id'])
else:
cmd_dict[cmd['process']] = [cmd['id']]
print(cmd_dict)
# print(cmd_dict)
# 增加状态
update_status = {
@ -142,22 +145,21 @@ def update_cine(request):
if redis_conn.exists(redis_key):
# update_status = copy.deepcopy(json.loads(redis_conn.get(redis_key)))
val = json.loads(redis_conn.get(redis_key))
print(val)
print(f'存在redis key {redis_key} 对应的数据', 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)
# print('update_status', update_status)
# redis_conn.set(f'cinema_update_status_{cinema_ip}', json.dumps(update_status), 10*60)
# print("ws调试", cinema_update_status)
short_release = req.get('version')
cmd_list = json.loads(req.get('cmd'))
print(cinema_ip, short_release, cmd_list)
print('执行升级任务:', cmd_list)
# cinema_ip = '172.16.3.88'
# short_release = '2.0.33.0338_Release'
@ -167,7 +169,7 @@ def update_cine(request):
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# 获取并写入cine.sql
print('获取并写入cine.sql')
print('-'*20, '步骤一:获取并写入cine.sql', '-'*20)
try:
update_status['git']['result'] = 'running'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
@ -177,19 +179,20 @@ def update_cine(request):
update_status['git']['msg'] = git_output
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e:
print('git', e)
print('步骤一:获取并写入cine.sql异常', e)
update_status['result'] = 'fail'
update_status['git']['result'] = 'fail'
update_status['git']['msg'] = str(e)
print('最后执行状态', json.dumps(update_status))
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key)
UpdateLog.objects.create(ip=cinema_ip, type='toolbox', origin_ver=org_ver, target_ver=short_release[:11],
time=update_time, result=update_status['result'],
time=update_time, result='fail',
result_detail=json.dumps(update_status))
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)})
# 执行setup
print('执行setup')
print('-'*20, '步骤二:执行setup', '-'*20)
try:
update_status['setup']['result'] = 'running'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
@ -203,10 +206,11 @@ def update_cine(request):
update_status['setup']['msg'] = setup_output
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e:
print('setup', e)
print('步骤二:执行setup异常', e)
update_status['result'] = 'fail'
update_status['setup']['result'] = 'fail'
update_status['setup']['msg'] = str(e)
print('最后执行状态', json.dumps(update_status))
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key)
UpdateLog.objects.create(ip=cinema_ip, type='toolbox', origin_ver=org_ver, target_ver=short_release[:11],
@ -217,7 +221,7 @@ def update_cine(request):
# 数据库对比
sql_list = list(set(cmd_list) & set(cmd_dict['sql']))
if len(sql_list) > 0:
print('数据库对比')
print('-'*20, '步骤三:数据库对比', '-'*20)
try:
update_status['sql']['result'] = 'running'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
@ -227,10 +231,11 @@ def update_cine(request):
update_status['sql']['msg'] = sql_output
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e:
print('sql', e)
print('步骤三:数据库对比异常', e)
update_status['result'] = 'fail'
update_status['sql']['result'] = 'fail'
update_status['sql']['msg'] = str(e)
print('最后执行状态', json.dumps(update_status))
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key)
UpdateLog.objects.create(ip=cinema_ip, type='toolbox', origin_ver=org_ver, target_ver=short_release[:11],
@ -239,7 +244,7 @@ def update_cine(request):
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)})
# 修改设置
print('执行config(teardown前)')
print('-'*20, '第四步:执行config(teardown前)', '-'*20)
try:
update_status['config']['result'] = 'running'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
@ -250,10 +255,11 @@ def update_cine(request):
update_status['config']['msg'] = config_before_output
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e:
print('config', e)
print('第四步:执行config(teardown前)异常', e)
update_status['result'] = 'fail'
update_status['config']['result'] = 'fail'
update_status['config']['msg'] = str(e)
print('最后执行状态', json.dumps(update_status))
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key)
UpdateLog.objects.create(ip=cinema_ip, type='toolbox', origin_ver=org_ver, target_ver=short_release[:11],
@ -264,7 +270,7 @@ def update_cine(request):
# 执行teardown
teardown_list = list(set(cmd_list) & set(cmd_dict['teardown']))
if len(teardown_list) > 0:
print('执行teardown')
print('-' * 20, '第五步:执行teardown', '-' * 20)
try:
update_status['teardown']['result'] = 'running'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
@ -276,10 +282,11 @@ def update_cine(request):
update_status['teardown']['msg'] = '执行升级脚本:执行成功'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e:
print('teardown', e)
print('第五步:执行teardown异常', e)
update_status['result'] = 'fail'
update_status['teardown']['result'] = 'fail'
update_status['teardown']['msg'] = str(e)
print('最后执行状态', json.dumps(update_status))
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key)
UpdateLog.objects.create(ip=cinema_ip, type='toolbox', origin_ver=org_ver, target_ver=short_release[:11],
@ -288,7 +295,7 @@ def update_cine(request):
return JsonResponse({'result': 'fail', 'ip': cinema_ip, 'msg': str(e)})
# 修改设置
print('执行config(teardown后)')
print('-' * 20, '第六步:执行config(teardown后)', '-' * 20)
try:
update_status['config']['result'] = 'running'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
@ -299,10 +306,11 @@ def update_cine(request):
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:
print('config', e)
print('第六步:执行config(teardown后)异常', e)
update_status['result'] = 'fail'
update_status['config']['result'] = 'fail'
update_status['config']['msg'] = config_before_output + '<br/>' + str(e)
print('最后执行状态', json.dumps(update_status))
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key)
UpdateLog.objects.create(ip=cinema_ip, type='toolbox', origin_ver=org_ver, target_ver=short_release[:11],
@ -313,21 +321,21 @@ def update_cine(request):
# 传输客户端
client_list = list(set(cmd_list) & set(cmd_dict['client']))
if len(client_list) > 0:
print('传输客户端1')
print('-' * 20, '第七步:传输客户端', '-' * 20)
try:
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_name = client_release.client_process(cinema_ip, short_release)
update_status['client']['result'] = 'success'
update_status['client']['msg'] = f'客户端上传成功: {client_name}'
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
except Exception as e:
print('client', e)
print('第七步:传输客户端异常', e)
update_status['result'] = 'fail'
update_status['client']['result'] = 'fail'
update_status['client']['msg'] = str(e)
print('最后执行状态', json.dumps(update_status))
redis_conn.set(redis_key, json.dumps(update_status), 5 * 60)
# redis_conn.delete(redis_key)
UpdateLog.objects.create(ip=cinema_ip, type='toolbox', origin_ver=org_ver, target_ver=short_release[:11],
@ -338,10 +346,8 @@ def update_cine(request):
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))
print('完成升级', json.dumps(update_status))
print('update_time', update_time)
UpdateLog.objects.create(ip=cinema_ip, type='toolbox', origin_ver=org_ver, target_ver=short_release[:11],
time=update_time, result=update_status['result'], result_detail=json.dumps(update_status))
return JsonResponse({'result': 'success', 'ip': cinema_ip, 'msg': ''})

Loading…
Cancel
Save