blob: d6a34c4db9aefa6d864290df65087b5259ac98c4 [file] [log] [blame]
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +03001package com.mirantis.mcp
2
Sergey Reshetnyak70b1fe62017-01-31 22:27:06 +03003@Grab(group='org.yaml', module='snakeyaml', version='1.17')
4import org.yaml.snakeyaml.Yaml
5
Ruslan Kamaldinov90d4e672016-11-11 18:31:00 +03006/**
7 * https://issues.jenkins-ci.org/browse/JENKINS-26481
8 * fix groovy List.collect()
9 **/
10@NonCPS
11def 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 */
20def getDatetime(format = "yyyyMMddHHmmss") {
21 def now = new Date();
22 return now.format(format, TimeZone.getTimeZone('UTC'));
Denis Egorenko8c606552016-12-07 14:22:50 +040023}
24
25/**
26 * Run tox with or without specified environment
27 * @param env String, name of environment
28 */
29def runTox(String env = null) {
30 if (env) {
31 sh "tox -v -e ${env}"
32 } else {
33 sh "tox -v"
34 }
35}
Sergey Kulanove897d8f2017-01-23 16:44:03 +020036
37/**
Sergey Reshetnyak70b1fe62017-01-31 22:27:06 +030038 * Convert YAML document to Map object
39 * @param data YAML string
40 */
41@NonCPS
42def 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
52def dumpYAML(Map map) {
53 def yaml = new Yaml()
54 return yaml.dump(map)
55}
56
57/**
Sergey Kulanov21c936c2017-02-02 13:43:14 +020058 * 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 */
72def renderJinjaTemplate(String templateVars, String templateFile, String resultFile) {
73
74 sh """
75 python -c "
76import sys
77import jinja2
78from jinja2 import Template
79
80# Useful for very coarse version differentiation.
81PY2 = sys.version_info[0] == 2
82PY3 = sys.version_info[0] == 3
83PY34 = sys.version_info[0:2] >= (3, 4)
84
85if PY3:
86 string_types = str,
87else:
88 string_types = basestring
89
90
91def 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
103def 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
111generate(${templateVars}, '${templateFile}', '${resultFile}')
112"
113 cat ${resultFile}
114 """
115}
116
117/**
Sergey Kulanove897d8f2017-01-23 16:44:03 +0200118 * 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 */
142def 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 Kulanovb1aa0ff2017-01-23 17:48:44 +0200150 if (jnlpSlaveImg == 'none') {
Sergey Kulanove897d8f2017-01-23 16:44:03 +0200151 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 Kulanovb36e36e2017-01-23 18:26:40 +0200196 return toRun()
Sergey Kulanove897d8f2017-01-23 16:44:03 +0200197 }
198 }
199 }
200 } //else
201
202}