aboutsummaryrefslogtreecommitdiff
path: root/jenkins/bisect.jpl
blob: 27af7c52003a9c2eec8227abd4a86c11d1f0ad5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env groovy

/*
  Copyright (C) 2017 Collabora Limited
  Author: Guillaume Tucker <guillaume.tucker@collabora.com>

  This module is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the Free
  Software Foundation; either version 2.1 of the License, or (at your option)
  any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
  details.

  You should have received a copy of the GNU Lesser General Public License
  along with this library; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

def getSHA(kdir) {
    def sha

    dir(kdir) {
        sha = sh(script: "git rev-parse HEAD", returnStdout: true)
    }

    return sha
}

def gitDescribe(kdir) {
    def describe

    dir(kdir) {
        describe = sh(script: "git describe", returnStdout: true).trim()
    }

    return describe
}

def createTag(kdir, iteration) {
    def tag = gitDescribe(kdir)

    dir(kdir) {
        tag += "-${env.JOB_NAME}-${currentBuild.number}-${iteration}"
        sh(script: "git tag -a ${tag} -m ${tag} HEAD")
    }

    return tag
}

def removeTag(kdir, tag) {
    dir(kdir) {
        sh(script: "git tag -d ${tag}")
    }
}

def checkoutRevision(git_dir, git_rev) {
    dir(git_dir) {
        sh(script: """
git clean -fd && \
git checkout --detach ${git_rev}
""")
    }
}

def cloneKCIBuild(kci_build) {
    sh(script: "rm -rf ${kci_build}")
    dir("${kci_build}") {
        git(url: params.KCI_BUILD_URL,
            branch: params.KCI_BUILD_BRANCH,
            poll: false)
    }
}

def cloneLinux(kdir) {
    echo """
Initialising kernel tree
  url:    ${params.KERNEL_URL}
  branch: ${params.KERNEL_BRANCH}
  path:   ${kdir}"""
    sh """
if [ -d ${kdir}/.git ]; then
    cd ${kdir}
    if git remote | grep -e '^${params.TREE}\$'; then
        git remote set-url ${params.TREE} ${params.KERNEL_URL}
        git remote update ${params.TREE}
        git remote prune ${params.TREE}
    else
        git remote add ${params.TREE} ${params.KERNEL_URL}
        git remote update ${params.TREE}
    fi
else
    git clone ${params.KERNEL_URL} ${kdir} -b ${params.KERNEL_BRANCH} -o ${params.TREE}
    cd ${kdir}
fi
git reset --hard
echo 'build-*' > .git/info/exclude
git clean -fd
git bisect reset || echo
git checkout --detach HEAD
git branch -D ${params.KERNEL_BRANCH} || echo
for t in \$(git tag -l | grep ${env.JOB_NAME}); do git tag -d \$t; done
git fetch ${params.TREE} ${params.KERNEL_BRANCH} --tags
git checkout FETCH_HEAD -b ${params.KERNEL_BRANCH}
git symbolic-ref HEAD refs/heads/${params.KERNEL_BRANCH}
cd -
"""
}

def cloneLAVA_CI(lava_ci) {
    sh(script: "rm -rf ${lava_ci}")
    dir("${lava_ci}") {
        git(url: params.LAVA_CI_URL,
            branch: params.LAVA_CI_BRANCH,
            poll: false)
    }
}

/* Working around some seemingly broken Python set-up... */
def eggCache() {
    def egg_cache = env.WORKSPACE + "/python-egg-cache"
    sh(script: "mkdir -p ${egg_cache}")
    return egg_cache
}

def buildKernel(kdir, kci_build) {
    dir(kdir) {
        withCredentials([string(credentialsId: params.KCI_TOKEN_ID,
                                variable: 'SECRET')]) {
            sh(script: """
API=${params.API} \
TOKEN=${SECRET} \
TREE_NAME=${params.TREE} \
ARCH=${params.ARCH} \
BRANCH=${params.KERNEL_BRANCH} \
""" + kci_build + "/build.py -e -g -i -c ${params.DEFCONFIG}")
        }
    }
}

def buildRevision(kdir, kci_build, git_rev, name) {
    checkoutRevision(kdir, git_rev)
    def tag = createTag(kdir, name)
    buildKernel(kdir, kci_build)
    return tag
}

def submitJob(lava_ci, describe) {
    dir(lava_ci) {
        sh(script: "rm -rf data; mkdir data")

        withCredentials([string(credentialsId: params.KCI_TOKEN_ID,
                                variable: 'SECRET')]) {
            sh(script: """
./lava-v2-jobs-from-api.py \
--lab=${params.LAB} \
--token=${SECRET} \
--api=${params.API} \
--storage=${params.STORAGE} \
--plans=${params.PLAN} \
--jobs=data \
--arch=${params.ARCH} \
--tree=${params.TREE} \
--describe=${describe} \
--branch=${params.KERNEL_BRANCH} \
--defconfig_full=${params.DEFCONFIG} \
--priority=${params.LAVA_PRIORITY} \
--callback=${params.CALLBACK} \
--targets=${params.TARGET}
""")
        }

        def egg_cache = eggCache()
        def token = "LAVA XMLRPC Token for " + params.LAB.split('-dev')[0]

        withCredentials([string(credentialsId: token, variable: 'SECRET')]) {
            sh(script: """
PYTHON_EGG_CACHE=${egg_cache} \
./lava-v2-submit-jobs.py \
--username=kernel-ci \
--token=${SECRET} \
--lab=${params.LAB} \
--jobs=data \
--submitted=data/submitted.json
""")
        }

        stash(name: "job-data", includes: "data/**")
    }
}

