-
Notifications
You must be signed in to change notification settings - Fork 55
/
addtask.py
94 lines (81 loc) · 2.72 KB
/
addtask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import sys
import os
import time
import json
import types
from lib.logLib import *
from lib.cmdLib import *
from lib.sftpLib import *
from lib.mysqlLib import *
import jenkinsapi
from jenkinsapi.jenkins import Jenkins
from jenkinsapi.jobs import Jobs
from jenkinsapi.job import Job
from jenkinsapi.build import Build
def addtask(jenkinsurl, jobname, build_params):
mysql = mysqlLib()
try:
#任务初始化,0
param = (jenkinsurl, jobname, json.dumps(build_params), 0)
n,last_id = mysql.add_task(param)
print(u"#任务初始化,0")
J = Jenkins(jenkinsurl)
jobs = Jobs(J)
job = jobs[jobname]
invoke = job.invoke(build_params=build_params)
'''is_queued = invoke.is_queued()
while(is_queued is False):
time.sleep(1)
is_queued = invoke.is_queued()
#任务排队中,1
param=(1, last_id)
mysql.update_task_status(param)
print(u"#任务排队,1")
is_running = invoke.is_running()
while(is_running is False):
time.sleep(1)
is_running = invoke.is_running()
#任务执行中,2
param=(2, last_id)
mysql.update_task_status(param)
print(u"#任务执行,2")'''
#更新任务 build_number
build_number = invoke.get_build_number()
param=(build_number, last_id)
mysql.update_task_build_number(param)
print build_number
'''is_running = invoke.is_running()
while(is_running is True):
time.sleep(1)
is_running = invoke.is_running()
#任务执行完成,3
param=(3, last_id)
mysql.update_task_status(param)
print(u"#任务完成,3")'''
except Exception as e:
logging.ERROR(e)
#任务执行异常,4
param=(4, last_id)
mysql.update_task_status(param)
print(u"#任务异常,4")
finally:
mysql.close()
if __name__ == '__main__':
jenkinsurl = sys.argv[1]
jobname = sys.argv[2]
build_params = json.loads(sys.argv[3])
for item in build_params:
'''try:
if(build_params[item]):
#tmpstr = json.dumps(build_params[item])
print build_params[item]
except Exception as e:
pass'''
if(type(build_params[item]) == list or type(build_params[item]) == dict):
print build_params[item]
print str(build_params[item])
build_params[item] = json.dumps(build_params[item])
print build_params
addtask(jenkinsurl, jobname, build_params)