| 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 kVersion implements Comparable<kVersion> { |
| 27 | |
| 28 | Integer major = 0; |
| 29 | Integer majorB = 0; |
| 30 | Integer minor = 0; |
| 31 | Integer patch = 0; |
| 32 | Integer rc = Integer.MAX_VALUE; |
| 33 | |
| 34 | kVersion() {} |
| 35 | |
| 36 | kVersion(version) { |
| 37 | this.parse(version) |
| 38 | } |
| 39 | |
| 40 | def parse(version) { |
| 41 | this.major = 0 |
| 42 | this.majorB = 0 |
| 43 | this.minor = 0 |
| 44 | this.patch = 0 |
| 45 | this.rc = Integer.MAX_VALUE |
| 46 | |
| 47 | def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rc(\d+))?$/ |
| 48 | if (!match) { |
| 49 | throw new Exception("Invalid kernel version: ${version}") |
| 50 | } |
| 51 | |
| 52 | Integer offset = 0; |
| 53 | |
| 54 | // Major |
| 55 | this.major = Integer.parseInt(match.group(1)) |
| 56 | if (this.major <= 2) { |
| 57 | offset = 2 |
| 58 | this.majorB = Integer.parseInt(match.group(2)) |
| 59 | } |
| 60 | |
| 61 | // Minor |
| 62 | if (match.group(2 + offset) != null) { |
| 63 | this.minor = Integer.parseInt(match.group(2 + offset)) |
| 64 | } |
| 65 | |
| 66 | // Patch level |
| 67 | if (match.group(4 + offset) != null) { |
| 68 | this.patch = Integer.parseInt(match.group(4 + offset)) |
| 69 | } |
| 70 | |
| 71 | // RC |
| 72 | if (match.group(8) != null) { |
| 73 | this.rc = Integer.parseInt(match.group(8)) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Return true if this version is a release candidate |
| 78 | Boolean isRC() { |
| 79 | return this.rc != Integer.MAX_VALUE |
| 80 | } |
| 81 | |
| 82 | // Return true if both version are of the same stable branch |
| 83 | Boolean isSameStable(kVersion o) { |
| 84 | if (this.major != o.major) { |
| 85 | return false |
| 86 | } |
| 87 | if (this.majorB != o.majorB) { |
| 88 | return false |
| 89 | } |
| 90 | if (this.minor != o.minor) { |
| 91 | return false |
| 92 | } |
| 93 | |
| 94 | return true |
| 95 | } |
| 96 | |
| 97 | @Override int compareTo(kVersion o) { |
| 98 | if (this.major != o.major) { |
| 99 | return Integer.compare(this.major, o.major) |
| 100 | } |
| 101 | if (this.majorB != o.majorB) { |
| 102 | return Integer.compare(this.majorB, o.majorB) |
| 103 | } |
| 104 | if (this.minor != o.minor) { |
| 105 | return Integer.compare(this.minor, o.minor) |
| 106 | } |
| 107 | if (this.patch != o.patch) { |
| 108 | return Integer.compare(this.patch, o.patch) |
| 109 | } |
| 110 | if (this.rc != o.rc) { |
| 111 | return Integer.compare(this.rc, o.rc) |
| 112 | } |
| 113 | |
| 114 | // Same version |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | String toString() { |
| 119 | String vString = "v${this.major}" |
| 120 | |
| 121 | if (this.majorB > 0) { |
| 122 | vString = vString.concat(".${this.majorB}") |
| 123 | } |
| 124 | |
| 125 | vString = vString.concat(".${this.minor}") |
| 126 | |
| 127 | if (this.patch > 0) { |
| 128 | vString = vString.concat(".${this.patch}") |
| 129 | } |
| 130 | |
| 131 | if (this.rc > 0 && this.rc < Integer.MAX_VALUE) { |
| 132 | vString = vString.concat("-rc${this.rc}") |
| 133 | } |
| 134 | return vString |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | // Retrieve parameters of the current build |
| 140 | def mversion = build.buildVariableResolver.resolve('mversion') |
| 141 | def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild') |
| 142 | def kgitrepo = build.buildVariableResolver.resolve('kgitrepo') |
| 143 | def kverfloor = new kVersion(build.buildVariableResolver.resolve('kverfloor')) |
| 144 | def kverfilter = build.buildVariableResolver.resolve('kverfilter') |
| 145 | def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob')) |
| 146 | def currentJobName = build.project.getFullDisplayName() |
| 147 | |
| 148 | // Get the out variable |
| 149 | def config = new HashMap() |
| 150 | def bindings = getBinding() |
| 151 | config.putAll(bindings.getVariables()) |
| 152 | def out = config['out'] |
| 153 | |
| 154 | def jlc = new jenkins.model.JenkinsLocationConfiguration() |
| 155 | def jenkinsUrl = jlc.url |
| 156 | |
| 157 | // Get tags from git repository |
| 158 | def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call(); |
| 159 | |
| 160 | // Get kernel versions to build |
| 161 | def kversions = [] |
| 162 | def kversionsRC = [] |
| 163 | for (ref in refs) { |
| 164 | def match = ref.getName() =~ /^refs\/tags\/(v[\d\.]+(-rc(\d+))?)$/ |
| 165 | |
| 166 | if (match) { |
| 167 | def v = new kVersion(match.group(1)) |
| 168 | |
| 169 | if (v >= kverfloor) { |
| 170 | if (v.isRC()) { |
| 171 | kversionsRC.add(v) |
| 172 | } else { |
| 173 | kversions.add(v) |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | kversions.sort() |
| 180 | kversionsRC.sort() |
| 181 | |
| 182 | switch (kverfilter) { |
| 183 | case 'stable-head': |
| 184 | // Keep only the head of each stable branch |
| 185 | println('Filter kernel versions to keep only the latest point release of each stable branch.') |
| 186 | |
| 187 | for (i = 0; i < kversions.size(); i++) { |
| 188 | def curr = kversions[i] |
| 189 | def next = i < kversions.size() - 1 ? kversions[i + 1] : null |
| 190 | |
| 191 | if (next != null) { |
| 192 | if (curr.isSameStable(next)) { |
| 193 | kversions.remove(i) |
| 194 | i-- |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | break |
| 199 | |
| 200 | default: |
| 201 | // No filtering of kernel versions |
| 202 | println('No kernel versions filtering selected.') |
| 203 | break |
| 204 | } |
| 205 | |
| 206 | // If the last RC version is newer than the last stable, add it to the build list |
| 207 | if (kversionsRC.last() > kversions.last()) { |
| 208 | kversions.add(kversionsRC.last()) |
| 209 | } |
| 210 | |
| 211 | // Debug |
| 212 | println "Building the following kernel versions:" |
| 213 | for (k in kversions) { |
| 214 | println k |
| 215 | } |
| 216 | |
| 217 | // Debug: Stop build here |
| 218 | //throw new InterruptedException() |
| 219 | |
| 220 | def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName) |
| 221 | |
| 222 | def allBuilds = [] |
| 223 | def ongoingBuild = [] |
| 224 | def failedRuns = [] |
| 225 | def isFailed = false |
| 226 | def similarJobQueued = 0; |
| 227 | |
| 228 | // Loop while we have kernel versions remaining or jobs running |
| 229 | while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) { |
| 230 | |
| 231 | if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) { |
| 232 | def kversion = kversions.pop() |
| 233 | def job_params = [ |
| 234 | new StringParameterValue('mversion', mversion), |
| 235 | new StringParameterValue('kversion', kversion.toString()), |
| 236 | new StringParameterValue('kgitrepo', kgitrepo), |
| 237 | ] |
| 238 | |
| 239 | // Launch the parametrized build |
| 240 | def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params)) |
| 241 | println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}" |
| 242 | |
| 243 | // Add it to the ongoing build queue |
| 244 | ongoingBuild.push(param_build) |
| 245 | |
| 246 | } else { |
| 247 | |
| 248 | println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size() |
| 249 | try { |
| 250 | Thread.sleep(5000) |
| 251 | } catch(e) { |
| 252 | if (e in InterruptedException) { |
| 253 | build.setResult(hudson.model.Result.ABORTED) |
| 254 | throw new InterruptedException() |
| 255 | } else { |
| 256 | throw(e) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // If a newer instance of this job is queued, abort to let it run |
| 261 | similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName} |
| 262 | if ( similarJobQueued > 0 ) { |
| 263 | // Abort since new build is queued |
| 264 | build.setResult(hudson.model.Result.ABORTED) |
| 265 | throw new InterruptedException() |
| 266 | } |
| 267 | |
| 268 | def i = ongoingBuild.iterator() |
| 269 | while ( i.hasNext() ) { |
| 270 | currentBuild = i.next() |
| 271 | if ( currentBuild.isCancelled() || currentBuild.isDone() ) { |
| 272 | // Remove from queue |
| 273 | i.remove() |
| 274 | |
| 275 | // Print results |
| 276 | def matrixParent = currentBuild.get() |
| 277 | allBuilds.add(matrixParent) |
| 278 | def kernelStr = matrixParent.buildVariableResolver.resolve("kversion") |
| 279 | println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}" |
| 280 | |
| 281 | // Process child runs of matrixBuild |
| 282 | def childRuns = matrixParent.getRuns() |
| 283 | for ( childRun in childRuns ) { |
| 284 | println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}" |
| 285 | if (childRun.result != Result.SUCCESS) { |
| 286 | failedRuns.add(childRun) |
| 287 | isFailed = true |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Get log of failed runs |
| 296 | for (failedRun in failedRuns) { |
| 297 | println "---START---" |
| 298 | failedRun.writeWholeLogTo(out) |
| 299 | println "---END---" |
| 300 | } |
| 301 | |
| 302 | println "---Build report---" |
| 303 | for (b in allBuilds) { |
| 304 | def kernelStr = b.buildVariableResolver.resolve("kversion") |
| 305 | println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}" |
| 306 | // Cleanup builds |
| 307 | try { |
| 308 | b.delete() |
| 309 | } catch (all) {} |
| 310 | } |
| 311 | |
| 312 | // Mark this build failed if any child build has failed |
| 313 | if (isFailed) { |
| 314 | build.getExecutor().interrupt(Result.FAILURE) |
| 315 | } |
| 316 | |
| 317 | // EOF |