| 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 | // Retrieve parameters of the current build |
| 27 | def mversion = build.buildVariableResolver.resolve('mversion') |
| 28 | def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild') |
| 29 | def kgitrepo = build.buildVariableResolver.resolve('kgitrepo') |
| 30 | def uversion = build.buildVariableResolver.resolve('uversion') |
| 31 | def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob')) |
| 32 | |
| 33 | // Get the out variable |
| 34 | def config = new HashMap() |
| 35 | def bindings = getBinding() |
| 36 | config.putAll(bindings.getVariables()) |
| 37 | def out = config['out'] |
| 38 | |
| 39 | def jlc = new jenkins.model.JenkinsLocationConfiguration() |
| 40 | def jenkinsUrl = jlc.url |
| 41 | |
| 42 | // Get tags from git repository |
| 43 | def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call(); |
| 44 | |
| 45 | // Get kernel versions to build |
| 46 | def kversions = [] |
| 47 | |
| 48 | def matchStrs = [] |
| 49 | |
| 50 | switch (uversion) { |
| 51 | case 'xenial': |
| 52 | matchStrs = [ |
| 53 | ~/^refs\/tags\/(Ubuntu-4\.4\.0-[\d\.]+)$/, |
| 54 | ~/^refs\/tags\/(Ubuntu-lts-.*_16\.04\.\d+)$/, |
| 55 | ] |
| 56 | break |
| 57 | |
| 58 | case 'trusty': |
| 59 | matchStrs = [ |
| 60 | ~/^refs\/tags\/(Ubuntu-3\.13\.0-[\d\.]+)$/, |
| 61 | ~/^refs\/tags\/(Ubuntu-lts-.*_14\.04\.\d+)$/, |
| 62 | ] |
| 63 | break |
| 64 | |
| 65 | default: |
| 66 | println 'Unsupported Ubuntu version: ${uversion}' |
| 67 | throw new InterruptedException() |
| 68 | break |
| 69 | } |
| 70 | |
| 71 | for (ref in refs) { |
| 72 | for (matchStr in matchStrs) { |
| 73 | def match = ref.getName() =~ matchStr |
| 74 | |
| 75 | if (match) { |
| 76 | kversions.add(match.group(1)) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | kversions.sort() |
| 82 | |
| 83 | // Debug |
| 84 | println "Building the following kernel versions:" |
| 85 | for (k in kversions) { |
| 86 | println k |
| 87 | } |
| 88 | |
| 89 | // Debug: Stop build here |
| 90 | //throw new InterruptedException() |
| 91 | |
| 92 | def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName) |
| 93 | |
| 94 | def allBuilds = [] |
| 95 | def ongoingBuild = [] |
| 96 | def failedRuns = [] |
| 97 | def isFailed = false |
| 98 | |
| 99 | // Loop while we have kernel versions remaining or jobs running |
| 100 | while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) { |
| 101 | |
| 102 | if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) { |
| 103 | def kversion = kversions.pop() |
| 104 | def job_params = [ |
| 105 | new StringParameterValue('mversion', mversion), |
| 106 | new StringParameterValue('kversion', kversion), |
| 107 | new StringParameterValue('kgitrepo', kgitrepo), |
| 108 | ] |
| 109 | |
| 110 | // Launch the parametrized build |
| 111 | def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params)) |
| 112 | println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}" |
| 113 | |
| 114 | // Add it to the ongoing build queue |
| 115 | ongoingBuild.push(param_build) |
| 116 | |
| 117 | } else { |
| 118 | |
| 119 | println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size() |
| 120 | try { |
| 121 | Thread.sleep(5000) |
| 122 | } catch(e) { |
| 123 | if (e in InterruptedException) { |
| 124 | build.setResult(hudson.model.Result.ABORTED) |
| 125 | throw new InterruptedException() |
| 126 | } else { |
| 127 | throw(e) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | def i = ongoingBuild.iterator() |
| 132 | while ( i.hasNext() ) { |
| 133 | currentBuild = i.next() |
| 134 | if ( currentBuild.isCancelled() || currentBuild.isDone() ) { |
| 135 | // Remove from queue |
| 136 | i.remove() |
| 137 | |
| 138 | // Print results |
| 139 | def matrixParent = currentBuild.get() |
| 140 | allBuilds.add(matrixParent) |
| 141 | def kernelStr = matrixParent.buildVariableResolver.resolve("kversion") |
| 142 | println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}" |
| 143 | |
| 144 | // Process child runs of matrixBuild |
| 145 | def childRuns = matrixParent.getRuns() |
| 146 | for ( childRun in childRuns ) { |
| 147 | println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}" |
| 148 | if (childRun.result != Result.SUCCESS) { |
| 149 | failedRuns.add(childRun) |
| 150 | isFailed = true |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Get log of failed runs |
| 159 | for (failedRun in failedRuns) { |
| 160 | println "---START---" |
| 161 | failedRun.writeWholeLogTo(out) |
| 162 | println "---END---" |
| 163 | } |
| 164 | |
| 165 | println "---Build report---" |
| 166 | for (b in allBuilds) { |
| 167 | def kernelStr = b.buildVariableResolver.resolve("kversion") |
| 168 | println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}" |
| 169 | } |
| 170 | |
| 171 | // Clean all builds |
| 172 | // TODO: Delete only builds generated from this job run |
| 173 | for (b in job.getBuilds()) { |
| 174 | b.delete() |
| 175 | } |
| 176 | |
| 177 | // Mark this build failed if any child build has failed |
| 178 | if (isFailed) { |
| 179 | build.getExecutor().interrupt(Result.FAILURE) |
| 180 | } |
| 181 | |
| 182 | // EOF |