Commit cf2c173a authored by 金凯强's avatar 金凯强 🎨

异步安装runner

parent 28d877b1
Pipeline #21126 passed with stage
in 27 seconds
......@@ -10,7 +10,7 @@ from sqlalchemy.orm import sessionmaker
from app.utils.db_tools import db_driver
class ConnectionService(HSBaseUCService):
class ConnectionService():
def get_project_info(self):
'''
获取项目信息
......
......@@ -210,5 +210,7 @@ where A.log_id={log_id}'''
获取升级日志
:return:
'''
logs = docker_client.logs('install_api')
logs = docker_client.logs('install_api',tail=200)
if isinstance(logs, bytes):
logs = logs.decode()
return logs
# -*- coding:utf-8 -*-
import os
import time
from threading import Thread
from huansi_utils.app.apploader import logger
from huansi_utils.exception.exception import HSException
from huansi_utils.server.service_uc import HSBaseUCService
......@@ -16,12 +18,24 @@ class InstallService(HSBaseUCService):
安装runner
:return:
'''
t = Thread(target=self._install_runner)
t.start()
return {"message": "开始安装runner"}
def _install_runner(self):
from flask_app import global_app
with global_app.app_context():
from flask import g
g.user = {}
g.language = 'cn'
conncetion_service = ConnectionService()
project_info = conncetion_service.get_project_info()
if not project_info:
raise HSException('项目信息未查到,请先配置')
project_code = project_info['project_no']
return self.install_huansi_runner(project_code)
self.install_huansi_runner(project_code)
def install_huansi_runner(self, HSCUSCODE):
'''
......@@ -30,7 +44,7 @@ class InstallService(HSBaseUCService):
:return:
'''
self.delete_setting_file()
s1 = os.system("docker run --rm -t -i -v /etc/gitlab-runner:/etc/gitlab-runner gitlab/gitlab-runner register \
s1 = os.system("docker run --rm -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=HSCUSCODE))
......@@ -39,7 +53,7 @@ class InstallService(HSBaseUCService):
self._edit_runner_setting()
self._remove_old_runner_container()
self.start_runner()
return {"message": "安装runner成功"}
logger.info('runner安装成功')
def install_huansi_runner_step1(self):
'''
......@@ -84,6 +98,7 @@ class InstallService(HSBaseUCService):
return {"message": "runner重启成功", "step": "3", "status": "success"}
def _edit_runner_setting(self):
logger.info('修改配置文件')
os.system("chmod 777 /etc/gitlab-runner/config.toml")
with open('/etc/gitlab-runner/config.toml', 'r') as f:
s = f.read()
......@@ -91,15 +106,19 @@ class InstallService(HSBaseUCService):
'volumes = ["/var/run/docker.sock:/var/run/docker.sock","/etc/profile.d/huansi.sh:/etc/profile.d/huansi.sh" ,"/cache","/huansi/gitlab-runner/builds:/builds/hs"]')
with open('/etc/gitlab-runner/config.toml', 'w') as f:
f.write(a)
logger.info('修改配置文件成功')
def _remove_old_runner_container(self):
'''
移除旧runner容器,主要作用于第二次第三次的重复安装
:return:
'''
logger.info('删除旧容器')
os.system("docker stop gitlab-runner && docker rm gitlab-runner")
logger.info('删除旧容器成功')
def start_runner(self):
logger.info('重新启动runner')
s = os.system("docker run -d --name gitlab-runner --restart always \
-v /etc/gitlab-runner:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
......@@ -108,9 +127,12 @@ class InstallService(HSBaseUCService):
if s != 0:
raise HSException('启动runner失败')
logger.info('重新启动runner成功')
def delete_setting_file(self):
logger.info('删除配置文件')
os.system('rm -f /etc/gitlab-runner/config.toml')
logger.info('删除配置文件成功')
def install_remote_service_docker(self):
'''
......
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