Commit 340b63da authored by 赵凯's avatar 赵凯 💬

自动识别项目集群部署方式,并进行集群部署

parent 54101814
...@@ -3,4 +3,5 @@ __pycache__/ ...@@ -3,4 +3,5 @@ __pycache__/
config.yaml config.yaml
*.cache *.cache
.DS_Store .DS_Store
.idea/ .idea/
\ No newline at end of file *.db
\ No newline at end of file
...@@ -26,4 +26,34 @@ def tasksPatch(taskid): ...@@ -26,4 +26,34 @@ def tasksPatch(taskid):
'cookie': cookie 'cookie': cookie
} }
response = requests.request("PATCH", url, headers=headers, data=payload) response = requests.request("PATCH", url, headers=headers, data=payload)
print("任务开始执行...") print("任务开始执行...")
\ No newline at end of file
# 集群部署任务
def taskMultiZone(taskid, target_k8):
cookie = getToken()
url = "https://gw.qimai.shop/api/jenkins/jenkinsjob/tasks/multi_zone/"
# payload = "{\"task_id\":218808,\"target_k8s\":[5]}"
payload = f'{{"task_id":{taskid},"target_k8s":[{target_k8}]}}'
headers = {
'cookie': cookie,
'authority': 'gw.qimai.shop',
'accept': 'application/json, text/plain, */*',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'content-type': 'application/json;charset=UTF-8',
'origin': 'https://opms.qimai.shop',
'pragma': 'no-cache',
'referer': 'https://opms.qimai.shop/',
'sec-ch-ua': '"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
'x-xsrftoken': 'undefined'
}
response = requests.request("POST", url, headers=headers, data=payload)
print("集群部署任务开始执行...")
...@@ -6,7 +6,7 @@ from apis.login_api import login ...@@ -6,7 +6,7 @@ from apis.login_api import login
from apis.get_prejects_api import getProjects from apis.get_prejects_api import getProjects
from apis.get_gits_api import getGits from apis.get_gits_api import getGits
from apis.tasks_api import tasks from apis.tasks_api import tasks
from apis.task_patch_api import tasksPatch from apis.task_patch_api import tasksPatch,taskMultiZone
from apis.task_status_api import taskstatus from apis.task_status_api import taskstatus
...@@ -21,7 +21,19 @@ def checkProjId(): ...@@ -21,7 +21,19 @@ def checkProjId():
proj_id = result['id'] proj_id = result['id']
print(f'识别出项目: {proj_name}, 项目ID为: {proj_id}') print(f'识别出项目: {proj_name}, 项目ID为: {proj_id}')
return proj_id return proj_id
raise Exception("无法识别的项目") raise Exception("无法识别的项目")
def checkProjIdGetResult():
# 获取当前执行目录
cwd = os.getcwd()
rst_results = getProjects()
for result in rst_results:
if cwd.find(result['name']) != -1 :
proj_name = result['name']
proj_id = result['id']
print(f'识别出项目: {proj_name}, 项目ID为: {proj_id}')
return result
raise Exception("无法识别的项目")
# 获取当前项目,当前环境最新commitid # 获取当前项目,当前环境最新commitid
def checkCommitid(projectid, env): def checkCommitid(projectid, env):
...@@ -56,14 +68,27 @@ else: ...@@ -56,14 +68,27 @@ else:
info = argv[2] info = argv[2]
# 识别项目 # 识别项目
projectid = checkProjId() projectResult = checkProjIdGetResult()
# 项目ID
projectid = projectResult['id']
# 项目环境部署信息
projectEnvRef = next((env_ref for env_ref in projectResult['env_ref'] if env_ref['env'] == env), None)
# print(projectResult)
# print(projectEnvRef)
# 拉取提交节点 # 拉取提交节点
commitid = checkCommitid(projectid, env) commitid = checkCommitid(projectid, env)
# 创建部署任务 # 创建部署任务
taskid = tasks(projectid, env, commitid, info) taskid = tasks(projectid, env, commitid, info)
time.sleep(3) time.sleep(3)
# 执行部署任务 # 执行部署任务 or 执行集群部署任务
tasksPatch(taskid) multi_zone_deploy = projectEnvRef.get('multi_zone_deploy', False)
k8sname = projectEnvRef.get('k8sname', 0)
if multi_zone_deploy:
taskMultiZone(taskid, k8sname)
else:
tasksPatch(taskid)
# 轮训任务状态 # 轮训任务状态
is_continue = 1 is_continue = 1
while is_continue == 1: while is_continue == 1:
......
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