2 * Copyright (C) 2016-2020 Michael Jeanson <mjeanson@efficios.com>
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.
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.
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/>.
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
26 class InvalidKVersionException extends Exception {
27 public InvalidKVersionException(String message) {
32 class EmptyKVersionException extends Exception {
33 public EmptyKVersionException(String message) {
38 class VanillaKVersion implements Comparable<VanillaKVersion> {
44 Integer rc = Integer.MAX_VALUE
48 VanillaKVersion(version) {
52 static VanillaKVersion minKVersion() {
53 return new VanillaKVersion("v0.0.0")
56 static VanillaKVersion maxKVersion() {
57 return new VanillaKVersion("v" + Integer.MAX_VALUE + ".0.0")
60 static VanillaKVersion factory(version) {
61 return new VanillaKVersion(version)
69 this.rc = Integer.MAX_VALUE
72 throw new EmptyKVersionException("Empty kernel version")
75 def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rc(\d+))?$/
77 throw new InvalidKVersionException("Invalid kernel version: ${version}")
83 this.major = Integer.parseInt(match.group(1))
84 if (this.major <= 2) {
86 this.majorB = Integer.parseInt(match.group(2))
90 if (match.group(2 + offset) != null) {
91 this.minor = Integer.parseInt(match.group(2 + offset))
95 if (match.group(4 + offset) != null) {
96 this.patch = Integer.parseInt(match.group(4 + offset))
100 if (match.group(8) != null) {
101 this.rc = Integer.parseInt(match.group(8))
105 // Return true if this version is a release candidate
107 return this.rc != Integer.MAX_VALUE
110 // Return true if both version are of the same stable branch
111 Boolean isSameStable(VanillaKVersion o) {
112 if (this.major != o.major) {
115 if (this.majorB != o.majorB) {
118 if (this.minor != o.minor) {
125 @Override int compareTo(VanillaKVersion o) {
126 if (this.major != o.major) {
127 return Integer.compare(this.major, o.major)
129 if (this.majorB != o.majorB) {
130 return Integer.compare(this.majorB, o.majorB)
132 if (this.minor != o.minor) {
133 return Integer.compare(this.minor, o.minor)
135 if (this.patch != o.patch) {
136 return Integer.compare(this.patch, o.patch)
138 if (this.rc != o.rc) {
139 return Integer.compare(this.rc, o.rc)
147 String vString = "v${this.major}"
149 if (this.majorB > 0) {
150 vString = vString.concat(".${this.majorB}")
153 vString = vString.concat(".${this.minor}")
155 if (this.patch > 0) {
156 vString = vString.concat(".${this.patch}")
159 if (this.rc > 0 && this.rc < Integer.MAX_VALUE) {
160 vString = vString.concat("-rc${this.rc}")
166 class UbuntuKVersion implements Comparable<UbuntuKVersion> {
175 Boolean isLTS = false
179 UbuntuKVersion(version) {
183 static UbuntuKVersion minKVersion() {
184 return new UbuntuKVersion("Ubuntu-lts-0.0.0-0.0")
187 static UbuntuKVersion maxKVersion() {
188 return new UbuntuKVersion("Ubuntu-" + Integer.MAX_VALUE + ".0.0-0.0")
191 static UbuntuKVersion factory(version) {
192 return new UbuntuKVersion(version)
205 throw new EmptyKVersionException("Empty kernel version")
208 //'Ubuntu-hwe-5.8-5.8.0-19.20_20.04.3',
209 //'Ubuntu-lts-4.8.0-27.29_16.04.1',
210 //'Ubuntu-4.4.0-70.91',
211 def match = version =~ /^Ubuntu-(lts-|hwe-)??(?:\d+\.\d+-)??(\d+)\.(\d+)\.(\d+)-(\d+)\.(\d+)(.*)??$/
213 throw new InvalidKVersionException("Invalid kernel version: ${version}")
216 if (match.group(1) != null) {
218 this.strLTS = match.group(1)
222 this.major = Integer.parseInt(match.group(2))
225 this.minor = Integer.parseInt(match.group(3))
228 this.patch = Integer.parseInt(match.group(4))
231 this.umajor = Integer.parseInt(match.group(5))
234 this.uminor = Integer.parseInt(match.group(6))
236 if (match.group(7) != null) {
237 this.suffix = match.group(7)
241 // Return true if this version is a release candidate
246 // Return true if both version are of the same stable branch
247 Boolean isSameStable(UbuntuKVersion o) {
248 if (this.isLTS != o.isLTS) {
251 if (this.major != o.major) {
254 if (this.minor != o.minor) {
257 if (this.patch != o.patch) {
264 @Override int compareTo(UbuntuKVersion o) {
265 if (this.major != o.major) {
266 return Integer.compare(this.major, o.major)
268 if (this.minor != o.minor) {
269 return Integer.compare(this.minor, o.minor)
271 if (this.patch != o.patch) {
272 return Integer.compare(this.patch, o.patch)
274 if (this.umajor != o.umajor) {
275 return Integer.compare(this.umajor, o.umajor)
277 if (this.uminor != o.uminor) {
278 return Integer.compare(this.uminor, o.uminor)
280 if (this.isLTS != o.isLTS) {
293 String vString = "Ubuntu-"
296 vString = vString.concat("${this.strLTS}")
299 // The tag pattern changed for HWE kernels >= 5.0
300 if (this.isLTS && this.major >= 5) {
301 vString = vString.concat("${this.major}.${this.minor}-${this.major}.${this.minor}.${this.patch}-${this.umajor}.${this.uminor}${this.suffix}")
303 vString = vString.concat("${this.major}.${this.minor}.${this.patch}-${this.umajor}.${this.uminor}${this.suffix}")
311 // Retrieve parameters of the current build
312 def mbranch = build.getEnvironment(listener).get('GIT_BRANCH').minus('origin/')
313 def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild')
314 def kgitrepo = build.buildVariableResolver.resolve('kgitrepo')
315 def kverfloor_raw = build.buildVariableResolver.resolve('kverfloor')
316 def kverceil_raw = build.buildVariableResolver.resolve('kverceil')
317 def kverfilter = build.buildVariableResolver.resolve('kverfilter')
318 def kverrc = build.buildVariableResolver.resolve('kverrc')
319 def uversion = build.buildVariableResolver.resolve('uversion')
320 def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob'))
321 def currentJobName = build.project.getFullDisplayName()
322 def gitmodpath = build.getEnvironment(listener).get('WORKSPACE') + "/src/lttng-modules"
324 // Get the out variable
325 def config = new HashMap()
326 def bindings = getBinding()
327 config.putAll(bindings.getVariables())
328 def out = config['out']
331 // Get the lttng-modules git url
332 def gitmodrepo = Git.open(new File(gitmodpath))
333 def mgitrepo = gitmodrepo.getRepository().getConfig().getString("remote", "origin", "url")
335 // Get tags from git repository
336 def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call()
338 // Get kernel versions to build
343 def kversionFactory = ""
345 if (uversion != null) {
346 kversionFactory = new UbuntuKVersion()
350 ~/^refs\/tags\/(Ubuntu-5\.15\.0-\d{1,3}?\.[\d]+)$/,
351 ~/^refs\/tags\/(Ubuntu-hwe-6\.2-6\.2\.0-.*_22\.04\.\d+)$/,
352 ~/^refs\/tags\/(Ubuntu-hwe-6\.5-6\.5\.0-.*_22\.04\.\d+)$/,
358 ~/^refs\/tags\/(Ubuntu-5\.4\.0-\d{1,3}?\.[\d]+)$/,
359 ~/^refs\/tags\/(Ubuntu-hwe-5\.13-5\.13\.0-.*_20\.04\.\d+)$/,
360 ~/^refs\/tags\/(Ubuntu-hwe-5\.15-5\.15\.0-.*_20\.04\.\d+)$/,
366 ~/^refs\/tags\/(Ubuntu-6\.8\.0-\d{1,3}?\.[\d]+)$/,
371 println "Unsupported Ubuntu version: ${uversion}"
372 throw new InterruptedException()
377 kversionFactory = new VanillaKVersion()
379 ~/^refs\/tags\/(v[\d\.]+(-rc(\d+))?)$/,
386 // Parse kernel versions
389 kverfloor = kversionFactory.factory(kverfloor_raw)
390 } catch (EmptyKVersionException e) {
391 kverfloor = kversionFactory.minKVersion()
396 kverceil = kversionFactory.factory(kverceil_raw)
397 } catch (EmptyKVersionException e) {
398 kverceil = kversionFactory.maxKVersion()
401 // Build a sorted list of versions to build
403 for (matchStr in matchStrs) {
404 def match = ref.getName() =~ matchStr
405 if (match && !blacklist.contains(match.group(1))) {
406 def v = kversionFactory.factory(match.group(1))
408 if ((v >= kverfloor) && (v < kverceil)) {
419 // The filtering step assumes the version lists are sorted
423 switch (kverfilter) {
425 // Keep only the head of each stable branch
426 println('Filter kernel versions to keep only the latest point release of each stable branch.')
428 for (i = 0; i < kversions.size(); i++) {
429 def curr = kversions[i]
430 def next = i < kversions.size() - 1 ? kversions[i + 1] : null
433 if (curr.isSameStable(next)) {
442 // Keep only the head of each LTS branch and the latest non-RC tag
443 println('Filter kernel versions to keep only the latest point release of each lts branch and the current stable.')
445 def lts_kversions = []
446 // Old LTS entries are kept so that "lts-head" is still meaningful in kernel
447 // version ranges that are supported by lttng-modules but no longer supported
448 // upstream, eg. lttng-modules stable-2.13 supports >= 3.0
449 lts_kversions.add(kversionFactory.factory("v3.0")) // LTS until October 2013
450 lts_kversions.add(kversionFactory.factory("v3.2")) // LTS until May 2018
451 lts_kversions.add(kversionFactory.factory("v3.4")) // LTS until October 2016
452 lts_kversions.add(kversionFactory.factory("v3.10")) // LTS until November 2017
453 lts_kversions.add(kversionFactory.factory("v3.12")) // LTS until May 2017
454 lts_kversions.add(kversionFactory.factory("v3.14")) // LTS until August 2016
455 lts_kversions.add(kversionFactory.factory("v3.16")) // LTS until October 2014
456 lts_kversions.add(kversionFactory.factory("v3.18")) // LTS until January 2017
457 lts_kversions.add(kversionFactory.factory("v4.1")) // LTS until May 2018
458 lts_kversions.add(kversionFactory.factory("v4.4")) // SLTS until 2026
459 lts_kversions.add(kversionFactory.factory("v4.9")) // LTS until January 2023
460 lts_kversions.add(kversionFactory.factory("v4.14")) // LTS until January 2024
461 lts_kversions.add(kversionFactory.factory("v4.19")) // LTS until December 2024
462 lts_kversions.add(kversionFactory.factory("v5.4")) // LTS until December 2025
463 lts_kversions.add(kversionFactory.factory("v5.10")) // LTS until December 2026
464 lts_kversions.add(kversionFactory.factory("v5.15")) // LTS until December 2026
465 lts_kversions.add(kversionFactory.factory("v6.1")) // LTS until December 2026
466 lts_kversions.add(kversionFactory.factory("v6.6")) // LTS until December 2026
468 // First filter the head of each branch
469 for (i = 0; i < kversions.size(); i++) {
470 def curr = kversions[i]
471 def next = i < kversions.size() - 1 ? kversions[i + 1] : null
474 if (curr.isSameStable(next)) {
481 for (i = 0; i < kversions.size(); i++) {
482 def curr = kversions[i]
484 // Keep the newest tag
485 if (i == kversions.size() - 1) {
489 // Prune non-LTS versions
491 for (j = 0; j < lts_kversions.size(); j++) {
492 if (curr.isSameStable(lts_kversions[j])) {
506 // No filtering of kernel versions
507 println('No kernel versions filtering selected.')
511 if (kverrc == "true") {
512 // If the last RC version is newer than the last stable, add it to the build list
513 if (kversionsRC.size() > 0 && kversionsRC.last() > kversions.last()) {
514 kversions.add(kversionsRC.last())
518 println "Building the following kernel versions:"
519 for (k in kversions) {
523 // Debug: Stop build here
524 //throw new InterruptedException()
526 def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
529 def ongoingBuild = []
532 def similarJobQueued = 0;
534 // Loop while we have kernel versions remaining or jobs running
535 while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) {
537 if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) {
538 def kversion = kversions.pop()
540 new StringParameterValue('mversion', mbranch),
541 new StringParameterValue('mgitrepo', mgitrepo),
542 new StringParameterValue('ktag', kversion.toString()),
543 new StringParameterValue('kgitrepo', kgitrepo),
546 // Launch the parametrized build
547 def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params))
548 println "triggering ${joburl} for the ${mbranch} branch on kernel ${kversion}"
550 // Add it to the ongoing build queue
551 ongoingBuild.push(param_build)
555 println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size()
559 if (e in InterruptedException) {
560 build.setResult(hudson.model.Result.ABORTED)
561 throw new InterruptedException()
567 // Abort job if a newer instance is queued
568 if (!currentJobName.contains("gerrit")) {
569 similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName}
570 if (similarJobQueued > 0) {
571 println "Abort, a newer instance of the job was queued"
572 build.setResult(hudson.model.Result.ABORTED)
573 throw new InterruptedException()
577 def i = ongoingBuild.iterator()
578 while ( i.hasNext() ) {
579 currentBuild = i.next()
580 if ( currentBuild.isCancelled() || currentBuild.isDone() ) {
585 def matrixParent = currentBuild.get()
586 allBuilds.add(matrixParent)
587 def kernelStr = matrixParent.buildVariableResolver.resolve("ktag")
588 println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}"
590 // Process child runs of matrixBuild
591 def childRuns = matrixParent.getRuns()
592 for ( childRun in childRuns ) {
593 println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}"
594 if (childRun.result != Result.SUCCESS) {
595 failedRuns.add(childRun)
604 // Get log of failed runs
605 for (failedRun in failedRuns) {
606 println "---START---"
607 failedRun.writeWholeLogTo(out)
611 println "---Build report---"
612 for (b in allBuilds) {
613 def kernelStr = b.buildVariableResolver.resolve("ktag")
614 println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}"
621 // Mark this build failed if any child build has failed
623 build.setResult(hudson.model.Result.FAILURE)