jjb: Add kversion ceiling to lttng-modules
[lttng-ci.git] / scripts / lttng-modules / master-vanilla.groovy
1 /**
2 * Copyright (C) 2016-2017 - 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 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
59 if (!version) {
60 throw new EmptykVersionException("Empty kernel version")
61 }
62
63 def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rc(\d+))?$/
64 if (!match) {
65 throw new InvalidkVersionException("Invalid kernel version: ${version}")
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
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
113 @Override int compareTo(kVersion o) {
114 if (this.major != o.major) {
115 return Integer.compare(this.major, o.major)
116 }
117 if (this.majorB != o.majorB) {
118 return Integer.compare(this.majorB, o.majorB)
119 }
120 if (this.minor != o.minor) {
121 return Integer.compare(this.minor, o.minor)
122 }
123 if (this.patch != o.patch) {
124 return Integer.compare(this.patch, o.patch)
125 }
126 if (this.rc != o.rc) {
127 return Integer.compare(this.rc, o.rc)
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')
159 def kverfloor_raw = build.buildVariableResolver.resolve('kverfloor')
160 def kverceil_raw = build.buildVariableResolver.resolve('kverceil')
161 def kverfilter = build.buildVariableResolver.resolve('kverfilter')
162 def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob'))
163 def currentJobName = build.project.getFullDisplayName()
164
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
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
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
194 if ((v >= kverfloor) && (v < kverceil)) {
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
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
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
251 def similarJobQueued = 0;
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
285 // If a newer instance of this job is queued, abort to let it run
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
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}"
331 // Cleanup builds
332 try {
333 b.delete()
334 } catch (all) {}
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
This page took 0.036403 seconds and 4 git commands to generate.