Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 1 | package com.mirantis.mcp |
| 2 | |
Sergey Reshetnyak | 70b1fe6 | 2017-01-31 22:27:06 +0300 | [diff] [blame] | 3 | @Grab(group='org.yaml', module='snakeyaml', version='1.17') |
| 4 | import org.yaml.snakeyaml.Yaml |
| 5 | |
Ruslan Kamaldinov | 90d4e67 | 2016-11-11 18:31:00 +0300 | [diff] [blame] | 6 | /** |
| 7 | * https://issues.jenkins-ci.org/browse/JENKINS-26481 |
| 8 | * fix groovy List.collect() |
| 9 | **/ |
| 10 | @NonCPS |
| 11 | def constructString(ArrayList options, String keyOption, String separator = " ") { |
| 12 | return options.collect { keyOption + it }.join(separator).replaceAll("\n", "") |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Generate current timestamp |
| 17 | * |
| 18 | * @param format Defaults to yyyyMMddHHmmss |
| 19 | */ |
| 20 | def getDatetime(format = "yyyyMMddHHmmss") { |
| 21 | def now = new Date(); |
| 22 | return now.format(format, TimeZone.getTimeZone('UTC')); |
Denis Egorenko | 8c60655 | 2016-12-07 14:22:50 +0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Run tox with or without specified environment |
| 27 | * @param env String, name of environment |
| 28 | */ |
| 29 | def runTox(String env = null) { |
| 30 | if (env) { |
| 31 | sh "tox -v -e ${env}" |
| 32 | } else { |
| 33 | sh "tox -v" |
| 34 | } |
| 35 | } |
Sergey Kulanov | e897d8f | 2017-01-23 16:44:03 +0200 | [diff] [blame] | 36 | |
| 37 | /** |
Sergey Reshetnyak | 70b1fe6 | 2017-01-31 22:27:06 +0300 | [diff] [blame] | 38 | * Convert YAML document to Map object |
| 39 | * @param data YAML string |
| 40 | */ |
| 41 | @NonCPS |
| 42 | def loadYAML(String data) { |
| 43 | def yaml = new Yaml() |
| 44 | return yaml.load(data) |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Convert Map object to YAML string |
| 49 | * @param map Map object |
| 50 | */ |
| 51 | @NonCPS |
| 52 | def dumpYAML(Map map) { |
| 53 | def yaml = new Yaml() |
| 54 | return yaml.dump(map) |
| 55 | } |
| 56 | |
| 57 | /** |
Sergey Kulanov | 21c936c | 2017-02-02 13:43:14 +0200 | [diff] [blame] | 58 | * Render jinja template |
| 59 | * @param templateVars String, A dict, a dict subclass, json or some keyword arguments |
| 60 | * @param templateFile String, jinja template file path |
| 61 | * @param resultFile String, result/generate file path |
| 62 | * |
| 63 | * Usage example: |
| 64 | * |
| 65 | * def common = new com.mirantis.mcp.Common() |
| 66 | * common.renderJinjaTemplate( |
| 67 | * "${NODE_JSON}", |
| 68 | * "${WORKSPACE}/inventory/inventory.cfg", |
| 69 | * "${WORKSPACE}/inventory/inventory.cfg") |
| 70 | * where NODE_JSON= data in json format |
| 71 | */ |
| 72 | def renderJinjaTemplate(String templateVars, String templateFile, String resultFile) { |
| 73 | |
| 74 | sh """ |
| 75 | python -c " |
| 76 | import sys |
| 77 | import jinja2 |
| 78 | from jinja2 import Template |
| 79 | |
| 80 | # Useful for very coarse version differentiation. |
| 81 | PY2 = sys.version_info[0] == 2 |
| 82 | PY3 = sys.version_info[0] == 3 |
| 83 | PY34 = sys.version_info[0:2] >= (3, 4) |
| 84 | |
| 85 | if PY3: |
| 86 | string_types = str, |
| 87 | else: |
| 88 | string_types = basestring |
| 89 | |
| 90 | |
| 91 | def to_bool(a): |
| 92 | ''' return a bool for the arg ''' |
| 93 | if a is None or type(a) == bool: |
| 94 | return a |
| 95 | if isinstance(a, string_types): |
| 96 | a = a.lower() |
| 97 | if a in ['yes', 'on', '1', 'true', 1]: |
| 98 | return True |
| 99 | else: |
| 100 | return False |
| 101 | |
| 102 | |
| 103 | def generate(templateVars, templateFile, resultFile): |
| 104 | templateLoader = jinja2.FileSystemLoader(searchpath='/') |
| 105 | templateEnv = jinja2.Environment(loader=templateLoader) |
| 106 | templateEnv.filters['bool'] = to_bool |
| 107 | template = templateEnv.get_template(templateFile) |
| 108 | outputText = template.render(templateVars) |
| 109 | Template(outputText).stream().dump(resultFile) |
| 110 | |
| 111 | generate(${templateVars}, '${templateFile}', '${resultFile}') |
| 112 | " |
| 113 | cat ${resultFile} |
| 114 | """ |
| 115 | } |
| 116 | |
| 117 | /** |
Sergey Kulanov | e897d8f | 2017-01-23 16:44:03 +0200 | [diff] [blame] | 118 | * Run function on k8s cluster |
| 119 | * |
| 120 | * @param config LinkedHashMap |
| 121 | * config includes next parameters: |
| 122 | * - label, pod label |
| 123 | * - function, code that should be run on k8s cluster |
| 124 | * - jnlpImg, jnlp slave image |
| 125 | * - slaveImg, slave image |
| 126 | * |
| 127 | * Usage example: |
| 128 | * |
| 129 | * def runFunc = new com.mirantis.mcp.Common() |
| 130 | * runFunc.runOnKubernetes ([ |
| 131 | * function : this.&buildCalicoContainers, |
| 132 | * jnlpImg: 'docker-prod-virtual.docker.mirantis.net/mirantis/jenkins-slave-images/jnlp-slave:latest', |
| 133 | * slaveImg : 'sandbox-docker-dev-local.docker.mirantis.net/skulanov/jenkins-slave-images/calico-slave:1' |
| 134 | * ]) |
| 135 | * // promotion example. In case of promotion we need only jnlp container |
| 136 | * def runFunc = new com.mirantis.mcp.Common() |
| 137 | * runFunc.runOnKubernetes ([ |
| 138 | * jnlpImg: 'docker-prod-virtual.docker.mirantis.net/mirantis/jenkins-slave-images/jnlp-slave:latest', |
| 139 | * function : this.&promote_artifacts |
| 140 | * ]) |
| 141 | */ |
| 142 | def runOnKubernetes(LinkedHashMap config) { |
| 143 | |
| 144 | |
| 145 | def jenkinsSlaveImg = config.get('slaveImg', 'none') |
| 146 | def jnlpSlaveImg = config.get('jnlpImg', 'none') |
| 147 | def lbl = config.get('label', "buildpod.${env.JOB_NAME}.${env.BUILD_NUMBER}".replace('-', '_').replace('/', '_')) |
| 148 | def toRun = config.get('function', 'none') |
| 149 | |
Sergey Kulanov | b1aa0ff | 2017-01-23 17:48:44 +0200 | [diff] [blame] | 150 | if (jnlpSlaveImg == 'none') { |
Sergey Kulanov | e897d8f | 2017-01-23 16:44:03 +0200 | [diff] [blame] | 151 | error('jnlp Slave image MUST be defined') |
| 152 | } |
| 153 | |
| 154 | if (toRun == 'none'){ |
| 155 | error('Code that should be run on k8s MUST be passed along with function parameter') |
| 156 | } |
| 157 | |
| 158 | if (jenkinsSlaveImg == 'none'){ |
| 159 | // we are running jnlp container only, since no jenkinsSlaveImg is specified, so |
| 160 | // we are in promotion mode |
| 161 | podTemplate(label: lbl, |
| 162 | containers: [ |
| 163 | containerTemplate( |
| 164 | name: 'jnlp', |
| 165 | image: jnlpSlaveImg, |
| 166 | args: '${computer.jnlpmac} ${computer.name}' |
| 167 | ) |
| 168 | ], |
| 169 | ) { |
| 170 | node(lbl){ |
| 171 | container('jnlp') { |
| 172 | toRun() |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | } else { |
| 178 | podTemplate(label: lbl, |
| 179 | containers: [ |
| 180 | containerTemplate( |
| 181 | name: 'jnlp', |
| 182 | image: jnlpSlaveImg, |
| 183 | args: '${computer.jnlpmac} ${computer.name}' |
| 184 | ), |
| 185 | containerTemplate( |
| 186 | name: 'k8s-slave', |
| 187 | image: jenkinsSlaveImg, |
| 188 | alwaysPullImage: false, |
| 189 | ttyEnabled: true, |
| 190 | privileged: true |
| 191 | ) |
| 192 | ], |
| 193 | ) { |
| 194 | node(lbl){ |
| 195 | container('k8s-slave') { |
Sergey Kulanov | b36e36e | 2017-01-23 18:26:40 +0200 | [diff] [blame] | 196 | return toRun() |
Sergey Kulanov | e897d8f | 2017-01-23 16:44:03 +0200 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | } //else |
| 201 | |
| 202 | } |