Commit | Line | Data |
---|---|---|
81fa42ce | 1 | /** |
6f1411b1 | 2 | * Copyright (C) 2016-2017 - Michael Jeanson <mjeanson@efficios.com> |
81fa42ce 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 | ||
6f1411b1 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 | } | |
81fa42ce | 37 | |
6f1411b1 | 38 | class RTKVersion implements Comparable<RTKVersion> { |
81fa42ce | 39 | |
6f1411b1 MJ |
40 | Integer major = 0 |
41 | Integer majorB = 0 | |
42 | Integer minor = 0 | |
43 | Integer patch = 0 | |
44 | Integer rt = 0 | |
81fa42ce | 45 | |
6f1411b1 MJ |
46 | RTKVersion() {} |
47 | ||
48 | RTKVersion(version) { | |
81fa42ce MJ |
49 | this.parse(version) |
50 | } | |
51 | ||
6f1411b1 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 | ||
81fa42ce MJ |
64 | def parse(version) { |
65 | this.major = 0 | |
66 | this.majorB = 0 | |
67 | this.minor = 0 | |
68 | this.patch = 0 | |
69 | this.rt = 0 | |
70 | ||
6f1411b1 MJ |
71 | if (!version) { |
72 | throw new EmptyKVersionException("Empty kernel version") | |
73 | } | |
74 | ||
81fa42ce MJ |
75 | def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rt(\d+)-rebase)$/ |
76 | if (!match) { | |
6f1411b1 | 77 | throw new InvalidKVersionException("Invalid kernel version: ${version}") |
81fa42ce 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 | ||
99 | // RT | |
100 | this.rt = Integer.parseInt(match.group(8)) | |
101 | } | |
102 | ||
6f1411b1 MJ |
103 | // Return true if both version are of the same stable branch |
104 | Boolean isSameStable(RTKVersion o) { | |
81fa42ce | 105 | if (this.major != o.major) { |
6f1411b1 | 106 | return false |
81fa42ce MJ |
107 | } |
108 | if (this.majorB != o.majorB) { | |
6f1411b1 | 109 | return false |
81fa42ce MJ |
110 | } |
111 | if (this.minor != o.minor) { | |
6f1411b1 MJ |
112 | return false |
113 | } | |
114 | ||
115 | return true | |
116 | } | |
117 | ||
118 | @Override int compareTo(RTKVersion o) { | |
119 | if (this.major != o.major) { | |
120 | return Integer.compare(this.major, o.major) | |
121 | } | |
122 | if (this.majorB != o.majorB) { | |
123 | return Integer.compare(this.majorB, o.majorB) | |
124 | } | |
125 | if (this.minor != o.minor) { | |
126 | return Integer.compare(this.minor, o.minor) | |
81fa42ce MJ |
127 | } |
128 | if (this.patch != o.patch) { | |
6f1411b1 | 129 | return Integer.compare(this.patch, o.patch) |
81fa42ce | 130 | } |
6f1411b1 MJ |
131 | if (this.rt != o.rt) { |
132 | return Integer.compare(this.rt, o.rt) | |
81fa42ce 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 | ||
152 | if (this.rt > 0) { | |
153 | vString = vString.concat("-rt${this.rt}-rebase") | |
154 | } | |
155 | return vString | |
156 | } | |
157 | } | |
158 | ||
159 | ||
160 | // Retrieve parameters of the current build | |
161 | def mversion = build.buildVariableResolver.resolve('mversion') | |
162 | def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild') | |
163 | def kgitrepo = build.buildVariableResolver.resolve('kgitrepo') | |
6f1411b1 MJ |
164 | def kverfloor_raw = build.buildVariableResolver.resolve('kverfloor') |
165 | def kverceil_raw = build.buildVariableResolver.resolve('kverceil') | |
166 | def kverfilter = build.buildVariableResolver.resolve('kverfilter') | |
81fa42ce MJ |
167 | def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob')) |
168 | def currentJobName = build.project.getFullDisplayName() | |
169 | ||
6f1411b1 | 170 | |
81fa42ce 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 | ||
81fa42ce MJ |
177 | |
178 | // Get tags from git repository | |
179 | def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call(); | |
180 | ||
181 | // Get kernel versions to build | |
182 | def kversions = [] | |
6f1411b1 MJ |
183 | def matchStrs = [ |
184 | ~/^refs\/tags\/(v[\d\.]+(-rt(\d+)-rebase))$/, | |
185 | ] | |
186 | def blacklist = [ | |
187 | 'v4.11.8-rt5-rebase', | |
188 | 'v4.11.9-rt6-rebase', | |
189 | 'v4.11.9-rt7-rebase', | |
190 | 'v4.11.12-rt8-rebase', | |
191 | 'v4.11.12-rt9-rebase', | |
192 | 'v4.11.12-rt10-rebase', | |
193 | 'v4.11.12-rt11-rebase', | |
194 | 'v4.11.12-rt12-rebase', | |
195 | 'v4.11.12-rt13-rebase', | |
196 | ] | |
197 | ||
198 | def kversionFactory = new RTKVersion() | |
199 | ||
200 | // Parse kernel versions | |
201 | def kverfloor = "" | |
202 | try { | |
203 | kverfloor = kversionFactory.factory(kverfloor_raw) | |
204 | } catch (EmptyKVersionException e) { | |
205 | kverfloor = kversionFactory.minKVersion() | |
206 | } | |
81fa42ce | 207 | |
6f1411b1 MJ |
208 | def kverceil = "" |
209 | try { | |
210 | kverceil = kversionFactory.factory(kverceil_raw) | |
211 | } catch (EmptyKVersionException e) { | |
212 | kverceil = kversionFactory.maxKVersion() | |
213 | } | |
81fa42ce | 214 | |
6f1411b1 MJ |
215 | // Build a sorted list of versions to build |
216 | for (ref in refs) { | |
217 | for (matchStr in matchStrs) { | |
218 | def match = ref.getName() =~ matchStr | |
219 | if (match && !blacklist.contains(match.group(1))) { | |
220 | def v = kversionFactory.factory(match.group(1)) | |
221 | ||
222 | if ((v >= kverfloor) && (v < kverceil)) { | |
223 | kversions.add(v) | |
224 | } | |
81fa42ce MJ |
225 | } |
226 | } | |
227 | } | |
228 | ||
229 | kversions.sort() | |
230 | ||
6f1411b1 MJ |
231 | switch (kverfilter) { |
232 | case 'stable-head': | |
233 | // Keep only the head of each stable branch | |
234 | println('Filter kernel versions to keep only the latest point release of each stable branch.') | |
235 | ||
236 | for (i = 0; i < kversions.size(); i++) { | |
237 | def curr = kversions[i] | |
238 | def next = i < kversions.size() - 1 ? kversions[i + 1] : null | |
239 | ||
240 | if (next != null) { | |
241 | if (curr.isSameStable(next)) { | |
242 | kversions.remove(i) | |
243 | i-- | |
244 | } | |
245 | } | |
246 | } | |
247 | break | |
248 | ||
249 | default: | |
250 | // No filtering of kernel versions | |
251 | println('No kernel versions filtering selected.') | |
252 | break | |
253 | } | |
254 | ||
255 | ||
81fa42ce MJ |
256 | println "Building the following kernel versions:" |
257 | for (k in kversions) { | |
258 | println k | |
259 | } | |
260 | ||
261 | // Debug: Stop build here | |
262 | //throw new InterruptedException() | |
263 | ||
264 | def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName) | |
265 | ||
266 | def allBuilds = [] | |
267 | def ongoingBuild = [] | |
268 | def failedRuns = [] | |
269 | def isFailed = false | |
6f1411b1 | 270 | def similarJobQueued = 0; |
81fa42ce MJ |
271 | |
272 | // Loop while we have kernel versions remaining or jobs running | |
273 | while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) { | |
274 | ||
275 | if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) { | |
276 | def kversion = kversions.pop() | |
277 | def job_params = [ | |
278 | new StringParameterValue('mversion', mversion), | |
6f1411b1 | 279 | new StringParameterValue('ktag', kversion.toString()), |
81fa42ce MJ |
280 | new StringParameterValue('kgitrepo', kgitrepo), |
281 | ] | |
282 | ||
283 | // Launch the parametrized build | |
284 | def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params)) | |
285 | println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}" | |
286 | ||
287 | // Add it to the ongoing build queue | |
288 | ongoingBuild.push(param_build) | |
289 | ||
290 | } else { | |
291 | ||
292 | println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size() | |
293 | try { | |
6f1411b1 | 294 | Thread.sleep(10000) |
81fa42ce MJ |
295 | } catch(e) { |
296 | if (e in InterruptedException) { | |
297 | build.setResult(hudson.model.Result.ABORTED) | |
298 | throw new InterruptedException() | |
299 | } else { | |
300 | throw(e) | |
301 | } | |
302 | } | |
303 | ||
6f1411b1 | 304 | // Abort job if a newer instance is queued |
81fa42ce MJ |
305 | similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName} |
306 | if ( similarJobQueued > 0 ) { | |
81fa42ce MJ |
307 | build.setResult(hudson.model.Result.ABORTED) |
308 | throw new InterruptedException() | |
309 | } | |
310 | ||
311 | def i = ongoingBuild.iterator() | |
312 | while ( i.hasNext() ) { | |
313 | currentBuild = i.next() | |
314 | if ( currentBuild.isCancelled() || currentBuild.isDone() ) { | |
315 | // Remove from queue | |
316 | i.remove() | |
317 | ||
318 | // Print results | |
319 | def matrixParent = currentBuild.get() | |
320 | allBuilds.add(matrixParent) | |
6f1411b1 | 321 | def kernelStr = matrixParent.buildVariableResolver.resolve("ktag") |
81fa42ce MJ |
322 | println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}" |
323 | ||
324 | // Process child runs of matrixBuild | |
325 | def childRuns = matrixParent.getRuns() | |
326 | for ( childRun in childRuns ) { | |
327 | println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}" | |
328 | if (childRun.result != Result.SUCCESS) { | |
329 | failedRuns.add(childRun) | |
330 | isFailed = true | |
331 | } | |
332 | } | |
333 | } | |
334 | } | |
335 | } | |
336 | } | |
337 | ||
338 | // Get log of failed runs | |
339 | for (failedRun in failedRuns) { | |
340 | println "---START---" | |
341 | failedRun.writeWholeLogTo(out) | |
342 | println "---END---" | |
343 | } | |
344 | ||
345 | println "---Build report---" | |
346 | for (b in allBuilds) { | |
6f1411b1 | 347 | def kernelStr = b.buildVariableResolver.resolve("ktag") |
81fa42ce MJ |
348 | println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}" |
349 | // Cleanup builds | |
350 | try { | |
351 | b.delete() | |
352 | } catch (all) {} | |
353 | } | |
354 | ||
355 | // Mark this build failed if any child build has failed | |
356 | if (isFailed) { | |
c5c05f73 | 357 | build.setResult(hudson.model.Result.FAILURE) |
81fa42ce MJ |
358 | } |
359 | ||
360 | // EOF |