jjb: fix ubuntu hwe kernels
[lttng-ci.git] / scripts / lttng-modules / master.groovy
1 /**
2 * Copyright (C) 2016-2018 - Michael Jeanson <mjeanson@efficios.com>
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
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
38 class VanillaKVersion implements Comparable<VanillaKVersion> {
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 VanillaKVersion() {}
47
48 VanillaKVersion(version) {
49 this.parse(version)
50 }
51
52 static VanillaKVersion minKVersion() {
53 return new VanillaKVersion("v0.0.0")
54 }
55
56 static VanillaKVersion maxKVersion() {
57 return new VanillaKVersion("v" + Integer.MAX_VALUE + ".0.0")
58 }
59
60 static VanillaKVersion factory(version) {
61 return new VanillaKVersion(version)
62 }
63
64 def parse(version) {
65 this.major = 0
66 this.majorB = 0
67 this.minor = 0
68 this.patch = 0
69 this.rc = Integer.MAX_VALUE
70
71 if (!version) {
72 throw new EmptyKVersionException("Empty kernel version")
73 }
74
75 def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rc(\d+))?$/
76 if (!match) {
77 throw new InvalidKVersionException("Invalid kernel version: ${version}")
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
99 // RC
100 if (match.group(8) != null) {
101 this.rc = Integer.parseInt(match.group(8))
102 }
103 }
104
105 // Return true if this version is a release candidate
106 Boolean isRC() {
107 return this.rc != Integer.MAX_VALUE
108 }
109
110 // Return true if both version are of the same stable branch
111 Boolean isSameStable(VanillaKVersion o) {
112 if (this.major != o.major) {
113 return false
114 }
115 if (this.majorB != o.majorB) {
116 return false
117 }
118 if (this.minor != o.minor) {
119 return false
120 }
121
122 return true
123 }
124
125 @Override int compareTo(VanillaKVersion o) {
126 if (this.major != o.major) {
127 return Integer.compare(this.major, o.major)
128 }
129 if (this.majorB != o.majorB) {
130 return Integer.compare(this.majorB, o.majorB)
131 }
132 if (this.minor != o.minor) {
133 return Integer.compare(this.minor, o.minor)
134 }
135 if (this.patch != o.patch) {
136 return Integer.compare(this.patch, o.patch)
137 }
138 if (this.rc != o.rc) {
139 return Integer.compare(this.rc, o.rc)
140 }
141
142 // Same version
143 return 0;
144 }
145
146 String toString() {
147 String vString = "v${this.major}"
148
149 if (this.majorB > 0) {
150 vString = vString.concat(".${this.majorB}")
151 }
152
153 vString = vString.concat(".${this.minor}")
154
155 if (this.patch > 0) {
156 vString = vString.concat(".${this.patch}")
157 }
158
159 if (this.rc > 0 && this.rc < Integer.MAX_VALUE) {
160 vString = vString.concat("-rc${this.rc}")
161 }
162 return vString
163 }
164 }
165
166 class UbuntuKVersion implements Comparable<UbuntuKVersion> {
167
168 Integer major = 0
169 Integer minor = 0
170 Integer patch = 0
171 Integer umajor = 0
172 Integer uminor = 0
173 String suffix = ""
174 String strLTS = ""
175 Boolean isLTS = false
176
177 UbuntuKVersion() {}
178
179 UbuntuKVersion(version) {
180 this.parse(version)
181 }
182
183 static UbuntuKVersion minKVersion() {
184 return new UbuntuKVersion("Ubuntu-lts-0.0.0-0.0")
185 }
186
187 static UbuntuKVersion maxKVersion() {
188 return new UbuntuKVersion("Ubuntu-" + Integer.MAX_VALUE + ".0.0-0.0")
189 }
190
191 static UbuntuKVersion factory(version) {
192 return new UbuntuKVersion(version)
193 }
194
195 def parse(version) {
196 this.major = 0
197 this.minor = 0
198 this.patch = 0
199 this.umajor = 0
200 this.uminor = 0
201 this.suffix = "";
202 this.isLTS = false
203
204 if (!version) {
205 throw new EmptyKVersionException("Empty kernel version")
206 }
207
208 //'Ubuntu-lts-4.8.0-27.29_16.04.1',
209 //'Ubuntu-4.4.0-70.91',
210 def match = version =~ /^Ubuntu-(lts-|hwe-)??(\d+)\.(\d+)\.(\d+)-(\d+)\.(\d+)(.*)??$/
211 if (!match) {
212 throw new InvalidKVersionException("Invalid kernel version: ${version}")
213 }
214
215 if (match.group(1) != null) {
216 this.isLTS = true
217 this.strLTS = match.group(1)
218 }
219
220 // Major
221 this.major = Integer.parseInt(match.group(2))
222
223 // Minor
224 this.minor = Integer.parseInt(match.group(3))
225
226 // Patch level
227 this.patch = Integer.parseInt(match.group(4))
228
229 // Ubuntu major
230 this.umajor = Integer.parseInt(match.group(5))
231
232 // Ubuntu minor
233 this.uminor = Integer.parseInt(match.group(6))
234
235 if (match.group(7) != null) {
236 this.suffix = match.group(7)
237 }
238 }
239
240 // Return true if this version is a release candidate
241 Boolean isRC() {
242 return false
243 }
244
245 // Return true if both version are of the same stable branch
246 Boolean isSameStable(UbuntuKVersion o) {
247 if (this.isLTS != o.isLTS) {
248 return false
249 }
250 if (this.major != o.major) {
251 return false
252 }
253 if (this.minor != o.minor) {
254 return false
255 }
256 if (this.patch != o.patch) {
257 return false
258 }
259
260 return true
261 }
262
263 @Override int compareTo(UbuntuKVersion o) {
264 if (this.major != o.major) {
265 return Integer.compare(this.major, o.major)
266 }
267 if (this.minor != o.minor) {
268 return Integer.compare(this.minor, o.minor)
269 }
270 if (this.patch != o.patch) {
271 return Integer.compare(this.patch, o.patch)
272 }
273 if (this.umajor != o.umajor) {
274 return Integer.compare(this.umajor, o.umajor)
275 }
276 if (this.uminor != o.uminor) {
277 return Integer.compare(this.uminor, o.uminor)
278 }
279 if (this.isLTS != o.isLTS) {
280 if (o.isLTS) {
281 return 1
282 } else {
283 return -1
284 }
285 }
286
287 // Same version
288 return 0;
289 }
290
291 String toString() {
292 String vString = "Ubuntu-"
293
294 if (this.isLTS) {
295 vString = vString.concat("${this.strLTS}")
296 }
297
298 vString = vString.concat("${this.major}.${this.minor}.${this.patch}-${this.umajor}.${this.uminor}${this.suffix}")
299
300 return vString
301 }
302 }
303
304
305 // Retrieve parameters of the current build
306 def mversion = build.buildVariableResolver.resolve('mversion')
307 def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild')
308 def kgitrepo = build.buildVariableResolver.resolve('kgitrepo')
309 def kverfloor_raw = build.buildVariableResolver.resolve('kverfloor')
310 def kverceil_raw = build.buildVariableResolver.resolve('kverceil')
311 def kverfilter = build.buildVariableResolver.resolve('kverfilter')
312 def uversion = build.buildVariableResolver.resolve('uversion')
313 def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob'))
314 def currentJobName = build.project.getFullDisplayName()
315 def gitmodpath = build.getEnvironment(listener).get('WORKSPACE') + "/src/lttng-modules"
316
317 // Get the out variable
318 def config = new HashMap()
319 def bindings = getBinding()
320 config.putAll(bindings.getVariables())
321 def out = config['out']
322
323
324 // Get the lttng-modules git url
325 def gitmodrepo = Git.open(new File(gitmodpath))
326 def mgitrepo = gitmodrepo.getRepository().getConfig().getString("remote", "origin", "url")
327
328 // Get tags from git repository
329 def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call()
330
331 // Get kernel versions to build
332 def kversions = []
333 def kversionsRC = []
334 def matchStrs = []
335 def blacklist = []
336 def kversionFactory = ""
337
338 if (uversion != null) {
339 kversionFactory = new UbuntuKVersion()
340 switch (uversion) {
341 case 'bionic':
342 matchStrs = [
343 ~/^refs\/tags\/(Ubuntu-4\.15\.0-\d{1,3}?\.[\d]+)$/,
344 ~/^refs\/tags\/(Ubuntu-hwe-4\.18\.0-.*_18\.04\.\d+)$/,
345 ]
346 break
347
348 case 'xenial':
349 matchStrs = [
350 ~/^refs\/tags\/(Ubuntu-4\.4\.0-\d{1,3}?\.[\d]+)$/,
351 ~/^refs\/tags\/(Ubuntu-hwe-4\.8\.0-.*_16\.04\.\d+)$/,
352 ~/^refs\/tags\/(Ubuntu-hwe-4\.10\.0-.*_16\.04\.\d+)$/,
353 ~/^refs\/tags\/(Ubuntu-hwe-4\.13\.0-.*_16\.04\.\d+)$/,
354 ~/^refs\/tags\/(Ubuntu-hwe-4\.15\.0-.*_16\.04\.\d+)$/,
355 ]
356
357 blacklist = [
358 'Ubuntu-lts-4.10.0-7.9_16.04.1',
359 ]
360 break
361
362 case 'trusty':
363 matchStrs = [
364 ~/^refs\/tags\/(Ubuntu-3\.13\.0-[\d\.]+)$/,
365 ~/^refs\/tags\/(Ubuntu-lts-.*_14\.04\.\d+)$/,
366 ]
367 break
368
369 default:
370 println "Unsupported Ubuntu version: ${uversion}"
371 throw new InterruptedException()
372 break
373 }
374 } else {
375 // Vanilla
376 kversionFactory = new VanillaKVersion()
377 matchStrs = [
378 ~/^refs\/tags\/(v[\d\.]+(-rc(\d+))?)$/,
379 ]
380 }
381
382 // Parse kernel versions
383 def kverfloor = ""
384 try {
385 kverfloor = kversionFactory.factory(kverfloor_raw)
386 } catch (EmptyKVersionException e) {
387 kverfloor = kversionFactory.minKVersion()
388 }
389
390 def kverceil = ""
391 try {
392 kverceil = kversionFactory.factory(kverceil_raw)
393 } catch (EmptyKVersionException e) {
394 kverceil = kversionFactory.maxKVersion()
395 }
396
397 // Build a sorted list of versions to build
398 for (ref in refs) {
399 for (matchStr in matchStrs) {
400 def match = ref.getName() =~ matchStr
401 if (match && !blacklist.contains(match.group(1))) {
402 def v = kversionFactory.factory(match.group(1))
403
404 if ((v >= kverfloor) && (v < kverceil)) {
405 if (v.isRC()) {
406 kversionsRC.add(v)
407 } else {
408 kversions.add(v)
409 }
410 }
411 }
412 }
413 }
414
415 kversions.sort()
416 kversionsRC.sort()
417
418 switch (kverfilter) {
419 case 'stable-head':
420 // Keep only the head of each stable branch
421 println('Filter kernel versions to keep only the latest point release of each stable branch.')
422
423 for (i = 0; i < kversions.size(); i++) {
424 def curr = kversions[i]
425 def next = i < kversions.size() - 1 ? kversions[i + 1] : null
426
427 if (next != null) {
428 if (curr.isSameStable(next)) {
429 kversions.remove(i)
430 i--
431 }
432 }
433 }
434 break
435
436 default:
437 // No filtering of kernel versions
438 println('No kernel versions filtering selected.')
439 break
440 }
441
442 // If the last RC version is newer than the last stable, add it to the build list
443 if (kversionsRC.size() > 0 && kversionsRC.last() > kversions.last()) {
444 kversions.add(kversionsRC.last())
445 }
446
447 println "Building the following kernel versions:"
448 for (k in kversions) {
449 println k
450 }
451
452 // Debug: Stop build here
453 //throw new InterruptedException()
454
455 def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
456
457 def allBuilds = []
458 def ongoingBuild = []
459 def failedRuns = []
460 def isFailed = false
461 def similarJobQueued = 0;
462
463 // Loop while we have kernel versions remaining or jobs running
464 while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) {
465
466 if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) {
467 def kversion = kversions.pop()
468 def job_params = [
469 new StringParameterValue('mversion', mversion),
470 new StringParameterValue('mgitrepo', mgitrepo),
471 new StringParameterValue('ktag', kversion.toString()),
472 new StringParameterValue('kgitrepo', kgitrepo),
473 ]
474
475 // Launch the parametrized build
476 def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params))
477 println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}"
478
479 // Add it to the ongoing build queue
480 ongoingBuild.push(param_build)
481
482 } else {
483
484 println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size()
485 try {
486 Thread.sleep(10000)
487 } catch(e) {
488 if (e in InterruptedException) {
489 build.setResult(hudson.model.Result.ABORTED)
490 throw new InterruptedException()
491 } else {
492 throw(e)
493 }
494 }
495
496 // Abort job if a newer instance is queued
497 similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName}
498 if ( similarJobQueued > 0 ) {
499 build.setResult(hudson.model.Result.ABORTED)
500 throw new InterruptedException()
501 }
502
503 def i = ongoingBuild.iterator()
504 while ( i.hasNext() ) {
505 currentBuild = i.next()
506 if ( currentBuild.isCancelled() || currentBuild.isDone() ) {
507 // Remove from queue
508 i.remove()
509
510 // Print results
511 def matrixParent = currentBuild.get()
512 allBuilds.add(matrixParent)
513 def kernelStr = matrixParent.buildVariableResolver.resolve("ktag")
514 println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}"
515
516 // Process child runs of matrixBuild
517 def childRuns = matrixParent.getRuns()
518 for ( childRun in childRuns ) {
519 println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}"
520 if (childRun.result != Result.SUCCESS) {
521 failedRuns.add(childRun)
522 isFailed = true
523 }
524 }
525 }
526 }
527 }
528 }
529
530 // Get log of failed runs
531 for (failedRun in failedRuns) {
532 println "---START---"
533 failedRun.writeWholeLogTo(out)
534 println "---END---"
535 }
536
537 println "---Build report---"
538 for (b in allBuilds) {
539 def kernelStr = b.buildVariableResolver.resolve("ktag")
540 println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}"
541 // Cleanup builds
542 try {
543 b.delete()
544 } catch (all) {}
545 }
546
547 // Mark this build failed if any child build has failed
548 if (isFailed) {
549 build.setResult(hudson.model.Result.FAILURE)
550 }
551
552 // EOF
This page took 0.041561 seconds and 5 git commands to generate.