-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobinfo.py
executable file
·181 lines (154 loc) · 6.1 KB
/
jobinfo.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/home/dmick/v/bin/python3
import argparse
import datetime
fromtimestamp=datetime.datetime.fromtimestamp
import jenkins
import json
import os
import re
import sys
import time
# set JENKINS_USER and JENKINS_TOKEN in environment
def parse_args():
ap = argparse.ArgumentParser()
ap.add_argument("-j", "--json", action='store_true', help="Output json")
ap.add_argument("-P", "--allparams", action='store_true', help="Output all job parameters")
ap.add_argument("-l", "--list", action='store_true', help="List all jobs and exit")
ap.add_argument("-c", "--count", type=int, help="Limit output to this many jobs")
ap.add_argument('jobre', type=str, nargs="?", default='^ceph-dev-new$', help="regexp to match job name")
return ap.parse_args()
def to_minsec(ms):
return sec_to_minsec(ms // 1000)
def sec_to_minsec(totalsec):
h = totalsec // 3600
m = (totalsec - (h * 3600)) // 60
s = totalsec - (h * 3600) - (m * 60)
return f'{h:02d}:{m:02d}:{s:02d}'
def decruft(reason):
'''
Remove some cruft from lines like:
GitHub pull request #54725 of commit 75e88727ef2bfd13bfcad68c6e60db6bf9d73364, no merge conflicts.
'''
cruftsubs = [
('GitHub pull request ', 'PR'),
('of commit ', ''),
(', no merge conflicts.', ''),
('build number ', '#'),
]
for s,r in cruftsubs:
reason = re.sub(s, r, reason)
return reason
def output(name, buildnum, reason, paramdict, start, age, bi, waittime, returndict=False):
age = sec_to_minsec(age)
if returndict:
outdict = {
"buildnum": buildnum,
"reason": reason,
"params": paramdict,
"started": start,
"building": bi["building"],
}
if bi["building"]:
outdict.update(dict(
estimatedDuration=to_minsec(bi["estimatedDuration"]),
age=age,
))
else:
outdict.update(dict(
buildtime=to_minsec(bi['duration']),
result=bi['result'],
waittime=waittime,
))
return outdict
nltab = "\n\t"
print(f'#{buildnum}: {reason}', end='')
if len(paramdict):
print(f'{nltab}{nltab.join(paramdict.values())}', end='')
print(f'{nltab}started: {start} ', end='')
if bi['building']:
print(f'building for {age}, est duration {to_minsec(bi["estimatedDuration"])}')
else:
print(f'waited {waittime}, took {to_minsec(bi["duration"])} {bi["result"]}')
def ts_to_str(ts):
return fromtimestamp(ts).strftime('%d %b %H:%M:%S')
def main():
jenkins_user=os.environ.get('JENKINS_USER')
jenkins_token=os.environ.get('JENKINS_TOKEN')
j=jenkins.Jenkins('https://jenkins.ceph.com', jenkins_user, jenkins_token)
args = parse_args()
if args.list:
ji = j.get_info()
jobs = ji['jobs']
for job in jobs:
print(f'{job["name"]}')
return 0
# jobinfo = j.get_job_info_regex(args.jobre)
# get_job_info_regex doesn't allow passing "fetch_all_builds", so
# recreate it here
joblist = j.get_all_jobs()
jobinfo = list()
for job in joblist:
if re.search(args.jobre, job['name']):
jobinfo.append(j.get_job_info(job['name'], fetch_all_builds=True))
for ji in jobinfo:
name=ji['name']
if args.json:
outdict = dict(name=name, builds=list())
buildcount = 0
for build in ji['builds']:
if args.count and buildcount >= args.count:
break
buildcount += 1
buildnum = build['number']
bi = j.get_build_info(name, buildnum)
'''
{'_class': 'hudson.model.CauseAction',
'causes': [{'_class': 'org.jenkinsci.plugins.ghprb.GhprbCause',
'shortDescription': 'GitHub pull request #56203 of commit '
'ab4c5daead7f26d41028625453d50bb58d3b02be,'
' no merge conflicts.'}]}
{'_class': 'jenkins.metrics.impl.TimeInQueueAction',
'blockedDurationMillis': 0,
'blockedTimeMillis': 0,
'buildableDurationMillis': 4,
'buildableTimeMillis': 4,
'buildingDurationMillis': 985724,
'executingTimeMillis': 985724,
'executorUtilization': 1.0,
'subTaskCount': 0,
'waitingDurationMillis': 6797,
'waitingTimeMillis': 6797},
'''
reason = "??"
paramnames = list()
paramdict=dict()
for act in bi['actions']:
cls = act.get('_class', None)
if cls is None:
continue
if cls.endswith('hudson.model.CauseAction'):
if len(act['causes']) > 1:
print(f'{name} #{buildnum} has more than one cause?', file=sys.stderr)
reason = act['causes'][0]['shortDescription']
if cls.endswith('ParametersAction'):
params = act['parameters']
pois = ['BRANCH', 'ARCHS', 'DISTROS', 'FLAVOR']
for param in params:
paramnames.append(param['name'])
if args.allparams or param['name'] in pois:
paramdict[param['name']] = param['value']
if cls.endswith('TimeInQueueAction'):
waittime = None
if bi['building'] == False:
waittime = to_minsec(act['waitingTimeMillis'])
reason = decruft(reason)
start = ts_to_str(bi['timestamp'] / 1000)
age = int(int(time.time()) - (bi['timestamp'] / 1000))
if args.json:
outdict['builds'].append(output(name, buildnum, reason, paramdict, start, age, bi, waittime, returndict=True))
else:
output(name, buildnum, reason, paramdict, start, age, bi, waittime, returndict=False)
if args.json:
print(json.dumps(outdict))
if __name__ == "__main__":
sys.exit(main())