From 32339fe0e4622267fe5c5e6f59d9d347e51ead84 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Wed, 10 Jan 2024 18:19:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- update/models.py | 2 +- update/serializers.py | 10 +-- update/urls.py | 4 +- update/utils/db_compare.py | 108 +++++++++++++++------------- update/utils/git_util.py | 141 ++++++++++++++++++++++++------------- update/views.py | 19 ++++- 6 files changed, 178 insertions(+), 106 deletions(-) diff --git a/update/models.py b/update/models.py index 0ec3445..d7c687a 100644 --- a/update/models.py +++ b/update/models.py @@ -57,7 +57,7 @@ class Release(BaseModels): help_text='版本状态, 0-无效,1-开发,2-测试,3-上线', default='0') def __str__(self): - return self.version + return self.release class Meta: verbose_name = '鼎新版本' diff --git a/update/serializers.py b/update/serializers.py index fc983b6..2198946 100644 --- a/update/serializers.py +++ b/update/serializers.py @@ -21,7 +21,7 @@ class CinemaUserSerializer(serializers.ModelSerializer): class ReleaseSerializer(serializers.ModelSerializer): class Meta: model = Release - fields = ('id', 'version', 'short_ver', 'status') + fields = ('id', 'release', 'short_release', 'main_ver', 'ver_id', 'status') class ClientReleaseSerializer(serializers.ModelSerializer): @@ -29,8 +29,9 @@ class ClientReleaseSerializer(serializers.ModelSerializer): class Meta: model = ClientRelease - fields = ('id', 'origin_name', 'client_ver', 'main_ver', 'sub_ver', 'upload_name', 'client_type', 'is_delete', - 'update_time') + fields = ( + 'id', 'origin_name', 'client_ver', 'main_ver', 'sub_ver', 'ver_id', 'upload_name', 'client_type', + 'is_delete', 'update_time') class UpdateCommandSerializer(serializers.ModelSerializer): @@ -38,4 +39,5 @@ class UpdateCommandSerializer(serializers.ModelSerializer): class Meta: 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') diff --git a/update/urls.py b/update/urls.py index 62a257e..f065874 100644 --- a/update/urls.py +++ b/update/urls.py @@ -30,7 +30,9 @@ urlpatterns = [ path('get_operation_cmd/', get_operation_cmd), # re_path(r'^cinema/search/ip/(?P[0-9.]*?)/version/(?P[\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[\S]*?) diff --git a/update/utils/db_compare.py b/update/utils/db_compare.py index b36c6bf..4c25fc3 100644 --- a/update/utils/db_compare.py +++ b/update/utils/db_compare.py @@ -1,35 +1,39 @@ import os import pymysql +from dingxin_toolbox_drf.settings import BASE_DIR from update.utils.git_util import GitUtil class DbCompare: - def __init__(self, target_ip, _release): + def __init__(self, target_ip, short_release): self.target_server = target_ip self.target_user = 'test' self.target_pwd = 'cine123456' self.target_port = '3306' - self.target_release = _release - self.diff_sql_path = '' - self.diff_sql_tmp_path = '' + self.target_release = short_release + self.cine_sql_path = os.path.join(BASE_DIR, 'dx', 'sql', self.target_release + '.sql') + 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): - serv_host = '127.0.0.1' - serv_user = 'dingxin' + # serv_host = '127.0.0.1' + # serv_user = 'dingxin' + # serv_pwd = 'cine123456' + # serv_port = '3306' + + serv_host = '172.16.3.112' + serv_user = 'test' serv_pwd = 'cine123456' serv_port = '3306' 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}' - git_util = GitUtil() - db_name = git_util.get_db_name(self.target_release) - - self.diff_sql_path = '../../dx/temp/diff_' + self.target_server + '.sql' - self.diff_sql_tmp_path = '../../dx/temp/temp_' + self.target_server + '.sql' + git_util = GitUtil(self.target_release) + 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_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) result = os.system(cmd) @@ -37,52 +41,56 @@ class DbCompare: return self.diff_sql_path # 生成差异sql - def gen_diff_sql(self): - diff_sql_list = [] - 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: - for line in tmp_f.readlines(): - if line.startswith('#'): - continue - if line == '': - continue - diff_file.write(line) - tmp_f.close() - diff_file.close() - with open(self.diff_sql_path, 'r', encoding='utf-8') as diff_f: - content = diff_f.read() - temp = content.split(';') - for sql in temp: - diff_sql_list.append(sql + ';') - diff_f.close() - print(diff_sql_list) - return diff_sql_list + # def gen_diff_sql(self): + # diff_sql_list = [] + # 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: + # for line in tmp_f.readlines(): + # if line.startswith('#'): + # continue + # if line == '': + # continue + # diff_file.write(line) + # tmp_f.close() + # diff_file.close() + # with open(self.diff_sql_path, 'r', encoding='utf-8') as diff_f: + # content = diff_f.read() + # temp = content.split(';') + # for sql in temp: + # diff_sql_list.append(sql + ';') + # diff_f.close() + # print(diff_sql_list) + # return diff_sql_list # 生成新表sql - def gen_create_sql(self): + def gen_create_file(self): 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(): if line.startswith('# TABLE'): create_sql_list.append(line.split(':')[1].strip()) 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): self.gen_diff_file() - diff_sql = self.gen_diff_sql() - create_sql = self.gen_create_sql() - all_sql = diff_sql + create_sql - db_config = { - 'host': self.target_server, - 'username': self.target_user, - 'password': self.target_pwd, - '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) + 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.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) + create_result = os.system(create_cmd) + print(create_result) diff --git a/update/utils/git_util.py b/update/utils/git_util.py index f1ea3c4..c15f962 100644 --- a/update/utils/git_util.py +++ b/update/utils/git_util.py @@ -6,13 +6,25 @@ import hashlib import pymysql from dingxin_toolbox_drf import settings +""" +功能: +1. clone代码(仅内部使用) +2. 拉去指定版本代码 +3. 获取所有版本并写入数据库 +4. 将指定版本的cine.sql拉取到本地 +5. 对本执行版本的cine.sql和本地同版本的文件是否一致 +6. 将本地的cine.sql写入数据库 +""" + class GitUtil: - def __init__(self): - self.local_path = os.path.join(settings.BASE_DIR, r'dx\code') + def __init__(self, short_release): + 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 = { - 'host': 'home.rogersun.cn', - 'user': 'dingxin', + 'host': '172.16.3.112', + 'user': 'test', 'password': 'cine123456', 'connect_timeout': 5, } @@ -20,17 +32,19 @@ class GitUtil: # 执行一次 clone代码到本地 def clone(self): 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): - local_repo = Repo(self.local_path) - local_repo.git.checkout(_release) - local_repo.git.pull() + def checkout_release(self): + local_repo = Repo(self.local_code_path) + r = local_repo.git.checkout(self.short_release) + print(r) + r = local_repo.git.pull() + print(r) # 获取版本并写入数据库 def set_release_to_db(self): - local_repo = Repo(self.local_path) + local_repo = Repo(self.local_code_path) release = [] for remote in local_repo.remotes: @@ -38,85 +52,114 @@ class GitUtil: release.append(ref.name) 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: if rel in saved_release_version_list: pass else: data = { - 'version': rel, - 'short_ver': rel[7:-8] if '_Release' in rel else rel, + 'release': 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' } Release.objects.create(**data) # 复制文cine.sql到本地路径 - def copy_cine_sql(self, _release): - self.checkout_release(_release) - short_ver = 'cine_' + self.get_short_version_by_release(_release) - org_path = os.path.join(self.local_path, 'install/cine.sql') - target_path = os.path.join(self.local_path, 'sql/' + _release + '.sql') - if os.path.exists(target_path): - os.remove(target_path) - target_file = open(target_path, 'a', encoding='utf-8') + def copy_cine_sql(self): + self.checkout_release() + short_ver = 'cine_' + self.short_release + org_path = os.path.join(self.local_code_path, 'install', 'cine.sql') + target_path = os.path.join(self.cine_sql_path, f'{self.short_release}.sql') + # if os.path.exists(target_path): + # os.remove(target_path) + with open(target_path, 'w', encoding='utf-8') as target_file: with open(org_path, 'r', encoding='utf') as f: lines = f.readlines() - for index, line in enumerate(lines): - if line == 'CREATE DATABASE /*!32312 IF NOT EXISTS*/`cine` /*!40100 DEFAULT CHARACTER SET utf8 */;': - lines[ - index] = f'CREATE DATABASE /*!32312 IF NOT EXISTS*/`{short_ver}` /*!40100 DEFAULT CHARACTER SET utf8 */;' - if line == 'USE `cine`;': - lines[index] = f'USE `{short_ver}`;' + target_lines = [] + for line in lines: + if line.startswith('CREATE DATABASE'): + print('CREATE DATABASE') + target_lines.append(f'DROP DATABASE IF EXISTS `{short_ver}`;\r\n') + 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() - target_file.writelines(lines) + target_file.writelines(target_lines) target_file.close() # 检查cine.sql是否是最新的 - def check_cine_sql(self, _release): - if os.path.exists(sql_path := os.path.join(self.local_path, 'sql/' + _release + '.sql')): + def check_cine_sql(self): + 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) - 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: + print('md5相同') return True - return False - - def write_sql(self, _release): - if os.path.exists(sql_path := os.path.join(self.local_path, 'sql/' + _release + '.sql')): - short_ver = 'cine_' + self.get_short_version_by_release(_release) - execute_sql_list = [f'DROP DATABASE IF EXISTS {short_ver};'] + else: + print('md5不同') + os.remove(sql_path) + return False + else: + 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: content = sql_file.read() - sql_list = content.split(';') + sql_list = content.split('\n\n') for sql in sql_list: sql = sql.strip() if sql.startswith('CREATE'): + print('CREATE') execute_sql_list.append(sql + ';') if sql.startswith('DROP'): + print('DROP') execute_sql_list.append(sql + ';') if sql.startswith('USE'): + print('USE') execute_sql_list.append(sql + ';') db_conn = pymysql.Connect(**self.db_config) db_cursor = db_conn.cursor() - for ext_sql in execute_sql_list: - result = db_cursor.execute(ext_sql) - print(result, ext_sql) + # for ext_sql in execute_sql_list: + # result = db_cursor.execute(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): - return 'cine_' + self.get_short_version_by_release(_release) + def get_db_name(self): + 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 - def get_short_version_by_release(_release): - data = Release.objects.filter(version=_release).first() - return data.short_version + # @staticmethod + # def get_short_version_by_release(_release): + # data = Release.objects.filter(short_release=_release).first() + # return data.short_release @staticmethod def get_short_version(): - data = Release.objects.exclude(status='0').order_by('-short_ver').all() - return [{'version': r.version, 'short_ver': r.short_ver, 'status': r.status} for r in data] + return Release.objects.exclude(status='0').order_by('-ver_id').all() @staticmethod def get_md5(path): diff --git a/update/views.py b/update/views.py index ebcbfa7..2142895 100644 --- a/update/views.py +++ b/update/views.py @@ -16,6 +16,7 @@ from update.utils.get_version import GetVersion from rest_framework.decorators import action from update.utils.git_util import GitUtil from update.utils.cmd_extcute import UpdateCommandUtil +from update.utils.db_compare import DbCompare # CacheResponseMixin 一定要放第一位 @@ -78,6 +79,22 @@ def get_operation_cmd(request): # 测试用接口 def write_git_version_to_db(request): - git_util = GitUtil() + git_util = GitUtil('') git_util.set_release_to_db() 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) \ No newline at end of file