整理代码

main
RogerWork 10 months ago
parent 08ea7aa9a1
commit 32339fe0e4
  1. 2
      update/models.py
  2. 10
      update/serializers.py
  3. 4
      update/urls.py
  4. 108
      update/utils/db_compare.py
  5. 141
      update/utils/git_util.py
  6. 19
      update/views.py

@ -57,7 +57,7 @@ class Release(BaseModels):
help_text='版本状态, 0-无效,1-开发,2-测试,3-上线', default='0') help_text='版本状态, 0-无效,1-开发,2-测试,3-上线', default='0')
def __str__(self): def __str__(self):
return self.version return self.release
class Meta: class Meta:
verbose_name = '鼎新版本' verbose_name = '鼎新版本'

@ -21,7 +21,7 @@ class CinemaUserSerializer(serializers.ModelSerializer):
class ReleaseSerializer(serializers.ModelSerializer): class ReleaseSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Release model = Release
fields = ('id', 'version', 'short_ver', 'status') fields = ('id', 'release', 'short_release', 'main_ver', 'ver_id', 'status')
class ClientReleaseSerializer(serializers.ModelSerializer): class ClientReleaseSerializer(serializers.ModelSerializer):
@ -29,8 +29,9 @@ class ClientReleaseSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = ClientRelease model = ClientRelease
fields = ('id', 'origin_name', 'client_ver', 'main_ver', 'sub_ver', 'upload_name', 'client_type', 'is_delete', fields = (
'update_time') 'id', 'origin_name', 'client_ver', 'main_ver', 'sub_ver', 'ver_id', 'upload_name', 'client_type',
'is_delete', 'update_time')
class UpdateCommandSerializer(serializers.ModelSerializer): class UpdateCommandSerializer(serializers.ModelSerializer):
@ -38,4 +39,5 @@ class UpdateCommandSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = UpdateCommand model = UpdateCommand
fields = ('id', 'desc', 'command', 'process', 'is_sys', 'is_checked', 'is_delete', 'update_time') fields = (
'id', 'desc', 'command', 'ver_id', 'process', 'run_num', 'is_sys', 'is_checked', 'is_delete', 'update_time')

@ -30,7 +30,9 @@ urlpatterns = [
path('get_operation_cmd/', get_operation_cmd), path('get_operation_cmd/', get_operation_cmd),
# re_path(r'^cinema/search/ip/(?P<ip>[0-9.]*?)/version/(?P<version>[\S]*?)/$', views.CinemaSearchAPIView.as_view()) # re_path(r'^cinema/search/ip/(?P<ip>[0-9.]*?)/version/(?P<version>[\S]*?)/$', views.CinemaSearchAPIView.as_view())
# 测试用的接口 # 测试用的接口
path('git/write_git_version_to_db/', write_git_version_to_db) path('test/write_git_version_to_db/', write_git_version_to_db),
path('test/write_cine_sql/', write_cine_sql),
path('test/db_compare/', compare_db_structor)
] ]
# &version=(?P<version>[\S]*?) # &version=(?P<version>[\S]*?)

