Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 1 | package com.mirantis.mcp |
| 2 | |
| 3 | /** |
| 4 | * https://issues.jenkins-ci.org/browse/JENKINS-26481 |
| 5 | * fix groovy List.collect() |
| 6 | **/ |
| 7 | @NonCPS |
| 8 | def constructString(ArrayList options, String keyOption, String separator = " ") { |
| 9 | return options.collect { keyOption + it }.join(separator).replaceAll("\n", "") |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Generate current timestamp |
| 14 | * |
| 15 | * @param format Defaults to yyyyMMddHHmmss |
| 16 | */ |
| 17 | def getDatetime(format = "yyyyMMddHHmmss") { |
| 18 | def now = new Date(); |
| 19 | return now.format(format, TimeZone.getTimeZone('UTC')); |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Run tox with or without specified environment |
| 24 | * @param env String, name of environment |
| 25 | */ |
| 26 | def runTox(String env = null) { |
| 27 | if (env) { |
| 28 | sh "tox -v -e ${env}" |
| 29 | } else { |
| 30 | sh "tox -v" |
| 31 | } |
| 32 | } |
Sergey Kulanov | e897d8f | 2017-01-23 16:44:03 +0200 | [diff] [blame^] | 33 | |
| 34 | /** |
| 35 | * Run function on k8s cluster |
| 36 | * |
| 37 | * @param config LinkedHashMap |
| 38 | * config includes next parameters: |
| 39 | * - label, pod label |
| 40 | * - function, code that should be run on k8s cluster |
| 41 | * - jnlpImg, jnlp slave image |
| 42 | * - slaveImg, slave image |
| 43 | * |
| 44 | * Usage example: |
| 45 | * |
| 46 | * def runFunc = new com.mirantis.mcp.Common() |
| 47 | * runFunc.runOnKubernetes ([ |
| 48 | * function : this.&buildCalicoContainers, |
| 49 | * jnlpImg: 'docker-prod-virtual.docker.mirantis.net/mirantis/jenkins-slave-images/jnlp-slave:latest', |
| 50 | * slaveImg : 'sandbox-docker-dev-local.docker.mirantis.net/skulanov/jenkins-slave-images/calico-slave:1' |
| 51 | * ]) |
| 52 | * // promotion example. In case of promotion we need only jnlp container |
| 53 | * def runFunc = new com.mirantis.mcp.Common() |
| 54 | * runFunc.runOnKubernetes ([ |
| 55 | * jnlpImg: 'docker-prod-virtual.docker.mirantis.net/mirantis/jenkins-slave-images/jnlp-slave:latest', |
| 56 | * function : this.&promote_artifacts |
| 57 | * ]) |
| 58 | */ |
| 59 | def runOnKubernetes(LinkedHashMap config) { |
| 60 | |
| 61 | |
| 62 | def jenkinsSlaveImg = config.get('slaveImg', 'none') |
| 63 | def jnlpSlaveImg = config.get('jnlpImg', 'none') |
| 64 | def lbl = config.get('label', "buildpod.${env.JOB_NAME}.${env.BUILD_NUMBER}".replace('-', '_').replace('/', '_')) |
| 65 | def toRun = config.get('function', 'none') |
| 66 | |
| 67 | id (jnlpSlaveImg == 'none') { |
| 68 | error('jnlp Slave image MUST be defined') |
| 69 | } |
| 70 | |
| 71 | if (toRun == 'none'){ |
| 72 | error('Code that should be run on k8s MUST be passed along with function parameter') |
| 73 | } |
| 74 | |
| 75 | if (jenkinsSlaveImg == 'none'){ |
| 76 | // we are running jnlp container only, since no jenkinsSlaveImg is specified, so |
| 77 | // we are in promotion mode |
| 78 | podTemplate(label: lbl, |
| 79 | containers: [ |
| 80 | containerTemplate( |
| 81 | name: 'jnlp', |
| 82 | image: jnlpSlaveImg, |
| 83 | args: '${computer.jnlpmac} ${computer.name}' |
| 84 | ) |
| 85 | ], |
| 86 | ) { |
| 87 | node(lbl){ |
| 88 | container('jnlp') { |
| 89 | toRun() |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } else { |
| 95 | podTemplate(label: lbl, |
| 96 | containers: [ |
| 97 | containerTemplate( |
| 98 | name: 'jnlp', |
| 99 | image: jnlpSlaveImg, |
| 100 | args: '${computer.jnlpmac} ${computer.name}' |
| 101 | ), |
| 102 | containerTemplate( |
| 103 | name: 'k8s-slave', |
| 104 | image: jenkinsSlaveImg, |
| 105 | alwaysPullImage: false, |
| 106 | ttyEnabled: true, |
| 107 | privileged: true |
| 108 | ) |
| 109 | ], |
| 110 | ) { |
| 111 | node(lbl){ |
| 112 | container('k8s-slave') { |
| 113 | toRun() |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } //else |
| 118 | |
| 119 | } |