2 * Copyright (C) 2016-2018 - 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-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+)(.*)??$/
212 throw new InvalidKVersionException("Invalid kernel version: ${version}")
215 if (match.group(1) != null) {
217 this.strLTS = match.group(1)
221 this.major = Integer.parseInt(match.group(2))
224 this.minor = Integer.parseInt(match.group(3))
227 this.patch = Integer.parseInt(match.group(4))
230 this.umajor = Integer.parseInt(match.group(5))
233 this.uminor = Integer.parseInt(match.group(6))
235 if (match.group(7) != null) {
236 this.suffix = match.group(7)
240 // Return true if this version is a release candidate
245 // Return true if both version are of the same stable branch
246 Boolean isSameStable(UbuntuKVersion o) {
247 if (this.isLTS != o.isLTS) {
250 if (this.major != o.major) {
253 if (this.minor != o.minor) {
256 if (this.patch != o.patch) {
263 @Override int compareTo(UbuntuKVersion o) {
264 if (this.major != o.major) {
265 return Integer.compare(this.major, o.major)
267 if (this.minor != o.minor) {
268 return Integer.compare(this.minor, o.minor)
270 if (this.patch != o.patch) {
271 return Integer.compare(this.patch, o.patch)
273 if (this.umajor != o.umajor) {
274 return Integer.compare(this.umajor, o.umajor)
276 if (this.uminor != o.uminor) {
277 return Integer.compare(this.uminor, o.uminor)
279 if (this.isLTS != o.isLTS) {
292 String vString = "Ubuntu-"
295 vString = vString.concat("${this.strLTS}")
298 vString = vString.concat("${this.major}.${this.minor}.${this.patch}-${this.umajor}.${this.uminor}${this.suffix}")
305 // Retrieve parameters of the current build
306 def mbranch = build.getEnvironment(listener).get('GIT_BRANCH').minus('origin/')
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 kverrc = build.buildVariableResolver.resolve('kverrc')
313 def uversion = build.buildVariableResolver.resolve('uversion')
314 def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob'))
315 def currentJobName = build.project.getFullDisplayName()
316 def gitmodpath = build.getEnvironment(listener).get('WORKSPACE') + "/src/lttng-modules"
318 // Get the out variable
319 def config = new HashMap()
320 def bindings = getBinding()
321 config.putAll(bindings.getVariables())
322 def out = config['out']
325 // Get the lttng-modules git url
326 def gitmodrepo = Git.open(new File(gitmodpath))
327 def mgitrepo = gitmodrepo.getRepository().getConfig().getString("remote", "origin", "url")
329 // Get tags from git repository
330 def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call()
332 // Get kernel versions to build
337 def kversionFactory = ""
339 if (uversion != null) {
340 kversionFactory = new UbuntuKVersion()
344 ~/^refs\/tags\/(Ubuntu-5\.4\.0-\d{1,3}?\.[\d]+)$/,
350 ~/^refs\/tags\/(Ubuntu-4\.15\.0-\d{1,3}?\.[\d]+)$/,
351 ~/^refs\/tags\/(Ubuntu-hwe-5\.0\.0-.*_18\.04\.\d+)$/,
352 ~/^refs\/tags\/(Ubuntu-hwe-5\.3\.0-.*_18\.04\.\d+)$/,
358 ~/^refs\/tags\/(Ubuntu-4\.4\.0-\d{1,3}?\.[\d]+)$/,
359 ~/^refs\/tags\/(Ubuntu-hwe-4\.15\.0-.*_16\.04\.\d+)$/,
363 'Ubuntu-lts-4.10.0-7.9_16.04.1',
369 ~/^refs\/tags\/(Ubuntu-3\.13\.0-[\d\.]+)$/,
370 ~/^refs\/tags\/(Ubuntu-lts-.*_14\.04\.\d+)$/,
375 println "Unsupported Ubuntu version: ${uversion}"
376 throw new InterruptedException()
381 kversionFactory = new VanillaKVersion()
383 ~/^refs\/tags\/(v[\d\.]+(-rc(\d+))?)$/,
390 // Parse kernel versions
393 kverfloor = kversionFactory.factory(kverfloor_raw)
394 } catch (EmptyKVersionException e) {
395 kverfloor = kversionFactory.minKVersion()
400 kverceil = kversionFactory.factory(kverceil_raw)
401 } catch (EmptyKVersionException e) {
402 kverceil = kversionFactory.maxKVersion()
405 // Build a sorted list of versions to build
407 for (matchStr in matchStrs) {
408 def match = ref.getName() =~ matchStr
409 if (match && !blacklist.contains(match.group(1))) {
410 def v = kversionFactory.factory(match.group(1))
412 if ((v >= kverfloor) && (v < kverceil)) {
426 switch (kverfilter) {
428 // Keep only the head of each stable branch
429 println('Filter kernel versions to keep only the latest point release of each stable branch.')
431 for (i = 0; i < kversions.size(); i++) {
432 def curr = kversions[i]
433 def next = i < kversions.size() - 1 ? kversions[i + 1] : null
436 if (curr.isSameStable(next)) {
445 // No filtering of kernel versions
446 println('No kernel versions filtering selected.')
450 if (kverrc == "true") {
451 // If the last RC version is newer than the last stable, add it to the build list
452 if (kversionsRC.size() > 0 && kversionsRC.last() > kversions.last()) {
453 kversions.add(kversionsRC.last())
457 println "Building the following kernel versions:"
458 for (k in kversions) {
462 // Debug: Stop build here
463 //throw new InterruptedException()
465 def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
468 def ongoingBuild = []
471 def similarJobQueued = 0;
473 // Loop while we have kernel versions remaining or jobs running
474 while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) {
476 if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) {
477 def kversion = kversions.pop()
479 new StringParameterValue('mversion', mbranch),
480 new StringParameterValue('mgitrepo', mgitrepo),
481 new StringParameterValue('ktag', kversion.toString()),
482 new StringParameterValue('kgitrepo', kgitrepo),
485 // Launch the parametrized build
486 def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params))
487 println "triggering ${joburl} for the ${mbranch} branch on kernel ${kversion}"
489 // Add it to the ongoing build queue
490 ongoingBuild.push(param_build)
494 println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size()
498 if (e in InterruptedException) {
499 build.setResult(hudson.model.Result.ABORTED)
500 throw new InterruptedException()
506 // Abort job if a newer instance is queued
507 if (!currentJobName.contains("gerrit")) {
508 similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName}
509 if (similarJobQueued > 0) {
510 println "Abort, a newer instance of the job was queued"
511 build.setResult(hudson.model.Result.ABORTED)
512 throw new InterruptedException()
516 def i = ongoingBuild.iterator()
517 while ( i.hasNext() ) {
518 currentBuild = i.next()
519 if ( currentBuild.isCancelled() || currentBuild.isDone() ) {
524 def matrixParent = currentBuild.get()
525 allBuilds.add(matrixParent)
526 def kernelStr = matrixParent.buildVariableResolver.resolve("ktag")
527 println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}"
529 // Process child runs of matrixBuild
530 def childRuns = matrixParent.getRuns()
531 for ( childRun in childRuns ) {
532 println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}"
533 if (childRun.result != Result.SUCCESS) {
534 failedRuns.add(childRun)
543 // Get log of failed runs
544 for (failedRun in failedRuns) {
545 println "---START---"
546 failedRun.writeWholeLogTo(out)
550 println "---Build report---"
551 for (b in allBuilds) {
552 def kernelStr = b.buildVariableResolver.resolve("ktag")
553 println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}"
560 // Mark this build failed if any child build has failed
562 build.setResult(hudson.model.Result.FAILURE)