@ -1,35 +1,39 @@
import os import os
import pymysql import pymysql
from dingxin_toolbox_drf.settings import BASE_DIR
from update.utils.git_util import GitUtil from update.utils.git_util import GitUtil
class DbCompare: class DbCompare:
def __init__(self, target_ip, _release): def __init__(self, target_ip, short_release):
self.target_server = target_ip self.target_server = target_ip
self.target_user = 'test' self.target_user = 'test'
self.target_pwd = 'cine123456' self.target_pwd = 'cine123456'
self.target_port = '3306' self.target_port = '3306'
self.target_release = _release self.target_release = short_release
self.diff_sql_path = '' self.cine_sql_path = os.path.join(BASE_DIR, 'dx', 'sql', self.target_release + '.sql')
self.diff_sql_tmp_path = '' self.diff_sql_path = os.path.join(BASE_DIR, 'dx', 'temp', 'diff_' + self.target_server + '.sql')
self.create_sql_path = os.path.join(BASE_DIR, 'dx', 'temp', 'create_' + self.target_server + '.sql')
# 生成对比文件 # 生成对比文件
def gen_diff_file(self): def gen_diff_file(self):
serv_host = '127.0.0.1' # serv_host = '127.0.0.1'
serv_user = 'dingxin' # serv_user = 'dingxin'
# serv_pwd = 'cine123456'
# serv_port = '3306'
serv_host = '172.16.3.112'
serv_user = 'test'
serv_pwd = 'cine123456' serv_pwd = 'cine123456'
serv_port = '3306' serv_port = '3306'
server_1 = f'--server1={serv_user}:{serv_pwd}@{serv_host}:{serv_port}' server_1 = f'--server1={serv_user}:{serv_pwd}@{serv_host}:{serv_port}'
server_2 = f'--server2={self.target_user}:{self.target_pwd}@{self.target_server}:{self.target_port}' server_2 = f'--server2={self.target_user}:{self.target_pwd}@{self.target_server}:{self.target_port}'
git_util = GitUtil() git_util = GitUtil(self.target_release)
db_name = git_util.get_db_name(self.target_release) db_name = git_util.get_db_name()
self.diff_sql_path = '../../dx/temp/diff_' + self.target_server + '.sql'
self.diff_sql_tmp_path = '../../dx/temp/temp_' + self.target_server + '.sql'
cmd = f'mysqldiff {server_1} {server_2} --changes-for=server2 --difftype=sql --force --quiet --skip-table-options {db_name}:cine > {self.diff_sql_tmp_path}' 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) result = os.system(cmd)
@ -37,52 +41,56 @@ class DbCompare:
return self.diff_sql_path return self.diff_sql_path
# 生成差异sql # 生成差异sql
def gen_diff_sql(self): # def gen_diff_sql(self):
diff_sql_list = [] # diff_sql_list = []
diff_file = open(self.diff_sql_path, 'w', encoding='utf-8') # diff_file = open(self.diff_sql_path, 'w', encoding='utf-8')
with open(self.diff_sql_tmp_path, 'r', encoding='utf-8') as tmp_f: # with open(self.diff_sql_tmp_path, 'r', encoding='utf-8') as tmp_f:
for line in tmp_f.readlines(): # for line in tmp_f.readlines():
if line.startswith('#'): # if line.startswith('#'):
continue # continue
if line == '': # if line == '':
continue # continue
diff_file.write(line) # diff_file.write(line)
tmp_f.close() # tmp_f.close()
diff_file.close() # diff_file.close()
with open(self.diff_sql_path, 'r', encoding='utf-8') as diff_f: # with open(self.diff_sql_path, 'r', encoding='utf-8') as diff_f:
content = diff_f.read() # content = diff_f.read()
temp = content.split(';') # temp = content.split(';')
for sql in temp: # for sql in temp:
diff_sql_list.append(sql + ';') # diff_sql_list.append(sql + ';')
diff_f.close() # diff_f.close()
print(diff_sql_list) # print(diff_sql_list)
return diff_sql_list # return diff_sql_list
# 生成新表sql # 生成新表sql
def gen_create_sql(self): def gen_create_file(self):
create_sql_list = [] create_sql_list = []
with open(self.diff_sql_tmp_path, 'r', encoding='utf-8') as f: with open(self.diff_sql_path, 'r', encoding='utf-8') as f:
for line in f.readlines(): for line in f.readlines():
if line.startswith('# TABLE'): if line.startswith('# TABLE'):
create_sql_list.append(line.split(':')[1].strip()) create_sql_list.append(line.split(':')[1].strip())
print(create_sql_list) print(create_sql_list)
return create_sql_list f.close()
create_table_sql_list = ['USE `cine`;\n\n']
with open(self.cine_sql_path, 'r', encoding='utf-8') as f:
content = f.read()
for sql in content.split(';\n'):
for table in create_sql_list:
if sql.find(f'CREATE TABLE `{table}`'):
create_table_sql_list.append(sql + ';\n\n')
f.close()
with open(self.create_sql_path, 'w', encoding='utf-8') as f:
f.writelines(create_table_sql_list)
f.close()
return self.create_sql_path
# 执行差异 # 执行差异
def exec_diff_sql(self): def exec_diff_sql(self):
self.gen_diff_file() self.gen_diff_file()
diff_sql = self.gen_diff_sql() self.gen_create_file()
create_sql = self.gen_create_sql() diff_cmd = f'mysql -h{self.target_server} -P{self.target_port} -u{self.target_user} -p{self.target_pwd} < {self.diff_sql_path}'
all_sql = diff_sql + create_sql create_cmd = f'mysql -h{self.target_server} -P{self.target_port} -u{self.target_user} -p{self.target_pwd} < {self.create_sql_path}'
db_config = { diff_result = os.system(diff_cmd)
'host': self.target_server, print(diff_result)
'username': self.target_user, create_result = os.system(create_cmd)
'password': self.target_pwd, print(create_result)
'port': self.target_port,
'database': 'cine',
}
db_conn = pymysql.Connect(**db_config)
db_cursor = db_conn.cursor()
for sql in all_sql:
result = db_cursor.execute(sql)
print(result)

