blob: 2ed356338754530251cb8da4738c84797f7974f9 [file] [log] [blame]
Denis Egorenko8c606552016-12-07 14:22:50 +04001package com.mirantis.mcp
2
Artem Panchenkobc13d262017-01-20 12:40:58 +02003
Denis Egorenko8c606552016-12-07 14:22:50 +04004/**
Artem Panchenkobc13d262017-01-20 12:40:58 +02005 * Checkout Calico repository stage
Denis Egorenko8c606552016-12-07 14:22:50 +04006 *
Artem Panchenkobc13d262017-01-20 12:40:58 +02007 * @param config LinkedHashMap
8 * config includes next parameters:
9 * - project_name String, Calico project to clone
10 * - projectNamespace String, gerrit namespace (optional)
11 * - commit String, Git commit to checkout
12 * - credentialsId String, gerrit credentials ID (optional)
13 * - host String, gerrit host
14 *
15 * Usage example:
16 *
17 * def calico = new com.mirantis.mcp.Calico()
18 * calico.checkoutCalico([
19 * project_name : 'cni-plugin',
20 * commit : 'mcp',
21 * host : 'gerrit.mcp.mirantis.net',
22 * ])
23 *
24 */
25def checkoutCalico(LinkedHashMap config) {
26
27 def git = new com.mirantis.mcp.Git()
28
29 def project_name = config.get('project_name')
30 def projectNamespace = config.get('projectNamespace', 'projectcalico')
31 def commit = config.get('commit')
32 def host = config.get('host')
Artem Panchenkod10610b2017-01-27 18:09:52 +020033 def credentialsId = config.get('credentialsId', 'mcp-ci-gerrit')
Artem Panchenkobc13d262017-01-20 12:40:58 +020034
35 if (!project_name) {
36 throw new RuntimeException("Parameter 'project_name' must be set for checkoutCalico() !")
37 }
38 if (!commit) {
39 throw new RuntimeException("Parameter 'commit' must be set for checkoutCalico() !")
40 }
41
42 stage ("Checkout ${project_name}"){
43 git.gitSSHCheckout([
44 credentialsId : credentialsId,
45 branch : commit,
46 host : host,
47 project : "${projectNamespace}/${project_name}",
48 withWipeOut : true,
49 ])
50 }
51}
52
53
54/**
55 * Build bird binaries stage
56 *
57 * Usage example:
58 *
59 * def calico = new com.mirantis.mcp.Calico()
60 * calico.buildCalicoBird()
61 *
62 */
63def buildCalicoBird() {
64 stage ('Build bird binaries'){
65 sh "/bin/sh -x build.sh"
66 }
67}
68
69
70/**
71 * Publish bird binaries stage
72 *
73 * @param config LinkedHashMap
74 * config includes next parameters:
75 * - artifactoryServerName String, artifactory server name
76 * - binaryRepo String, repository (artifactory) for binary files
77 * - projectNamespace String, artifactory server namespace (optional)
78 * - publishInfo Boolean, whether publish a build-info object to Artifactory (optional)
79 *
80 * Usage example:
81 *
82 * def calico = new com.mirantis.mcp.Calico()
83 * calico.publishCalicoBird([
84 * artifactoryServerName : 'mcp-ci',
85 * binaryRepo : 'sandbox-binary-dev-local',
86 * ])
87 *
88 */
89def publishCalicoBird(LinkedHashMap config) {
90
91 def common = new com.mirantis.mcp.Common()
92 def git = new com.mirantis.mcp.Git()
93 def artifactory = new com.mirantis.mcp.MCPArtifactory()
94
95 def artifactoryServerName = config.get('artifactoryServerName')
96 def binaryRepo = config.get('binaryRepo')
97 def projectNamespace = config.get('projectNamespace', 'mirantis/projectcalico')
98 def publishInfo = config.get('publishInfo', true)
99
100 if (!artifactoryServerName) {
101 throw new RuntimeException("Parameter 'artifactoryServerName' must be set for publishCalicoBird() !")
102 }
103 if (!binaryRepo) {
104 throw new RuntimeException("Parameter 'binaryRepo' must be set for publishCalicoBird() !")
105 }
106
107 def artifactoryServer = Artifactory.server(artifactoryServerName)
108 def buildInfo = Artifactory.newBuildInfo()
109
110 stage('Publishing bird artifacts') {
111 dir("artifacts"){
112 // define tag for bird
113 binaryTag = git.getGitDescribe(true) + "-" + common.getDatetime()
114 sh """
115 cp ../dist/bird bird-${binaryTag}
116 cp ../dist/bird6 bird6-${binaryTag}
117 cp ../dist/birdcl birdcl-${binaryTag}
118 """
119 writeFile file: "latest", text: "${binaryTag}"
120 // define mandatory properties for binary artifacts
121 // and some additional
122 def properties = artifactory.getBinaryBuildProperties([
123 "tag=${binaryTag}",
124 "project=bird"
125 ])
126
127 def uploadSpec = """{
128 "files": [
129 {
130 "pattern": "**",
131 "target": "${binaryRepo}/${projectNamespace}/bird/",
132 "props": "${properties}"
133 }
134 ]
135 }"""
136
137 // Upload to Artifactory.
138 artifactory.uploadBinariesToArtifactory(artifactoryServer, buildInfo, uploadSpec, publishInfo)
139 }// dir
140 }
141 return binaryTag
142}
143
144
145/**
146 * Test confd stage
147 *
148 *
149 * Usage example:
150 *
151 * def calico = new com.mirantis.mcp.Calico()
152 * calico.testCalicoConfd()
153 *
154 */
155def testCalicoConfd() {
156 stage ('Run unittest for confd'){
157 sh """
158 docker run --rm \
159 -v \$(pwd):/usr/src/confd \
160 -w /usr/src/confd \
161 golang:1.7 \
162 bash -c \
163 \"go get github.com/constabulary/gb/...; gb test -v\"
164 """
165 }
166}
167
168
169/**
170 * Build confd binaries stage
171 *
172 *
173 * Usage example:
174 *
175 * def calico = new com.mirantis.mcp.Calico()
176 * calico.buildCalicoConfd()
177 *
178 */
179def buildCalicoConfd() {
180 def container_src_dir = "/usr/src/confd"
181 def src_suffix = "src/github.com/kelseyhightower/confd"
182 def container_workdir = "${container_src_dir}/${src_suffix}"
183 def container_gopath = "${container_src_dir}/vendor:${container_src_dir}"
184
185 stage ('Build confd binary'){
186 sh """
187 docker run --rm \
188 -v \$(pwd):${container_src_dir} \
189 -w ${container_workdir} \
190 -e GOPATH=${container_gopath} \
191 golang:1.7 \
192 bash -c \
193 \"go build -a -installsuffix cgo -ldflags '-extld ld -extldflags -static' -a -x .\"
194 """
195 }
196}
197
198
199/**
200 * Publish confd binaries stage
201 *
202 * @param config LinkedHashMap
203 * config includes next parameters:
204 * - artifactoryServerName String, artifactory server name
205 * - binaryRepo String, repository (artifactory) for binary files
206 * - projectNamespace String, artifactory server namespace (optional)
207 * - publishInfo Boolean, whether publish a build-info object to Artifactory (optional)
208 *
209 * Usage example:
210 *
211 * def calico = new com.mirantis.mcp.Calico()
212 * calico.publishCalicoConfd([
213 * artifactoryServerName : 'mcp-ci',
214 * binaryRepo : 'sandbox-binary-dev-local',
215 * ])
216 *
217 */
218def publishCalicoConfd(LinkedHashMap config) {
219
220 def common = new com.mirantis.mcp.Common()
221 def git = new com.mirantis.mcp.Git()
222 def artifactory = new com.mirantis.mcp.MCPArtifactory()
223
224 def artifactoryServerName = config.get('artifactoryServerName')
225 def binaryRepo = config.get('binaryRepo')
226 def projectNamespace = config.get('projectNamespace', 'mirantis/projectcalico')
227 def publishInfo = config.get('publishInfo', true)
228 def src_suffix = "src/github.com/kelseyhightower/confd"
229
230 if (!artifactoryServerName) {
231 throw new RuntimeException("Parameter 'artifactoryServerName' must be set for publishCalicoConfd() !")
232 }
233 if (!binaryRepo) {
234 throw new RuntimeException("Parameter 'binaryRepo' must be set for publishCalicoConfd() !")
235 }
236
237 def artifactoryServer = Artifactory.server(artifactoryServerName)
238 def buildInfo = Artifactory.newBuildInfo()
239
240 stage('Publishing confd artifacts') {
241
242 dir("artifacts"){
243 // define tag for confd
244 binaryTag = git.getGitDescribe(true) + "-" + common.getDatetime()
245 // create two files confd and confd+tag
246 sh "cp ../${src_suffix}/confd confd-${binaryTag}"
247 writeFile file: "latest", text: "${binaryTag}"
248
249 // define mandatory properties for binary artifacts
250 // and some additional
251 def properties = artifactory.getBinaryBuildProperties([
252 "tag=${binaryTag}",
253 "project=confd"
254 ])
255
256 def uploadSpec = """{
257 "files": [
258 {
259 "pattern": "**",
260 "target": "${binaryRepo}/${projectNamespace}/confd/",
261 "props": "${properties}"
262 }
263 ]
264 }"""
265
266 // Upload to Artifactory.
267 artifactory.uploadBinariesToArtifactory(artifactoryServer, buildInfo, uploadSpec, publishInfo)
268 }// dir
269 }
270 return binaryTag
271}
272
273
274/**
275 * Test libcalico stage
276 *
277 * Usage example:
278 *
279 * def calico = new com.mirantis.mcp.Calico()
280 * calico.testLibcalico()
281 *
282 */
283def testLibcalico() {
284 stage ('Run libcalico unittests'){
285 sh "make test"
286 }
287}
288
289
290/**
291 * Build calico/build image stage
292 *
293 * @param config LinkedHashMap
294 * config includes next parameters:
295 * - dockerRegistry String, Docker registry host to push image to (optional)
296 * - projectNamespace String, artifactory server namespace (optional)
297 * - buildImageTag String, calico/build image name (optional)
298 * - imageTag String, tag of docker image (optional)
299 *
300 * Usage example:
301 *
302 * def calicoFunc = new com.mirantis.mcp.Calico()
303 * calicoFunc.buildLibcalico([
304 * dockerRegistry : 'sandbox-docker-dev-virtual.docker.mirantis.net',
305 * ])
306 *
307 */
308def buildLibcalico(LinkedHashMap config) {
309
310 def common = new com.mirantis.mcp.Common()
311 def docker = new com.mirantis.mcp.Docker()
312 def git = new com.mirantis.mcp.Git()
313
314 def dockerRegistry = config.get('dockerRegistry')
315 def projectNamespace = config.get('projectNamespace', 'mirantis/projectcalico')
316
317 def buildImage = config.get('buildImage', "calico/build")
318 def buildImageTag = config.get('buildImageTag', git.getGitDescribe(true) + "-" + common.getDatetime())
319
320 def buildContainerName = dockerRegistry ? "${dockerRegistry}/${projectNamespace}/${buildImage}:${buildImageTag}" : "${buildImage}:${buildImageTag}"
321
322 stage ('Build calico/build image') {
323 docker.setDockerfileLabels("./Dockerfile", ["docker.imgTag=${buildImageTag}"])
324 sh """
325 make calico/build BUILD_CONTAINER_NAME=${buildContainerName}
326 """
327 }
328 return [buildImage : buildImage,
329 buildImageTag : buildImageTag]
330}
331
332
333/**
334 * Switch Calico to use dowstream libcalico-go repository stage
335 *
336 * @param libCalicoGoCommit String, libcalico-go repository commit to checkout to
337 * @param host String, gerrit host
338 * @param glideLockFilePath String, relative path to glide.lock file
339 *
340 * Usage example:
341 *
342 * def calico = new com.mirantis.mcp.Calico()
343 * // Checkout calico code using calico.checkoutCalico() and then call this method from the same dir
344 * calico.switchCalicoToDownstreamLibcalicoGo('mcp', 'gerrit.mcp.mirantis.net', './glide.lock')
345 *
346 */
347def switchCalicoToDownstreamLibcalicoGo(String libCalicoGoCommit, String host, String glideLockFilePath) {
Sergey Reshetnyak70b1fe62017-01-31 22:27:06 +0300348 def common = new com.mirantis.mcp.Common()
Artem Panchenkobc13d262017-01-20 12:40:58 +0200349 def git = new com.mirantis.mcp.Git()
350
351 stage ('Switch to downstream libcalico-go') {
352 def libcalicogo_path = "${env.WORKSPACE}/tmp_libcalico-go"
353
354 git.gitSSHCheckout([
Artem Panchenkod10610b2017-01-27 18:09:52 +0200355 credentialsId : "mcp-ci-gerrit",
Artem Panchenkobc13d262017-01-20 12:40:58 +0200356 branch : libCalicoGoCommit,
357 host : host,
358 project : "projectcalico/libcalico-go",
359 targetDir : libcalicogo_path,
360 withWipeOut : true,
361 ])
362
Sergey Kulanovdef4df02017-02-01 14:17:46 +0200363 //FIXME(skulanov) we need to clean local cache for libcalico-go
364 sh "rm -rf ~/.glide/cache/src/file-*"
365
Artem Panchenkobc13d262017-01-20 12:40:58 +0200366 sh "cp ${glideLockFilePath} ${glideLockFilePath}.bak"
367 def glideLockFileContent = readFile file: glideLockFilePath
Sergey Reshetnyak70b1fe62017-01-31 22:27:06 +0300368 def glideMap = common.loadYAML(glideLockFileContent)
Artem Panchenkobc13d262017-01-20 12:40:58 +0200369
370 for (goImport in glideMap['imports']) {
371 if (goImport['name'].contains('libcalico-go')) {
372 goImport['repo'] = 'file:///go/src/github.com/projectcalico/libcalico-go'
373 goImport['vcs'] = 'git'
374 }
375 }
376
Sergey Reshetnyak70b1fe62017-01-31 22:27:06 +0300377 writeFile file: glideLockFilePath, text: common.dumpYAML(glideMap)
Artem Panchenkobc13d262017-01-20 12:40:58 +0200378
379 sh "LIBCALICOGO_PATH=${libcalicogo_path} make vendor"
Artem Panchenkod79430f2017-02-01 00:34:21 +0200380 // need this to reset glide.lock changes (vendor dir is already compiled)
381 // otherwise binaries will be versioned with '-dirty' suffix
382 sh "git checkout ."
Artem Panchenkobc13d262017-01-20 12:40:58 +0200383 }
384}
385
386
387/**
388 * Test Felix stage
389 *
390 * Usage example:
391 *
392 * def calico = new com.mirantis.mcp.Calico()
393 * calico.testFelix()
394 *
395 */
396def testFelix() {
397 stage ('Run felix unittests'){
398 // inject COMPARE_BRANCH variable for felix tests coverage (python code) check
399 def COMPARE_BRANCH = env.GERRIT_BRANCH ? "gerrit/${env.GERRIT_BRANCH}" : "origin/mcp"
400 sh "make ut UT_COMPARE_BRANCH=${COMPARE_BRANCH}"
401 }
402}
403
404
405/**
406 * Build calico/felix image stage
407 *
408 * @param config LinkedHashMap
409 * config includes next parameters:
410 * - dockerRegistry String, Docker registry host to push image to (optional)
411 * - projectNamespace String, artifactory server namespace (optional)
412 * - felixImage String, calico/felix image name (optional)
413 * - felixImageTag String, tag of docker image (optional)
414 *
415 * Usage example:
416 *
417 * def calicoFunc = new com.mirantis.mcp.Calico()
418 * calicoFunc.buildFelix([
419 * dockerRegistry : 'sandbox-docker-dev-virtual.docker.mirantis.net',
420 * ])
421 *
422 */
423def buildFelix(LinkedHashMap config) {
424
425 def common = new com.mirantis.mcp.Common()
426 def docker = new com.mirantis.mcp.Docker()
427 def git = new com.mirantis.mcp.Git()
428
429 def dockerRegistry = config.get('dockerRegistry')
430 def projectNamespace = config.get('projectNamespace', 'mirantis/projectcalico')
431
432 def felixImage = config.get('felixImage', "calico/felix")
433 def felixImageTag = config.get('felixImageTag', git.getGitDescribe(true) + "-" + common.getDatetime())
434
435 def felixContainerName = dockerRegistry ? "${dockerRegistry}/${projectNamespace}/${felixImage}:${felixImageTag}" : "${felixImage}:${felixImageTag}"
436
437 stage ('Build calico/felix image') {
438 docker.setDockerfileLabels("./Dockerfile", ["docker.imgTag=${felixImageTag}"])
439 sh """
440 make calico/felix
441 docker tag calico/felix ${felixContainerName}
442 """
443 }
444 return [felixImage : felixImage,
445 felixImageTag : felixImageTag]
446}
447
448/**
449 * Test Calicoctl stage
450 *
451 * Usage example:
452 *
453 * def calico = new com.mirantis.mcp.Calico()
454 * calico.testCalicoctl()
455 *
456 */
457def testCalicoctl() {
458 stage ('Run calicoctl unittests'){
459 sh "make test-containerized"
460 }
461}
462
463
464/**
465 * Build Calico containers stages
466 *
467 * @param config LinkedHashMap
468 * config includes next parameters:
469 * - dockerRegistry String, repo with docker images
470 * - projectNamespace String, artifactory server namespace
471 * - artifactoryURL String, URL to repo with calico-binaries
Denis Egorenko8c606552016-12-07 14:22:50 +0400472 * - imageTag String, tag of images
473 * - nodeImage String, Calico Node image name
474 * - ctlImage String, Calico CTL image name
475 * - buildImage String, Calico Build image name
476 * - felixImage String, Calico Felix image name
477 * - confdBuildId String, Version of Calico Confd
478 * - confdUrl String, URL to Calico Confd
479 * - birdUrl, URL to Calico Bird
480 * - birdBuildId, Version of Calico Bird
481 * - bird6Url, URL to Calico Bird6
482 * - birdclUrl, URL to Calico BirdCL
483 *
484 * Usage example:
485 *
486 * def calicoFunc = new com.mirantis.mcp.Calico()
Artem Panchenkobc13d262017-01-20 12:40:58 +0200487 * calicoFunc.buildCalicoContainers([
488 * dockerRegistry : 'sandbox-docker-dev-virtual.docker.mirantis.net',
489 * artifactoryURL : 'https://artifactory.mcp.mirantis.net/artifactory/sandbox',
490 * ])
Denis Egorenko8c606552016-12-07 14:22:50 +0400491 *
492 */
Artem Panchenkobc13d262017-01-20 12:40:58 +0200493def buildCalicoContainers(LinkedHashMap config) {
Denis Egorenko8c606552016-12-07 14:22:50 +0400494
Artem Panchenkobc13d262017-01-20 12:40:58 +0200495 def common = new com.mirantis.mcp.Common()
496 def docker = new com.mirantis.mcp.Docker()
497 def git = new com.mirantis.mcp.Git()
Denis Egorenko8c606552016-12-07 14:22:50 +0400498
Artem Panchenkobc13d262017-01-20 12:40:58 +0200499 def dockerRegistry = config.get('dockerRegistry')
500 def projectNamespace = config.get('projectNamespace', 'mirantis/projectcalico')
501 def artifactoryURL = config.get('artifactoryURL')
Denis Egorenko8c606552016-12-07 14:22:50 +0400502
Artem Panchenkobc13d262017-01-20 12:40:58 +0200503 if (! dockerRegistry ) {
504 error('dockerRegistry parameter has to be set.')
Denis Egorenko8c606552016-12-07 14:22:50 +0400505 }
506
Artem Panchenkobc13d262017-01-20 12:40:58 +0200507 if (! artifactoryURL ) {
508 error('artifactoryURL parameter has to be set.')
Denis Egorenko8c606552016-12-07 14:22:50 +0400509 }
510
Artem Panchenkobc13d262017-01-20 12:40:58 +0200511 def imgTag = config.get('imageTag', git.getGitDescribe(true) + "-" + common.getDatetime())
Denis Egorenko8c606552016-12-07 14:22:50 +0400512
Artem Panchenkobc13d262017-01-20 12:40:58 +0200513 def nodeImage = config.get('nodeImage', "calico/node")
514 def nodeRepo = "${dockerRegistry}/${projectNamespace}/${nodeImage}"
515 def nodeName = "${nodeRepo}:${imgTag}"
Denis Egorenko8c606552016-12-07 14:22:50 +0400516
Artem Panchenkobc13d262017-01-20 12:40:58 +0200517 def ctlImage = config.get('ctlImage', "calico/ctl")
518 def ctlRepo = "${dockerRegistry}/${projectNamespace}/${ctlImage}"
519 def ctlName = "${ctlRepo}:${imgTag}"
Denis Egorenko8c606552016-12-07 14:22:50 +0400520
521 // calico/build goes from libcalico
Artem Panchenkobc13d262017-01-20 12:40:58 +0200522 def buildImage = config.get('buildImage',"${dockerRegistry}/${projectNamespace}/calico/build:latest")
Denis Egorenko8c606552016-12-07 14:22:50 +0400523 // calico/felix goes from felix
Artem Panchenkobc13d262017-01-20 12:40:58 +0200524 def felixImage = config.get('felixImage', "${dockerRegistry}/${projectNamespace}/calico/felix:latest")
Denis Egorenko8c606552016-12-07 14:22:50 +0400525
Artem Panchenkobc13d262017-01-20 12:40:58 +0200526 def confdBuildId = config.get('confdBuildId', "${artifactoryURL}/${projectNamespace}/confd/latest".toURL().text.trim())
527 def confdUrl = config.get('confdUrl', "${artifactoryURL}/${projectNamespace}/confd/confd-${confdBuildId}")
Denis Egorenko8c606552016-12-07 14:22:50 +0400528
Artem Panchenkobc13d262017-01-20 12:40:58 +0200529 def birdBuildId = config.get('birdBuildId', "${artifactoryURL}/${projectNamespace}/bird/latest".toURL().text.trim())
530 def birdUrl = config.get('birdUrl', "${artifactoryURL}/${projectNamespace}/bird/bird-${birdBuildId}")
531 def bird6Url = config.get('bird6Url', "${artifactoryURL}/${projectNamespace}/bird/bird6-${birdBuildId}")
532 def birdclUrl = config.get('birdclUrl', "${artifactoryURL}/${projectNamespace}/bird/birdcl-${birdBuildId}")
Denis Egorenko8c606552016-12-07 14:22:50 +0400533
534 // add LABELs to dockerfiles
Denis Egorenko2bc89172016-12-21 17:31:19 +0400535 docker.setDockerfileLabels("./calicoctl/Dockerfile.calicoctl",
536 ["docker.imgTag=${imgTag}",
537 "calico.buildImage=${buildImage}",
538 "calico.birdclUrl=${birdclUrl}"])
Denis Egorenko8c606552016-12-07 14:22:50 +0400539
Denis Egorenko2bc89172016-12-21 17:31:19 +0400540 docker.setDockerfileLabels("./calico_node/Dockerfile",
541 ["docker.imgTag=${imgTag}",
542 "calico.buildImage=${buildImage}",
543 "calico.felixImage=${felixImage}",
544 "calico.confdUrl=${confdUrl}",
545 "calico.birdUrl=${birdUrl}",
546 "calico.bird6Url=${bird6Url}",
547 "calico.birdclUrl=${birdclUrl}"])
Denis Egorenko8c606552016-12-07 14:22:50 +0400548
549 // Start build section
550 stage ('Build calico/ctl image'){
551 sh """
552 make calico/ctl \
553 CTL_CONTAINER_NAME=${ctlName} \
554 PYTHON_BUILD_CONTAINER_NAME=${buildImage} \
555 BIRDCL_URL=${birdclUrl}
556 """
557 }
558
559
560 stage('Build calico/node'){
561 sh """
562 make calico/node \
563 NODE_CONTAINER_NAME=${nodeName} \
564 PYTHON_BUILD_CONTAINER_NAME=${buildImage} \
565 FELIX_CONTAINER_NAME=${felixImage} \
566 CONFD_URL=${confdUrl} \
567 BIRD_URL=${birdUrl} \
568 BIRD6_URL=${bird6Url} \
569 BIRDCL_URL=${birdclUrl}
570 """
571 }
572
573
574 return [
Artem Panchenkobc13d262017-01-20 12:40:58 +0200575 CTL_CONTAINER_NAME:"${ctlImage}",
576 NODE_CONTAINER_NAME:"${nodeImage}",
577 CALICO_NODE_IMAGE_REPO:"${nodeRepo}",
578 CALICOCTL_IMAGE_REPO:"${ctlRepo}",
Denis Egorenko8c606552016-12-07 14:22:50 +0400579 CALICO_VERSION: "${imgTag}"
580 ]
581
582}
Artem Panchenkobc13d262017-01-20 12:40:58 +0200583
584
585/**
586 * Test Calico CNI plugin stage
587 *
588 * Usage example:
589 *
590 * def calico = new com.mirantis.mcp.Calico()
591 * calico.testCniPlugin()
592 *
593 */
594def testCniPlugin() {
595 stage ('Run cni-plugin unittests'){
596 // 'static-checks-containerized' target is removed from master
597 // and kept here only for backward compatibility
598 sh "make static-checks || make static-checks-containerized"
599 sh "make stop-etcd stop-kubernetes-master"
600 // 'stop-k8s-apiserver' target doesn't exist in Calico v2.0.0,
601 // so do not fail the stage if it's not found
602 sh "make stop-k8s-apiserver || true"
603 sh "make test-containerized"
604 }
605}
606
607
608/**
609 * Build calico/cni image stage
610 *
611 * @param config LinkedHashMap
612 * config includes next parameters:
613 * - dockerRegistry String, Docker registry host to push image to (optional)
614 * - projectNamespace String, artifactory server namespace (optional)
615 * - cniImage String, calico/cni image name (optional)
616 * - cniImageTag String, tag of docker image (optional)
617 *
618 * Usage example:
619 *
620 * def calicoFunc = new com.mirantis.mcp.Calico()
621 * calicoFunc.buildFelix([
622 * dockerRegistry : 'sandbox-docker-dev-virtual.docker.mirantis.net',
623 * ])
624 *
625 */
626def buildCniPlugin(LinkedHashMap config) {
627
628 def common = new com.mirantis.mcp.Common()
629 def docker = new com.mirantis.mcp.Docker()
630 def git = new com.mirantis.mcp.Git()
631
632 def dockerRegistry = config.get('dockerRegistry')
633 def projectNamespace = config.get('projectNamespace', 'mirantis/projectcalico')
634
635 def cniImage = config.get('cniImage', "calico/cni")
636 def cniImageTag = config.get('cniImageTag', git.getGitDescribe(true) + "-" + common.getDatetime())
637
638 def cniContainerName = dockerRegistry ? "${dockerRegistry}/${projectNamespace}/${cniImage}:${cniImageTag}" : "${cniImage}:${cniImageTag}"
639
640 stage ('Build calico/cni image') {
641 docker.setDockerfileLabels("./Dockerfile", ["docker.imgTag=${cniImageTag}"])
642 sh """
643 make docker-image
644 docker tag calico/cni ${cniContainerName}
645 """
646 }
647 return [cniImage : cniImage,
648 cniImageTag : cniImageTag]
649}
650
651
652/**
653 * Publish calico docker image stage
654 *
655 * @param config LinkedHashMap
656 * config includes next parameters:
657 * - artifactoryServerName String, artifactory server name
658 * - dockerRegistry String, Docker registry host to push image to
Sergey Kulanova9e65042017-01-31 14:20:24 +0200659 * - dockerRepo String, repository (artifactory) for docker images, must not be Virtual
Artem Panchenkobc13d262017-01-20 12:40:58 +0200660 * - imageName String, Docker image name
661 * - imageTag String, Docker image tag
662 * - projectNamespace String, artifactory server namespace (optional)
663 * - publishInfo Boolean, whether publish a build-info object to Artifactory (optional)
664 *
665 * Usage example:
666 *
667 * def calico = new com.mirantis.mcp.Calico()
668 * calico.publishCalicoImage([
669 * artifactoryServerName : 'mcp-ci',
670 * dockerRegistry : 'sandbox-docker-dev-local.docker.mirantis.net'
671 * dockerRepo : 'sandbox-docker-dev-local',
672 * imageName : 'calico/node',
673 * imageTag : 'v.1.0.0',
674 * ])
675 *
676 */
Sergey Kulanova9e65042017-01-31 14:20:24 +0200677def publishCalicoImage(LinkedHashMap config) {
Artem Panchenkobc13d262017-01-20 12:40:58 +0200678 def artifactory = new com.mirantis.mcp.MCPArtifactory()
679
680 def artifactoryServerName = config.get('artifactoryServerName')
681 def dockerRegistry = config.get('dockerRegistry')
682 def dockerRepo = config.get('dockerRepo')
683 def imageName = config.get('imageName')
684 def imageTag = config.get('imageTag')
685 def projectNamespace = config.get('projectNamespace', 'mirantis/projectcalico')
686 def publishInfo = config.get('publishInfo', true)
687
688 if (!artifactoryServerName) {
689 throw new RuntimeException("Parameter 'artifactoryServerName' must be set for publishCalicoImage() !")
690 }
691 if (!dockerRegistry) {
692 throw new RuntimeException("Parameter 'dockerRegistry' must be set for publishCalicoImage() !")
693 }
694 if (!dockerRepo) {
Sergey Kulanova9e65042017-01-31 14:20:24 +0200695 throw new RuntimeException("Parameter 'dockerRepo' must be set for publishCalicoImage() !")
Artem Panchenkobc13d262017-01-20 12:40:58 +0200696 }
697 if (!imageName) {
698 throw new RuntimeException("Parameter 'imageName' must be set for publishCalicoImage() !")
699 }
700 if (!imageTag) {
701 throw new RuntimeException("Parameter 'imageTag' must be set for publishCalicoImage() !")
702 }
703
704 def artifactoryServer = Artifactory.server(artifactoryServerName)
705 def buildInfo = publishInfo ? Artifactory.newBuildInfo() : null
706
707 stage("Publishing ${imageName}") {
708 artifactory.uploadImageToArtifactory(artifactoryServer,
709 dockerRegistry,
710 "${projectNamespace}/${imageName}",
711 imageTag,
712 dockerRepo,
713 buildInfo)
714 }
715 return "${dockerRegistry}/${projectNamespace}/${imageName}:${imageTag}"
716}
717
718
719/**
720 * Promote calico docker image stage
721 *
722 * @param config LinkedHashMap
723 * config includes next parameters:
724 * - imageProperties Map, docker image search properties in artifactory
725 * - artifactoryServerName String, artifactory server name
726 * - dockerLookupRepo String, docker repository (artifactory) to take image from
727 * - dockerPromoteRepo String, docker repository (artifactory) to promote image to
728 * - imageName String, Docker image name to promote with
729 * - imageTag String, Docker image tag to promote with
730 * - projectNamespace String, artifactory server namespace (optional)
731 * - defineLatest Boolean, promote with latest tag if true, default false (optional)
732 *
733 * Usage example:
734 *
735 * def calico = new com.mirantis.mcp.Calico()
736 * calico.promoteCalicoImage([
737 * imageProperties: [
738 * 'com.mirantis.targetImg': 'mirantis/projectcalico/calico/node',
739 * 'com.mirantis.targetTag': 'v1.0.0-2017010100000',
740 * ]
741 * artifactoryServerName : 'mcp-ci',
742 * dockerLookupRepo : 'sandbox-docker-dev-local',
743 * dockerPromoteRepo: 'sandbox-docker-prod-local',
744 * imageName: 'calico/node',
745 * imageTag: 'v1.0.0',
746 * defineLatest: true
747 * ])
748 *
749 */
750def promoteCalicoImage (LinkedHashMap config) {
751 def common = new com.mirantis.mcp.Common()
752 def git = new com.mirantis.mcp.Git()
753 def artifactory = new com.mirantis.mcp.MCPArtifactory()
754
755 def imageProperties = config.get('imageProperties')
756 def artifactoryServerName = config.get('artifactoryServerName')
757 def dockerLookupRepo = config.get('dockerLookupRepo')
758 def dockerPromoteRepo = config.get('dockerPromoteRepo')
759 def imageName = config.get('imageName')
760 def imageTag = config.get('imageTag')
761 def projectNamespace = config.get('projectNamespace', 'mirantis/projectcalico')
762 def defineLatest = config.get('defineLatest', false)
763
764if (!imageProperties) {
765 throw new RuntimeException("Parameter 'imageProperties' must be set for promoteCalicoImage() !")
766 }
767 if (!artifactoryServerName) {
768 throw new RuntimeException("Parameter 'artifactoryServerName' must be set for promoteCalicoImage() !")
769 }
770 if (!dockerLookupRepo) {
771 throw new RuntimeException("Parameter 'dockerLookupRepo' must be set for promoteCalicoImage() !")
772 }
773 if (!dockerPromoteRepo) {
774 throw new RuntimeException("Parameter 'dockerPromoteRepo' must be set for promoteCalicoImage() !")
775 }
776 if (!imageName) {
777 throw new RuntimeException("Parameter 'imageName' must be set for promoteCalicoImage() !")
778 }
779 if (!imageTag) {
780 throw new RuntimeException("Parameter 'imageTag' must be set for promoteCalicoImage() !")
781 }
782
783 def artifactoryServer = Artifactory.server(artifactoryServerName)
784 def artifactURI = artifactory.uriByProperties(artifactoryServer.getUrl(), imageProperties)
785
786 stage("Promote ${imageName}") {
787 if ( artifactURI ) {
788 def buildProperties = artifactory.getPropertiesForArtifact(artifactURI)
789 if (defineLatest) {
790 artifactory.promoteDockerArtifact(
791 artifactoryServer.getUrl(),
792 dockerLookupRepo,
793 dockerPromoteRepo,
794 "${projectNamespace}/${imageName}",
795 buildProperties.get('com.mirantis.targetTag').join(','),
796 'latest',
797 true
798 )
799 }
800 artifactory.promoteDockerArtifact(
801 artifactoryServer.getUrl(),
802 dockerLookupRepo,
803 dockerPromoteRepo,
804 "${projectNamespace}/${imageName}",
805 buildProperties.get('com.mirantis.targetTag').join(','),
806 "${imageTag}",
807 false
808 )
809 }
810 else {
811 throw new RuntimeException("Artifacts were not found, nothing to promote! "
812 +"Given image properties: ${imageProperties}")
813 }
814 }
815}
816
817
818def calicoFixOwnership() {
819 // files created inside container could be owned by root, fixing that
820 sh "sudo chown -R \$(id -u):\$(id -g) ${env.WORKSPACE} ${env.HOME}/.glide || true"
821}