| 1 | /** |
| 2 | * Copyright (C) 2016 - 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 rt = 0; |
| 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.rt = 0 |
| 46 | |
| 47 | def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rt(\d+)-rebase)$/ |
| 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 | // RT |
| 72 | this.rt = Integer.parseInt(match.group(8)) |
| 73 | } |
| 74 | |
| 75 | @Override int compareTo(kVersion o) { |
| 76 | if (this.major != o.major) { |
| 77 | return Integer.compare(this.major, o.major); |
| 78 | } |
| 79 | if (this.majorB != o.majorB) { |
| 80 | return Integer.compare(this.majorB, o.majorB); |
| 81 | } |
| 82 | if (this.minor != o.minor) { |
| 83 | return Integer.compare(this.minor, o.minor); |
| 84 | } |
| 85 | if (this.patch != o.patch) { |
| 86 | return Integer.compare(this.patch, o.patch); |
| 87 | } |
| 88 | if (this.rt != o.rc) { |
| 89 | return Integer.compare(this.rt, o.rt); |
| 90 | } |
| 91 | |
| 92 | // Same version |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | String toString() { |
| 97 | String vString = "v${this.major}" |
| 98 | |
| 99 | if (this.majorB > 0) { |
| 100 | vString = vString.concat(".${this.majorB}") |
| 101 | } |
| 102 | |
| 103 | vString = vString.concat(".${this.minor}") |
| 104 | |
| 105 | if (this.patch > 0) { |
| 106 | vString = vString.concat(".${this.patch}") |
| 107 | } |
| 108 | |
| 109 | if (this.rt > 0) { |
| 110 | vString = vString.concat("-rt${this.rt}-rebase") |
| 111 | } |
| 112 | return vString |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | // Retrieve parameters of the current build |
| 118 | def mversion = build.buildVariableResolver.resolve('mversion') |
| 119 | def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild') |
| 120 | def kgitrepo = build.buildVariableResolver.resolve('kgitrepo') |
| 121 | def kverfloor = new kVersion(build.buildVariableResolver.resolve('kverfloor')) |
| 122 | def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob')) |
| 123 | |
| 124 | // Get the out variable |
| 125 | def config = new HashMap() |
| 126 | def bindings = getBinding() |
| 127 | config.putAll(bindings.getVariables()) |
| 128 | def out = config['out'] |
| 129 | |
| 130 | def jlc = new jenkins.model.JenkinsLocationConfiguration() |
| 131 | def jenkinsUrl = jlc.url |
| 132 | |
| 133 | // Get tags from git repository |
| 134 | def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call(); |
| 135 | |
| 136 | // Get kernel versions to build |
| 137 | def kversions = [] |
| 138 | for (ref in refs) { |
| 139 | def match = ref.getName() =~ /^refs\/tags\/(v[\d\.]+(-rt(\d+)-rebase))$/ |
| 140 | |
| 141 | if (match) { |
| 142 | def v = new kVersion(match.group(1)) |
| 143 | |
| 144 | if (v >= kverfloor) { |
| 145 | kversions.add(v) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | kversions.sort() |
| 151 | |
| 152 | // Debug |
| 153 | println "Building the following kernel versions:" |
| 154 | for (k in kversions) { |
| 155 | println k |
| 156 | } |
| 157 | |
| 158 | // Debug: Stop build here |
| 159 | //throw new InterruptedException() |
| 160 | |
| 161 | def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName) |
| 162 | |
| 163 | def allBuilds = [] |
| 164 | def ongoingBuild = [] |
| 165 | def failedRuns = [] |
| 166 | def isFailed = false |
| 167 | |
| 168 | // Loop while we have kernel versions remaining or jobs running |
| 169 | while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) { |
| 170 | |
| 171 | if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) { |
| 172 | def kversion = kversions.pop() |
| 173 | def job_params = [ |
| 174 | new StringParameterValue('mversion', mversion), |
| 175 | new StringParameterValue('kversion', kversion.toString()), |
| 176 | new StringParameterValue('kgitrepo', kgitrepo), |
| 177 | ] |
| 178 | |
| 179 | // Launch the parametrized build |
| 180 | def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params)) |
| 181 | println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}" |
| 182 | |
| 183 | // Add it to the ongoing build queue |
| 184 | ongoingBuild.push(param_build) |
| 185 | |
| 186 | } else { |
| 187 | |
| 188 | println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size() |
| 189 | try { |
| 190 | Thread.sleep(5000) |
| 191 | } catch(e) { |
| 192 | if (e in InterruptedException) { |
| 193 | build.setResult(hudson.model.Result.ABORTED) |
| 194 | throw new InterruptedException() |
| 195 | } else { |
| 196 | throw(e) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | def i = ongoingBuild.iterator() |
| 201 | while ( i.hasNext() ) { |
| 202 | currentBuild = i.next() |
| 203 | if ( currentBuild.isCancelled() || currentBuild.isDone() ) { |
| 204 | // Remove from queue |
| 205 | i.remove() |
| 206 | |
| 207 | // Print results |
| 208 | def matrixParent = currentBuild.get() |
| 209 | allBuilds.add(matrixParent) |
| 210 | def kernelStr = matrixParent.buildVariableResolver.resolve("kversion") |
| 211 | println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}" |
| 212 | |
| 213 | // Process child runs of matrixBuild |
| 214 | def childRuns = matrixParent.getRuns() |
| 215 | for ( childRun in childRuns ) { |
| 216 | println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}" |
| 217 | if (childRun.result != Result.SUCCESS) { |
| 218 | failedRuns.add(childRun) |
| 219 | isFailed = true |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Get log of failed runs |
| 228 | for (failedRun in failedRuns) { |
| 229 | println "---START---" |
| 230 | failedRun.writeWholeLogTo(out) |
| 231 | println "---END---" |
| 232 | } |
| 233 | |
| 234 | println "---Build report---" |
| 235 | for (b in allBuilds) { |
| 236 | def kernelStr = b.buildVariableResolver.resolve("kversion") |
| 237 | println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}" |
| 238 | } |
| 239 | |
| 240 | // Clean all builds |
| 241 | // TODO: Delete only builds generated from this job run |
| 242 | for (b in job.getBuilds()) { |
| 243 | b.delete() |
| 244 | } |
| 245 | |
| 246 | // Mark this build failed if any child build has failed |
| 247 | if (isFailed) { |
| 248 | build.getExecutor().interrupt(Result.FAILURE) |
| 249 | } |
| 250 | |
| 251 | // EOF |