Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 1 | package com.mirantis.mk |
Jakub Josef | 6d8082b | 2017-08-09 12:40:50 +0200 | [diff] [blame] | 2 | |
Jakub Josef | b41c8d5 | 2017-03-24 13:52:24 +0100 | [diff] [blame] | 3 | import static groovy.json.JsonOutput.prettyPrint |
| 4 | import static groovy.json.JsonOutput.toJson |
Jakub Josef | 6d8082b | 2017-08-09 12:40:50 +0200 | [diff] [blame] | 5 | |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 6 | import com.cloudbees.groovy.cps.NonCPS |
Jakub Josef | b7ab847 | 2017-04-05 14:56:53 +0200 | [diff] [blame] | 7 | import groovy.json.JsonSlurperClassic |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 8 | /** |
| 9 | * |
| 10 | * Common functions |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Generate current timestamp |
| 16 | * |
| 17 | * @param format Defaults to yyyyMMddHHmmss |
| 18 | */ |
| 19 | def getDatetime(format="yyyyMMddHHmmss") { |
| 20 | def now = new Date(); |
| 21 | return now.format(format, TimeZone.getTimeZone('UTC')); |
| 22 | } |
| 23 | |
| 24 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 25 | * Return workspace. |
| 26 | * Currently implemented by calling pwd so it won't return relevant result in |
| 27 | * dir context |
| 28 | */ |
Jakub Josef | a661b8c | 2018-01-17 14:51:25 +0100 | [diff] [blame^] | 29 | def getWorkspace(includeBuildNum=false) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 30 | def workspace = sh script: 'pwd', returnStdout: true |
| 31 | workspace = workspace.trim() |
Jakub Josef | a661b8c | 2018-01-17 14:51:25 +0100 | [diff] [blame^] | 32 | if(includeBuildNum){ |
| 33 | if(!workspace.endsWith("/")){ |
| 34 | workspace += "/" |
| 35 | } |
| 36 | workspace += env.BUILD_NUMBER |
| 37 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 38 | return workspace |
| 39 | } |
| 40 | |
| 41 | /** |
Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 42 | * Get UID of jenkins user. |
| 43 | * Must be run from context of node |
| 44 | */ |
| 45 | def getJenkinsUid() { |
| 46 | return sh ( |
| 47 | script: 'id -u', |
| 48 | returnStdout: true |
| 49 | ).trim() |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get GID of jenkins user. |
| 54 | * Must be run from context of node |
| 55 | */ |
| 56 | def getJenkinsGid() { |
| 57 | return sh ( |
| 58 | script: 'id -g', |
| 59 | returnStdout: true |
| 60 | ).trim() |
| 61 | } |
| 62 | |
| 63 | /** |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 64 | * |
| 65 | * Find credentials by ID |
| 66 | * |
| 67 | * @param credsId Credentials ID |
| 68 | * @param credsType Credentials type (optional) |
| 69 | * |
| 70 | */ |
| 71 | def getCredentialsById(String credsId, String credsType = 'any') { |
| 72 | def credClasses = [ // ordered by class name |
| 73 | sshKey: com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.class, |
| 74 | cert: com.cloudbees.plugins.credentials.common.CertificateCredentials.class, |
| 75 | password: com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class, |
| 76 | any: com.cloudbees.plugins.credentials.impl.BaseStandardCredentials.class, |
| 77 | dockerCert: org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials.class, |
| 78 | file: org.jenkinsci.plugins.plaincredentials.FileCredentials.class, |
| 79 | string: org.jenkinsci.plugins.plaincredentials.StringCredentials.class, |
| 80 | ] |
| 81 | return com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( |
| 82 | credClasses[credsType], |
| 83 | jenkins.model.Jenkins.instance |
| 84 | ).findAll {cred -> cred.id == credsId}[0] |
| 85 | } |
| 86 | |
| 87 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 88 | * Get credentials from store |
| 89 | * |
| 90 | * @param id Credentials name |
| 91 | */ |
Jakub Josef | 3d9d9ab | 2017-03-14 15:09:03 +0100 | [diff] [blame] | 92 | def getCredentials(id, cred_type = "username_password") { |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 93 | warningMsg('You are using obsolete function. Please switch to use `getCredentialsById()`') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 94 | |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 95 | type_map = [ |
| 96 | username_password: 'password', |
| 97 | key: 'sshKey', |
| 98 | ] |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 99 | |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 100 | return getCredentialsById(id, type_map[cred_type]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Abort build, wait for some time and ensure we will terminate |
| 105 | */ |
| 106 | def abortBuild() { |
| 107 | currentBuild.build().doStop() |
| 108 | sleep(180) |
| 109 | // just to be sure we will terminate |
| 110 | throw new InterruptedException() |
| 111 | } |
| 112 | |
| 113 | /** |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 114 | * Print pretty-printed string representation of given item |
| 115 | * @param item item to be pretty-printed (list, map, whatever) |
| 116 | */ |
| 117 | def prettyPrint(item){ |
| 118 | println prettify(item) |
| 119 | } |
| 120 | |
| 121 | /** |
Jakub Josef | b41c8d5 | 2017-03-24 13:52:24 +0100 | [diff] [blame] | 122 | * Return pretty-printed string representation of given item |
| 123 | * @param item item to be pretty-printed (list, map, whatever) |
| 124 | * @return pretty-printed string |
| 125 | */ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 126 | def prettify(item){ |
| 127 | return groovy.json.JsonOutput.prettyPrint(toJson(item)).replace('\\n', System.getProperty('line.separator')) |
Jakub Josef | b41c8d5 | 2017-03-24 13:52:24 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 131 | * Print informational message |
| 132 | * |
| 133 | * @param msg |
| 134 | * @param color Colorful output or not |
| 135 | */ |
| 136 | def infoMsg(msg, color = true) { |
| 137 | printMsg(msg, "cyan") |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Print error message |
| 142 | * |
| 143 | * @param msg |
| 144 | * @param color Colorful output or not |
| 145 | */ |
| 146 | def errorMsg(msg, color = true) { |
| 147 | printMsg(msg, "red") |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Print success message |
| 152 | * |
| 153 | * @param msg |
| 154 | * @param color Colorful output or not |
| 155 | */ |
| 156 | def successMsg(msg, color = true) { |
| 157 | printMsg(msg, "green") |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Print warning message |
| 162 | * |
| 163 | * @param msg |
| 164 | * @param color Colorful output or not |
| 165 | */ |
| 166 | def warningMsg(msg, color = true) { |
Jakub Josef | 0e7bd63 | 2017-03-16 16:25:05 +0100 | [diff] [blame] | 167 | printMsg(msg, "yellow") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
Jakub Josef | 952ae0b | 2017-03-14 19:04:21 +0100 | [diff] [blame] | 171 | * Print debug message, this message will show only if DEBUG global variable is present |
| 172 | * @param msg |
| 173 | * @param color Colorful output or not |
| 174 | */ |
| 175 | def debugMsg(msg, color = true){ |
Jakub Josef | 9a836ac | 2017-04-24 12:26:02 +0200 | [diff] [blame] | 176 | // if debug property exists on env, debug is enabled |
Jakub Josef | 66976f6 | 2017-04-24 16:32:23 +0200 | [diff] [blame] | 177 | if(env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true"){ |
Jakub Josef | 74b3469 | 2017-03-15 12:10:57 +0100 | [diff] [blame] | 178 | printMsg("[DEBUG] ${msg}", "red") |
Jakub Josef | 952ae0b | 2017-03-14 19:04:21 +0100 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 183 | * Print message |
| 184 | * |
| 185 | * @param msg Message to be printed |
| 186 | * @param level Level of message (default INFO) |
| 187 | * @param color Color to use for output or false (default) |
| 188 | */ |
| 189 | def printMsg(msg, color = false) { |
| 190 | colors = [ |
| 191 | 'red' : '\u001B[31m', |
| 192 | 'black' : '\u001B[30m', |
| 193 | 'green' : '\u001B[32m', |
| 194 | 'yellow': '\u001B[33m', |
| 195 | 'blue' : '\u001B[34m', |
| 196 | 'purple': '\u001B[35m', |
| 197 | 'cyan' : '\u001B[36m', |
| 198 | 'white' : '\u001B[37m', |
| 199 | 'reset' : '\u001B[0m' |
| 200 | ] |
| 201 | if (color != false) { |
Jakub Josef | e6c562e | 2017-08-09 14:41:03 +0200 | [diff] [blame] | 202 | print "${colors[color]}${msg}${colors.reset}" |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 203 | } else { |
| 204 | print "[${level}] ${msg}" |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Traverse directory structure and return list of files |
| 210 | * |
| 211 | * @param path Path to search |
| 212 | * @param type Type of files to search (groovy.io.FileType.FILES) |
| 213 | */ |
| 214 | @NonCPS |
| 215 | def getFiles(path, type=groovy.io.FileType.FILES) { |
| 216 | files = [] |
| 217 | new File(path).eachFile(type) { |
| 218 | files[] = it |
| 219 | } |
| 220 | return files |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Helper method to convert map into form of list of [key,value] to avoid |
| 225 | * unserializable exceptions |
| 226 | * |
| 227 | * @param m Map |
| 228 | */ |
| 229 | @NonCPS |
| 230 | def entries(m) { |
| 231 | m.collect {k, v -> [k, v]} |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Opposite of build-in parallel, run map of steps in serial |
| 236 | * |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 237 | * @param steps Map of String<name>: CPSClosure2<step> (or list of closures) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 238 | */ |
| 239 | def serial(steps) { |
| 240 | stepsArray = entries(steps) |
| 241 | for (i=0; i < stepsArray.size; i++) { |
Jakub Josef | d31de30 | 2017-05-15 13:59:18 +0200 | [diff] [blame] | 242 | def step = stepsArray[i] |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 243 | def dummySteps = [:] |
| 244 | def stepKey |
Jakub Josef | 228aae9 | 2017-05-15 19:04:43 +0200 | [diff] [blame] | 245 | if(step[1] instanceof List || step[1] instanceof Map){ |
Jakub Josef | 538be16 | 2017-05-15 19:11:48 +0200 | [diff] [blame] | 246 | for(j=0;j < step[1].size(); j++){ |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 247 | if(step[1] instanceof List){ |
| 248 | stepKey = j |
| 249 | }else if(step[1] instanceof Map){ |
| 250 | stepKey = step[1].keySet()[j] |
| 251 | } |
| 252 | dummySteps.put("step-${step[0]}-${stepKey}",step[1][stepKey]) |
Jakub Josef | d31de30 | 2017-05-15 13:59:18 +0200 | [diff] [blame] | 253 | } |
| 254 | }else{ |
| 255 | dummySteps.put(step[0], step[1]) |
| 256 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 257 | parallel dummySteps |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 262 | * Partition given list to list of small lists |
| 263 | * @param inputList input list |
| 264 | * @param partitionSize (partition size, optional, default 5) |
| 265 | */ |
| 266 | def partitionList(inputList, partitionSize=5){ |
| 267 | List<List<String>> partitions = new ArrayList<>(); |
| 268 | for (int i=0; i<inputList.size(); i += partitionSize) { |
| 269 | partitions.add(new ArrayList<String>(inputList.subList(i, Math.min(i + partitionSize, inputList.size())))); |
| 270 | } |
| 271 | return partitions |
| 272 | } |
| 273 | |
| 274 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 275 | * Get password credentials from store |
| 276 | * |
| 277 | * @param id Credentials name |
| 278 | */ |
| 279 | def getPasswordCredentials(id) { |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 280 | return getCredentialsById(id, 'password') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Get SSH credentials from store |
| 285 | * |
| 286 | * @param id Credentials name |
| 287 | */ |
| 288 | def getSshCredentials(id) { |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 289 | return getCredentialsById(id, 'sshKey') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 290 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 291 | |
| 292 | /** |
| 293 | * Tests Jenkins instance for existence of plugin with given name |
| 294 | * @param pluginName plugin short name to test |
| 295 | * @return boolean result |
| 296 | */ |
| 297 | @NonCPS |
| 298 | def jenkinsHasPlugin(pluginName){ |
| 299 | return Jenkins.instance.pluginManager.plugins.collect{p -> p.shortName}.contains(pluginName) |
| 300 | } |
| 301 | |
| 302 | @NonCPS |
| 303 | def _needNotification(notificatedTypes, buildStatus, jobName) { |
| 304 | if(notificatedTypes && notificatedTypes.contains("onchange")){ |
| 305 | if(jobName){ |
| 306 | def job = Jenkins.instance.getItem(jobName) |
| 307 | def numbuilds = job.builds.size() |
| 308 | if (numbuilds > 0){ |
| 309 | //actual build is first for some reasons, so last finished build is second |
| 310 | def lastBuild = job.builds[1] |
| 311 | if(lastBuild){ |
| 312 | if(lastBuild.result.toString().toLowerCase().equals(buildStatus)){ |
| 313 | println("Build status didn't changed since last build, not sending notifications") |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | }else if(!notificatedTypes.contains(buildStatus)){ |
| 320 | return false; |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Send notification to all enabled notifications services |
| 327 | * @param buildStatus message type (success, warning, error), null means SUCCESSFUL |
| 328 | * @param msgText message text |
| 329 | * @param enabledNotifications list of enabled notification types, types: slack, hipchat, email, default empty |
| 330 | * @param notificatedTypes types of notifications will be sent, default onchange - notificate if current build result not equal last result; |
| 331 | * otherwise use - ["success","unstable","failed"] |
| 332 | * @param jobName optional job name param, if empty env.JOB_NAME will be used |
Jakub Josef | d057115 | 2017-07-17 14:11:39 +0200 | [diff] [blame] | 333 | * @param buildNumber build number param, if empty env.BUILD_NUM will be used |
| 334 | * @param buildUrl build url param, if empty env.BUILD_URL will be used |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 335 | * @param mailFrom mail FROM param, if empty "jenkins" will be used, it's mandatory for sending email notifications |
Jakub Josef | d057115 | 2017-07-17 14:11:39 +0200 | [diff] [blame] | 336 | * @param mailTo mail TO param, it's mandatory for sending email notifications, this option enable mail notification |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 337 | */ |
| 338 | def sendNotification(buildStatus, msgText="", enabledNotifications = [], notificatedTypes=["onchange"], jobName=null, buildNumber=null, buildUrl=null, mailFrom="jenkins", mailTo=null){ |
| 339 | // Default values |
| 340 | def colorName = 'blue' |
| 341 | def colorCode = '#0000FF' |
| 342 | def buildStatusParam = buildStatus != null && buildStatus != "" ? buildStatus : "SUCCESS" |
| 343 | def jobNameParam = jobName != null && jobName != "" ? jobName : env.JOB_NAME |
| 344 | def buildNumberParam = buildNumber != null && buildNumber != "" ? buildNumber : env.BUILD_NUMBER |
| 345 | def buildUrlParam = buildUrl != null && buildUrl != "" ? buildUrl : env.BUILD_URL |
| 346 | def subject = "${buildStatusParam}: Job '${jobNameParam} [${buildNumberParam}]'" |
| 347 | def summary = "${subject} (${buildUrlParam})" |
| 348 | |
| 349 | if(msgText != null && msgText != ""){ |
| 350 | summary+="\n${msgText}" |
| 351 | } |
| 352 | if(buildStatusParam.toLowerCase().equals("success")){ |
| 353 | colorCode = "#00FF00" |
| 354 | colorName = "green" |
| 355 | }else if(buildStatusParam.toLowerCase().equals("unstable")){ |
| 356 | colorCode = "#FFFF00" |
| 357 | colorName = "yellow" |
| 358 | }else if(buildStatusParam.toLowerCase().equals("failure")){ |
| 359 | colorCode = "#FF0000" |
| 360 | colorName = "red" |
| 361 | } |
| 362 | if(_needNotification(notificatedTypes, buildStatusParam.toLowerCase(), jobNameParam)){ |
| 363 | if(enabledNotifications.contains("slack") && jenkinsHasPlugin("slack")){ |
| 364 | try{ |
| 365 | slackSend color: colorCode, message: summary |
| 366 | }catch(Exception e){ |
| 367 | println("Calling slack plugin failed") |
| 368 | e.printStackTrace() |
| 369 | } |
| 370 | } |
| 371 | if(enabledNotifications.contains("hipchat") && jenkinsHasPlugin("hipchat")){ |
| 372 | try{ |
| 373 | hipchatSend color: colorName.toUpperCase(), message: summary |
| 374 | }catch(Exception e){ |
| 375 | println("Calling hipchat plugin failed") |
| 376 | e.printStackTrace() |
| 377 | } |
| 378 | } |
| 379 | if(enabledNotifications.contains("email") && mailTo != null && mailTo != "" && mailFrom != null && mailFrom != ""){ |
| 380 | try{ |
| 381 | mail body: summary, from: mailFrom, subject: subject, to: mailTo |
| 382 | }catch(Exception e){ |
| 383 | println("Sending mail plugin failed") |
| 384 | e.printStackTrace() |
| 385 | } |
| 386 | } |
| 387 | } |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 388 | } |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * Execute linux command and catch nth element |
| 392 | * @param cmd command to execute |
| 393 | * @param index index to retrieve |
| 394 | * @return index-th element |
| 395 | */ |
| 396 | |
| 397 | def cutOrDie(cmd, index) |
| 398 | { |
| 399 | def common = new com.mirantis.mk.Common() |
| 400 | def output |
| 401 | try { |
| 402 | output = sh(script: cmd, returnStdout: true) |
| 403 | def result = output.tokenize(" ")[index] |
| 404 | return result; |
| 405 | } catch (Exception e) { |
| 406 | common.errorMsg("Failed to execute cmd: ${cmd}\n output: ${output}") |
| 407 | } |
Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 408 | } |
Tomáš Kukrál | 767dd73 | 2017-03-23 10:38:59 +0100 | [diff] [blame] | 409 | |
| 410 | /** |
| 411 | * Check variable contains keyword |
| 412 | * @param variable keywork is searched (contains) here |
| 413 | * @param keyword string to look for |
| 414 | * @return True if variable contains keyword (case insensitive), False if do not contains or any of input isn't a string |
| 415 | */ |
| 416 | |
| 417 | def checkContains(variable, keyword) { |
Jakub Josef | 7a8dea2 | 2017-03-23 19:51:32 +0100 | [diff] [blame] | 418 | if(env.getEnvironment().containsKey(variable)){ |
| 419 | return env[variable] && env[variable].toLowerCase().contains(keyword.toLowerCase()) |
Tomáš Kukrál | 767dd73 | 2017-03-23 10:38:59 +0100 | [diff] [blame] | 420 | } else { |
Tomáš Kukrál | c76c1e0 | 2017-03-23 19:06:59 +0100 | [diff] [blame] | 421 | return false |
Tomáš Kukrál | 767dd73 | 2017-03-23 10:38:59 +0100 | [diff] [blame] | 422 | } |
| 423 | } |
Jakub Josef | a877db5 | 2017-04-05 14:22:30 +0200 | [diff] [blame] | 424 | |
| 425 | /** |
| 426 | * Parse JSON string to hashmap |
| 427 | * @param jsonString input JSON string |
| 428 | * @return created hashmap |
| 429 | */ |
| 430 | def parseJSON(jsonString){ |
| 431 | def m = [:] |
Jakub Josef | b7ab847 | 2017-04-05 14:56:53 +0200 | [diff] [blame] | 432 | def lazyMap = new JsonSlurperClassic().parseText(jsonString) |
Jakub Josef | a877db5 | 2017-04-05 14:22:30 +0200 | [diff] [blame] | 433 | m.putAll(lazyMap) |
| 434 | return m |
| 435 | } |
Jakub Josef | ed239cd | 2017-05-09 15:27:33 +0200 | [diff] [blame] | 436 | |
| 437 | /** |
| 438 | * Test pipeline input parameter existence and validity (not null and not empty string) |
| 439 | * @param paramName input parameter name (usually uppercase) |
| 440 | */ |
| 441 | def validInputParam(paramName){ |
| 442 | return env.getEnvironment().containsKey(paramName) && env[paramName] != null && env[paramName] != "" |
Tomáš Kukrál | d34fe87 | 2017-06-13 10:50:50 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Take list of hashmaps and count number of hashmaps with parameter equals eq |
| 447 | * @param lm list of hashmaps |
| 448 | * @param param define parameter of hashmap to read and compare |
| 449 | * @param eq desired value of hashmap parameter |
| 450 | * @return count of hashmaps meeting defined condition |
| 451 | */ |
| 452 | |
| 453 | @NonCPS |
| 454 | def countHashMapEquals(lm, param, eq) { |
Tomáš Kukrál | 5dd1207 | 2017-06-13 15:54:44 +0200 | [diff] [blame] | 455 | return lm.stream().filter{i -> i[param].equals(eq)}.collect(java.util.stream.Collectors.counting()) |
Tomáš Kukrál | d34fe87 | 2017-06-13 10:50:50 +0200 | [diff] [blame] | 456 | } |
Vasyl Saienko | fb055b1 | 2017-11-23 18:15:23 +0200 | [diff] [blame] | 457 | |
| 458 | /** |
| 459 | * Execute shell command and return stdout, stderr and status |
| 460 | * |
| 461 | * @param cmd Command to execute |
| 462 | * @return map with stdout, stderr, status keys |
| 463 | */ |
| 464 | |
| 465 | def shCmdStatus(cmd) { |
| 466 | def res = [:] |
| 467 | def stderr = sh(script: 'mktemp', returnStdout: true).trim() |
| 468 | def stdout = sh(script: 'mktemp', returnStdout: true).trim() |
| 469 | |
| 470 | try { |
| 471 | def status = sh(script:"${cmd} 1>${stdout} 2>${stderr}", returnStatus: true) |
| 472 | res['stderr'] = sh(script: "cat ${stderr}", returnStdout: true) |
| 473 | res['stdout'] = sh(script: "cat ${stdout}", returnStdout: true) |
| 474 | res['status'] = status |
| 475 | } finally { |
| 476 | sh(script: "rm ${stderr}", returnStdout: true) |
| 477 | sh(script: "rm ${stdout}", returnStdout: true) |
| 478 | } |
| 479 | |
| 480 | return res |
| 481 | } |