def getResult(lava_ci) {
    def status

    dir(lava_ci) {
        sh(script: "rm -rf data")
        unstash("job-data")
        def egg_cache = eggCache()
        withCredentials([string(credentialsId: params.KCI_TOKEN_ID,
                                variable: 'SECRET')]) {
            status = sh(returnStatus: true, script: """
PYTHON_EGG_CACHE=${egg_cache} \
./lava-v2-get-boot-results.py \
--lab=${params.LAB} \
--token=${SECRET} \
--api=${params.API} \
--submitted=data/submitted.json \
--period=30 \
--output=data/boots.json
""")
        }
    }

    return status
}

def runTest(lava_ci, describe) {
    cloneLAVA_CI(lava_ci)
    submitJob(lava_ci, describe)
    return getResult(lava_ci)
}

def bisectStart(kdir) {
    def status

    dir(kdir) {
        status = sh(returnStatus: true, script: """
git bisect start
git bisect good ${params.GOOD_COMMIT}
git bisect bad ${params.BAD_COMMIT}
""")
    }

    return status
}

def bisectNext(kdir, status) {
    dir(kdir) {
        sh(script: "git clean -fd")

        switch (status) {
        case 0:
            sh(script: "git bisect good")
            break
        case 2:
            sh(script: "git bisect bad")
            break
        case 1:
            echo "Iteration failed, skipping"
            sh(script: "git bisect skip")
            break
        default:
            echo "Unexpected status, skipping"
            sh(script: "git bisect skip")
            break
        }
    }
}

node("bisection") {
    def kci_build = env.WORKSPACE + '/kernelci-build'
    def kdir = env.WORKSPACE + '/linux'
    def check = null

    echo """\
Lab:       ${params.LAB}
Target:    ${params.TARGET}
Tree:      ${params.TREE}
Branch:    ${params.KERNEL_BRANCH}
Defconfig: ${params.DEFCONFIG}
Plan:      ${params.PLAN}
Good:      ${params.GOOD_COMMIT}
Bad:       ${params.BAD_COMMIT}"""

    if ((params.PLAN != 'boot') && (params.PLAN != 'simple')) {
        echo "Only doing boot and simple plans for now, aborting."
        currentBuild.result = 'ABORTED'
        return
    }

    timeout(time: 30, unit: 'MINUTES') {
        stage("Init") {
            parallel(
                p1: { cloneKCIBuild(kci_build) },
                p2: { cloneLinux(kdir) },
            )
        }
    }

    stage("Check pass") {
        def tag = null

        lock("${env.NODE_NAME}-build-lock") {
            timeout(time: 60, unit: 'MINUTES') {
                try {
                    tag = buildRevision(kdir, kci_build, params.GOOD_COMMIT,
                                        'pass')
                    check = true
                } catch (error) {
                    check = false
                }
            }
        }

        if (!check)
            return

        def describe = gitDescribe(kdir)

        node("kernel-boot-v2") {
            timeout(time: 120, unit: 'MINUTES') {
                def lava_ci = env.WORKSPACE + '/lava-ci'
                def status = runTest(lava_ci, describe)
                check = (status == 0 ? true : false)
            }
        }

        removeTag(kdir, tag)
    }

    if (!check) {
        echo "Good revision check failed"
        currentBuild.result = 'ABORTED'
        return
    }

    stage("Check fail") {
        def tag = null

        lock("${env.NODE_NAME}-build-lock") {
            timeout(time: 60, unit: 'MINUTES') {
                try {
                    tag = buildRevision(kdir, kci_build, params.BAD_COMMIT,
                                        'fail')
                    check = true
                } catch (error) {
                    check = false
                }
            }
        }

        if (!check)
            return

        def describe = gitDescribe(kdir)

        node("kernel-boot-v2") {
            timeout(time: 120, unit: 'MINUTES') {
                def lava_ci = env.WORKSPACE + '/lava-ci'
                def status = runTest(lava_ci, describe)
                check = (status == 2 ? true : false)
            }
        }

        removeTag(kdir, tag)
    }

    if (!check) {
        echo "Bad revision check failed"
        currentBuild.result = 'ABORTED'
        return
    }

    timeout(time: 5, unit: 'MINUTES') {
        stage("Start") {
            def start_result = bisectStart(kdir)

            if (start_result) {
                echo "Failed to start bisection, commits range may be invalid."
                currentBuild.result = 'ABORTED'
                return
            }
        }
    }

    def previous = params.GOOD_COMMIT
    def current = getSHA(kdir)
    def iteration = 1

    while (previous != current) {
        def tag = createTag(kdir, iteration)
        def status = null

        echo "Iteration #${iteration}: ${tag}"

        lock("${env.NODE_NAME}-build-lock") {
            timeout(time: 60, unit: 'MINUTES') {
                stage("Build ${iteration}") {
                    try {
                        buildKernel(kdir, kci_build)
                        status = 0
                    } catch (error) {
                        status = 1
                    }
                }
            }
        }

        if (status == 0) {
            def describe = gitDescribe(kdir)

            node("kernel-boot-v2") {
                timeout(time: 120, unit: 'MINUTES') {
                    stage("Test ${iteration}") {
                        def lava_ci = env.WORKSPACE + '/lava-ci'
                        status = runTest(lava_ci, describe)
                    }
                }
            }
        }

        removeTag(kdir, tag)

        timeout(time: 5, unit: 'MINUTES') {
            stage("Next") {
                bisectNext(kdir, status)
            }
        }

        previous = current
        current = getSHA(kdir)
        iteration += 1
    }
}