Commit | Line | Data |
---|---|---|
f3d8604b | 1 | /** |
e9b44189 | 2 | * Copyright (C) 2016-2017 - Michael Jeanson <mjeanson@efficios.com> |
f3d8604b MJ |
3 | * |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | import hudson.model.* | |
19 | import hudson.AbortException | |
20 | import hudson.console.HyperlinkNote | |
21 | import java.util.concurrent.CancellationException | |
22 | import org.eclipse.jgit.api.Git | |
23 | import org.eclipse.jgit.lib.Ref | |
24 | ||
25 | ||
591756e5 MJ |
26 | class InvalidkVersionException extends Exception { |
27 | public InvalidkVersionException(String message) { | |
28 | super(message) | |
29 | } | |
30 | } | |
31 | ||
32 | class EmptykVersionException extends Exception { | |
33 | public EmptykVersionException(String message) { | |
34 | super(message) | |
35 | } | |
36 | } | |
37 | ||
f3d8604b MJ |
38 | class kVersion implements Comparable<kVersion> { |
39 | ||
40 | Integer major = 0; | |
41 | Integer majorB = 0; | |
42 | Integer minor = 0; | |
43 | Integer patch = 0; | |
44 | Integer rc = Integer.MAX_VALUE; | |
45 | ||
46 | kVersion() {} | |
47 | ||
48 | kVersion(version) { | |
49 | this.parse(version) | |
50 | } | |
51 | ||
52 | def parse(version) { | |
53 | this.major = 0 | |
54 | this.majorB = 0 | |
55 | this.minor = 0 | |
56 | this.patch = 0 | |
57 | this.rc = Integer.MAX_VALUE | |
58 | ||
591756e5 MJ |
59 | if (!version) { |
60 | throw new EmptykVersionException("Empty kernel version") | |
61 | } | |
62 | ||
f3d8604b MJ |
63 | def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rc(\d+))?$/ |
64 | if (!match) { | |
591756e5 | 65 | throw new InvalidkVersionException("Invalid kernel version: ${version}") |
f3d8604b MJ |
66 | } |
67 | ||
68 | Integer offset = 0; | |
69 | ||
70 | // Major | |
71 | this.major = Integer.parseInt(match.group(1)) | |
72 | if (this.major <= 2) { | |
73 | offset = 2 | |
74 | this.majorB = Integer.parseInt(match.group(2)) | |
75 | } | |
76 | ||
77 | // Minor | |
78 | if (match.group(2 + offset) != null) { | |
79 | this.minor = Integer.parseInt(match.group(2 + offset)) | |
80 | } | |
81 | ||
82 | // Patch level | |
83 | if (match.group(4 + offset) != null) { | |
84 | this.patch = Integer.parseInt(match.group(4 + offset)) | |
85 | } | |
86 | ||
87 | // RC | |
88 | if (match.group(8) != null) { | |
89 | this.rc = Integer.parseInt(match.group(8)) | |
90 | } | |
91 | } | |
92 | ||
93 | // Return true if this version is a release candidate | |
94 | Boolean isRC() { | |
95 | return this.rc != Integer.MAX_VALUE | |
96 | } | |
97 | ||
e9b44189 MJ |
98 | // Return true if both version are of the same stable branch |
99 | Boolean isSameStable(kVersion o) { | |
100 | if (this.major != o.major) { | |
101 | return false | |
102 | } | |
103 | if (this.majorB != o.majorB) { | |
104 | return false | |
105 | } | |
106 | if (this.minor != o.minor) { | |
107 | return false | |
108 | } | |
109 | ||
110 | return true | |
111 | } | |
112 | ||
f3d8604b MJ |
113 | @Override int compareTo(kVersion o) { |
114 | if (this.major != o.major) { | |
e9b44189 | 115 | return Integer.compare(this.major, o.major) |
f3d8604b MJ |
116 | } |
117 | if (this.majorB != o.majorB) { | |
e9b44189 | 118 | return Integer.compare(this.majorB, o.majorB) |
f3d8604b MJ |
119 | } |
120 | if (this.minor != o.minor) { | |
e9b44189 | 121 | return Integer.compare(this.minor, o.minor) |
f3d8604b MJ |
122 | } |
123 | if (this.patch != o.patch) { | |
e9b44189 | 124 | return Integer.compare(this.patch, o.patch) |
f3d8604b MJ |
125 | } |
126 | if (this.rc != o.rc) { | |
e9b44189 | 127 | return Integer.compare(this.rc, o.rc) |
f3d8604b MJ |
128 | } |
129 | ||
130 | // Same version | |
131 | return 0; | |
132 | } | |
133 | ||
134 | String toString() { | |
135 | String vString = "v${this.major}" | |
136 | ||
137 | if (this.majorB > 0) { | |
138 | vString = vString.concat(".${this.majorB}") | |
139 | } | |
140 | ||
141 | vString = vString.concat(".${this.minor}") | |
142 | ||
143 | if (this.patch > 0) { | |
144 | vString = vString.concat(".${this.patch}") | |
145 | } | |
146 | ||
147 | if (this.rc > 0 && this.rc < Integer.MAX_VALUE) { | |
148 | vString = vString.concat("-rc${this.rc}") | |
149 | } | |
150 | return vString | |
151 | } | |
152 | } | |
153 | ||
154 | ||
155 | // Retrieve parameters of the current build | |
156 | def mversion = build.buildVariableResolver.resolve('mversion') | |
157 | def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild') | |
158 | def kgitrepo = build.buildVariableResolver.resolve('kgitrepo') | |
591756e5 MJ |
159 | def kverfloor_raw = build.buildVariableResolver.resolve('kverfloor') |
160 | def kverceil_raw = build.buildVariableResolver.resolve('kverceil') | |
e9b44189 | 161 | def kverfilter = build.buildVariableResolver.resolve('kverfilter') |
f3d8604b | 162 | def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob')) |
483859f3 | 163 | def currentJobName = build.project.getFullDisplayName() |
f3d8604b | 164 | |
591756e5 MJ |
165 | // Parse kernel versions |
166 | def kverfloor = new kVersion(kverfloor_raw) | |
167 | def kverceil = "" | |
168 | ||
169 | try { | |
170 | kverceil = new kVersion(kverceil_raw) | |
171 | } catch (EmptykVersionException e) { | |
172 | kverceil = new kVersion("v" + Integer.MAX_VALUE + ".0.0") | |
173 | } | |
174 | ||
f3d8604b MJ |
175 | // Get the out variable |
176 | def config = new HashMap() | |
177 | def bindings = getBinding() | |
178 | config.putAll(bindings.getVariables()) | |
179 | def out = config['out'] | |
180 | ||
f3d8604b MJ |
181 | |
182 | // Get tags from git repository | |
183 | def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call(); | |
184 | ||
185 | // Get kernel versions to build | |
186 | def kversions = [] | |
187 | def kversionsRC = [] | |
188 | for (ref in refs) { | |
189 | def match = ref.getName() =~ /^refs\/tags\/(v[\d\.]+(-rc(\d+))?)$/ | |
190 | ||
191 | if (match) { | |
192 | def v = new kVersion(match.group(1)) | |
193 | ||
591756e5 | 194 | if ((v >= kverfloor) && (v < kverceil)) { |
f3d8604b MJ |
195 | if (v.isRC()) { |
196 | kversionsRC.add(v) | |
197 | } else { | |
198 | kversions.add(v) | |
199 | } | |
200 | } | |
201 | } | |
202 | } | |
203 | ||
204 | kversions.sort() | |
205 | kversionsRC.sort() | |
206 | ||
e9b44189 MJ |
207 | switch (kverfilter) { |
208 | case 'stable-head': | |
209 | // Keep only the head of each stable branch | |
210 | println('Filter kernel versions to keep only the latest point release of each stable branch.') | |
211 | ||
212 | for (i = 0; i < kversions.size(); i++) { | |
213 | def curr = kversions[i] | |
214 | def next = i < kversions.size() - 1 ? kversions[i + 1] : null | |
215 | ||
216 | if (next != null) { | |
217 | if (curr.isSameStable(next)) { | |
218 | kversions.remove(i) | |
219 | i-- | |
220 | } | |
221 | } | |
222 | } | |
223 | break | |
224 | ||
225 | default: | |
226 | // No filtering of kernel versions | |
227 | println('No kernel versions filtering selected.') | |
228 | break | |
229 | } | |
230 | ||
f3d8604b MJ |
231 | // If the last RC version is newer than the last stable, add it to the build list |
232 | if (kversionsRC.last() > kversions.last()) { | |
233 | kversions.add(kversionsRC.last()) | |
234 | } | |
235 | ||
236 | // Debug | |
237 | println "Building the following kernel versions:" | |
238 | for (k in kversions) { | |
239 | println k | |
240 | } | |
241 | ||
242 | // Debug: Stop build here | |
243 | //throw new InterruptedException() | |
244 | ||
245 | def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName) | |
246 | ||
247 | def allBuilds = [] | |
248 | def ongoingBuild = [] | |
249 | def failedRuns = [] | |
250 | def isFailed = false | |
483859f3 | 251 | def similarJobQueued = 0; |
f3d8604b MJ |
252 | |
253 | // Loop while we have kernel versions remaining or jobs running | |
254 | while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) { | |
255 | ||
256 | if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) { | |
257 | def kversion = kversions.pop() | |
258 | def job_params = [ | |
259 | new StringParameterValue('mversion', mversion), | |
260 | new StringParameterValue('kversion', kversion.toString()), | |
261 | new StringParameterValue('kgitrepo', kgitrepo), | |
262 | ] | |
263 | ||
264 | // Launch the parametrized build | |
265 | def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params)) | |
266 | println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}" | |
267 | ||
268 | // Add it to the ongoing build queue | |
269 | ongoingBuild.push(param_build) | |
270 | ||
271 | } else { | |
272 | ||
273 | println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size() | |
274 | try { | |
275 | Thread.sleep(5000) | |
276 | } catch(e) { | |
277 | if (e in InterruptedException) { | |
278 | build.setResult(hudson.model.Result.ABORTED) | |
279 | throw new InterruptedException() | |
280 | } else { | |
281 | throw(e) | |
282 | } | |
283 | } | |
284 | ||
e9b44189 | 285 | // If a newer instance of this job is queued, abort to let it run |
483859f3 JR |
286 | similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName} |
287 | if ( similarJobQueued > 0 ) { | |
288 | // Abort since new build is queued | |
289 | build.setResult(hudson.model.Result.ABORTED) | |
290 | throw new InterruptedException() | |
291 | } | |
292 | ||
f3d8604b MJ |
293 | def i = ongoingBuild.iterator() |
294 | while ( i.hasNext() ) { | |
295 | currentBuild = i.next() | |
296 | if ( currentBuild.isCancelled() || currentBuild.isDone() ) { | |
297 | // Remove from queue | |
298 | i.remove() | |
299 | ||
300 | // Print results | |
301 | def matrixParent = currentBuild.get() | |
302 | allBuilds.add(matrixParent) | |
303 | def kernelStr = matrixParent.buildVariableResolver.resolve("kversion") | |
304 | println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}" | |
305 | ||
306 | // Process child runs of matrixBuild | |
307 | def childRuns = matrixParent.getRuns() | |
308 | for ( childRun in childRuns ) { | |
309 | println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}" | |
310 | if (childRun.result != Result.SUCCESS) { | |
311 | failedRuns.add(childRun) | |
312 | isFailed = true | |
313 | } | |
314 | } | |
315 | } | |
316 | } | |
317 | } | |
318 | } | |
319 | ||
320 | // Get log of failed runs | |
321 | for (failedRun in failedRuns) { | |
322 | println "---START---" | |
323 | failedRun.writeWholeLogTo(out) | |
324 | println "---END---" | |
325 | } | |
326 | ||
327 | println "---Build report---" | |
328 | for (b in allBuilds) { | |
329 | def kernelStr = b.buildVariableResolver.resolve("kversion") | |
330 | println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}" | |
7e02032c | 331 | // Cleanup builds |
7e942863 MJ |
332 | try { |
333 | b.delete() | |
334 | } catch (all) {} | |
f3d8604b MJ |
335 | } |
336 | ||
337 | // Mark this build failed if any child build has failed | |
338 | if (isFailed) { | |
339 | build.getExecutor().interrupt(Result.FAILURE) | |
340 | } | |
341 | ||
342 | // EOF |