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 |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 8 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 9 | /** |
| 10 | * |
| 11 | * Common functions |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Generate current timestamp |
| 17 | * |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 18 | * @param format Defaults to yyyyMMddHHmmss |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 19 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 20 | def getDatetime(format = "yyyyMMddHHmmss") { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 21 | def now = new Date(); |
| 22 | return now.format(format, TimeZone.getTimeZone('UTC')); |
| 23 | } |
| 24 | |
| 25 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 26 | * Return workspace. |
| 27 | * Currently implemented by calling pwd so it won't return relevant result in |
| 28 | * dir context |
| 29 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 30 | def getWorkspace(includeBuildNum = false) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 31 | def workspace = sh script: 'pwd', returnStdout: true |
| 32 | workspace = workspace.trim() |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 33 | if (includeBuildNum) { |
| 34 | if (!workspace.endsWith("/")) { |
| 35 | workspace += "/" |
| 36 | } |
| 37 | workspace += env.BUILD_NUMBER |
Jakub Josef | a661b8c | 2018-01-17 14:51:25 +0100 | [diff] [blame] | 38 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 39 | return workspace |
| 40 | } |
| 41 | |
| 42 | /** |
Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 43 | * Get UID of jenkins user. |
| 44 | * Must be run from context of node |
| 45 | */ |
| 46 | def getJenkinsUid() { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 47 | return sh( |
Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 48 | script: 'id -u', |
| 49 | returnStdout: true |
| 50 | ).trim() |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get GID of jenkins user. |
| 55 | * Must be run from context of node |
| 56 | */ |
| 57 | def getJenkinsGid() { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 58 | return sh( |
Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 59 | script: 'id -g', |
| 60 | returnStdout: true |
| 61 | ).trim() |
| 62 | } |
| 63 | |
| 64 | /** |
Jakub Josef | c8074db | 2018-01-30 13:33:20 +0100 | [diff] [blame] | 65 | * Returns Jenkins user uid and gid in one list (in that order) |
| 66 | * Must be run from context of node |
| 67 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 68 | def getJenkinsUserIds() { |
Jakub Josef | c8074db | 2018-01-30 13:33:20 +0100 | [diff] [blame] | 69 | return sh(script: "id -u && id -g", returnStdout: true).tokenize("\n") |
| 70 | } |
| 71 | |
| 72 | /** |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 73 | * |
| 74 | * Find credentials by ID |
| 75 | * |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 76 | * @param credsId Credentials ID |
| 77 | * @param credsType Credentials type (optional) |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 78 | * |
| 79 | */ |
| 80 | def getCredentialsById(String credsId, String credsType = 'any') { |
| 81 | def credClasses = [ // ordered by class name |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 82 | sshKey : com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.class, |
| 83 | cert : com.cloudbees.plugins.credentials.common.CertificateCredentials.class, |
| 84 | password : com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class, |
| 85 | any : com.cloudbees.plugins.credentials.impl.BaseStandardCredentials.class, |
| 86 | dockerCert: org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials.class, |
| 87 | file : org.jenkinsci.plugins.plaincredentials.FileCredentials.class, |
| 88 | string : org.jenkinsci.plugins.plaincredentials.StringCredentials.class, |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 89 | ] |
| 90 | return com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( |
| 91 | credClasses[credsType], |
| 92 | jenkins.model.Jenkins.instance |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 93 | ).findAll { cred -> cred.id == credsId }[0] |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 97 | * Get credentials from store |
| 98 | * |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 99 | * @param id Credentials name |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 100 | */ |
Jakub Josef | 3d9d9ab | 2017-03-14 15:09:03 +0100 | [diff] [blame] | 101 | def getCredentials(id, cred_type = "username_password") { |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 102 | warningMsg('You are using obsolete function. Please switch to use `getCredentialsById()`') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 103 | |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 104 | type_map = [ |
| 105 | username_password: 'password', |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 106 | key : 'sshKey', |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 107 | ] |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 108 | |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 109 | return getCredentialsById(id, type_map[cred_type]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Abort build, wait for some time and ensure we will terminate |
| 114 | */ |
| 115 | def abortBuild() { |
| 116 | currentBuild.build().doStop() |
| 117 | sleep(180) |
| 118 | // just to be sure we will terminate |
| 119 | throw new InterruptedException() |
| 120 | } |
| 121 | |
| 122 | /** |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 123 | * Print pretty-printed string representation of given item |
| 124 | * @param item item to be pretty-printed (list, map, whatever) |
| 125 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 126 | def prettyPrint(item) { |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 127 | println prettify(item) |
| 128 | } |
| 129 | |
| 130 | /** |
Jakub Josef | b41c8d5 | 2017-03-24 13:52:24 +0100 | [diff] [blame] | 131 | * Return pretty-printed string representation of given item |
| 132 | * @param item item to be pretty-printed (list, map, whatever) |
| 133 | * @return pretty-printed string |
| 134 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 135 | def prettify(item) { |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 136 | 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] | 137 | } |
| 138 | |
| 139 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 140 | * Print informational message |
| 141 | * |
| 142 | * @param msg |
| 143 | * @param color Colorful output or not |
| 144 | */ |
| 145 | def infoMsg(msg, color = true) { |
| 146 | printMsg(msg, "cyan") |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Print error message |
| 151 | * |
| 152 | * @param msg |
| 153 | * @param color Colorful output or not |
| 154 | */ |
| 155 | def errorMsg(msg, color = true) { |
| 156 | printMsg(msg, "red") |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Print success message |
| 161 | * |
| 162 | * @param msg |
| 163 | * @param color Colorful output or not |
| 164 | */ |
| 165 | def successMsg(msg, color = true) { |
| 166 | printMsg(msg, "green") |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Print warning message |
| 171 | * |
| 172 | * @param msg |
| 173 | * @param color Colorful output or not |
| 174 | */ |
| 175 | def warningMsg(msg, color = true) { |
Jakub Josef | 0e7bd63 | 2017-03-16 16:25:05 +0100 | [diff] [blame] | 176 | printMsg(msg, "yellow") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
Jakub Josef | 952ae0b | 2017-03-14 19:04:21 +0100 | [diff] [blame] | 180 | * Print debug message, this message will show only if DEBUG global variable is present |
| 181 | * @param msg |
| 182 | * @param color Colorful output or not |
| 183 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 184 | def debugMsg(msg, color = true) { |
Jakub Josef | 9a836ac | 2017-04-24 12:26:02 +0200 | [diff] [blame] | 185 | // if debug property exists on env, debug is enabled |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 186 | if (env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true") { |
Jakub Josef | 74b3469 | 2017-03-15 12:10:57 +0100 | [diff] [blame] | 187 | printMsg("[DEBUG] ${msg}", "red") |
Jakub Josef | 952ae0b | 2017-03-14 19:04:21 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 191 | def getColorizedString(msg, color) { |
| 192 | def colorMap = [ |
| 193 | 'red' : '\u001B[31m', |
| 194 | 'black' : '\u001B[30m', |
| 195 | 'green' : '\u001B[32m', |
| 196 | 'yellow': '\u001B[33m', |
| 197 | 'blue' : '\u001B[34m', |
| 198 | 'purple': '\u001B[35m', |
| 199 | 'cyan' : '\u001B[36m', |
| 200 | 'white' : '\u001B[37m', |
| 201 | 'reset' : '\u001B[0m' |
| 202 | ] |
Vasyl Saienko | 00ef98b | 2018-09-05 10:34:32 +0300 | [diff] [blame] | 203 | |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 204 | return "${colorMap[color]}${msg}${colorMap.reset}" |
Vasyl Saienko | 00ef98b | 2018-09-05 10:34:32 +0300 | [diff] [blame] | 205 | } |
| 206 | |
Jakub Josef | 952ae0b | 2017-03-14 19:04:21 +0100 | [diff] [blame] | 207 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 208 | * Print message |
| 209 | * |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 210 | * @param msg Message to be printed |
Vasyl Saienko | 00ef98b | 2018-09-05 10:34:32 +0300 | [diff] [blame] | 211 | * @param color Color to use for output |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 212 | */ |
Vasyl Saienko | 00ef98b | 2018-09-05 10:34:32 +0300 | [diff] [blame] | 213 | def printMsg(msg, color) { |
| 214 | print getColorizedString(msg, color) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Traverse directory structure and return list of files |
| 219 | * |
| 220 | * @param path Path to search |
| 221 | * @param type Type of files to search (groovy.io.FileType.FILES) |
| 222 | */ |
| 223 | @NonCPS |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 224 | def getFiles(path, type = groovy.io.FileType.FILES) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 225 | files = [] |
| 226 | new File(path).eachFile(type) { |
| 227 | files[] = it |
| 228 | } |
| 229 | return files |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Helper method to convert map into form of list of [key,value] to avoid |
| 234 | * unserializable exceptions |
| 235 | * |
| 236 | * @param m Map |
| 237 | */ |
| 238 | @NonCPS |
| 239 | def entries(m) { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 240 | m.collect { k, v -> [k, v] } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Opposite of build-in parallel, run map of steps in serial |
| 245 | * |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 246 | * @param steps Map of String<name>: CPSClosure2<step> (or list of closures) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 247 | */ |
| 248 | def serial(steps) { |
| 249 | stepsArray = entries(steps) |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 250 | for (i = 0; i < stepsArray.size; i++) { |
Jakub Josef | d31de30 | 2017-05-15 13:59:18 +0200 | [diff] [blame] | 251 | def step = stepsArray[i] |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 252 | def dummySteps = [:] |
| 253 | def stepKey |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 254 | if (step[1] instanceof List || step[1] instanceof Map) { |
| 255 | for (j = 0; j < step[1].size(); j++) { |
| 256 | if (step[1] instanceof List) { |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 257 | stepKey = j |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 258 | } else if (step[1] instanceof Map) { |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 259 | stepKey = step[1].keySet()[j] |
| 260 | } |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 261 | dummySteps.put("step-${step[0]}-${stepKey}", step[1][stepKey]) |
Jakub Josef | d31de30 | 2017-05-15 13:59:18 +0200 | [diff] [blame] | 262 | } |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 263 | } else { |
Jakub Josef | d31de30 | 2017-05-15 13:59:18 +0200 | [diff] [blame] | 264 | dummySteps.put(step[0], step[1]) |
| 265 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 266 | parallel dummySteps |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 271 | * Partition given list to list of small lists |
| 272 | * @param inputList input list |
| 273 | * @param partitionSize (partition size, optional, default 5) |
| 274 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 275 | def partitionList(inputList, partitionSize = 5) { |
| 276 | List<List<String>> partitions = new ArrayList<>(); |
| 277 | for (int i = 0; i < inputList.size(); i += partitionSize) { |
| 278 | partitions.add(new ArrayList<String>(inputList.subList(i, Math.min(i + partitionSize, inputList.size())))); |
| 279 | } |
| 280 | return partitions |
Jakub Josef | 7fb8bbd | 2017-05-15 16:02:44 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 284 | * Get password credentials from store |
| 285 | * |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 286 | * @param id Credentials name |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 287 | */ |
| 288 | def getPasswordCredentials(id) { |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 289 | return getCredentialsById(id, 'password') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Get SSH credentials from store |
| 294 | * |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 295 | * @param id Credentials name |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 296 | */ |
| 297 | def getSshCredentials(id) { |
Alexander Evseev | bc1fea4 | 2017-12-13 10:03:03 +0100 | [diff] [blame] | 298 | return getCredentialsById(id, 'sshKey') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 299 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * Tests Jenkins instance for existence of plugin with given name |
| 303 | * @param pluginName plugin short name to test |
| 304 | * @return boolean result |
| 305 | */ |
| 306 | @NonCPS |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 307 | def jenkinsHasPlugin(pluginName) { |
| 308 | return Jenkins.instance.pluginManager.plugins.collect { p -> p.shortName }.contains(pluginName) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @NonCPS |
| 312 | def _needNotification(notificatedTypes, buildStatus, jobName) { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 313 | if (notificatedTypes && notificatedTypes.contains("onchange")) { |
| 314 | if (jobName) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 315 | def job = Jenkins.instance.getItem(jobName) |
| 316 | def numbuilds = job.builds.size() |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 317 | if (numbuilds > 0) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 318 | //actual build is first for some reasons, so last finished build is second |
| 319 | def lastBuild = job.builds[1] |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 320 | if (lastBuild) { |
| 321 | if (lastBuild.result.toString().toLowerCase().equals(buildStatus)) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 322 | println("Build status didn't changed since last build, not sending notifications") |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 328 | } else if (!notificatedTypes.contains(buildStatus)) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 329 | return false; |
| 330 | } |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Send notification to all enabled notifications services |
| 336 | * @param buildStatus message type (success, warning, error), null means SUCCESSFUL |
| 337 | * @param msgText message text |
| 338 | * @param enabledNotifications list of enabled notification types, types: slack, hipchat, email, default empty |
| 339 | * @param notificatedTypes types of notifications will be sent, default onchange - notificate if current build result not equal last result; |
| 340 | * otherwise use - ["success","unstable","failed"] |
| 341 | * @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] | 342 | * @param buildNumber build number param, if empty env.BUILD_NUM will be used |
| 343 | * @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] | 344 | * @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] | 345 | * @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] | 346 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 347 | def sendNotification(buildStatus, msgText = "", enabledNotifications = [], notificatedTypes = ["onchange"], jobName = null, buildNumber = null, buildUrl = null, mailFrom = "jenkins", mailTo = null) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 348 | // Default values |
| 349 | def colorName = 'blue' |
| 350 | def colorCode = '#0000FF' |
| 351 | def buildStatusParam = buildStatus != null && buildStatus != "" ? buildStatus : "SUCCESS" |
| 352 | def jobNameParam = jobName != null && jobName != "" ? jobName : env.JOB_NAME |
| 353 | def buildNumberParam = buildNumber != null && buildNumber != "" ? buildNumber : env.BUILD_NUMBER |
| 354 | def buildUrlParam = buildUrl != null && buildUrl != "" ? buildUrl : env.BUILD_URL |
| 355 | def subject = "${buildStatusParam}: Job '${jobNameParam} [${buildNumberParam}]'" |
| 356 | def summary = "${subject} (${buildUrlParam})" |
| 357 | |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 358 | if (msgText != null && msgText != "") { |
| 359 | summary += "\n${msgText}" |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 360 | } |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 361 | if (buildStatusParam.toLowerCase().equals("success")) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 362 | colorCode = "#00FF00" |
| 363 | colorName = "green" |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 364 | } else if (buildStatusParam.toLowerCase().equals("unstable")) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 365 | colorCode = "#FFFF00" |
| 366 | colorName = "yellow" |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 367 | } else if (buildStatusParam.toLowerCase().equals("failure")) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 368 | colorCode = "#FF0000" |
| 369 | colorName = "red" |
| 370 | } |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 371 | if (_needNotification(notificatedTypes, buildStatusParam.toLowerCase(), jobNameParam)) { |
| 372 | if (enabledNotifications.contains("slack") && jenkinsHasPlugin("slack")) { |
| 373 | try { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 374 | slackSend color: colorCode, message: summary |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 375 | } catch (Exception e) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 376 | println("Calling slack plugin failed") |
| 377 | e.printStackTrace() |
| 378 | } |
| 379 | } |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 380 | if (enabledNotifications.contains("hipchat") && jenkinsHasPlugin("hipchat")) { |
| 381 | try { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 382 | hipchatSend color: colorName.toUpperCase(), message: summary |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 383 | } catch (Exception e) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 384 | println("Calling hipchat plugin failed") |
| 385 | e.printStackTrace() |
| 386 | } |
| 387 | } |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 388 | if (enabledNotifications.contains("email") && mailTo != null && mailTo != "" && mailFrom != null && mailFrom != "") { |
| 389 | try { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 390 | mail body: summary, from: mailFrom, subject: subject, to: mailTo |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 391 | } catch (Exception e) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 392 | println("Sending mail plugin failed") |
| 393 | e.printStackTrace() |
| 394 | } |
| 395 | } |
| 396 | } |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 397 | } |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 398 | |
| 399 | /** |
| 400 | * Execute linux command and catch nth element |
| 401 | * @param cmd command to execute |
| 402 | * @param index index to retrieve |
| 403 | * @return index-th element |
| 404 | */ |
| 405 | |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 406 | def cutOrDie(cmd, index) { |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 407 | def common = new com.mirantis.mk.Common() |
| 408 | def output |
| 409 | try { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 410 | output = sh(script: cmd, returnStdout: true) |
| 411 | def result = output.tokenize(" ")[index] |
| 412 | return result; |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 413 | } catch (Exception e) { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 414 | common.errorMsg("Failed to execute cmd: ${cmd}\n output: ${output}") |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 415 | } |
Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 416 | } |
Tomáš Kukrál | 767dd73 | 2017-03-23 10:38:59 +0100 | [diff] [blame] | 417 | |
| 418 | /** |
| 419 | * Check variable contains keyword |
| 420 | * @param variable keywork is searched (contains) here |
| 421 | * @param keyword string to look for |
| 422 | * @return True if variable contains keyword (case insensitive), False if do not contains or any of input isn't a string |
| 423 | */ |
| 424 | |
| 425 | def checkContains(variable, keyword) { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 426 | if (env.getEnvironment().containsKey(variable)) { |
Jakub Josef | 7a8dea2 | 2017-03-23 19:51:32 +0100 | [diff] [blame] | 427 | return env[variable] && env[variable].toLowerCase().contains(keyword.toLowerCase()) |
Tomáš Kukrál | 767dd73 | 2017-03-23 10:38:59 +0100 | [diff] [blame] | 428 | } else { |
Tomáš Kukrál | c76c1e0 | 2017-03-23 19:06:59 +0100 | [diff] [blame] | 429 | return false |
Tomáš Kukrál | 767dd73 | 2017-03-23 10:38:59 +0100 | [diff] [blame] | 430 | } |
| 431 | } |
Jakub Josef | a877db5 | 2017-04-05 14:22:30 +0200 | [diff] [blame] | 432 | |
| 433 | /** |
| 434 | * Parse JSON string to hashmap |
| 435 | * @param jsonString input JSON string |
| 436 | * @return created hashmap |
| 437 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 438 | def parseJSON(jsonString) { |
| 439 | def m = [:] |
| 440 | def lazyMap = new JsonSlurperClassic().parseText(jsonString) |
| 441 | m.putAll(lazyMap) |
| 442 | return m |
Jakub Josef | a877db5 | 2017-04-05 14:22:30 +0200 | [diff] [blame] | 443 | } |
Jakub Josef | ed239cd | 2017-05-09 15:27:33 +0200 | [diff] [blame] | 444 | |
| 445 | /** |
Vasyl Saienko | 79d6f3b | 2018-10-19 09:13:46 +0300 | [diff] [blame] | 446 | * |
| 447 | * Deep merge of Map items. Merges variable number of maps in to onto. |
| 448 | * Using the following rules: |
| 449 | * - Lists are appended |
| 450 | * - Maps are updated |
| 451 | * - other object types are replaced. |
| 452 | * |
| 453 | * |
| 454 | * @param onto Map object to merge in |
| 455 | * @param overrides Map objects to merge to onto |
| 456 | */ |
| 457 | def mergeMaps(Map onto, Map... overrides){ |
| 458 | if (!overrides){ |
| 459 | return onto |
| 460 | } |
| 461 | else if (overrides.length == 1) { |
| 462 | overrides[0]?.each { k, v -> |
| 463 | if (v in Map && onto[k] in Map){ |
| 464 | mergeMaps((Map) onto[k], (Map) v) |
| 465 | } else if (v in List) { |
| 466 | onto[k] += v |
| 467 | } else { |
| 468 | onto[k] = v |
| 469 | } |
| 470 | } |
| 471 | return onto |
| 472 | } |
| 473 | return overrides.inject(onto, { acc, override -> mergeMaps(acc, override ?: [:]) }) |
| 474 | } |
| 475 | |
| 476 | /** |
Jakub Josef | ed239cd | 2017-05-09 15:27:33 +0200 | [diff] [blame] | 477 | * Test pipeline input parameter existence and validity (not null and not empty string) |
| 478 | * @param paramName input parameter name (usually uppercase) |
azvyagintsev | c8ecdfd | 2018-09-11 12:47:15 +0300 | [diff] [blame] | 479 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 480 | def validInputParam(paramName) { |
azvyagintsev | c8ecdfd | 2018-09-11 12:47:15 +0300 | [diff] [blame] | 481 | if (paramName instanceof java.lang.String) { |
| 482 | return env.getEnvironment().containsKey(paramName) && env[paramName] != null && env[paramName] != "" |
| 483 | } |
| 484 | return false |
Tomáš Kukrál | d34fe87 | 2017-06-13 10:50:50 +0200 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Take list of hashmaps and count number of hashmaps with parameter equals eq |
| 489 | * @param lm list of hashmaps |
| 490 | * @param param define parameter of hashmap to read and compare |
| 491 | * @param eq desired value of hashmap parameter |
| 492 | * @return count of hashmaps meeting defined condition |
| 493 | */ |
| 494 | |
| 495 | @NonCPS |
| 496 | def countHashMapEquals(lm, param, eq) { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 497 | 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] | 498 | } |
Vasyl Saienko | fb055b1 | 2017-11-23 18:15:23 +0200 | [diff] [blame] | 499 | |
| 500 | /** |
| 501 | * Execute shell command and return stdout, stderr and status |
| 502 | * |
| 503 | * @param cmd Command to execute |
| 504 | * @return map with stdout, stderr, status keys |
| 505 | */ |
| 506 | |
| 507 | def shCmdStatus(cmd) { |
| 508 | def res = [:] |
| 509 | def stderr = sh(script: 'mktemp', returnStdout: true).trim() |
| 510 | def stdout = sh(script: 'mktemp', returnStdout: true).trim() |
| 511 | |
| 512 | try { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 513 | def status = sh(script: "${cmd} 1>${stdout} 2>${stderr}", returnStatus: true) |
Vasyl Saienko | fb055b1 | 2017-11-23 18:15:23 +0200 | [diff] [blame] | 514 | res['stderr'] = sh(script: "cat ${stderr}", returnStdout: true) |
| 515 | res['stdout'] = sh(script: "cat ${stdout}", returnStdout: true) |
| 516 | res['status'] = status |
| 517 | } finally { |
| 518 | sh(script: "rm ${stderr}", returnStdout: true) |
| 519 | sh(script: "rm ${stdout}", returnStdout: true) |
| 520 | } |
| 521 | |
| 522 | return res |
| 523 | } |
Richard Felkl | 66a242d | 2018-01-25 15:27:15 +0100 | [diff] [blame] | 524 | |
Richard Felkl | 66a242d | 2018-01-25 15:27:15 +0100 | [diff] [blame] | 525 | /** |
| 526 | * Retry commands passed to body |
| 527 | * |
| 528 | * @param times Number of retries |
Jakub Josef | 61463c7 | 2018-02-13 16:10:56 +0100 | [diff] [blame] | 529 | * @param delay Delay between retries (in seconds) |
Richard Felkl | 66a242d | 2018-01-25 15:27:15 +0100 | [diff] [blame] | 530 | * @param body Commands to be in retry block |
| 531 | * @return calling commands in body |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 532 | * @example retry ( 3 , 5 ) { function body }* retry{ function body } |
Richard Felkl | 66a242d | 2018-01-25 15:27:15 +0100 | [diff] [blame] | 533 | */ |
| 534 | |
| 535 | def retry(int times = 5, int delay = 0, Closure body) { |
| 536 | int retries = 0 |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 537 | while (retries++ < times) { |
Richard Felkl | 66a242d | 2018-01-25 15:27:15 +0100 | [diff] [blame] | 538 | try { |
| 539 | return body.call() |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 540 | } catch (e) { |
Denis Egorenko | 900a3af | 2019-01-14 12:54:56 +0400 | [diff] [blame^] | 541 | errorMsg(e.toString()) |
Richard Felkl | 66a242d | 2018-01-25 15:27:15 +0100 | [diff] [blame] | 542 | sleep(delay) |
| 543 | } |
| 544 | } |
Richard Felkl | 66a242d | 2018-01-25 15:27:15 +0100 | [diff] [blame] | 545 | throw new Exception("Failed after $times retries") |
| 546 | } |
Dmitry Pyzhov | 0d0d852 | 2018-05-15 22:37:37 +0300 | [diff] [blame] | 547 | |
Dmitry Pyzhov | 0d0d852 | 2018-05-15 22:37:37 +0300 | [diff] [blame] | 548 | /** |
| 549 | * Wait for user input with timeout |
| 550 | * |
| 551 | * @param timeoutInSeconds Timeout |
| 552 | * @param options Options for input widget |
| 553 | */ |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 554 | def waitForInputThenPass(timeoutInSeconds, options = [message: 'Ready to go?']) { |
| 555 | def userInput = true |
| 556 | try { |
| 557 | timeout(time: timeoutInSeconds, unit: 'SECONDS') { |
| 558 | userInput = input options |
| 559 | } |
| 560 | } catch (err) { // timeout reached or input false |
| 561 | def user = err.getCauses()[0].getUser() |
| 562 | if ('SYSTEM' == user.toString()) { // SYSTEM means timeout. |
| 563 | println("Timeout, proceeding") |
| 564 | } else { |
| 565 | userInput = false |
| 566 | println("Aborted by: [${user}]") |
| 567 | throw err |
| 568 | } |
Dmitry Pyzhov | 0d0d852 | 2018-05-15 22:37:37 +0300 | [diff] [blame] | 569 | } |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 570 | return userInput |
Dmitry Pyzhov | 0d0d852 | 2018-05-15 22:37:37 +0300 | [diff] [blame] | 571 | } |
Oleksii Grudev | 9e1d97a | 2018-06-29 16:04:30 +0300 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * Function receives Map variable as input and sorts it |
| 575 | * by values ascending. Returns sorted Map |
| 576 | * @param _map Map variable |
| 577 | */ |
| 578 | @NonCPS |
| 579 | def SortMapByValueAsc(_map) { |
azvyagintsev | 6bda942 | 2018-08-20 11:57:05 +0300 | [diff] [blame] | 580 | def sortedMap = _map.sort { it.value } |
Oleksii Grudev | 9e1d97a | 2018-06-29 16:04:30 +0300 | [diff] [blame] | 581 | return sortedMap |
| 582 | } |
azvyagintsev | 99d3584 | 2018-08-17 20:26:34 +0300 | [diff] [blame] | 583 | |
| 584 | /** |
| 585 | * Compare 'old' and 'new' dir's recursively |
| 586 | * @param diffData =' Only in new/XXX/infra: secrets.yml |
| 587 | Files old/XXX/init.yml and new/XXX/init.yml differ |
| 588 | Only in old/XXX/infra: secrets11.yml ' |
| 589 | * |
| 590 | * @return |
| 591 | * - new: |
| 592 | - XXX/secrets.yml |
| 593 | - diff: |
| 594 | - XXX/init.yml |
| 595 | - removed: |
| 596 | - XXX/secrets11.yml |
| 597 | |
| 598 | */ |
| 599 | def diffCheckMultidir(diffData) { |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 600 | common = new com.mirantis.mk.Common() |
| 601 | // Some global constants. Don't change\move them! |
| 602 | keyNew = 'new' |
| 603 | keyRemoved = 'removed' |
| 604 | keyDiff = 'diff' |
| 605 | def output = [ |
| 606 | new : [], |
| 607 | removed: [], |
| 608 | diff : [], |
| 609 | ] |
| 610 | String pathSep = '/' |
| 611 | diffData.each { line -> |
| 612 | def job_file = '' |
| 613 | def job_type = '' |
| 614 | if (line.startsWith('Files old/')) { |
| 615 | job_file = new File(line.replace('Files old/', '').tokenize()[0]) |
| 616 | job_type = keyDiff |
| 617 | } else if (line.startsWith('Only in new/')) { |
| 618 | // get clean normalized filepath, under new/ |
| 619 | job_file = new File(line.replace('Only in new/', '').replace(': ', pathSep)).toString() |
| 620 | job_type = keyNew |
| 621 | } else if (line.startsWith('Only in old/')) { |
| 622 | // get clean normalized filepath, under old/ |
| 623 | job_file = new File(line.replace('Only in old/', '').replace(': ', pathSep)).toString() |
| 624 | job_type = keyRemoved |
| 625 | } else { |
| 626 | common.warningMsg("Not parsed diff line: ${line}!") |
| 627 | } |
| 628 | if (job_file != '') { |
| 629 | output[job_type].push(job_file) |
| 630 | } |
azvyagintsev | 99d3584 | 2018-08-17 20:26:34 +0300 | [diff] [blame] | 631 | } |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 632 | return output |
azvyagintsev | 99d3584 | 2018-08-17 20:26:34 +0300 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Compare 2 folder, file by file |
| 637 | * Structure should be: |
| 638 | * ${compRoot}/ |
| 639 | └── diff - diff results will be save here |
| 640 | ├── new - input folder with data |
| 641 | ├── old - input folder with data |
| 642 | ├── pillar.diff - globall diff will be saved here |
| 643 | * b_url - usual env.BUILD_URL, to be add into description |
azvyagintsev | 3bbcafe | 2018-08-20 19:36:16 +0300 | [diff] [blame] | 644 | * grepOpts - General grep cmdline; Could be used to pass some magic |
| 645 | * regexp into after-diff listing file(pillar.diff) |
| 646 | * Example: '-Ev infra/secrets.yml' |
azvyagintsev | 99d3584 | 2018-08-17 20:26:34 +0300 | [diff] [blame] | 647 | * return - html-based string |
| 648 | * TODO: allow to specify subdir for results? |
azvyagintsev | 99d3584 | 2018-08-17 20:26:34 +0300 | [diff] [blame] | 649 | **/ |
Denis Egorenko | 4551f37 | 2018-09-11 16:36:13 +0400 | [diff] [blame] | 650 | |
azvyagintsev | 3bbcafe | 2018-08-20 19:36:16 +0300 | [diff] [blame] | 651 | def comparePillars(compRoot, b_url, grepOpts) { |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 652 | |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 653 | // Some global constants. Don't change\move them! |
| 654 | keyNew = 'new' |
| 655 | keyRemoved = 'removed' |
| 656 | keyDiff = 'diff' |
| 657 | def diff_status = 0 |
| 658 | // FIXME |
| 659 | httpWS = b_url + '/artifact/' |
| 660 | dir(compRoot) { |
azvyagintsev | 3bbcafe | 2018-08-20 19:36:16 +0300 | [diff] [blame] | 661 | // If diff empty - exit 0 |
| 662 | diff_status = sh(script: 'diff -q -r old/ new/ > pillar.diff', |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 663 | returnStatus: true, |
| 664 | ) |
azvyagintsev | 24d4965 | 2018-08-21 19:33:51 +0300 | [diff] [blame] | 665 | } |
azvyagintsev | c0fe144 | 2018-08-21 20:01:34 +0300 | [diff] [blame] | 666 | // Unfortunately, diff not able to work with dir-based regexp |
| 667 | if (diff_status == 1 && grepOpts) { |
| 668 | dir(compRoot) { |
| 669 | grep_status = sh(script: """ |
azvyagintsev | 3bbcafe | 2018-08-20 19:36:16 +0300 | [diff] [blame] | 670 | cp -v pillar.diff pillar_orig.diff |
| 671 | grep ${grepOpts} pillar_orig.diff > pillar.diff |
| 672 | """, |
azvyagintsev | c0fe144 | 2018-08-21 20:01:34 +0300 | [diff] [blame] | 673 | returnStatus: true |
| 674 | ) |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 675 | if (grep_status == 1) { |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 676 | warningMsg("Grep regexp ${grepOpts} removed all diff!") |
azvyagintsev | c0fe144 | 2018-08-21 20:01:34 +0300 | [diff] [blame] | 677 | diff_status = 0 |
azvyagintsev | b99f87c | 2018-08-21 19:43:59 +0300 | [diff] [blame] | 678 | } |
azvyagintsev | 3bbcafe | 2018-08-20 19:36:16 +0300 | [diff] [blame] | 679 | } |
azvyagintsev | c0fe144 | 2018-08-21 20:01:34 +0300 | [diff] [blame] | 680 | } |
| 681 | // Set job description |
Denis Egorenko | 4551f37 | 2018-09-11 16:36:13 +0400 | [diff] [blame] | 682 | description = '' |
azvyagintsev | c0fe144 | 2018-08-21 20:01:34 +0300 | [diff] [blame] | 683 | if (diff_status == 1) { |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 684 | // Analyse output file and prepare array with results |
| 685 | String data_ = readFile file: "${compRoot}/pillar.diff" |
| 686 | def diff_list = diffCheckMultidir(data_.split("\\r?\\n")) |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 687 | infoMsg(diff_list) |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 688 | dir(compRoot) { |
| 689 | if (diff_list[keyDiff].size() > 0) { |
| 690 | if (!fileExists('diff')) { |
| 691 | sh('mkdir -p diff') |
| 692 | } |
| 693 | description += '<b>CHANGED</b><ul>' |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 694 | infoMsg('Changed items:') |
Denis Egorenko | 4551f37 | 2018-09-11 16:36:13 +0400 | [diff] [blame] | 695 | def stepsForParallel = [:] |
| 696 | stepsForParallel.failFast = true |
| 697 | diff_list[keyDiff].each { |
| 698 | stepsForParallel.put("Differ for:${it}", |
| 699 | { |
| 700 | // We don't want to handle sub-dirs structure. So, simply make diff 'flat' |
| 701 | def item_f = it.toString().replace('/', '_') |
| 702 | description += "<li><a href=\"${httpWS}/diff/${item_f}/*view*/\">${it}</a></li>" |
| 703 | // Generate diff file |
| 704 | def diff_exit_code = sh([ |
| 705 | script : "diff -U 50 old/${it} new/${it} > diff/${item_f}", |
| 706 | returnStdout: false, |
| 707 | returnStatus: true, |
| 708 | ]) |
| 709 | // catch normal errors, diff should always return 1 |
| 710 | if (diff_exit_code != 1) { |
| 711 | error 'Error with diff file generation' |
| 712 | } |
| 713 | }) |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 714 | } |
Denis Egorenko | 4551f37 | 2018-09-11 16:36:13 +0400 | [diff] [blame] | 715 | |
| 716 | parallel stepsForParallel |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 717 | } |
| 718 | if (diff_list[keyNew].size() > 0) { |
| 719 | description += '<b>ADDED</b><ul>' |
| 720 | for (item in diff_list[keyNew]) { |
| 721 | description += "<li><a href=\"${httpWS}/new/${item}/*view*/\">${item}</a></li>" |
| 722 | } |
| 723 | } |
| 724 | if (diff_list[keyRemoved].size() > 0) { |
| 725 | description += '<b>DELETED</b><ul>' |
| 726 | for (item in diff_list[keyRemoved]) { |
| 727 | description += "<li><a href=\"${httpWS}/old/${item}/*view*/\">${item}</a></li>" |
| 728 | } |
| 729 | } |
azvyagintsev | 99d3584 | 2018-08-17 20:26:34 +0300 | [diff] [blame] | 730 | |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 731 | } |
azvyagintsev | 99d3584 | 2018-08-17 20:26:34 +0300 | [diff] [blame] | 732 | } |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 733 | |
| 734 | if (description != '') { |
| 735 | dir(compRoot) { |
| 736 | archiveArtifacts([ |
| 737 | artifacts : '**', |
| 738 | allowEmptyArchive: true, |
| 739 | ]) |
| 740 | } |
| 741 | return description.toString() |
| 742 | } else { |
azvyagintsev | a57c82a | 2018-09-20 12:17:24 +0300 | [diff] [blame] | 743 | return '<b>No job changes</b>' |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 744 | } |
azvyagintsev | 99d3584 | 2018-08-17 20:26:34 +0300 | [diff] [blame] | 745 | } |
azvyagintsev | ab5637b | 2018-08-20 18:18:15 +0300 | [diff] [blame] | 746 | |
| 747 | /** |
| 748 | * Simple function, to get basename from string. |
| 749 | * line - path-string |
| 750 | * remove_ext - string, optionl. Drop file extenstion. |
| 751 | **/ |
| 752 | def GetBaseName(line, remove_ext) { |
| 753 | filename = line.toString().split('/').last() |
| 754 | if (remove_ext && filename.endsWith(remove_ext.toString())) { |
| 755 | filename = filename.take(filename.lastIndexOf(remove_ext.toString())) |
| 756 | } |
| 757 | return filename |
Vasyl Saienko | 00ef98b | 2018-09-05 10:34:32 +0300 | [diff] [blame] | 758 | } |
Vasyl Saienko | 723cbbc | 2018-09-05 11:08:52 +0300 | [diff] [blame] | 759 | |
| 760 | /** |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 761 | * Return colored string of specific stage in stageMap |
| 762 | * |
| 763 | * @param stageMap LinkedHashMap object. |
| 764 | * @param stageName The name of current stage we are going to execute. |
| 765 | * @param color Text color |
| 766 | * */ |
| 767 | def getColoredStageView(stageMap, stageName, color) { |
| 768 | def stage = stageMap[stageName] |
| 769 | def banner = [] |
| 770 | def currentStageIndex = new ArrayList<String>(stageMap.keySet()).indexOf(stageName) |
| 771 | def numberOfStages = stageMap.keySet().size() - 1 |
Vasyl Saienko | 723cbbc | 2018-09-05 11:08:52 +0300 | [diff] [blame] | 772 | |
Vasyl Saienko | 723cbbc | 2018-09-05 11:08:52 +0300 | [diff] [blame] | 773 | banner.add(getColorizedString( |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 774 | "=========== Stage ${currentStageIndex}/${numberOfStages}: ${stageName} ===========", color)) |
| 775 | for (stage_item in stage.keySet()) { |
| 776 | banner.add(getColorizedString( |
| 777 | "${stage_item}: ${stage[stage_item]}", color)) |
Vasyl Saienko | 723cbbc | 2018-09-05 11:08:52 +0300 | [diff] [blame] | 778 | } |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 779 | banner.add('\n') |
| 780 | |
| 781 | return banner |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Pring stageMap to console with specified color |
| 786 | * |
| 787 | * @param stageMap LinkedHashMap object with stages information. |
| 788 | * @param currentStage The name of current stage we are going to execute. |
| 789 | * |
| 790 | * */ |
| 791 | def printCurrentStage(stageMap, currentStage) { |
| 792 | print getColoredStageView(stageMap, currentStage, "cyan").join('\n') |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Pring stageMap to console with specified color |
| 797 | * |
| 798 | * @param stageMap LinkedHashMap object. |
| 799 | * @param baseColor Text color (default white) |
| 800 | * */ |
| 801 | def printStageMap(stageMap, baseColor = "white") { |
| 802 | def banner = [] |
| 803 | def index = 0 |
| 804 | for (stage_name in stageMap.keySet()) { |
| 805 | banner.addAll(getColoredStageView(stageMap, stage_name, baseColor)) |
| 806 | } |
| 807 | print banner.join('\n') |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * Wrap provided code in stage, and do interactive retires if needed. |
| 812 | * |
| 813 | * @param stageMap LinkedHashMap object with stages information. |
| 814 | * @param currentStage The name of current stage we are going to execute. |
| 815 | * @param target Target host to execute stage on. |
| 816 | * @param interactive Boolean flag to specify if interaction with user is enabled. |
| 817 | * @param body Command to be in stage block. |
| 818 | * */ |
| 819 | def stageWrapper(stageMap, currentStage, target, interactive = true, Closure body) { |
| 820 | def common = new com.mirantis.mk.Common() |
| 821 | def banner = [] |
| 822 | |
| 823 | printCurrentStage(stageMap, currentStage) |
| 824 | |
| 825 | stage(currentStage) { |
Vasyl Saienko | bc6debb | 2018-09-10 14:08:09 +0300 | [diff] [blame] | 826 | if (interactive){ |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 827 | input message: getColorizedString("We are going to execute stage \'${currentStage}\' on the following target ${target}.\nPlease review stage information above.", "yellow") |
Vasyl Saienko | bc6debb | 2018-09-10 14:08:09 +0300 | [diff] [blame] | 828 | } |
| 829 | try { |
Vasyl Saienko | bc6debb | 2018-09-10 14:08:09 +0300 | [diff] [blame] | 830 | stageMap[currentStage]['Status'] = "SUCCESS" |
Victor Ryzhenkin | 49d6781 | 2019-01-09 15:28:21 +0400 | [diff] [blame] | 831 | return body.call() |
Vasyl Saienko | bc6debb | 2018-09-10 14:08:09 +0300 | [diff] [blame] | 832 | } catch (Exception err) { |
| 833 | def msg = "Stage ${currentStage} failed with the following exception:\n${err}" |
| 834 | print getColorizedString(msg, "yellow") |
| 835 | common.errorMsg(err) |
| 836 | if (interactive) { |
| 837 | input message: getColorizedString("Please make sure problem is fixed to proceed with retry. Ready to proceed?", "yellow") |
| 838 | stageMap[currentStage]['Status'] = "RETRYING" |
| 839 | stageWrapper(stageMap, currentStage, target, interactive, body) |
| 840 | } else { |
| 841 | error(msg) |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 842 | } |
Vasyl Saienko | bc6debb | 2018-09-10 14:08:09 +0300 | [diff] [blame] | 843 | } |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Ugly transition solution for internal tests. |
| 849 | * 1) Check input => transform to static result, based on runtime and input |
| 850 | * 2) Check remote-binary repo for exact resource |
azvyagintsev | f6fce3d | 2018-12-28 15:30:33 +0200 | [diff] [blame] | 851 | * Return: changes each linux_system_* cto false, in case broken url in some of them |
| 852 | */ |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 853 | |
| 854 | def checkRemoteBinary(LinkedHashMap config, List extraScmExtensions = []) { |
| 855 | def common = new com.mirantis.mk.Common() |
azvyagintsev | f6fce3d | 2018-12-28 15:30:33 +0200 | [diff] [blame] | 856 | def res = [:] |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 857 | res['MirrorRoot'] = config.get('globalMirrorRoot', env["BIN_MIRROR_ROOT"] ? env["BIN_MIRROR_ROOT"] : "http://mirror.mirantis.com/") |
| 858 | // Reclass-like format's. To make life eazy! |
azvyagintsev | 603d95b | 2018-11-09 15:37:10 +0200 | [diff] [blame] | 859 | res['mcp_version'] = config.get('mcp_version', env["BIN_APT_MCP_VERSION"] ? env["BIN_APT_MCP_VERSION"] : 'nightly') |
azvyagintsev | f6fce3d | 2018-12-28 15:30:33 +0200 | [diff] [blame] | 860 | res['linux_system_repo_url'] = config.get('linux_system_repo_url', env['BIN_linux_system_repo_url'] ? env['BIN_linux_system_repo_url'] : "${res['MirrorRoot']}/${res['mcp_version']}/") |
| 861 | res['linux_system_repo_ubuntu_url'] = config.get('linux_system_repo_ubuntu_url', env['BIN_linux_system_repo_ubuntu_url'] ? env['BIN_linux_system_repo_ubuntu_url'] : "${res['MirrorRoot']}/${res['mcp_version']}/ubuntu/") |
| 862 | res['linux_system_repo_mcp_salt_url'] = config.get('linux_system_repo_mcp_salt_url', env['BIN_linux_system_repo_mcp_salt_url'] ? env['BIN_linux_system_repo_mcp_salt_url'] : "${res['MirrorRoot']}/${res['mcp_version']}/salt-formulas/") |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 863 | |
| 864 | if (config.get('verify', true)) { |
azvyagintsev | f6fce3d | 2018-12-28 15:30:33 +0200 | [diff] [blame] | 865 | res.each { key, val -> |
| 866 | if (key.toString().startsWith('linux_system_repo')) { |
| 867 | def MirrorRootStatus = sh(script: "wget --auth-no-challenge --spider ${val} 2>/dev/null", returnStatus: true) |
| 868 | if (MirrorRootStatus != 0) { |
| 869 | common.warningMsg("Resource: '${key}' at '${val}' not exist!") |
| 870 | res[key] = false |
| 871 | } |
| 872 | } |
azvyagintsev | f6e7791 | 2018-09-07 15:41:09 +0300 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | return res |
Vasyl Saienko | 723cbbc | 2018-09-05 11:08:52 +0300 | [diff] [blame] | 876 | } |
Denis Egorenko | eded0d4 | 2018-09-26 13:25:49 +0400 | [diff] [blame] | 877 | |
| 878 | /** |
| 879 | * Workaround to update env properties, like GERRIT_* vars, |
| 880 | * which should be passed from upstream job to downstream. |
| 881 | * Will not fail entire job in case any issues. |
| 882 | * @param envVar - EnvActionImpl env job |
| 883 | * @param extraVars - Multiline YAML text with extra vars |
| 884 | */ |
| 885 | def mergeEnv(envVar, extraVars) { |
Denis Egorenko | 6d3cc23 | 2018-09-27 17:42:58 +0400 | [diff] [blame] | 886 | def common = new com.mirantis.mk.Common() |
Denis Egorenko | eded0d4 | 2018-09-26 13:25:49 +0400 | [diff] [blame] | 887 | try { |
| 888 | def extraParams = readYaml text: extraVars |
| 889 | for(String key in extraParams.keySet()) { |
| 890 | envVar[key] = extraParams[key] |
| 891 | common.warningMsg("Parameter ${key} is updated from EXTRA vars.") |
| 892 | } |
| 893 | } catch (Exception e) { |
| 894 | common.errorMsg("Can't update env parameteres, because: ${e.toString()}") |
| 895 | } |
| 896 | } |
Denis Egorenko | 599bd63 | 2018-09-28 15:24:37 +0400 | [diff] [blame] | 897 | |
| 898 | /** |
| 899 | * Wrapper around parallel pipeline function |
| 900 | * with ability to restrict number of parallel threads |
| 901 | * running simultaneously |
| 902 | * |
| 903 | * @param branches - Map with Clousers to be executed |
| 904 | * @param maxParallelJob - Integer number of parallel threads allowed |
| 905 | * to run simultaneously |
| 906 | */ |
| 907 | def runParallel(branches, maxParallelJob = 10) { |
| 908 | def runningSteps = 0 |
| 909 | branches.each { branchName, branchBody -> |
| 910 | if (branchBody instanceof Closure) { |
| 911 | branches[branchName] = { |
| 912 | while (!(runningSteps < maxParallelJob)) { |
| 913 | continue |
| 914 | } |
| 915 | runningSteps += 1 |
| 916 | branchBody.call() |
| 917 | runningSteps -= 1 |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | if (branches) { |
| 922 | parallel branches |
| 923 | } |
| 924 | } |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 925 | |
| 926 | /** |
| 927 | * Ugly processing basic funcs with /etc/apt |
Denis Egorenko | 5cea141 | 2018-10-18 16:40:11 +0400 | [diff] [blame] | 928 | * @param repoConfig YAML text or Map |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 929 | * Example : |
Denis Egorenko | 5cea141 | 2018-10-18 16:40:11 +0400 | [diff] [blame] | 930 | repoConfig = ''' |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 931 | --- |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 932 | aprConfD: |- |
Denis Egorenko | e02a1b2 | 2018-10-19 17:47:53 +0400 | [diff] [blame] | 933 | APT::Get::AllowUnauthenticated 'true'; |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 934 | repo: |
Denis Egorenko | e02a1b2 | 2018-10-19 17:47:53 +0400 | [diff] [blame] | 935 | mcp_saltstack: |
| 936 | source: "deb [arch=amd64] http://mirror.mirantis.com/nightly/saltstack-2017.7/xenial xenial main" |
| 937 | pin: |
| 938 | - package: "libsodium18" |
| 939 | pin: "release o=SaltStack" |
| 940 | priority: 50 |
| 941 | - package: "*" |
| 942 | pin: "release o=SaltStack" |
| 943 | priority: "1100" |
| 944 | repo_key: "http://mirror.mirantis.com/public.gpg" |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 945 | ''' |
| 946 | * |
| 947 | */ |
| 948 | |
Denis Egorenko | 5cea141 | 2018-10-18 16:40:11 +0400 | [diff] [blame] | 949 | def debianExtraRepos(repoConfig) { |
| 950 | def config = null |
| 951 | if (repoConfig instanceof Map) { |
| 952 | config = repoConfig |
| 953 | } else { |
| 954 | config = readYaml text: repoConfig |
| 955 | } |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 956 | if (config.get('repo', false)) { |
| 957 | for (String repo in config['repo'].keySet()) { |
Denis Egorenko | 395aa21 | 2018-10-11 15:11:28 +0400 | [diff] [blame] | 958 | source = config['repo'][repo]['source'] |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 959 | warningMsg("Write ${source} > /etc/apt/sources.list.d/${repo}.list") |
| 960 | sh("echo '${source}' > /etc/apt/sources.list.d/${repo}.list") |
Denis Egorenko | 395aa21 | 2018-10-11 15:11:28 +0400 | [diff] [blame] | 961 | if (config['repo'][repo].containsKey('repo_key')) { |
| 962 | key = config['repo'][repo]['repo_key'] |
| 963 | sh("wget -O - '${key}' | apt-key add -") |
| 964 | } |
Denis Egorenko | e02a1b2 | 2018-10-19 17:47:53 +0400 | [diff] [blame] | 965 | if (config['repo'][repo]['pin']) { |
| 966 | def repoPins = [] |
| 967 | for (Map pin in config['repo'][repo]['pin']) { |
| 968 | repoPins.add("Package: ${pin['package']}") |
| 969 | repoPins.add("Pin: ${pin['pin']}") |
| 970 | repoPins.add("Pin-Priority: ${pin['priority']}") |
Denis Egorenko | 1c93d12 | 2018-11-02 12:14:05 +0400 | [diff] [blame] | 971 | // additional empty line between pins |
| 972 | repoPins.add('\n') |
Denis Egorenko | e02a1b2 | 2018-10-19 17:47:53 +0400 | [diff] [blame] | 973 | } |
| 974 | if (repoPins) { |
| 975 | repoPins.add(0, "### Extra ${repo} repo pin start ###") |
| 976 | repoPins.add("### Extra ${repo} repo pin end ###") |
| 977 | repoPinning = repoPins.join('\n') |
| 978 | warningMsg("Adding pinning \n${repoPinning}\n => /etc/apt/preferences.d/${repo}") |
| 979 | sh("echo '${repoPinning}' > /etc/apt/preferences.d/${repo}") |
| 980 | } |
| 981 | } |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | if (config.get('aprConfD', false)) { |
| 985 | for (String pref in config['aprConfD'].tokenize('\n')) { |
| 986 | warningMsg("Adding ${pref} => /etc/apt/apt.conf.d/99setupAndTestNode") |
| 987 | sh("echo '${pref}' >> /etc/apt/apt.conf.d/99setupAndTestNode") |
| 988 | } |
| 989 | sh('cat /etc/apt/apt.conf.d/99setupAndTestNode') |
| 990 | } |
| 991 | } |