Commit 11207ecc authored by 金凯强's avatar 金凯强 🎨

远程升级拆成6步

parent 2323a614
Pipeline #20840 passed with stage
in 24 seconds
......@@ -49,14 +49,40 @@ class UpgradeAPI(ApiController):
log_id = request.args.get('log_id')
return UpgradeService().remote_upgrade_step2(log_id)
@api('remote/step3')
def get_remote_upgrade3(self):
@api('remote/step3/<int:app_count>/')
def get_remote_upgrade2(self, app_count):
'''
远程升级---3
:return:
'''
log_id = request.args.get('log_id')
return UpgradeService().remote_upgrade_step3(log_id)
return UpgradeService().remote_upgrade_step3(app_count, log_id)
@api('remote/step4')
def get_remote_upgrade4(self):
'''
远程升级---4
:return:
'''
log_id = request.args.get('log_id')
return UpgradeService().remote_upgrade_step4(log_id)
@api('remote/step5/<int:tar_file_index>')
def get_remote_upgrade5(self, tar_file_index):
'''
远程升级---5
:return:
'''
log_id = request.args.get('log_id')
return UpgradeService().remote_upgrade_step5(tar_file_index, log_id)
@api('remote/step6')
def get_remote_upgrade6(self):
'''
远程升级---6
:return:
'''
return UpgradeService().remote_upgrade_step6()
@api('package_images/<int:log_id>')
def get_package_images(self, log_id):
......
......@@ -184,19 +184,67 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
远程升级2
:return:
'''
upgrade_no = self.get_upgrade_no_by_log_id(log_id)
# 分步骤
# 1、获取文件下所有要升级的app,如果有则删除这个目录下的tar,并返回要升级app的数量
# 2、根据传入的顺序打包
docker_compose_file_dir = self.find_compose_by_upgrade_no(upgrade_no)
# 打包所有当前版本的所有app 到 /data下
# 获取要升级的镜镜像名称
deploy_image_list = self._get_deploy_image(docker_compose_file_dir)
logger.info('要升级的镜像信息如下:\n{}'.format(deploy_image_list))
self._package_images(upgrade_no)
if not deploy_image_list:
raise HSException('未在docker-compose文件中找到要升级的app')
return {"message": f'打包完成,请到D:\\HuanSi\\LinuxData\\upgrade\\upgrade_tools_data\\backup\\{upgrade_no}查看',
"step": "2", "status": "running", "later_message": "正在上传服务器并升级,该过程会很漫长,请耐心等待..."}
upgrade_no_back_up_dir = os.path.join(back_up_dir, upgrade_no)
def remote_upgrade_step3(self, log_id=None):
if not os.path.exists(upgrade_no_back_up_dir):
raise HSException(f'{upgrade_no_back_up_dir}找不到')
# 删除旧镜像
os.system(f'rm -f {upgrade_no_back_up_dir}/*.tar')
return {"app_cout": len(deploy_image_list)}
def remote_upgrade_step3(self, app_count, log_id=None):
'''
远程升级3
:param app_count:
:param log_id:
:return:
'''
upgrade_no = self.get_upgrade_no_by_log_id(log_id)
# 分步骤
# 1、获取文件下所有要升级的app,如果有则删除这个目录下的tar,并返回要升级app的数量
# 2、根据传入的顺序打包
docker_compose_file_dir = self.find_compose_by_upgrade_no(upgrade_no)
# 打包所有当前版本的所有app 到 /data下
# 获取要升级的镜镜像名称
deploy_image_list = self._get_deploy_image(docker_compose_file_dir)
try:
deploy_image = deploy_image_list[int(app_count) - 1]
except IndexError:
raise HSException('请检查app顺序')
upgrade_no_back_up_dir = os.path.join(back_up_dir, upgrade_no)
logger.info('生成镜像:{}'.format(deploy_image))
file_name = deploy_image.replace('/', '___').replace(':', '__') + '.tar'
_cmd = 'docker save {} -o {}/{}'.format(deploy_image, upgrade_no_back_up_dir, file_name)
result = os.system(_cmd)
if result == 1:
raise RuntimeError("{}执行出错".format(_cmd))
logger.info(f'镜像生成完毕,目录为{upgrade_no_back_up_dir}')
def remote_upgrade_step4(self, log_id=None):
'''
远程升级4
:return:
'''
# 分步骤
# 上传除tar文件的数量,找tar文件数量,返回数量
# 根据顺序挨个上tar文件
# 远程执行升级命令
upgrade_no = self.get_upgrade_no_by_log_id(log_id)
# 找到服务器ip
# 上传app包到服务器
......@@ -212,19 +260,75 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
# 复制文件到待上传的目录
upgrade_back_up_dir = os.path.join(temp_file_dir, 'backup', upgrade_no)
tar_file_index = 0
with ssh_conenct as ssh:
for files in os.listdir(upgrade_back_up_dir):
path = os.path.join(upgrade_back_up_dir, files)
# 隐藏文件不复制
if files.startswith('.'):
continue
# 镜像文件单独上传
if files.endswith('.tar'):
tar_file_index += 1
continue
logger.info(f"地址:{path}---->/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(profile_dir, 'huansi.sh'), '/huansi/upgrade/huansi.sh')
ssh.upload(os.path.join(system_file_dir, 'load_handle.py'), '/huansi/upgrade/load_handle.py')
return {"app_cout": tar_file_index}
def remote_upgrade_step5(self, tar_file_index, log_id=None):
'''
远程升级5
:return:
'''
upgrade_no = self.get_upgrade_no_by_log_id(log_id)
# 找到服务器ip
# 上传app包到服务器
remote_server_info = ConnectionService().get_remote_server_info()
if not remote_server_info:
raise HSException('远端服务器信息未查到,请先配置')
ssh_conenct = SSHConnect(host_ip=remote_server_info['server_ip'],
host_port=remote_server_info['server_ssh_port'],
user_name=remote_server_info['server_user'],
password=remote_server_info['server_password'])
# 复制文件到待上传的目录
upgrade_back_up_dir = os.path.join(temp_file_dir, 'backup', upgrade_no)
local_tar_file_list = [item for item in os.listdir(upgrade_back_up_dir) if item.endswith('.tar')]
with ssh_conenct as ssh:
try:
local_tar_file = local_tar_file_list[int(tar_file_index - 1)]
except IndexError:
raise HSException('请检查app顺序')
path = os.path.join(upgrade_back_up_dir, local_tar_file)
logger.info(f"地址:{path}---->/huansi/upgrade/{local_tar_file}")
ssh.upload(path, f'/huansi/upgrade/{local_tar_file}')
def remote_upgrade_step6(self):
'''
远程升级6
:return:
'''
# 找到服务器ip
# 上传app包到服务器
remote_server_info = ConnectionService().get_remote_server_info()
if not remote_server_info:
raise HSException('远端服务器信息未查到,请先配置')
ssh_conenct = SSHConnect(host_ip=remote_server_info['server_ip'],
host_port=remote_server_info['server_ssh_port'],
user_name=remote_server_info['server_user'],
password=remote_server_info['server_password'])
with ssh_conenct as ssh:
out, err = ssh.exec_command("sh /huansi/upgrade/load.sh")
logger.info(f'远程升级信息:{out},远程错误信息:{err}')
......
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