049ce6824e82728b113396602b40b542787dafae
[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("2.6.36", "")
91 def modulesBranches = ["master", "stable-2.5", "stable-2.6"]
92
93 //def modulesBranches = ["master","stable-2.5","stable-2.6", "stable-2.4"]
94
95 def linuxURL = "git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git"
96 def modulesURL = "https://github.com/lttng/lttng-modules.git"
97
98 // Linux specific variable
99 String linuxCheckoutTo = "linux-source"
100 String recipeCheckoutTo = "recipe"
101 String modulesCheckoutTo = "lttng-modules"
102
103 def linuxGitReference = "/home/jenkins/gitcache/linux-stable.git"
104
105 // Check if we are on jenkins
106 // Useful for outside jenkins devellopment related to groovy only scripting
107 def isJenkinsInstance = binding.variables.containsKey('JENKINS_HOME')
108
109 // Fetch tags and format
110 // Split the string into sections based on |
111 // And pipe the results together
112 String process = "git ls-remote -t $linuxURL | cut -c42- | sort -V"
113 def out = new StringBuilder()
114 def err = new StringBuilder()
115 Process result = process.tokenize( '|' ).inject( null ) { p, c ->
116 if( p )
117 p | c.execute()
118 else
119 c.execute()
120 }
121
122 result.waitForProcessOutput(out,err)
123
124 if ( result.exitValue() == 0 ) {
125 def branches = out.readLines().collect {
126 // Scrap special string tag
127 it.replaceAll("\\^\\{\\}", '')
128 }
129
130 branches = branches.unique()
131
132 List versions = []
133 branches.each { branch ->
134 def stripBranch = branch.replaceAll("rc", '').replaceAll(/refs\/tags\/v/,'')
135 BasicVersion kVersion = new BasicVersion(stripBranch, branch)
136 versions.add(kVersion)
137 }
138
139 // Sort the version via Comparable implementation of KernelVersion
140 versions = versions.sort()
141
142 // Find the version cutoff
143 def cutoffPos = versions.findIndexOf{(it.major >= kernelTagCutOff.major) && (it.minor >= kernelTagCutOff.minor) && (it.revision >= kernelTagCutOff.revision) && (it.build >= kernelTagCutOff.build) && (it.rc >= kernelTagCutOff.rc)}
144
145 // If error set cutoff on last so no job are created
146 if (cutoffPos == -1) {
147 cutoffPos = versions.size()
148 }
149 // Get last version and include only last rc
150 def last
151 def lastNoRcPos
152 last = versions.last()
153 if (last.rc != -1) {
154 int i = versions.size()-1
155 while (i > -1 && versions[i].rc != -1 ) {
156 i--
157 }
158 lastNoRcPos = i + 1
159 } else {
160 lastNoRcPos = versions.size()
161 }
162
163 String modulesPrefix = "lttng-modules"
164 String kernelPrefix = "dsl-kernel"
165 String separator = "-"
166
167
168 println("CutOff index")
169 println(cutoffPos)
170
171
172 // Actual job creation
173 for (int i = cutoffPos; i < versions.size() ; i++) {
174
175 // Only create for valid build
176 if ( (i < lastNoRcPos && versions[i].rc == -1) || (i >= lastNoRcPos)) {
177 println ("Preparing job for")
178
179 String jobName = kernelPrefix + separator + versions[i].print()
180
181 // Generate modules job based on supported modules jobs
182 def modulesJob = [:]
183 modulesBranches.each { branch ->
184 modulesJob[branch] = modulesPrefix + separator + branch + separator + jobName
185 }
186
187 // Jenkins only dsl
188 println(jobName)
189 if (isJenkinsInstance) {
190 matrixJob("${jobName}") {
191 using("linux-master")
192 scm {
193 git {
194 remote {
195 url("${linuxURL}")
196 }
197 branch(versions[i].gitRefs)
198 shallowClone(true)
199 relativeTargetDir(linuxCheckoutTo)
200 reference(linuxGitReference)
201 }
202 }
203 publishers {
204 modulesJob.each {
205 downstream(it.value, 'SUCCESS')
206 }
207 }
208 }
209 }
210 // Corresponding Module job
211 modulesJob.each { job ->
212 println("\t" + job.key + " " + job.value)
213 if (isJenkinsInstance) {
214 matrixJob(job.value) {
215 using("modules")
216 multiscm {
217 git {
218 remote {
219 name(kernelPrefix)
220 url("${linuxURL}")
221 }
222 branch(versions[i].gitRefs)
223 shallowClone(true)
224 relativeTargetDir(linuxCheckoutTo)
225 reference(linuxGitReference)
226 }
227 git {
228 remote {
229 name(modulesPrefix)
230 url(modulesURL)
231 }
232 branch(job.key)
233 relativeTargetDir(modulesCheckoutTo)
234 }
235 }
236 steps {
237 copyArtifacts("${jobName}/arch=\$arch,label=kernel", "linux-artifact/**", '', false, false) {
238 latestSuccessful(true) // Latest successful build
239 }
240 shell(readFileFromWorkspace('lttng-modules/lttng-modules-dsl-master.sh'))
241 }
242 }
243 }
244 }
245 }
246 }
247
248 // Trigger generations
249 def dslTriggerKernel = """\
250 import hudson.model.*
251 import jenkins.model.*
252 import hudson.AbortException
253 import hudson.console.HyperlinkNote
254 import java.util.concurrent.CancellationException
255 import java.util.Random
256
257
258 Random random = new Random()
259 def jobs = hudson.model.Hudson.instance.items
260 def fail = false
261 def jobStartWithKernel = "KERNELPREFIX"
262 def jobStartWithModule = "MODULEPREFIX"
263 def toBuild = []
264 def counter = 0
265 def limitQueue = 4
266
267 def anotherBuild
268 jobs.each { job ->
269 def jobName = job.getName()
270 if (jobName.startsWith(jobStartWithKernel)) {
271 counter = counter + 1
272 def lastBuild = job.getLastBuild()
273 if (lastBuild == null || lastBuild.result != Result.SUCCESS) {
274 toBuild.push(job)
275 } else {
276 println("\tAlready built")
277 }
278 }
279 }
280
281 println "Kernel total "+ counter
282 println "Kernel to build "+ toBuild.size()
283
284
285 def kernelEnabledNode = 0
286 hudson.model.Hudson.instance.nodes.each { node ->
287 if (node.getLabelString().contains("kernel")){
288 kernelEnabledNode++
289 }
290 }
291 println "Nb of live kernel enabled build node "+ kernelEnabledNode
292
293 def ongoingBuild = []
294 def q = jenkins.model.Jenkins.getInstance().getQueue()
295
296 while (toBuild.size() != 0) {
297 // Throttle the build with both the number of current parent task and queued
298 // task.Look for both kernel and downstream module from previous kernel.
299 def queuedTask = q.getItems().findAll {
300 it.task.getParent().name.startsWith(jobStartWithKernel) ||
301 it.task.getParent().name.startsWith(jobStartWithModule)
302 }
303 if ((ongoingBuild.size() <= kernelEnabledNode.intdiv(2)) && (queuedTask.size() < limitQueue)) {
304 def job = toBuild.pop()
305 ongoingBuild.push(job.scheduleBuild2(0))
306 println "\t trigering " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
307 } else {
308 println "Currently " + ongoingBuild.size() + " build currently on execution. Limit: " + kernelEnabledNode.intdiv(2)
309 println "Currently " + queuedTask.findAll{it.task.getParent().name.startsWith(jobStartWithModule)}.size() + " module jobs are queued. Limit: " + limitQueue
310 println "Currently " + queuedTask.findAll{it.task.getParent().name.startsWith(jobStartWithKernel)}.size() + " kernel jobs are queued. Limit: " + limitQueue
311 Thread.sleep(random.nextInt(60000))
312 ongoingBuild.removeAll{ it.isCancelled() || it.isDone() }
313 }
314 }
315
316 if (fail){
317 throw new AbortException("Some job failed")
318 }
319 """
320 def dslTriggerModule = """\
321 import hudson.model.*
322 import hudson.AbortException
323 import hudson.console.HyperlinkNote
324 import java.util.concurrent.CancellationException
325 import java.util.Random
326
327
328 Random random = new Random()
329 def jobs = hudson.model.Hudson.instance.items
330 def fail = false
331 def jobStartWith = "JOBPREFIX"
332 def toBuild = []
333 def counter = 0
334 def limitQueue = 4
335
336 def anotherBuild
337 jobs.each { job ->
338 def jobName = job.getName()
339 if (jobName.startsWith(jobStartWith)) {
340 counter = counter + 1
341 toBuild.push(job)
342 }
343 }
344
345 // Get valid node
346 def kernelEnabledNode = 0
347 hudson.model.Hudson.instance.nodes.each { node ->
348 if (node.getLabelString().contains("kernel")){
349 kernelEnabledNode++
350 }
351 }
352
353 def ongoingBuild = []
354 def q = jenkins.model.Jenkins.getInstance().getQueue()
355
356 while (toBuild.size() != 0) {
357 // Throttle the build with both the number of current parent task and queued
358 // task.Look for both kernel and downstream module from previous kernel.
359 def queuedTask = q.getItems().findAll {
360 it.task.getParent().name.startsWith(jobStartWithKernel) ||
361 it.task.getParent().name.startsWith(jobStartWithModule)
362 }
363
364 if ((ongoingBuild.size() <= kernelEnabledNode.intdiv(2)) && (queuedTask.size() < limitQueue)) {
365 def job = toBuild.pop()
366 ongoingBuild.push(job.scheduleBuild2(0))
367 println "\\t trigering " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
368 } else {
369 Thread.sleep(random.nextInt(60000))
370 ongoingBuild.removeAll{ it.isCancelled() || it.isDone() }
371 }
372 }
373
374 if (fail){
375 throw new AbortException("Some job failed")
376 }
377 """
378
379 dslTriggerKernel = dslTriggerKernel.replaceAll("KERNELPREFIX", kernelPrefix)
380 dslTriggerKernel = dslTriggerKernel.replaceAll("MODULEPREFIX", modulesPrefix)
381 if (isJenkinsInstance) {
382 freeStyleJob("dsl-trigger-kernel") {
383 steps {
384 systemGroovyCommand(dslTriggerKernel)
385 }
386 triggers {
387 cron("H 0 * * *")
388 }
389 }
390
391 modulesBranches.each { branch ->
392 dslTriggerModule = dslTriggerModule.replaceAll("JOBPREFIX",modulesPrefix + separator + branch + separator)
393 freeStyleJob("dsl-trigger-module-${branch}") {
394 steps {
395 systemGroovyCommand(dslTriggerModule)
396 }
397 triggers {
398 scm('@daily')
399 }
400 }
401 }
402 }
403 }
This page took 0.037779 seconds and 4 git commands to generate.