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