-
Notifications
You must be signed in to change notification settings - Fork 15
/
job.py
executable file
·153 lines (124 loc) · 4.77 KB
/
job.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
#!/usr/bin/env python3
#
# Copyright (C) 2019 Collabora Limited
# Author: Guillaume Tucker <[email protected]>
#
# This module is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import argparse
import jenkins
import json
import sys
import time
import kernelci
ACTIONS = [
"trigger",
"enable",
"disable",
"wait",
"abort",
"flush_kernel_trigger_jobs",
]
def cmd_trigger(args, api):
if args.json:
with open(args.json) as f:
params = json.load(f)
elif args.no_params:
params = None
else:
params = {'_': ''} # silly...
api.build_job(args.job, params)
def cmd_enable(args, api):
api.enable_job(args.job)
def cmd_disable(args, api):
api.disable_job(args.job)
def _get_building_jobs(api, job_name, depth=100):
job_info = api.get_job_info(job_name)
builds = job_info['builds']
building = set()
for build in builds[:depth]:
build_number = build['number']
build_info = api.get_build_info(job_name, build_number)
build_status = build_info['building']
if build_status:
print("building: {} #{}".format(job_name, build_number))
building.add(build_number)
return building
def cmd_flush_kernel_trigger_jobs(args, api):
"""Abort jobs with no artifacts, useful mostly for kernel build triggers"""
job_info = api.get_job_info(args.job)
builds = job_info['builds']
for build in builds:
build_number = build['number']
build_info = api.get_build_info(args.job, build_number)
artifacts = build_info['artifacts']
if not artifacts:
print("Aborting {}".format(build_number))
api.stop_build(args.job, build_number)
def cmd_wait(args, api):
building = _get_building_jobs(api, args.job)
while building:
while building:
print("still building: {}".format(len(building)))
print("waiting...")
time.sleep(15)
still_building = set()
for number in building:
is_building = api.get_build_info(args.job, number)['building']
if is_building:
still_building.add(number)
building = still_building
building = _get_building_jobs(api, args.job)
print("No more {} jobs running.".format(args.job))
def cmd_abort(args, api):
building = _get_building_jobs(api, args.job)
for number in building:
print("aborting {} #{}".format(args.job, number))
api.stop_build(args.job, number)
print("All running {} jobs have been aborted.".format(args.job))
def main(args):
settings = kernelci.Settings(args.settings, args.section)
url = args.url or settings.get('url')
user = args.user or settings.get('user')
token = args.token or settings.get('token')
if not all((url, user, token)):
print("Missing info: url, user and token are required")
return False
api = jenkins.Jenkins(url, user, token)
cmd = "_".join(["cmd", args.action])
globals()[cmd](args, api)
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser("Control Jenkins jobs")
parser.add_argument("action", choices=ACTIONS,
help="action to perform")
parser.add_argument("job",
help="Name of the Jenkins job")
parser.add_argument("--url",
help="Jenkins API URL")
parser.add_argument("--user",
help="Jenkins user name")
parser.add_argument("--token",
help="Jenkins API token")
parser.add_argument("--json",
help="Path to a JSON file with job parameters")
parser.add_argument("--no-params", action='store_true',
help="Trigger job with no parameters")
parser.add_argument("--settings", default="data/staging-jenkins.ini",
help="Path to a settings file")
parser.add_argument("--section", default="jenkins",
help="Section in the settings file")
args = parser.parse_args(sys.argv[1:])
ret = main(args)
sys.exit(0 if ret is True else 1)