@ -6,13 +6,25 @@ import hashlib
import pymysql import pymysql
from dingxin_toolbox_drf import settings from dingxin_toolbox_drf import settings
"""
功能
1. clone代码仅内部使用
2. 拉去指定版本代码
3. 获取所有版本并写入数据库
4. 将指定版本的cine.sql拉取到本地
5. 对本执行版本的cine.sql和本地同版本的文件是否一致
6. 将本地的cine.sql写入数据库
"""
class GitUtil: class GitUtil:
def __init__(self): def __init__(self, short_release):
self.local_path = os.path.join(settings.BASE_DIR, r'dx\code') self.short_release = short_release
self.local_code_path = os.path.join(settings.BASE_DIR, r'dx\code')
self.cine_sql_path = os.path.join(settings.BASE_DIR, r'dx\sql')
self.db_config = { self.db_config = {
'host': 'home.rogersun.cn', 'host': '172.16.3.112',
'user': 'dingxin', 'user': 'test',
'password': 'cine123456', 'password': 'cine123456',
'connect_timeout': 5, 'connect_timeout': 5,
} }
@ -20,17 +32,19 @@ class GitUtil:
# 执行一次 clone代码到本地 # 执行一次 clone代码到本地
def clone(self): def clone(self):
git_url = 'http://dingxin_readonly:cine123456@172.16.3.3:8081/root/dingxin.git' git_url = 'http://dingxin_readonly:cine123456@172.16.3.3:8081/root/dingxin.git'
Repo.clone_from(git_url, to_path=self.local_path, branch='master') Repo.clone_from(git_url, to_path=self.local_code_path, branch='master')
# 获取指定版本的最新代码 # 获取指定版本的最新代码
def checkout_release(self, _release): def checkout_release(self):
local_repo = Repo(self.local_path) local_repo = Repo(self.local_code_path)
local_repo.git.checkout(_release) r = local_repo.git.checkout(self.short_release)
local_repo.git.pull() print(r)
r = local_repo.git.pull()
print(r)
# 获取版本并写入数据库 # 获取版本并写入数据库
def set_release_to_db(self): def set_release_to_db(self):
local_repo = Repo(self.local_path) local_repo = Repo(self.local_code_path)
release = [] release = []
for remote in local_repo.remotes: for remote in local_repo.remotes:
@ -38,85 +52,114 @@ class GitUtil:
release.append(ref.name) release.append(ref.name)
saved_release = Release.objects.all() saved_release = Release.objects.all()
saved_release_version_list = [rel['version'] for rel in saved_release] saved_release_version_list = [rel.short_release for rel in saved_release]
for rel in release: for rel in release:
if rel in saved_release_version_list: if rel in saved_release_version_list:
pass pass
else: else:
data = { data = {
'version': rel, 'release': rel,
'short_ver': rel[7:-8] if '_Release' in rel else rel, 'short_release': rel[7:-8] if '_Release' in rel else rel,
'main_ver': rel[7:18],
'ver_id': int(rel[14:18]),
'status': '0' 'status': '0'
} }
Release.objects.create(**data) Release.objects.create(**data)
# 复制文cine.sql到本地路径 # 复制文cine.sql到本地路径
def copy_cine_sql(self, _release): def copy_cine_sql(self):
self.checkout_release(_release) self.checkout_release()
short_ver = 'cine_' + self.get_short_version_by_release(_release) short_ver = 'cine_' + self.short_release
org_path = os.path.join(self.local_path, 'install/cine.sql') org_path = os.path.join(self.local_code_path, 'install', 'cine.sql')
target_path = os.path.join(self.local_path, 'sql/' + _release + '.sql') target_path = os.path.join(self.cine_sql_path, f'{self.short_release}.sql')
if os.path.exists(target_path): # if os.path.exists(target_path):
os.remove(target_path) # os.remove(target_path)
target_file = open(target_path, 'a', encoding='utf-8') with open(target_path, 'w', encoding='utf-8') as target_file:
with open(org_path, 'r', encoding='utf') as f: with open(org_path, 'r', encoding='utf') as f:
lines = f.readlines() lines = f.readlines()
for index, line in enumerate(lines): target_lines = []
if line == 'CREATE DATABASE /*!32312 IF NOT EXISTS*/`cine` /*!40100 DEFAULT CHARACTER SET utf8 */;': for line in lines:
lines[ if line.startswith('CREATE DATABASE'):
index] = f'CREATE DATABASE /*!32312 IF NOT EXISTS*/`{short_ver}` /*!40100 DEFAULT CHARACTER SET utf8 */;' print('CREATE DATABASE')
if line == 'USE `cine`;': target_lines.append(f'DROP DATABASE IF EXISTS `{short_ver}`;\r\n')
lines[index] = f'USE `{short_ver}`;' target_lines.append(
f'CREATE DATABASE /*!32312 IF NOT EXISTS*/`{short_ver}` /*!40100 DEFAULT CHARACTER SET utf8 */;\r\n')
elif line.startswith('USE `cine`'):
print('USE `cine`')
target_lines.append(f'USE `{short_ver}`;\r\n')
else:
target_lines.append(line)
f.close() f.close()
target_file.writelines(lines) target_file.writelines(target_lines)
target_file.close() target_file.close()
# 检查cine.sql是否是最新的 # 检查cine.sql是否是最新的
def check_cine_sql(self, _release): def check_cine_sql(self):
if os.path.exists(sql_path := os.path.join(self.local_path, 'sql/' + _release + '.sql')): if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.short_release}.sql')):
saved_md5 = self.get_md5(sql_path) saved_md5 = self.get_md5(sql_path)
new_md5 = self.get_md5(os.path.join(self.local_path, 'install/cine.sql')) new_md5 = self.get_md5(os.path.join(self.local_code_path, 'install', 'cine.sql'))
if saved_md5 == new_md5: if saved_md5 == new_md5:
print('md5相同')
return True return True
return False else:
print('md5不同')
def write_sql(self, _release): os.remove(sql_path)
if os.path.exists(sql_path := os.path.join(self.local_path, 'sql/' + _release + '.sql')): return False
short_ver = 'cine_' + self.get_short_version_by_release(_release) else:
execute_sql_list = [f'DROP DATABASE IF EXISTS {short_ver};'] print('文件不存在')
return False
def write_sql(self):
if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.short_release}.sql')):
execute_sql_list = []
with open(sql_path, 'r', encoding='utf-8') as sql_file: with open(sql_path, 'r', encoding='utf-8') as sql_file:
content = sql_file.read() content = sql_file.read()
sql_list = content.split(';') sql_list = content.split('\n\n')
for sql in sql_list: for sql in sql_list:
sql = sql.strip() sql = sql.strip()
if sql.startswith('CREATE'): if sql.startswith('CREATE'):
print('CREATE')
execute_sql_list.append(sql + ';') execute_sql_list.append(sql + ';')
if sql.startswith('DROP'): if sql.startswith('DROP'):
print('DROP')
execute_sql_list.append(sql + ';') execute_sql_list.append(sql + ';')
if sql.startswith('USE'): if sql.startswith('USE'):
print('USE')
execute_sql_list.append(sql + ';') execute_sql_list.append(sql + ';')
db_conn = pymysql.Connect(**self.db_config) db_conn = pymysql.Connect(**self.db_config)
db_cursor = db_conn.cursor() db_cursor = db_conn.cursor()
for ext_sql in execute_sql_list: # for ext_sql in execute_sql_list:
result = db_cursor.execute(ext_sql) # result = db_cursor.execute(ext_sql)
print(result, ext_sql) # print(result, ext_sql)
def write_cine_sql_by_mysql(self):
if os.path.exists(sql_path := os.path.join(self.cine_sql_path, f'{self.short_release}.sql')):
cmd = f'mysql -h172.16.3.112 -P3306 -utest -pcine123456 < {sql_path}'
r = os.system(cmd)
print(type(r))
print(r)
# 获取数据库名称 # 获取数据库名称
def get_db_name(self, _release): def get_db_name(self):
return 'cine_' + self.get_short_version_by_release(_release) return 'cine_' + self.short_release
# 写入cine.sql
def handle_create_cine(self):
if not self.check_cine_sql():
self.copy_cine_sql()
self.write_cine_sql_by_mysql()
# 获取端短版本号 # 获取端短版本号
@staticmethod # @staticmethod
def get_short_version_by_release(_release): # def get_short_version_by_release(_release):
data = Release.objects.filter(version=_release).first() # data = Release.objects.filter(short_release=_release).first()
return data.short_version # return data.short_release
@staticmethod @staticmethod
def get_short_version(): def get_short_version():
data = Release.objects.exclude(status='0').order_by('-short_ver').all() return Release.objects.exclude(status='0').order_by('-ver_id').all()
return [{'version': r.version, 'short_ver': r.short_ver, 'status': r.status} for r in data]
@staticmethod @staticmethod
def get_md5(path): def get_md5(path):

