Commit | Line | Data |
---|---|---|
f3d8604b | 1 | /** |
5a196804 | 2 | * Copyright (C) 2016-2018 - 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 | ||
af3990ed 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 | } | |
f3d8604b | 37 | |
af3990ed | 38 | class RTKVersion implements Comparable<RTKVersion> { |
f3d8604b | 39 | |
af3990ed MJ |
40 | Integer major = 0 |
41 | Integer majorB = 0 | |
42 | Integer minor = 0 | |
43 | Integer patch = 0 | |
44 | Integer rt = 0 | |
f3d8604b | 45 | |
af3990ed MJ |
46 | RTKVersion() {} |
47 | ||
48 | RTKVersion(version) { | |
f3d8604b MJ |
49 | this.parse(version) |
50 | } | |
51 | ||
af3990ed MJ |
52 | static RTKVersion minKVersion() { |
53 | return new RTKVersion("v0.0.0-rt0-rebase") | |
54 | } | |
55 | ||
56 | static RTKVersion maxKVersion() { | |
57 | return new RTKVersion("v" + Integer.MAX_VALUE + ".0.0-rt0-rebase") | |
58 | } | |
59 | ||
60 | static RTKVersion factory(version) { | |
61 | return new RTKVersion(version) | |
62 | } | |
63 | ||
f3d8604b MJ |
64 | def parse(version) { |
65 | this.major = 0 | |
66 | this.majorB = 0 | |
67 | this.minor = 0 | |
68 | this.patch = 0 | |
2c1d386a | 69 | this.rt = 0 |
f3d8604b | 70 | |
af3990ed MJ |
71 | if (!version) { |
72 | throw new EmptyKVersionException("Empty kernel version") | |
73 | } | |
74 | ||
2c1d386a | 75 | def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rt(\d+)-rebase)$/ |
f3d8604b | 76 | if (!match) { |
af3990ed | 77 | throw new InvalidKVersionException("Invalid kernel version: ${version}") |
f3d8604b MJ |
78 | } |
79 | ||
80 | Integer offset = 0; | |
81 | ||
82 | // Major | |
83 | this.major = Integer.parseInt(match.group(1)) | |
84 | if (this.major <= 2) { | |
85 | offset = 2 | |
86 | this.majorB = Integer.parseInt(match.group(2)) | |
87 | } | |
88 | ||
89 | // Minor | |
90 | if (match.group(2 + offset) != null) { | |
91 | this.minor = Integer.parseInt(match.group(2 + offset)) | |
92 | } | |
93 | ||
94 | // Patch level | |
95 | if (match.group(4 + offset) != null) { | |
96 | this.patch = Integer.parseInt(match.group(4 + offset)) | |
97 | } | |
98 | ||
2c1d386a MJ |
99 | // RT |
100 | this.rt = Integer.parseInt(match.group(8)) | |
f3d8604b MJ |
101 | } |
102 | ||
af3990ed MJ |
103 | // Return true if both version are of the same stable branch |
104 | Boolean isSameStable(RTKVersion o) { | |
105 | if (this.major != o.major) { | |
106 | return false | |
107 | } | |
108 | if (this.majorB != o.majorB) { | |
109 | return false | |
110 | } | |
111 | if (this.minor != o.minor) { | |
112 | return false | |
113 | } | |
114 | ||
115 | return true | |
116 | } | |
117 | ||
118 | @Override int compareTo(RTKVersion o) { | |
f3d8604b | 119 | if (this.major != o.major) { |
af3990ed | 120 | return Integer.compare(this.major, o.major) |
f3d8604b MJ |
121 | } |
122 | if (this.majorB != o.majorB) { | |
af3990ed | 123 | return Integer.compare(this.majorB, o.majorB) |
f3d8604b MJ |
124 | } |
125 | if (this.minor != o.minor) { | |
af3990ed | 126 | return Integer.compare(this.minor, o.minor) |
f3d8604b MJ |
127 | } |
128 | if (this.patch != o.patch) { | |
af3990ed | 129 | return Integer.compare(this.patch, o.patch) |
f3d8604b | 130 | } |
af3990ed MJ |
131 | if (this.rt != o.rt) { |
132 | return Integer.compare(this.rt, o.rt) | |
f3d8604b MJ |
133 | } |
134 | ||
135 | // Same version | |
136 | return 0; | |
137 | } | |
138 | ||
139 | String toString() { | |
140 | String vString = "v${this.major}" | |
141 | ||
142 | if (this.majorB > 0) { | |
143 | vString = vString.concat(".${this.majorB}") | |
144 | } | |
145 | ||
146 | vString = vString.concat(".${this.minor}") | |
147 | ||
148 | if (this.patch > 0) { | |
149 | vString = vString.concat(".${this.patch}") | |
150 | } | |
151 | ||
2c1d386a MJ |
152 | if (this.rt > 0) { |
153 | vString = vString.concat("-rt${this.rt}-rebase") | |
f3d8604b MJ |
154 | } |
155 | return vString | |
156 | } | |
157 | } | |
158 | ||
159 | ||
160 | // Retrieve parameters of the current build | |
28b10e79 | 161 | def mbranch = build.getEnvironment(listener).get('GIT_BRANCH').minus('origin/') |
f3d8604b MJ |
162 | def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild') |
163 | def kgitrepo = build.buildVariableResolver.resolve('kgitrepo') | |
af3990ed MJ |
164 | def kverfloor_raw = build.buildVariableResolver.resolve('kverfloor') |
165 | def kverceil_raw = build.buildVariableResolver.resolve('kverceil') | |
166 | def kverfilter = build.buildVariableResolver.resolve('kverfilter') | |
f3d8604b | 167 | def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob')) |
bcf4d9d0 | 168 | def currentJobName = build.project.getFullDisplayName() |
5a196804 | 169 | def gitmodpath = build.getEnvironment(listener).get('WORKSPACE') + "/src/lttng-modules" |
af3990ed | 170 | |
f3d8604b MJ |
171 | // Get the out variable |
172 | def config = new HashMap() | |
173 | def bindings = getBinding() | |
174 | config.putAll(bindings.getVariables()) | |
175 | def out = config['out'] | |
176 | ||
f3d8604b | 177 | |
5a196804 MJ |
178 | // Get the lttng-modules git url |
179 | def gitmodrepo = Git.open(new File(gitmodpath)) | |
180 | def mgitrepo = gitmodrepo.getRepository().getConfig().getString("remote", "origin", "url") | |
181 | ||
f3d8604b | 182 | // Get tags from git repository |
5a196804 | 183 | def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call() |
f3d8604b MJ |
184 | |
185 | // Get kernel versions to build | |
186 | def kversions = [] | |
c9c7c76b | 187 | def tagMatchStrs = [ |
af3990ed MJ |
188 | ~/^refs\/tags\/(v[\d\.]+(-rt(\d+)-rebase))$/, |
189 | ] | |
190 | def blacklist = [ | |
c9c7c76b MJ |
191 | ~/v4\.11\.8-rt5-rebase/, |
192 | ~/v4\.11\.9-rt6-rebase/, | |
193 | ~/v4\.11\.9-rt7-rebase/, | |
194 | ~/v4\.11\.12-rt8-rebase/, | |
195 | ~/v4\.11\.12-rt9-rebase/, | |
196 | ~/v4\.11\.12-rt10-rebase/, | |
197 | ~/v4\.11\.12-rt11-rebase/, | |
198 | ~/v4\.11\.12-rt12-rebase/, | |
199 | ~/v4\.11\.12-rt13-rebase/, | |
200 | ~/v3\.6.*-rebase/, | |
201 | ~/v3\.8.*-rebase/, | |
af3990ed MJ |
202 | ] |
203 | ||
204 | def kversionFactory = new RTKVersion() | |
205 | ||
206 | // Parse kernel versions | |
207 | def kverfloor = "" | |
208 | try { | |
209 | kverfloor = kversionFactory.factory(kverfloor_raw) | |
210 | } catch (EmptyKVersionException e) { | |
211 | kverfloor = kversionFactory.minKVersion() | |
212 | } | |
f3d8604b | 213 | |
af3990ed MJ |
214 | def kverceil = "" |
215 | try { | |
216 | kverceil = kversionFactory.factory(kverceil_raw) | |
217 | } catch (EmptyKVersionException e) { | |
218 | kverceil = kversionFactory.maxKVersion() | |
219 | } | |
220 | ||
221 | // Build a sorted list of versions to build | |
222 | for (ref in refs) { | |
c9c7c76b MJ |
223 | for (tagMatchStr in tagMatchStrs) { |
224 | def tagMatch = ref.getName() =~ tagMatchStr | |
f3d8604b | 225 | |
c9c7c76b MJ |
226 | if (tagMatch) { |
227 | def kversion_raw = tagMatch.group(1) | |
228 | def blacklisted = false | |
229 | ||
230 | // Check if the kversion is blacklisted | |
231 | for (blackMatchStr in blacklist) { | |
232 | def blackMatch = kversion_raw =~ blackMatchStr | |
233 | ||
234 | if (blackMatch) { | |
235 | blacklisted = true | |
236 | break; | |
237 | } | |
238 | } | |
239 | ||
240 | if (!blacklisted) { | |
241 | def v = kversionFactory.factory(kversion_raw) | |
242 | ||
243 | if ((v >= kverfloor) && (v < kverceil)) { | |
244 | kversions.add(v) | |
245 | } | |
af3990ed | 246 | } |
f3d8604b MJ |
247 | } |
248 | } | |
249 | } | |
250 | ||
251 | kversions.sort() | |
f3d8604b | 252 | |
c9c7c76b MJ |
253 | //println "Pre filtering kernel versions:" |
254 | //for (k in kversions) { | |
255 | // println k | |
256 | //} | |
257 | ||
af3990ed MJ |
258 | switch (kverfilter) { |
259 | case 'stable-head': | |
260 | // Keep only the head of each stable branch | |
261 | println('Filter kernel versions to keep only the latest point release of each stable branch.') | |
262 | ||
263 | for (i = 0; i < kversions.size(); i++) { | |
264 | def curr = kversions[i] | |
265 | def next = i < kversions.size() - 1 ? kversions[i + 1] : null | |
266 | ||
267 | if (next != null) { | |
268 | if (curr.isSameStable(next)) { | |
269 | kversions.remove(i) | |
270 | i-- | |
271 | } | |
272 | } | |
273 | } | |
274 | break | |
275 | ||
276 | default: | |
277 | // No filtering of kernel versions | |
278 | println('No kernel versions filtering selected.') | |
279 | break | |
280 | } | |
281 | ||
282 | ||
f3d8604b MJ |
283 | println "Building the following kernel versions:" |
284 | for (k in kversions) { | |
285 | println k | |
286 | } | |
287 | ||
288 | // Debug: Stop build here | |
289 | //throw new InterruptedException() | |
290 | ||
291 | def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName) | |
292 | ||
293 | def allBuilds = [] | |
294 | def ongoingBuild = [] | |
295 | def failedRuns = [] | |
296 | def isFailed = false | |
af3990ed | 297 | def similarJobQueued = 0; |
f3d8604b MJ |
298 | |
299 | // Loop while we have kernel versions remaining or jobs running | |
300 | while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) { | |
301 | ||
302 | if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) { | |
303 | def kversion = kversions.pop() | |
304 | def job_params = [ | |
28b10e79 | 305 | new StringParameterValue('mversion', mbranch), |
5a196804 | 306 | new StringParameterValue('mgitrepo', mgitrepo), |
a1ae361e | 307 | new StringParameterValue('ktag', kversion.toString()), |
f3d8604b MJ |
308 | new StringParameterValue('kgitrepo', kgitrepo), |
309 | ] | |
310 | ||
311 | // Launch the parametrized build | |
312 | def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params)) | |
28b10e79 | 313 | println "triggering ${joburl} for the ${mbranch} branch on kernel ${kversion}" |
f3d8604b MJ |
314 | |
315 | // Add it to the ongoing build queue | |
316 | ongoingBuild.push(param_build) | |
317 | ||
318 | } else { | |
319 | ||
320 | println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size() | |
321 | try { | |
af3990ed | 322 | Thread.sleep(10000) |
f3d8604b MJ |
323 | } catch(e) { |
324 | if (e in InterruptedException) { | |
325 | build.setResult(hudson.model.Result.ABORTED) | |
326 | throw new InterruptedException() | |
327 | } else { | |
328 | throw(e) | |
329 | } | |
330 | } | |
331 | ||
af3990ed | 332 | // Abort job if a newer instance is queued |
bcf4d9d0 JR |
333 | similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName} |
334 | if ( similarJobQueued > 0 ) { | |
bcf4d9d0 JR |
335 | build.setResult(hudson.model.Result.ABORTED) |
336 | throw new InterruptedException() | |
337 | } | |
338 | ||
f3d8604b MJ |
339 | def i = ongoingBuild.iterator() |
340 | while ( i.hasNext() ) { | |
341 | currentBuild = i.next() | |
342 | if ( currentBuild.isCancelled() || currentBuild.isDone() ) { | |
343 | // Remove from queue | |
344 | i.remove() | |
345 | ||
346 | // Print results | |
347 | def matrixParent = currentBuild.get() | |
348 | allBuilds.add(matrixParent) | |
a1ae361e | 349 | def kernelStr = matrixParent.buildVariableResolver.resolve("ktag") |
f3d8604b MJ |
350 | println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}" |
351 | ||
352 | // Process child runs of matrixBuild | |
353 | def childRuns = matrixParent.getRuns() | |
354 | for ( childRun in childRuns ) { | |
355 | println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}" | |
356 | if (childRun.result != Result.SUCCESS) { | |
357 | failedRuns.add(childRun) | |
358 | isFailed = true | |
359 | } | |
360 | } | |
361 | } | |
362 | } | |
363 | } | |
364 | } | |
365 | ||
366 | // Get log of failed runs | |
367 | for (failedRun in failedRuns) { | |
368 | println "---START---" | |
369 | failedRun.writeWholeLogTo(out) | |
370 | println "---END---" | |
371 | } | |
372 | ||
373 | println "---Build report---" | |
374 | for (b in allBuilds) { | |
a1ae361e | 375 | def kernelStr = b.buildVariableResolver.resolve("ktag") |
f3d8604b | 376 | println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}" |
7e02032c | 377 | // Cleanup builds |
7e942863 MJ |
378 | try { |
379 | b.delete() | |
380 | } catch (all) {} | |
f3d8604b MJ |
381 | } |
382 | ||
383 | // Mark this build failed if any child build has failed | |
384 | if (isFailed) { | |
c5c05f73 | 385 | build.setResult(hudson.model.Result.FAILURE) |
f3d8604b MJ |
386 | } |
387 | ||
388 | // EOF |