blob: ebc80c19ecfaa28f5e8ee5b5e1b046a1b3ae56f2 [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package com.mirantis.mk
Jakub Josefb41c8d52017-03-24 13:52:24 +01002import static groovy.json.JsonOutput.prettyPrint
3import static groovy.json.JsonOutput.toJson
Jakub Josefbceaa322017-06-13 18:28:27 +02004import com.cloudbees.groovy.cps.NonCPS
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 Josefbceaa322017-06-13 18:28:27 +020093 * Print pretty-printed string representation of given item
94 * @param item item to be pretty-printed (list, map, whatever)
95 */
96def prettyPrint(item){
97 println prettify(item)
98}
99
100/**
Jakub Josefb41c8d52017-03-24 13:52:24 +0100101 * Return pretty-printed string representation of given item
102 * @param item item to be pretty-printed (list, map, whatever)
103 * @return pretty-printed string
104 */
Jakub Josefbceaa322017-06-13 18:28:27 +0200105def prettify(item){
106 return groovy.json.JsonOutput.prettyPrint(toJson(item)).replace('\\n', System.getProperty('line.separator'))
Jakub Josefb41c8d52017-03-24 13:52:24 +0100107}
108
109/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100110 * Print informational message
111 *
112 * @param msg
113 * @param color Colorful output or not
114 */
115def infoMsg(msg, color = true) {
116 printMsg(msg, "cyan")
117}
118
119/**
120 * Print error message
121 *
122 * @param msg
123 * @param color Colorful output or not
124 */
125def errorMsg(msg, color = true) {
126 printMsg(msg, "red")
127}
128
129/**
130 * Print success message
131 *
132 * @param msg
133 * @param color Colorful output or not
134 */
135def successMsg(msg, color = true) {
136 printMsg(msg, "green")
137}
138
139/**
140 * Print warning message
141 *
142 * @param msg
143 * @param color Colorful output or not
144 */
145def warningMsg(msg, color = true) {
Jakub Josef0e7bd632017-03-16 16:25:05 +0100146 printMsg(msg, "yellow")
Jakub Josef79ecec32017-02-17 14:36:28 +0100147}
148
149/**
Jakub Josef952ae0b2017-03-14 19:04:21 +0100150 * Print debug message, this message will show only if DEBUG global variable is present
151 * @param msg
152 * @param color Colorful output or not
153 */
154def debugMsg(msg, color = true){
Jakub Josef9a836ac2017-04-24 12:26:02 +0200155 // if debug property exists on env, debug is enabled
Jakub Josef66976f62017-04-24 16:32:23 +0200156 if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){
Jakub Josef74b34692017-03-15 12:10:57 +0100157 printMsg("[DEBUG] ${msg}", "red")
Jakub Josef952ae0b2017-03-14 19:04:21 +0100158 }
159}
160
161/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100162 * Print message
163 *
164 * @param msg Message to be printed
165 * @param level Level of message (default INFO)
166 * @param color Color to use for output or false (default)
167 */
168def printMsg(msg, color = false) {
169 colors = [
170 'red' : '\u001B[31m',
171 'black' : '\u001B[30m',
172 'green' : '\u001B[32m',
173 'yellow': '\u001B[33m',
174 'blue' : '\u001B[34m',
175 'purple': '\u001B[35m',
176 'cyan' : '\u001B[36m',
177 'white' : '\u001B[37m',
178 'reset' : '\u001B[0m'
179 ]
180 if (color != false) {
181 wrap([$class: 'AnsiColorBuildWrapper']) {
182 print "${colors[color]}${msg}${colors.reset}"
183 }
184 } else {
185 print "[${level}] ${msg}"
186 }
187}
188
189/**
190 * Traverse directory structure and return list of files
191 *
192 * @param path Path to search
193 * @param type Type of files to search (groovy.io.FileType.FILES)
194 */
195@NonCPS
196def getFiles(path, type=groovy.io.FileType.FILES) {
197 files = []
198 new File(path).eachFile(type) {
199 files[] = it
200 }
201 return files
202}
203
204/**
205 * Helper method to convert map into form of list of [key,value] to avoid
206 * unserializable exceptions
207 *
208 * @param m Map
209 */
210@NonCPS
211def entries(m) {
212 m.collect {k, v -> [k, v]}
213}
214
215/**
216 * Opposite of build-in parallel, run map of steps in serial
217 *
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200218 * @param steps Map of String<name>: CPSClosure2<step> (or list of closures)
Jakub Josef79ecec32017-02-17 14:36:28 +0100219 */
220def serial(steps) {
221 stepsArray = entries(steps)
222 for (i=0; i < stepsArray.size; i++) {
Jakub Josefd31de302017-05-15 13:59:18 +0200223 def step = stepsArray[i]
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200224 def dummySteps = [:]
225 def stepKey
Jakub Josef228aae92017-05-15 19:04:43 +0200226 if(step[1] instanceof List || step[1] instanceof Map){
Jakub Josef538be162017-05-15 19:11:48 +0200227 for(j=0;j < step[1].size(); j++){
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200228 if(step[1] instanceof List){
229 stepKey = j
230 }else if(step[1] instanceof Map){
231 stepKey = step[1].keySet()[j]
232 }
233 dummySteps.put("step-${step[0]}-${stepKey}",step[1][stepKey])
Jakub Josefd31de302017-05-15 13:59:18 +0200234 }
235 }else{
236 dummySteps.put(step[0], step[1])
237 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100238 parallel dummySteps
239 }
240}
241
242/**
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200243 * Partition given list to list of small lists
244 * @param inputList input list
245 * @param partitionSize (partition size, optional, default 5)
246 */
247def partitionList(inputList, partitionSize=5){
248 List<List<String>> partitions = new ArrayList<>();
249 for (int i=0; i<inputList.size(); i += partitionSize) {
250 partitions.add(new ArrayList<String>(inputList.subList(i, Math.min(i + partitionSize, inputList.size()))));
251 }
252 return partitions
253}
254
255/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100256 * Get password credentials from store
257 *
258 * @param id Credentials name
259 */
260def getPasswordCredentials(id) {
261 def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
262 com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
263 jenkins.model.Jenkins.instance
264 )
265
266 for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
267 c = credsIter.next();
268 if ( c.id == id ) {
269 return c;
270 }
271 }
272
273 throw new Exception("Could not find credentials for ID ${id}")
274}
275
276/**
277 * Get SSH credentials from store
278 *
279 * @param id Credentials name
280 */
281def getSshCredentials(id) {
282 def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
283 com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
284 jenkins.model.Jenkins.instance
285 )
286
287 for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
288 c = credsIter.next();
289 if ( c.id == id ) {
290 return c;
291 }
292 }
293
294 throw new Exception("Could not find credentials for ID ${id}")
295}
Jakub Josef79ecec32017-02-17 14:36:28 +0100296
297/**
298 * Tests Jenkins instance for existence of plugin with given name
299 * @param pluginName plugin short name to test
300 * @return boolean result
301 */
302@NonCPS
303def jenkinsHasPlugin(pluginName){
304 return Jenkins.instance.pluginManager.plugins.collect{p -> p.shortName}.contains(pluginName)
305}
306
307@NonCPS
308def _needNotification(notificatedTypes, buildStatus, jobName) {
309 if(notificatedTypes && notificatedTypes.contains("onchange")){
310 if(jobName){
311 def job = Jenkins.instance.getItem(jobName)
312 def numbuilds = job.builds.size()
313 if (numbuilds > 0){
314 //actual build is first for some reasons, so last finished build is second
315 def lastBuild = job.builds[1]
316 if(lastBuild){
317 if(lastBuild.result.toString().toLowerCase().equals(buildStatus)){
318 println("Build status didn't changed since last build, not sending notifications")
319 return false;
320 }
321 }
322 }
323 }
324 }else if(!notificatedTypes.contains(buildStatus)){
325 return false;
326 }
327 return true;
328}
329
330/**
331 * Send notification to all enabled notifications services
332 * @param buildStatus message type (success, warning, error), null means SUCCESSFUL
333 * @param msgText message text
334 * @param enabledNotifications list of enabled notification types, types: slack, hipchat, email, default empty
335 * @param notificatedTypes types of notifications will be sent, default onchange - notificate if current build result not equal last result;
336 * otherwise use - ["success","unstable","failed"]
337 * @param jobName optional job name param, if empty env.JOB_NAME will be used
338 * @param buildNumber build number param, if empty env.JOB_NAME will be used
339 * @param buildUrl build url param, if empty env.JOB_NAME will be used
340 * @param mailFrom mail FROM param, if empty "jenkins" will be used, it's mandatory for sending email notifications
341 * @param mailTo mail TO param, it's mandatory for sending email notifications
342 */
343def sendNotification(buildStatus, msgText="", enabledNotifications = [], notificatedTypes=["onchange"], jobName=null, buildNumber=null, buildUrl=null, mailFrom="jenkins", mailTo=null){
344 // Default values
345 def colorName = 'blue'
346 def colorCode = '#0000FF'
347 def buildStatusParam = buildStatus != null && buildStatus != "" ? buildStatus : "SUCCESS"
348 def jobNameParam = jobName != null && jobName != "" ? jobName : env.JOB_NAME
349 def buildNumberParam = buildNumber != null && buildNumber != "" ? buildNumber : env.BUILD_NUMBER
350 def buildUrlParam = buildUrl != null && buildUrl != "" ? buildUrl : env.BUILD_URL
351 def subject = "${buildStatusParam}: Job '${jobNameParam} [${buildNumberParam}]'"
352 def summary = "${subject} (${buildUrlParam})"
353
354 if(msgText != null && msgText != ""){
355 summary+="\n${msgText}"
356 }
357 if(buildStatusParam.toLowerCase().equals("success")){
358 colorCode = "#00FF00"
359 colorName = "green"
360 }else if(buildStatusParam.toLowerCase().equals("unstable")){
361 colorCode = "#FFFF00"
362 colorName = "yellow"
363 }else if(buildStatusParam.toLowerCase().equals("failure")){
364 colorCode = "#FF0000"
365 colorName = "red"
366 }
367 if(_needNotification(notificatedTypes, buildStatusParam.toLowerCase(), jobNameParam)){
368 if(enabledNotifications.contains("slack") && jenkinsHasPlugin("slack")){
369 try{
370 slackSend color: colorCode, message: summary
371 }catch(Exception e){
372 println("Calling slack plugin failed")
373 e.printStackTrace()
374 }
375 }
376 if(enabledNotifications.contains("hipchat") && jenkinsHasPlugin("hipchat")){
377 try{
378 hipchatSend color: colorName.toUpperCase(), message: summary
379 }catch(Exception e){
380 println("Calling hipchat plugin failed")
381 e.printStackTrace()
382 }
383 }
384 if(enabledNotifications.contains("email") && mailTo != null && mailTo != "" && mailFrom != null && mailFrom != ""){
385 try{
386 mail body: summary, from: mailFrom, subject: subject, to: mailTo
387 }catch(Exception e){
388 println("Sending mail plugin failed")
389 e.printStackTrace()
390 }
391 }
392 }
Filip Pytloun49d66302017-03-06 10:26:22 +0100393}
chnyda4e5ac792017-03-14 15:24:18 +0100394
395/**
396 * Execute linux command and catch nth element
397 * @param cmd command to execute
398 * @param index index to retrieve
399 * @return index-th element
400 */
401
402def cutOrDie(cmd, index)
403{
404 def common = new com.mirantis.mk.Common()
405 def output
406 try {
407 output = sh(script: cmd, returnStdout: true)
408 def result = output.tokenize(" ")[index]
409 return result;
410 } catch (Exception e) {
411 common.errorMsg("Failed to execute cmd: ${cmd}\n output: ${output}")
412 }
Filip Pytloun81c864d2017-03-21 15:19:30 +0100413}
Tomáš Kukrál767dd732017-03-23 10:38:59 +0100414
415/**
416 * Check variable contains keyword
417 * @param variable keywork is searched (contains) here
418 * @param keyword string to look for
419 * @return True if variable contains keyword (case insensitive), False if do not contains or any of input isn't a string
420 */
421
422def checkContains(variable, keyword) {
Jakub Josef7a8dea22017-03-23 19:51:32 +0100423 if(env.getEnvironment().containsKey(variable)){
424 return env[variable] && env[variable].toLowerCase().contains(keyword.toLowerCase())
Tomáš Kukrál767dd732017-03-23 10:38:59 +0100425 } else {
Tomáš Kukrálc76c1e02017-03-23 19:06:59 +0100426 return false
Tomáš Kukrál767dd732017-03-23 10:38:59 +0100427 }
428}
Jakub Josefa877db52017-04-05 14:22:30 +0200429
430/**
431 * Parse JSON string to hashmap
432 * @param jsonString input JSON string
433 * @return created hashmap
434 */
435def parseJSON(jsonString){
436 def m = [:]
Jakub Josefb7ab8472017-04-05 14:56:53 +0200437 def lazyMap = new JsonSlurperClassic().parseText(jsonString)
Jakub Josefa877db52017-04-05 14:22:30 +0200438 m.putAll(lazyMap)
439 return m
440}
Jakub Josefed239cd2017-05-09 15:27:33 +0200441
442/**
443 * Test pipeline input parameter existence and validity (not null and not empty string)
444 * @param paramName input parameter name (usually uppercase)
445 */
446def validInputParam(paramName){
447 return env.getEnvironment().containsKey(paramName) && env[paramName] != null && env[paramName] != ""
Tomáš Kukráld34fe872017-06-13 10:50:50 +0200448}
449
450/**
451 * Take list of hashmaps and count number of hashmaps with parameter equals eq
452 * @param lm list of hashmaps
453 * @param param define parameter of hashmap to read and compare
454 * @param eq desired value of hashmap parameter
455 * @return count of hashmaps meeting defined condition
456 */
457
458@NonCPS
459def countHashMapEquals(lm, param, eq) {
Tomáš Kukrál5dd12072017-06-13 15:54:44 +0200460 return lm.stream().filter{i -> i[param].equals(eq)}.collect(java.util.stream.Collectors.counting())
Tomáš Kukráld34fe872017-06-13 10:50:50 +0200461}