临时代码

main
RogerWork 10 months ago
parent b9df7114e8
commit 59f8ca151f
  1. 6126
      logs/dingxin.log
  2. 18
      update/migrations/0006_updatecommand_process.py
  3. 1
      update/models.py
  4. 0
      update/utils/client_util.py
  5. 49
      update/utils/cmd_extcute.py
  6. 88
      update/utils/db_compare.py
  7. 29
      update/utils/git_util.py

File diff suppressed because it is too large Load Diff

@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-01-08 03:12
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('update', '0005_cinema_user'),
]
operations = [
migrations.AddField(
model_name='updatecommand',
name='process',
field=models.CharField(default='setup', help_text='执行顺序 setup 对比数据库前执行 teardown 对比数据库后执行', max_length=20, verbose_name='执行顺序'),
),
]

@ -87,6 +87,7 @@ class UpdateCommand(BaseModels):
id = models.AutoField(primary_key=True)
desc = models.CharField(verbose_name='命令作用描述', max_length=200, null=False, help_text='命令作用描述')
command = models.CharField(verbose_name='需执行的命令', max_length=2000, null=False, help_text='需执行的命令')
process = models.CharField(verbose_name='执行顺序', max_length=20, default='setup', help_text='执行顺序 setup 对比数据库前执行 teardown 对比数据库后执行')
is_sys = models.BooleanField(verbose_name='系统命令', default=False, help_text='系统命令,默认执行前台不展示')
is_checked = models.BooleanField(verbose_name='默认勾选', default=False, help_text='前台页面默认是否勾选')
is_delete = models.BooleanField(verbose_name='是否有效', default=True, help_text='是否有效')

@ -0,0 +1,49 @@
import paramiko
from update.models import UpdateCommand
from django.db.models import Q
class UpdateCommandUtil:
def __init__(self, cinema_ip):
self.cinema_config = {'hostname': cinema_ip, 'port': 22, 'username': 'root', 'password': 'cine123456'}
self.client = paramiko.SSHClient()
self.model = UpdateCommand
def connect(self):
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(**self.cinema_config)
def disconnect(self):
self.client.close()
def get_all_cmd(self):
return self.model.objects.order_by().all()
def get_sys_cmd(self):
return self.model.objects.filter(is_sys='1').order_by(id).all()
def get_no_sys_cmd(self):
return self.model.objects.exclude(is_sys='1').order_by(id).all()
def get_checked_cmd(self, cmd_list):
sys_cmd = self.get_sys_cmd()
checked_cmd = self.model.objects.filter(id__in=cmd_list).order_by(id).all()
return sys_cmd + checked_cmd
def exec_cmd(self, cmd_list, _release):
exec_cmd_list = self.get_checked_cmd(cmd_list)
for cmd in cmd_list:
if '<param>' in cmd:
cmd = cmd.replace('<param>', _release)
exec_cmd_list.append(cmd)
elif cmd.startswith('<multi>'):
cmd = cmd.replace('<multi>', _release)
for c in cmd.split('###'):
exec_cmd_list.append(c)
else:
exec_cmd_list.append(cmd)
self.connect()
for exec_cmd in exec_cmd_list:
stdin, stdout, stderr = self.client.exec_command(exec_cmd)
print(stdout.read().decode('utf-8'))
self.disconnect()

@ -0,0 +1,88 @@
import os
import pymysql
from update.utils.git_util import GitUtil
class DbCompare:
def __init__(self, target_ip, _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 = ''
# 生成对比文件
def gen_diff_file(self):
serv_host = '127.0.0.1'
serv_user = 'dingxin'
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'
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}'
print(cmd)
result = os.system(cmd)
print(result)
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
# 生成新表sql
def gen_create_sql(self):
create_sql_list = []
with open(self.diff_sql_tmp_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
# 执行差异
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)

@ -61,7 +61,8 @@ class GitUtil:
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 */;'
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}`;'
f.close()
@ -70,7 +71,7 @@ class GitUtil:
# 检查cine.sql是否是最新的
def check_cine_sql(self, _release):
if sql_path := os.path.exists(os.path.join(self.local_path, 'sql/' + _release + '.sql')):
if os.path.exists(sql_path := os.path.join(self.local_path, 'sql/' + _release + '.sql')):
saved_md5 = self.get_md5(sql_path)
new_md5 = self.get_md5(os.path.join(self.local_path, 'install/cine.sql'))
if saved_md5 == new_md5:
@ -78,10 +79,30 @@ class GitUtil:
return False
def write_sql(self, _release):
if sql_path := os.path.exists(os.path.join(self.local_path, 'sql/' + _release + '.sql')):
if os.path.exists(sql_path := os.path.join(self.local_path, 'sql/' + _release + '.sql')):
short_ver = 'cine_' + self.get_short_version(_release)
execute_sql_list = [f'DROP DATABASE IF EXISTS {short_ver};']
with open(sql_path, 'r', encoding='utf-8') as sql_file:
content = sql_file.read()
sql_list = content.split(';')
for sql in sql_list:
sql = sql.strip()
if sql.startswith('CREATE'):
execute_sql_list.append(sql + ';')
if sql.startswith('DROP'):
execute_sql_list.append(sql + ';')
if sql.startswith('USE'):
execute_sql_list.append(sql + ';')
db_conn = pymysql.Connect(**self.db_config)
db_cursor = db_conn.cursor()
db_cursor.executemany()
for ext_sql in execute_sql_list:
result = db_cursor.execute(ext_sql)
print(result, ext_sql)
# 获取数据库名称
def get_db_name(self, _release):
return 'cine_' + self.get_short_version(_release)
# 获取端短版本号
@staticmethod

Loading…
Cancel
Save