Commit 8163e547 authored by 金凯强's avatar 金凯强 🎨

拆分升级步骤

parent 49937009
......@@ -15,6 +15,30 @@ class InstallAPI(ApiController):
'''
return InstallService().install_runner()
@api('runner/step1')
def get_install_runner_step1(self):
'''
安装runner1
:return:
'''
return InstallService().install_huansi_runner_step1()
@api('runner/step2')
def get_install_runner_step2(self):
'''
安装runner2
:return:
'''
return InstallService().install_huansi_runner_step2()
@api('runner/step3')
def get_install_runner_step3(self):
'''
安装runner3
:return:
'''
return InstallService().install_huansi_runner_step3()
@api('remote_server/docker/')
def get_install_remote_server_docker(self):
'''
......
......@@ -29,7 +29,6 @@ class InstallService(HSBaseUCService):
:param HSCUSCODE:
:return:
'''
time.sleep(60)
self.delete_setting_file()
s1 = os.system("docker run --rm -t -i -v /etc/gitlab-runner:/etc/gitlab-runner gitlab/gitlab-runner register \
-n -u http://47.110.145.204:8085/ -r WwpzH4qk19KjvAjEwoTz --executor docker --docker-image docker \
......@@ -42,6 +41,49 @@ class InstallService(HSBaseUCService):
self.start_runner()
return {"message": "安装runner成功"}
def install_huansi_runner_step1(self):
'''
安装runner第一步
:param HSCUSCODE:
:return:
'''
conncetion_service = ConnectionService()
project_info = conncetion_service.get_project_info()
if not project_info:
raise HSException('项目信息未查到,请先配置')
project_code = project_info['project_no']
# 测试下载时间
time.sleep(20)
self.delete_setting_file()
s1 = os.system("docker run --rm -t -i -v /etc/gitlab-runner:/etc/gitlab-runner gitlab/gitlab-runner register \
-n -u http://47.110.145.204:8085/ -r WwpzH4qk19KjvAjEwoTz --executor docker --docker-image docker \
--description huansi.{hscode} \
--tag-list huansi.{hscode}".format(hscode=project_code))
if s1 != 0:
raise HSException('gitlab-runner安装失败')
return {"message": "runner注册成功", "step": "1", "status": "running", "later_message": "正在修改runner配置..."}
def install_huansi_runner_step2(self):
'''
安装runner第二步
:param HSCUSCODE:
:return:
'''
self._edit_runner_setting()
self._remove_old_runner_container()
return {"message": "runner配置修改成功", "step": "2", "status": "running", "later_message": "正在重启runner..."}
def install_huansi_runner_step3(self):
'''
安装runner第三步
:param HSCUSCODE:
:return:
'''
self.start_runner()
return {"message": "runner重启成功", "step": "3", "status": "success"}
def _edit_runner_setting(self):
os.system("chmod 777 /etc/gitlab-runner/config.toml")
with open('/etc/gitlab-runner/config.toml', 'r') as f:
......
......@@ -32,6 +32,32 @@ class UpgradeAPI(ApiController):
log_id = request.args.get('log_id')
return UpgradeService().remote_upgrade(log_id)
@api('remote/step1')
def get_remote_upgrade(self):
'''
远程升级---1
:return:
'''
return UpgradeService().remote_upgrade_step1()
@api('remote/step2')
def get_remote_upgrade(self):
'''
远程升级---2
:return:
'''
log_id = request.args.get('log_id')
return UpgradeService().remote_upgrade_step2(log_id)
@api('remote/step3')
def get_remote_upgrade(self):
'''
远程升级---3
:return:
'''
log_id = request.args.get('log_id')
return UpgradeService().remote_upgrade_step3(log_id)
@api('package_images/<int:log_id>')
def get_package_images(self, log_id):
'''
......
......@@ -70,11 +70,17 @@ class UpgradeService():
# 调用本机的docker-compose
ssh_conenct = SSHConnect(user_name='root', password='huansi.net', host_port='1111', host_ip=host_ip)
with ssh_conenct as ssh:
# todo 处理*的情况
out, err = ssh.exec_command(
f'''export HUANSI_REGISTRY_URL=47.110.145.204:8084 &&\
# 处理*的情况
if app_list == '*':
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 --no-deps {app_list}''')
docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.yml -p deploy up -d --force-recreate''')
else:
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 --no-deps {app_list}''')
# 会有问题,docker-compose编译的时候,会把过程放到err上,所以注释这段
# if err:
# raise HSException(f'回滚失败:{err}')
......@@ -150,6 +156,81 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
return {'message': '升级完成', out: err}
def remote_upgrade_step1(self):
'''
远程升级
: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:
# 验证远端服务器有没有安装docker
self.validate_remote_server_install_docker(ssh)
return {"message": f'成功验证远端服务器已经安装docker',
"step": "1", "status": "running", "later_message": "正在打包程序,该过程会很漫长,请耐心等待..."}
def remote_upgrade_step2(self, log_id=None):
'''
远程升级2
:return:
'''
upgrade_no = self.get_upgrade_no_by_log_id(log_id)
self._package_images(upgrade_no)
return {"message": f'打包完成,请到D:\\HuanSi\\LinuxData\\upgrade\\upgrade_tools_data\\backup\\{upgrade_no}查看',
"step": "2", "status": "running", "later_message": "正在上传服务器并升级,该过程会很漫长,请耐心等待..."}
def remote_upgrade_step3(self, log_id=None):
'''
远程升级3
: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)
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
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')
out, err = ssh.exec_command("sh /huansi/upgrade/load.sh")
logger.info(f'远程升级信息:{out},远程错误信息:{out}')
return {"message": f'已经成功上传服务器并升级', "step": "3", "status": "success"}
def package_images(self, log_id):
'''
打包镜像
......@@ -164,7 +245,7 @@ docker-compose -f /data/upgrade_tools_data/backup/{upgrade_no}/docker-compose.ym
self._package_images(upgrade_no)
return {'message': '打包完成,请到D:\\HuanSi\\LinuxData\\upgrade\\upgrade_tools_data\\backup查看'}
return {'message': f'打包完成,请到D:\\HuanSi\\LinuxData\\upgrade\\upgrade_tools_data\\backup\\{upgrade_no}查看'}
def _package_images(self, upgrade_no):
docker_compose_file_dir = self.find_compose_by_upgrade_no(upgrade_no)
......
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