Fix: if no cutoff was found -1 was returned with side effect of applying not
[lttng-ci.git] / dsl / kernel-lttng-modules.seed.groovy
1 enum KernelVersioning {
2 MAJOR,MINOR,REVISION,BUILD
3 }
4
5 class BasicVersion implements Comparable<BasicVersion> {
6 int major = -1
7 int minor = -1
8 int revision = -1
9 int build = -1
10 int rc = -1
11 String gitRefs
12
13 // Default Constructor
14 BasicVersion() {}
15
16 // Parse a version string of format X.Y.Z.W-A
17 BasicVersion(String version, String ref) {
18 gitRefs = ref
19 def tokenVersion
20 def token
21 if (version.contains('-')) {
22 // Release canditate
23 token = version.tokenize('-')
24 tokenVersion = token[0]
25 if (token[1]?.isInteger()) {
26 rc = token[1].toInteger()
27 }
28 } else {
29 tokenVersion = version
30 }
31
32 tokenVersion = tokenVersion.tokenize('.')
33
34 def tagEnum = KernelVersioning.MAJOR
35 tokenVersion.each {
36 if (it?.isInteger()) {
37 switch (tagEnum) {
38 case KernelVersioning.MAJOR:
39 major = it.toInteger()
40 tagEnum = KernelVersioning.MINOR
41 break
42 case KernelVersioning.MINOR:
43 minor = it.toInteger()
44 tagEnum = KernelVersioning.REVISION
45 break
46 case KernelVersioning.REVISION:
47 revision = it.toInteger()
48 tagEnum = KernelVersioning.BUILD
49 break
50 case KernelVersioning.BUILD:
51 build = it.toInteger()
52 tagEnum = -1
53 break
54 default:
55 println("Unsupported version extension")
56 println("Trying to parse: ${version}")
57 println("Invalid sub version value: ${it}")
58 //TODO: throw exception for jenkins
59 }
60 }
61 }
62 }
63
64 String print() {
65 String ret = ""
66 if (major != -1) {
67 ret += major
68 if (minor != -1) {
69 ret += "." + minor
70 if (revision != -1) {
71 ret += "." + revision
72 if (build != -1) {
73 ret += "." + build
74 }
75 }
76 }
77 if (rc != -1) {
78 ret += "-rc" + rc
79 }
80 }
81 return ret
82 }
83
84 @Override
85 int compareTo(BasicVersion kernelVersion) {
86 return major <=> kernelVersion.major ?: minor <=> kernelVersion.minor ?: revision <=> kernelVersion.revision ?: build <=> kernelVersion.build ?: rc <=> kernelVersion.rc
87 }
88 }
89
90 def kernelTagCutOff = new BasicVersion("4.3", "")
91 def modulesBranches = ["master","stable-2.5","stable-2.6", "stable-2.4"]
92
93
94 def linuxURL = "git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git"
95 def modulesURL = "git://git.lttng.org/lttng-modules.git"
96
97 // Linux specific variable
98 String linuxCheckoutTo = "linux-source"
99 String recipeCheckoutTo = "recipe"
100 String modulesCheckoutTo = "lttng-modules"
101
102 def linuxGitReference = "/home/jenkins/gitcache/linux-stable.git"
103
104 // Check if we are on jenkins
105 // Useful for outside jenkins devellopment related to groovy only scripting
106 def isJenkinsInstance = binding.variables.containsKey('JENKINS_HOME')
107
108 // Fetch tags and format
109 // Split the string into sections based on |
110 // And pipe the results together
111 String process = "git ls-remote -t $linuxURL | cut -c42- | sort -V"
112 def out = new StringBuilder()
113 def err = new StringBuilder()
114 Process result = process.tokenize( '|' ).inject( null ) { p, c ->
115 if( p )
116 p | c.execute()
117 else
118 c.execute()
119 }
120
121 result.waitForProcessOutput(out,err)
122
123 if ( result.exitValue() == 0 ) {
124 def branches = out.readLines().collect {
125 // Scrap special string tag
126 it.replaceAll("\\^\\{\\}", '')
127 }
128
129 branches = branches.unique()
130
131 List versions = []
132 branches.each { branch ->
133 def stripBranch = branch.replaceAll("rc", '').replaceAll(/refs\/tags\/v/,'')
134 BasicVersion kVersion = new BasicVersion(stripBranch, branch)
135 versions.add(kVersion)
136 }
137
138 // Sort the version via Comparable implementation of KernelVersion
139 versions = versions.sort()
140
141 // Find the version cutoff
142 def cutoffPos = versions.findIndexOf{(it.major >= kernelTagCutOff.major) && (it.minor >= kernelTagCutOff.minor) && (it.revision >= kernelTagCutOff.revision) && (it.build >= kernelTagCutOff.build) && (it.rc >= kernelTagCutOff.rc)}
143
144 // If error set cutoff on last so no job are created
145 if (cutoffPos == -1) {
146 cutoffPos = versions.size()
147 }
148 // Get last version and include only last rc
149 def last
150 def lastNoRcPos
151 last = versions.last()
152 if (last.rc != -1) {
153 int i = versions.size()-1
154 while (i > -1 && versions[i].rc != -1 ) {
155 i--
156 }
157 lastNoRcPos = i + 1
158 } else {
159 lastNoRcPos = versions.size()
160 }
161
162 String modulesPrefix = "lttng-modules"
163 String kernelPrefix = "dsl-kernel"
164 String separator = "-"
165
166 // Actual job creation
167 for (int i = cutoffPos; i < versions.size() ; i++) {
168
169 // Only create for valid build
170 if ( (i < lastNoRcPos && versions[i].rc == -1) || (i >= lastNoRcPos)) {
171 println ("Preparing job for")
172
173 String jobName = kernelPrefix + separator + versions[i].print()
174
175 // Generate modules job based on supported modules jobs
176 def modulesJob = [:]
177 modulesBranches.each { branch ->
178 modulesJob[branch] = modulesPrefix + separator + branch + separator + jobName
179 }
180
181 // Jenkins only dsl
182 println(jobName)
183 if (isJenkinsInstance) {
184 matrixJob("${jobName}") {
185 using("linux-master")
186 scm {
187 git {
188 remote {
189 url("${linuxURL}")
190 }
191 branch(versions[i].gitRefs)
192 shallowClone(true)
193 relativeTargetDir(linuxCheckoutTo)
194 reference(linuxGitReference)
195 }
196 }
197 publishers {
198 modulesJob.each {
199 downstream(it.value, 'SUCCESS')
200 }
201 }
202 }
203 }
204 // Corresponding Module job
205 modulesJob.each { job ->
206 println("\t" + job.key + " " + job.value)
207 if (isJenkinsInstance) {
208 matrixJob(job.value) {
209 using("modules")
210 multiscm {
211 git {
212 remote {
213 name(kernelPrefix)
214 url("${linuxURL}")
215 }
216 branch(versions[i].gitRefs)
217 shallowClone(true)
218 relativeTargetDir(linuxCheckoutTo)
219 reference(linuxGitReference)
220 }
221 git {
222 remote {
223 name(modulesPrefix)
224 url(modulesURL)
225 }
226 branch(job.key)
227 relativeTargetDir(modulesCheckoutTo)
228 }
229 }
230 steps {
231 copyArtifacts("${jobName}/arch=\$arch", "linux-artifact/**", '', false, false) {
232 latestSuccessful(true) // Latest successful build
233 }
234 shell(readFileFromWorkspace('lttng-modules/lttng-modules-dsl-master.sh'))
235 }
236 }
237 }
238 }
239 }
240 }
241
242 // Trigger generations
243 def dslTriggerKernel = """\
244
245 import hudson.model.*
246 import hudson.AbortException
247 import hudson.console.HyperlinkNote
248 import java.util.concurrent.CancellationException
249
250
251 def jobs = hudson.model.Hudson.instance.items
252 def fail = false
253 def jobStartWith = "${kernelPrefix}"
254
255 def anotherBuild
256 jobs.each { job ->
257 def jobName = job.getName()
258 if (jobName.startsWith(jobStartWith)) {
259 def lastBuild = job.getLastBuild()
260 if (lastBuild == null) {
261 try {
262 def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build))
263 println "\\tWaiting for the completion of " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
264 anotherBuild = future.get()
265 } catch (CancellationException x) {
266 throw new AbortException("\${job.fullDisplayName} aborted.")
267 }
268 println HyperlinkNote.encodeTo('/' + anotherBuild.url, anotherBuild.fullDisplayName) + " completed. Result was " + anotherBuild.result
269
270 build.result = anotherBuild.result
271 if (anotherBuild.result != Result.SUCCESS && anotherBuild.result != Result.UNSTABLE) {
272 // We abort this build right here and now.
273 fail = true
274 println("Build Failed")
275 }
276 } else {
277 println("\\tAlready built")
278 }
279 }
280 }
281
282 if (fail){
283 throw new AbortException("Some job failed")
284 }
285 """
286 def dslTriggerModule = """\
287 import hudson.model.*
288 import hudson.AbortException
289 import hudson.console.HyperlinkNote
290 import java.util.concurrent.CancellationException
291
292
293 def jobs = hudson.model.Hudson.instance.items
294 def fail = false
295 def jobStartWith = "JOBPREFIX"
296
297 def anotherBuild
298 jobs.each { job ->
299 def jobName = job.getName()
300 if (jobName.startsWith(jobStartWith)) {
301 def lastBuild = job.getLastBuild()
302 if (lastBuild == null) {
303 try {
304 def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build))
305 println "\\tWaiting for the completion of " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
306 anotherBuild = future.get()
307 } catch (CancellationException x) {
308 throw new AbortException("\${job.fullDisplayName} aborted.")
309 }
310 println HyperlinkNote.encodeTo('/' + anotherBuild.url, anotherBuild.fullDisplayName) + " completed. Result was " + anotherBuild.result
311
312 build.result = anotherBuild.result
313 if (anotherBuild.result != Result.SUCCESS && anotherBuild.result != Result.UNSTABLE) {
314 // We abort this build right here and now.
315 fail = true
316 println("Build Failed")
317 }
318 } else {
319 println("\\tAlready built")
320 }
321 }
322 }
323
324 if (fail){
325 throw new AbortException("Some job failed")
326 }
327 """
328 if (isJenkinsInstance) {
329 freeStyleJob("dsl-trigger-kernel") {
330 steps {
331 systemGroovyCommand(dslTriggerKernel)
332 }
333 triggers {
334 cron("H 0 * * *")
335 }
336 }
337
338 modulesBranches.each { branch ->
339 freeStyleJob("dsl-trigger-module-${branch}") {
340 steps {
341 systemGroovyCommand(dslTriggerModule.replaceAll("JOBPREFIX",modulesPrefix + separator + branch + separator))
342 }
343 triggers {
344 scm('@daily')
345 }
346 }
347 }
348 }
349 }
This page took 0.040222 seconds and 4 git commands to generate.