增加修改设置的逻辑

main
RogerWork 10 months ago
parent 9ca73dac30
commit acda8b7d6e
  1. 1
      update/models.py
  2. 2
      update/serializers.py
  3. 40
      update/utils/cmd_extcute.py
  4. 37
      update/views.py

@ -108,6 +108,7 @@ class UpdateCommand(BaseModels):
is_checked = models.BooleanField(verbose_name='默认勾选', default=False, help_text='前台页面默认是否勾选')
is_force = models.BooleanField(verbose_name='必选项', default=False, help_text='必选项')
is_delete = models.BooleanField(verbose_name='是否有效', default=True, help_text='是否有效')
comment = models.TextField(verbose_name='页面显示提示', default='',help_text='页面显示提示')
def __str__(self):
return self.desc

@ -41,4 +41,4 @@ class UpdateCommandSerializer(serializers.ModelSerializer):
model = UpdateCommand
fields = (
'id', 'desc', 'command', 'ver_id', 'process', 'run_num', 'is_sys', 'is_checked', 'is_force', 'is_delete',
'update_time')
'comment', 'update_time')

@ -1,7 +1,11 @@
import json
import time
import paramiko
from update.models import UpdateCommand
import pymysql
from update.models import UpdateCommand, Cinema
from update.serializers import UpdateCommandSerializer
from django.db.models import Q
from time import sleep
@ -84,3 +88,37 @@ class UpdateCommandUtil:
else:
exec_cmd_list.append(cmd)
self.exec_cmd(cinema_ip, exec_cmd_list)
# 执行设置相关
class UpdateConfigUtil:
def __init__(self, cinema_ip, checked_list):
self.ip = cinema_ip
self.checked_list = checked_list
def exec_config(self):
cmd_list = self.get_all_exec_cmd()
db_config = Cinema.objects.filter(ip=self.ip).values()[0]
print(db_config)
print(db_config['db_user'], db_config['db_pwd'])
db_conn = pymysql.Connect(host=self.ip, port=3306, user=db_config['db_user'], passwd=db_config['db_pwd'],
database='cine')
db_cursor = db_conn.cursor()
for cmds in cmd_list:
for cmd in cmds:
db_cursor.execute(cmd)
db_conn.commit()
db_cursor.close()
db_conn.close()
def get_all_exec_cmd(self):
all_config_obj = UpdateCommand.objects.filter(Q(process='config') & Q(is_delete=False)).values()
print(all_config_obj)
cmd_list = []
for config_item in all_config_obj:
print(config_item)
if config_item['id'] in self.checked_list:
cmd_list.append(json.loads(config_item['command'])['checked'])
else:
cmd_list.append(json.loads(config_item['command'])['unchecked'])
return cmd_list

@ -18,7 +18,7 @@ from django.core.cache import cache
from update.utils.get_version import GetVersion
from rest_framework.decorators import action
from update.utils.git_util import GitUtil, GitDbUtil
from update.utils.cmd_extcute import UpdateCommandUtil
from update.utils.cmd_extcute import UpdateCommandUtil, UpdateConfigUtil
from update.utils.db_compare import DbCompare
from update.utils.client_util import ClientUtil
@ -93,7 +93,15 @@ def get_git_version(request):
def update_cine(request):
req = request.GET.dict()
cinema_ip = req.get('ip')
# 处理命令格式
cmd_dict = {}
cmd_obj = UpdateCommand.objects.filter(is_sys=0).values('id', 'process')
for cmd in cmd_obj:
if cmd['process'] in cmd_dict.keys():
cmd_dict[cmd['process']].append(cmd['id'])
else:
cmd_dict[cmd['process']] = [cmd['id']]
print(cmd_dict)
update_status = {
'git': {'result': 'success', 'msg': ''},
'setup': {'result': 'success', 'msg': ''},
@ -115,24 +123,39 @@ def update_cine(request):
print('获取并写入cine.sql')
git_util = GitUtil(short_release)
git_result = git_util.handle_create_cine()
# 执行setup
print('执行setup')
setup_list = list(set(cmd_list) & set(cmd_dict['setup']))
update_cmd = UpdateCommandUtil()
update_cmd.exec_cmd_by_type(cinema_ip, 'setup', cmd_list, short_release)
update_cmd.exec_cmd_by_type(cinema_ip, 'setup', setup_list, short_release)
# 数据库对比
if 19 in cmd_list:
sql_list = list(set(cmd_list) & set(cmd_dict['sql']))
if len(sql_list) > 0:
print('数据库对比')
db_compare = DbCompare(cinema_ip, short_release)
db_compare.exec_diff_sql()
# 执行teardown
if 17 in cmd_list:
teardown_list = list(set(cmd_list) & set(cmd_dict['teardown']))
if len(teardown_list) > 0:
print('执行teardown')
update_cmd.exec_cmd_by_type(cinema_ip, 'teardown', cmd_list, short_release)
update_cmd.exec_cmd_by_type(cinema_ip, 'teardown', teardown_list, short_release)
# 修改设置
print('执行config_list')
config_list = list(set(cmd_list) & set(cmd_dict['config']))
update_config = UpdateConfigUtil(cinema_ip, config_list)
update_config.exec_config()
# 传输客户端
if 20 in cmd_list:
client_list = list(set(cmd_list) & set(cmd_dict['client']))
if len(client_list) > 0:
print('传输客户端')
client_release = ClientUtil()
client_release.client_process(cinema_ip, short_release)
return JsonResponse({'result': 'success', 'ip': cinema_ip})

Loading…
Cancel
Save