Commit 0eae11bf authored by 金凯强's avatar 金凯强 🎨

应用修改和远程DB配置

parent 3d348e2f
...@@ -33,11 +33,22 @@ if __name__ == '__main__': ...@@ -33,11 +33,22 @@ if __name__ == '__main__':
# # 设置远程服务器连接 # # 设置远程服务器连接
# res = requests.post('http://localhost:5000/connection/remote_server/', json=json_data) # res = requests.post('http://localhost:5000/connection/remote_server/', json=json_data)
# print(res.json()) # print(res.json())
#
# # 获取远程服务器连接 # # 获取远程服务器连接
# res = requests.get('http://localhost:5000/connection/remote_server/') # res = requests.get('http://localhost:5000/connection/remote_server/')
# print(res.json()) # print(res.json())
# json_data = {"project_no":"jkq_test","db_ip":"192.168.0.1"
# ,"db_port":"22","db_user":"hhhh","db_password":"2222","mes_db_name":"4324324","tiip_db_name":"tiiip"}
# #
# # 设置远程服务器DB连接
# res = requests.post('http://localhost:5000/connection/remote_db/', json=json_data)
# print(res.json())
#
# # 获取远程服务器DB连接
# res = requests.get('http://localhost:5000/connection/remote_db/')
# print(res.json())
# # 测试端口是否开放 # # 测试端口是否开放
# res = requests.get('http://localhost:5000/connection/test_port/23002/') # res = requests.get('http://localhost:5000/connection/test_port/23002/')
# print(res.json()) # print(res.json())
......
...@@ -50,6 +50,22 @@ class ConnectionAPI(ApiController): ...@@ -50,6 +50,22 @@ class ConnectionAPI(ApiController):
''' '''
return ConnectionService().set_remote_server_info(request.json) return ConnectionService().set_remote_server_info(request.json)
@api('remote_db')
def post_set_remote_db_info(self):
'''
设置远程服务器数据库配置
:return:
'''
return ConnectionService().set_remote_db_info(request.get_json())
@api('remote_db')
def get_remote_db_info(self):
'''
获取远程服务器数据库配置
:return:
'''
return ConnectionService().get_remote_db_info()
@api('test_port/<int:port>') @api('test_port/<int:port>')
def get_test_port(self, port): def get_test_port(self, port):
''' '''
......
...@@ -213,3 +213,66 @@ values ...@@ -213,3 +213,66 @@ values
raise HSException(f'{host_ip}:{port}连接超时') raise HSException(f'{host_ip}:{port}连接超时')
except Exception as e: except Exception as e:
raise HSException(f'{host_ip}:{port}连接失败') raise HSException(f'{host_ip}:{port}连接失败')
def set_remote_db_info(self, param):
'''
设置远程服务器数据库配置
:param param:
:return:
'''
'''project_no,db_ip,db_port,db_user,db_password,mes_db_name,tiip_db_name'''
import os
from static_file import profile_dir
remote_db_info_path = os.path.join(profile_dir, 'huansi_server.sh')
# 写入
with open(remote_db_info_path, "w") as f:
for k, v in param.items():
k = self.math_name(k)
if k in ['ID', 'CREATE_TIME']:
continue
f.writelines("export {}={}\n".format(k, v))
# mes的数据库写两次,兼容之前的
if k == 'HSDB_NAME':
f.writelines("export {}={}\n".format('MESDB_NAME', v))
return {'message': "保存成功"}
def get_remote_db_info(self):
'''
获取远程服务器数据库配置
:return:
'''
import os
from static_file import profile_dir
remote_db_info_path = os.path.join(profile_dir, 'huansi_server.sh')
if not os.path.exists(remote_db_info_path):
return {}
with open(remote_db_info_path, "r") as f:
data = f.read()
data = data.replace('export ', '')
data_item_list = data.split('\n')
data_dict = {}
for data_item in data_item_list:
_l = data_item.split('=')
if _l[0] == 'HSCUSCODE':
data_dict['project_no'] = _l[1]
if _l[0] == 'HSDB_HOST':
data_dict['db_ip'] = _l[1]
if _l[0] == 'HSDB_PORT':
data_dict['db_port'] = _l[1]
if _l[0] == 'HSDB_USER':
data_dict['db_user'] = _l[1]
if _l[0] == 'HSDB_PASSWORD':
data_dict['db_password'] = _l[1]
if _l[0] == 'HSDB_NAME':
data_dict['mes_db_name'] = _l[1]
if _l[0] == 'TIIP_DB_NAME':
data_dict['tiip_db_name'] = _l[1]
return data_dict
...@@ -14,6 +14,13 @@ class UpgradeAPI(ApiController): ...@@ -14,6 +14,13 @@ class UpgradeAPI(ApiController):
3、导出安装包 3、导出安装包
4、导入安装包升级(不紧急) 4、导入安装包升级(不紧急)
''' '''
@api('apply')
def get_apply_db_setting(self):
'''
应用数据库配置
:return:
'''
return UpgradeService().apply_db_setting()
@api('rollback/<int:log_id>') @api('rollback/<int:log_id>')
def get_rollback_upgrade(self, log_id): def get_rollback_upgrade(self, log_id):
......
...@@ -118,6 +118,11 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym ...@@ -118,6 +118,11 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
g.language = 'cn' g.language = 'cn'
upgrade_no = self.get_upgrade_no_by_log_id(log_id) upgrade_no = self.get_upgrade_no_by_log_id(log_id)
# 校验远程服务器db有没有配置
if not os.path.exists(os.path.join(profile_dir, 'huansi_server.sh')):
logger.info('远程升级失败')
raise HSException('服务器DB未配置,请先配置')
# 找到服务器ip # 找到服务器ip
# 上传app包到服务器 # 上传app包到服务器
remote_server_info = ConnectionService().get_remote_server_info() remote_server_info = ConnectionService().get_remote_server_info()
...@@ -151,7 +156,8 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym ...@@ -151,7 +156,8 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
ssh.upload(path, f'/huansi/upgrade/{files}') ssh.upload(path, f'/huansi/upgrade/{files}')
ssh.upload(os.path.join(system_file_dir, 'load.sh'), '/huansi/upgrade/load.sh') ssh.upload(os.path.join(system_file_dir, 'load.sh'), '/huansi/upgrade/load.sh')
ssh.upload(os.path.join(profile_dir, 'huansi.sh'), '/huansi/upgrade/huansi.sh') # 传入服务器配置的DB
ssh.upload(os.path.join(profile_dir, 'huansi_server.sh'), '/huansi/upgrade/huansi.sh')
ssh.upload(os.path.join(system_file_dir, 'load_handle.py'), '/huansi/upgrade/load_handle.py') ssh.upload(os.path.join(system_file_dir, 'load_handle.py'), '/huansi/upgrade/load_handle.py')
out, err = ssh.exec_command("sh /huansi/upgrade/load.sh") out, err = ssh.exec_command("sh /huansi/upgrade/load.sh")
...@@ -473,7 +479,7 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym ...@@ -473,7 +479,7 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
return deploy_image_list return deploy_image_list
def get_upgrade_no_by_log_id(self, log_id): def get_upgrade_no_by_log_id(self, log_id=None):
''' '''
获取升级编号 获取升级编号
:param log_id: :param log_id:
...@@ -507,3 +513,34 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym ...@@ -507,3 +513,34 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
logger.info('验证远端服务器是否安装docker日志:{}'.format(err)) logger.info('验证远端服务器是否安装docker日志:{}'.format(err))
logger.info('远程升级失败') logger.info('远程升级失败')
raise HSException('远端服务器未安装docker,请先安装后再升级') raise HSException('远端服务器未安装docker,请先安装后再升级')
def apply_db_setting(self):
'''
应用数据库db配置
:return:
'''
if not os.path.exists(os.path.join(profile_dir, 'huansi.sh')):
raise HSException('请先配置数据库信息')
query_app_sql = 'select * from app_upgrade_log where default_version=1'
with db_driver as session:
app_info = session.retrive_sql(query_app_sql)
if not app_info:
raise HSException('未查询到本机有升级信息,不用应用修改')
else:
upgrade_no = app_info['upgrade_no']
project_info = ConnectionService().get_project_info()
host_ip = project_info['host_ip']
# 调用本机的docker-compose
ssh_conenct = SSHConnect(user_name='root', password='huansi.net', host_port='1111', host_ip=host_ip)
with ssh_conenct as ssh:
out, err = ssh.exec_command(
f'''export HUANSI_REGISTRY_URL=47.110.145.204:8084 &&\
source /etc/profile.d/huansi.sh &&\
docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.yml -p deploy up -d --force-recreate''')
logger.info(f'应用操作信息:{out},err:{err}')
logger.info("应用修改成功")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment