完成后台升级逻辑

main
RogerWork 10 months ago
parent f43c65e4e4
commit 4139c71361
  1. 9
      dingxin_toolbox_drf/settings.py
  2. 3
      mock/views.py
  3. 18
      update/utils/db_compare.py
  4. 7
      update/utils/git_util.py
  5. 16
      update/views.py

@ -17,6 +17,11 @@ import os
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
print(BASE_DIR) print(BASE_DIR)
# 服务器IP
SERVER_IP = '172.16.1.63'
# SERVER_IP = ''
# 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/
@ -41,6 +46,7 @@ INSTALLED_APPS = [
'corsheaders', # coreapi生成接口文档 'corsheaders', # coreapi生成接口文档
'rest_framework.authtoken', # Token验证应用 'rest_framework.authtoken', # Token验证应用
'django_filters', 'django_filters',
'channels',
'update', 'update',
'mock', 'mock',
] ]
@ -76,6 +82,9 @@ TEMPLATES = [
WSGI_APPLICATION = 'dingxin_toolbox_drf.wsgi.application' WSGI_APPLICATION = 'dingxin_toolbox_drf.wsgi.application'
# 设置ASGI
ASGI_APPLICATION = ''
# Database # Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases # https://docs.djangoproject.com/en/4.2/ref/settings/#databases

@ -14,6 +14,7 @@ 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
ZZ_URL = 'https://zzcs.yinghezhong.com' ZZ_URL = 'https://zzcs.yinghezhong.com'
mock_config = dict() mock_config = dict()
@ -212,6 +213,8 @@ def get_response(request):
# 获取本机 IP 地址: # 获取本机 IP 地址:
def get_local_ip(): def get_local_ip():
ip = SERVER_IP
if SERVER_IP == '':
r = runserver() r = runserver()
ip = socket.gethostbyname(socket.gethostname()) ip = socket.gethostbyname(socket.gethostname())
print(ip) print(ip)

@ -1,4 +1,5 @@
import os import os
import subprocess
import pymysql import pymysql
from update.utils.git_util import GitUtil from update.utils.git_util import GitUtil
from dingxin_toolbox_drf.settings import BASE_DIR from dingxin_toolbox_drf.settings import BASE_DIR
@ -20,7 +21,9 @@ class DbCompare:
# 生成对比文件 # 生成对比文件
def gen_diff_file(self): def gen_diff_file(self):
if os.path.exists(self.diff_sql_path): if os.path.exists(self.diff_sql_path):
print('remove diff_sql_path')
os.remove(self.diff_sql_path) os.remove(self.diff_sql_path)
print(os.path.exists(self.diff_sql_path))
# serv_host = '127.0.0.1' # serv_host = '127.0.0.1'
# serv_user = 'dingxin' # serv_user = 'dingxin'
@ -41,15 +44,18 @@ class DbCompare:
cmd = f'mysqldiff {server_1} {server_2} --changes-for=server2 --difftype=sql --force --quiet --skip-table-options {db_name}:cine > {self.diff_sql_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 = subprocess.run(cmd)
result = os.system(cmd) result = os.system(cmd)
print(result) print('gen_diff_file', result)
return self.diff_sql_path return self.diff_sql_path
# 针对降级的情况,需要屏蔽掉语句中的drop语句,此处可能有错误的风险,待验证 # 针对降级的情况,需要屏蔽掉语句中的drop语句,此处可能有错误的风险,待验证
def remove_drop(self): def remove_drop(self):
# 如果清理后的文件存在则删除 # 如果清理后的文件存在则删除
if os.path.exists(self.clear_diff_sql_path): if os.path.exists(self.clear_diff_sql_path):
print('remove clear_diff_sql_path')
os.remove(self.clear_diff_sql_path) os.remove(self.clear_diff_sql_path)
print(os.path.exists(self.clear_diff_sql_path))
# 打开生成的差异sql,如果遇到ALTER语句则进入alter模式,将ALTER下面的语句写入temp中,如果是DROP就跳过,当遇到换行时就结束alter模式,把temp写入主列表 # 打开生成的差异sql,如果遇到ALTER语句则进入alter模式,将ALTER下面的语句写入temp中,如果是DROP就跳过,当遇到换行时就结束alter模式,把temp写入主列表
with open(self.diff_sql_path, 'r', encoding='utf-8') as f: with open(self.diff_sql_path, 'r', encoding='utf-8') as f:
sql_list = [] sql_list = []
@ -66,6 +72,12 @@ class DbCompare:
pass pass
elif line.strip().startswith('[PASS]'): elif line.strip().startswith('[PASS]'):
pass pass
elif line.strip().startswith('+'):
pass
elif line.strip().startswith('-'):
pass
elif line.strip().startswith('@'):
pass
elif line.strip().startswith('ALTER DEFINER'): elif line.strip().startswith('ALTER DEFINER'):
temp.append(line) temp.append(line)
alter_definer = 1 alter_definer = 1
@ -123,6 +135,6 @@ class DbCompare:
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}' 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}' 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) diff_result = os.system(diff_cmd)
print(diff_result) print('diff_result', diff_result)
create_result = os.system(create_cmd) create_result = os.system(create_cmd)
print(create_result) print('create_result', create_result)

@ -162,6 +162,13 @@ class GitUtil:
return m.hexdigest() # 返回md5对象 return m.hexdigest() # 返回md5对象
class GitDbUtil:
def __init__(self):
pass
@staticmethod
def get_short_version():
return Release.objects.exclude(status='0').order_by('-ver_id').all()
# if __name__ == '__main__': # if __name__ == '__main__':
# git_util = GitUtil() # git_util = GitUtil()
# # git_util.clone() # # git_util.clone()

@ -17,7 +17,7 @@ from django_filters.rest_framework import DjangoFilterBackend
from django.core.cache import cache from django.core.cache import cache
from update.utils.get_version import GetVersion 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, GitDbUtil
from update.utils.cmd_extcute import UpdateCommandUtil from update.utils.cmd_extcute import UpdateCommandUtil
from update.utils.db_compare import DbCompare from update.utils.db_compare import DbCompare
from update.utils.client_util import ClientUtil from update.utils.client_util import ClientUtil
@ -75,13 +75,6 @@ def update_user(request, *args, **kwargs):
return JsonResponse({'result': 'success'} if result == 1 else {'result': 'fail'}) return JsonResponse({'result': 'success'} if result == 1 else {'result': 'fail'})
def get_git_version(request):
git_util = GitUtil()
git_ver = git_util.get_short_version()
serializer = ReleaseSerializer(instance=git_ver, many=True)
return JsonResponse(serializer.data, safe=False)
def get_operation_cmd(request): def get_operation_cmd(request):
update_cmd = UpdateCommandUtil() update_cmd = UpdateCommandUtil()
no_sys_cmd = update_cmd.get_no_sys_cmd() no_sys_cmd = update_cmd.get_no_sys_cmd()
@ -89,6 +82,13 @@ def get_operation_cmd(request):
return JsonResponse(serializer.data, safe=False) return JsonResponse(serializer.data, safe=False)
def get_git_version(request):
git_db_util = GitDbUtil
git_ver = git_db_util.get_short_version()
serializer = ReleaseSerializer(instance=git_ver, many=True)
return JsonResponse(serializer.data, safe=False)
# 测试用接口 # 测试用接口
def write_git_version_to_db(request): def write_git_version_to_db(request):
git_util = GitUtil('') git_util = GitUtil('')

Loading…
Cancel
Save