blob: b31f40d56696f49657a5aa96888fbebb7b7bf577 [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package com.mirantis.mk
2
Jakub Josefb41c8d52017-03-24 13:52:24 +01003import static groovy.json.JsonOutput.prettyPrint
4import static groovy.json.JsonOutput.toJson
Jakub Josefb7ab8472017-04-05 14:56:53 +02005import groovy.json.JsonSlurperClassic
Jakub Josef79ecec32017-02-17 14:36:28 +01006/**
7 *
8 * Common functions
9 *
10 */
11
12/**
13 * Generate current timestamp
14 *
15 * @param format Defaults to yyyyMMddHHmmss
16 */
17def getDatetime(format="yyyyMMddHHmmss") {
18 def now = new Date();
19 return now.format(format, TimeZone.getTimeZone('UTC'));
20}
21
22/**
Jakub Josef79ecec32017-02-17 14:36:28 +010023 * Return workspace.
24 * Currently implemented by calling pwd so it won't return relevant result in
25 * dir context
26 */
27def getWorkspace() {
28 def workspace = sh script: 'pwd', returnStdout: true
29 workspace = workspace.trim()
30 return workspace
31}
32
33/**
Filip Pytloun81c864d2017-03-21 15:19:30 +010034 * Get UID of jenkins user.
35 * Must be run from context of node
36 */
37def getJenkinsUid() {
38 return sh (
39 script: 'id -u',
40 returnStdout: true
41 ).trim()
42}
43
44/**
45 * Get GID of jenkins user.
46 * Must be run from context of node
47 */
48def getJenkinsGid() {
49 return sh (
50 script: 'id -g',
51 returnStdout: true
52 ).trim()
53}
54
55/**
Jakub Josef79ecec32017-02-17 14:36:28 +010056 * Get credentials from store
57 *
58 * @param id Credentials name
59 */
Jakub Josef3d9d9ab2017-03-14 15:09:03 +010060def getCredentials(id, cred_type = "username_password") {
61 def credClass;
62 if(cred_type == "username_password"){
63 credClass = com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class
64 }else if(cred_type == "key"){
65 credClass = com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.class
66 }
Jakub Josef79ecec32017-02-17 14:36:28 +010067 def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
Jakub Josef3d9d9ab2017-03-14 15:09:03 +010068 credClass,
Jakub Josef79ecec32017-02-17 14:36:28 +010069 jenkins.model.Jenkins.instance
70 )
71
72 for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
73 c = credsIter.next();
74 if ( c.id == id ) {
75 return c;
76 }
77 }
78
79 throw new Exception("Could not find credentials for ID ${id}")
80}
81
82/**
83 * Abort build, wait for some time and ensure we will terminate
84 */
85def abortBuild() {
86 currentBuild.build().doStop()
87 sleep(180)
88 // just to be sure we will terminate
89 throw new InterruptedException()
90}
91
92/**
Jakub Josefb41c8d52017-03-24 13:52:24 +010093 * Return pretty-printed string representation of given item
94 * @param item item to be pretty-printed (list, map, whatever)
95 * @return pretty-printed string
96 */
97def prettyPrint(item){
98 return prettyPrint(toJson(item)).replace('\\n', System.getProperty('line.separator'))
99}
100
101/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100102 * Print informational message
103 *
104 * @param msg
105 * @param color Colorful output or not
106 */
107def infoMsg(msg, color = true) {
108 printMsg(msg, "cyan")
109}
110
111/**
112 * Print error message
113 *
114 * @param msg
115 * @param color Colorful output or not
116 */
117def errorMsg(msg, color = true) {
118 printMsg(msg, "red")
119}
120
121/**
122 * Print success message
123 *
124 * @param msg
125 * @param color Colorful output or not
126 */
127def successMsg(msg, color = true) {
128 printMsg(msg, "green")
129}
130
131/**
132 * Print warning message
133 *
134 * @param msg
135 * @param color Colorful output or not
136 */
137def warningMsg(msg, color = true) {
Jakub Josef0e7bd632017-03-16 16:25:05 +0100138 printMsg(msg, "yellow")
Jakub Josef79ecec32017-02-17 14:36:28 +0100139}
140
141/**
Jakub Josef952ae0b2017-03-14 19:04:21 +0100142 * Print debug message, this message will show only if DEBUG global variable is present
143 * @param msg
144 * @param color Colorful output or not
145 */
146def debugMsg(msg, color = true){
Jakub Josef9a836ac2017-04-24 12:26:02 +0200147 // if debug property exists on env, debug is enabled
Jakub Josef66976f62017-04-24 16:32:23 +0200148 if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){
Jakub Josef74b34692017-03-15 12:10:57 +0100149 printMsg("[DEBUG] ${msg}", "red")
Jakub Josef952ae0b2017-03-14 19:04:21 +0100150 }
151}
152
153/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100154 * Print message
155 *
156 * @param msg Message to be printed
157 * @param level Level of message (default INFO)
158 * @param color Color to use for output or false (default)
159 */
160def printMsg(msg, color = false) {
161 colors = [
162 'red' : '\u001B[31m',
163 'black' : '\u001B[30m',
164 'green' : '\u001B[32m',
165 'yellow': '\u001B[33m',
166 'blue' : '\u001B[34m',
167 'purple': '\u001B[35m',
168 'cyan' : '\u001B[36m',
169 'white' : '\u001B[37m',
170 'reset' : '\u001B[0m'
171 ]
172 if (color != false) {
173 wrap([$class: 'AnsiColorBuildWrapper']) {
174 print "${colors[color]}${msg}${colors.reset}"
175 }
176 } else {
177 print "[${level}] ${msg}"
178 }
179}
180
181/**
182 * Traverse directory structure and return list of files
183 *
184 * @param path Path to search
185 * @param type Type of files to search (groovy.io.FileType.FILES)
186 */
187@NonCPS
188def getFiles(path, type=groovy.io.FileType.FILES) {
189 files = []
190 new File(path).eachFile(type) {
191 files[] = it
192 }
193 return files
194}
195
196/**
197 * Helper method to convert map into form of list of [key,value] to avoid
198 * unserializable exceptions
199 *
200 * @param m Map
201 */
202@NonCPS
203def entries(m) {
204 m.collect {k, v -> [k, v]}
205}
206
207/**
208 * Opposite of build-in parallel, run map of steps in serial
209 *
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200210 * @param steps Map of String<name>: CPSClosure2<step> (or list of closures)
Jakub Josef79ecec32017-02-17 14:36:28 +0100211 */
212def serial(steps) {
213 stepsArray = entries(steps)
214 for (i=0; i < stepsArray.size; i++) {
Jakub Josefd31de302017-05-15 13:59:18 +0200215 def step = stepsArray[i]
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200216 def dummySteps = [:]
217 def stepKey
Jakub Josefd31de302017-05-15 13:59:18 +0200218 if(step[1] instanceof Iterable){
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200219 for(j=0;j < step[1].size; j++){
220 if(step[1] instanceof List){
221 stepKey = j
222 }else if(step[1] instanceof Map){
223 stepKey = step[1].keySet()[j]
224 }
225 dummySteps.put("step-${step[0]}-${stepKey}",step[1][stepKey])
Jakub Josefd31de302017-05-15 13:59:18 +0200226 }
227 }else{
228 dummySteps.put(step[0], step[1])
229 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100230 parallel dummySteps
231 }
232}
233
234/**
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200235 * Partition given list to list of small lists
236 * @param inputList input list
237 * @param partitionSize (partition size, optional, default 5)
238 */
239def partitionList(inputList, partitionSize=5){
240 List<List<String>> partitions = new ArrayList<>();
241 for (int i=0; i<inputList.size(); i += partitionSize) {
242 partitions.add(new ArrayList<String>(inputList.subList(i, Math.min(i + partitionSize, inputList.size()))));
243 }
244 return partitions
245}
246
247/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100248 * Get password credentials from store
249 *
250 * @param id Credentials name
251 */
252def getPasswordCredentials(id) {
253 def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
254 com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
255 jenkins.model.Jenkins.instance
256 )
257
258 for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
259 c = credsIter.next();
260 if ( c.id == id ) {
261 return c;
262 }
263 }
264
265 throw new Exception("Could not find credentials for ID ${id}")
266}
267
268/**
269 * Get SSH credentials from store
270 *
271 * @param id Credentials name
272 */
273def getSshCredentials(id) {
274 def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
275 com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
276 jenkins.model.Jenkins.instance
277 )
278
279 for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
280 c = credsIter.next();
281 if ( c.id == id ) {
282 return c;
283 }
284 }
285
286 throw new Exception("Could not find credentials for ID ${id}")
287}
Jakub Josef79ecec32017-02-17 14:36:28 +0100288
289/**
290 * Tests Jenkins instance for existence of plugin with given name
291 * @param pluginName plugin short name to test
292 * @return boolean result
293 */
294@NonCPS
295def jenkinsHasPlugin(pluginName){
296 return Jenkins.instance.pluginManager.plugins.collect{p -> p.shortName}.contains(pluginName)
297}
298
299@NonCPS
300def _needNotification(notificatedTypes, buildStatus, jobName) {
301 if(notificatedTypes && notificatedTypes.contains("onchange")){
302 if(jobName){
303 def job = Jenkins.instance.getItem(jobName)
304 def numbuilds = job.builds.size()
305 if (numbuilds > 0){
306 //actual build is first for some reasons, so last finished build is second
307 def lastBuild = job.builds[1]
308 if(lastBuild){
309 if(lastBuild.result.toString().toLowerCase().equals(buildStatus)){
310 println("Build status didn't changed since last build, not sending notifications")
311 return false;
312 }
313 }
314 }
315 }
316 }else if(!notificatedTypes.contains(buildStatus)){
317 return false;
318 }
319 return true;
320}
321
322/**
323 * Send notification to all enabled notifications services
324 * @param buildStatus message type (success, warning, error), null means SUCCESSFUL
325 * @param msgText message text
326 * @param enabledNotifications list of enabled notification types, types: slack, hipchat, email, default empty
327 * @param notificatedTypes types of notifications will be sent, default onchange - notificate if current build result not equal last result;
328 * otherwise use - ["success","unstable","failed"]
329 * @param jobName optional job name param, if empty env.JOB_NAME will be used
330 * @param buildNumber build number param, if empty env.JOB_NAME will be used
331 * @param buildUrl build url param, if empty env.JOB_NAME will be used
332 * @param mailFrom mail FROM param, if empty "jenkins" will be used, it's mandatory for sending email notifications
333 * @param mailTo mail TO param, it's mandatory for sending email notifications
334 */
335def sendNotification(buildStatus, msgText="", enabledNotifications = [], notificatedTypes=["onchange"], jobName=null, buildNumber=null, buildUrl=null, mailFrom="jenkins", mailTo=null){
336 // Default values
337 def colorName = 'blue'
338 def colorCode = '#0000FF'
339 def buildStatusParam = buildStatus != null && buildStatus != "" ? buildStatus : "SUCCESS"
340 def jobNameParam = jobName != null && jobName != "" ? jobName : env.JOB_NAME
341 def buildNumberParam = buildNumber != null && buildNumber != "" ? buildNumber : env.BUILD_NUMBER
342 def buildUrlParam = buildUrl != null && buildUrl != "" ? buildUrl : env.BUILD_URL
343 def subject = "${buildStatusParam}: Job '${jobNameParam} [${buildNumberParam}]'"
344 def summary = "${subject} (${buildUrlParam})"
345
346 if(msgText != null && msgText != ""){
347 summary+="\n${msgText}"
348 }
349 if(buildStatusParam.toLowerCase().equals("success")){
350 colorCode = "#00FF00"
351 colorName = "green"
352 }else if(buildStatusParam.toLowerCase().equals("unstable")){
353 colorCode = "#FFFF00"
354 colorName = "yellow"
355 }else if(buildStatusParam.toLowerCase().equals("failure")){
356 colorCode = "#FF0000"
357 colorName = "red"
358 }
359 if(_needNotification(notificatedTypes, buildStatusParam.toLowerCase(), jobNameParam)){
360 if(enabledNotifications.contains("slack") && jenkinsHasPlugin("slack")){
361 try{
362 slackSend color: colorCode, message: summary
363 }catch(Exception e){
364 println("Calling slack plugin failed")
365 e.printStackTrace()
366 }
367 }
368 if(enabledNotifications.contains("hipchat") && jenkinsHasPlugin("hipchat")){
369 try{
370 hipchatSend color: colorName.toUpperCase(), message: summary
371 }catch(Exception e){
372 println("Calling hipchat plugin failed")
373 e.printStackTrace()
374 }
375 }
376 if(enabledNotifications.contains("email") && mailTo != null && mailTo != "" && mailFrom != null && mailFrom != ""){
377 try{
378 mail body: summary, from: mailFrom, subject: subject, to: mailTo
379 }catch(Exception e){
380 println("Sending mail plugin failed")
381 e.printStackTrace()
382 }
383 }
384 }
Filip Pytloun49d66302017-03-06 10:26:22 +0100385}
chnyda4e5ac792017-03-14 15:24:18 +0100386
387/**
388 * Execute linux command and catch nth element
389 * @param cmd command to execute
390 * @param index index to retrieve
391 * @return index-th element
392 */
393
394def cutOrDie(cmd, index)
395{
396 def common = new com.mirantis.mk.Common()
397 def output
398 try {
399 output = sh(script: cmd, returnStdout: true)
400 def result = output.tokenize(" ")[index]
401 return result;
402 } catch (Exception e) {
403 common.errorMsg("Failed to execute cmd: ${cmd}\n output: ${output}")
404 }
Filip Pytloun81c864d2017-03-21 15:19:30 +0100405}
Tomáš Kukrál767dd732017-03-23 10:38:59 +0100406
407/**
408 * Check variable contains keyword
409 * @param variable keywork is searched (contains) here
410 * @param keyword string to look for
411 * @return True if variable contains keyword (case insensitive), False if do not contains or any of input isn't a string
412 */
413
414def checkContains(variable, keyword) {
Jakub Josef7a8dea22017-03-23 19:51:32 +0100415 if(env.getEnvironment().containsKey(variable)){
416 return env[variable] && env[variable].toLowerCase().contains(keyword.toLowerCase())
Tomáš Kukrál767dd732017-03-23 10:38:59 +0100417 } else {
Tomáš Kukrálc76c1e02017-03-23 19:06:59 +0100418 return false
Tomáš Kukrál767dd732017-03-23 10:38:59 +0100419 }
420}
Jakub Josefa877db52017-04-05 14:22:30 +0200421
422/**
423 * Parse JSON string to hashmap
424 * @param jsonString input JSON string
425 * @return created hashmap
426 */
427def parseJSON(jsonString){
428 def m = [:]
Jakub Josefb7ab8472017-04-05 14:56:53 +0200429 def lazyMap = new JsonSlurperClassic().parseText(jsonString)
Jakub Josefa877db52017-04-05 14:22:30 +0200430 m.putAll(lazyMap)
431 return m
432}
Jakub Josefed239cd2017-05-09 15:27:33 +0200433
434/**
435 * Test pipeline input parameter existence and validity (not null and not empty string)
436 * @param paramName input parameter name (usually uppercase)
437 */
438def validInputParam(paramName){
439 return env.getEnvironment().containsKey(paramName) && env[paramName] != null && env[paramName] != ""
440}