@ -16,6 +16,7 @@ from update.utils.get_version import GetVersion
from rest_framework.decorators import action from rest_framework.decorators import action
from update.utils.git_util import GitUtil from update.utils.git_util import GitUtil
from update.utils.cmd_extcute import UpdateCommandUtil from update.utils.cmd_extcute import UpdateCommandUtil
from update.utils.db_compare import DbCompare
# CacheResponseMixin 一定要放第一位 # CacheResponseMixin 一定要放第一位
@ -78,6 +79,22 @@ def get_operation_cmd(request):
# 测试用接口 # 测试用接口
def write_git_version_to_db(request): def write_git_version_to_db(request):
git_util = GitUtil() git_util = GitUtil('')
git_util.set_release_to_db() git_util.set_release_to_db()
return JsonResponse({'result': 'success'}) return JsonResponse({'result': 'success'})
def write_cine_sql(request):
git_util = GitUtil('2.0.33.0338_Release')
git_util.handle_create_cine()
return JsonResponse({'result': 'success'})
def compare_db_structor(request):
git_util = GitUtil('')
# # git_util.checkout_release('origin/2.0.33.0338_Release')
# git_util.check_cine_sql('origin/2.0.33.0338_Release')
# git_util.copy_cine_sql('origin/2.0.33.0338_Release')
#
# db_compare = DbCompare('172.16.3.111', 'origin/2.0.33.0338_Release')
# path = db_compare.gen_diff_file()
# print(path)
Loading…
Cancel
Save