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