blob: 448935fedd2353304c350b748ae6e5b96ef76635 [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package com.mirantis.mk
Jakub Josef6d8082b2017-08-09 12:40:50 +02002
Jakub Josefb41c8d52017-03-24 13:52:24 +01003import static groovy.json.JsonOutput.prettyPrint
4import static groovy.json.JsonOutput.toJson
Jakub Josef6d8082b2017-08-09 12:40:50 +02005
Alexandr Lovtsov2b668e52020-12-16 15:58:24 +03006import groovy.time.TimeCategory
7
Jakub Josefbceaa322017-06-13 18:28:27 +02008import com.cloudbees.groovy.cps.NonCPS
Jakub Josefb7ab8472017-04-05 14:56:53 +02009import groovy.json.JsonSlurperClassic
azvyagintsev6bda9422018-08-20 11:57:05 +030010
Dmitry Teselkin4badf832020-05-27 17:48:36 +030011import org.jenkinsci.plugins.workflow.cps.EnvActionImpl
12
Jakub Josef79ecec32017-02-17 14:36:28 +010013/**
14 *
15 * Common functions
16 *
17 */
18
19/**
20 * Generate current timestamp
21 *
azvyagintsev6bda9422018-08-20 11:57:05 +030022 * @param format Defaults to yyyyMMddHHmmss
Jakub Josef79ecec32017-02-17 14:36:28 +010023 */
azvyagintsev6bda9422018-08-20 11:57:05 +030024def getDatetime(format = "yyyyMMddHHmmss") {
Jakub Josef79ecec32017-02-17 14:36:28 +010025 def now = new Date();
26 return now.format(format, TimeZone.getTimeZone('UTC'));
27}
28
29/**
Alexandr Lovtsov2b668e52020-12-16 15:58:24 +030030 * Return Duration for given datetime period with suffix
31 *
32 * @param input String in format '\d+[smhd]', to convert given number into seconds, minutes, hours
33 * and days duration respectively. For example: '7d' is for 7 days, '10m' - 10 minutes
34 * and so on. Return null if input in incorrect format
35 */
36def getDuration(String input) {
37 // Verify input format
38 if (!input.matches('[0-9]+[smhd]')) {
39 errorMsg("Incorrect input data for getDuration(): ${input}")
40 return
41 }
42 switch (input[-1]) {
43 case 's':
44 return TimeCategory.getSeconds(input[0..-2].toInteger())
45 case 'm':
46 return TimeCategory.getMinutes(input[0..-2].toInteger())
47 case 'h':
48 return TimeCategory.getHours(input[0..-2].toInteger())
49 case 'd':
50 return TimeCategory.getDays(input[0..-2].toInteger())
51 }
52}
53
54/**
Jakub Josef79ecec32017-02-17 14:36:28 +010055 * Return workspace.
56 * Currently implemented by calling pwd so it won't return relevant result in
57 * dir context
58 */
azvyagintsev6bda9422018-08-20 11:57:05 +030059def getWorkspace(includeBuildNum = false) {
Jakub Josef79ecec32017-02-17 14:36:28 +010060 def workspace = sh script: 'pwd', returnStdout: true
61 workspace = workspace.trim()
azvyagintsev6bda9422018-08-20 11:57:05 +030062 if (includeBuildNum) {
63 if (!workspace.endsWith("/")) {
64 workspace += "/"
65 }
66 workspace += env.BUILD_NUMBER
Jakub Josefa661b8c2018-01-17 14:51:25 +010067 }
Jakub Josef79ecec32017-02-17 14:36:28 +010068 return workspace
69}
70
71/**
Dmitry Teselkind4adf972020-02-13 18:24:59 +030072 * Get absolute path via 'realink'
73 * -m, --canonicalize-missing
74 * canonicalize by following every symlink in every component of the given name recursively,
75 * without requirements on components existence
76 */
77def getAbsolutePath(String path) {
78 def absPath = sh script: "readlink -m ${path}", returnStdout: true
79 return absPath.trim()
80}
81
82/**
Filip Pytloun81c864d2017-03-21 15:19:30 +010083 * Get UID of jenkins user.
84 * Must be run from context of node
85 */
86def getJenkinsUid() {
azvyagintsev6bda9422018-08-20 11:57:05 +030087 return sh(
Filip Pytloun81c864d2017-03-21 15:19:30 +010088 script: 'id -u',
89 returnStdout: true
90 ).trim()
91}
92
93/**
94 * Get GID of jenkins user.
95 * Must be run from context of node
96 */
97def getJenkinsGid() {
azvyagintsev6bda9422018-08-20 11:57:05 +030098 return sh(
Filip Pytloun81c864d2017-03-21 15:19:30 +010099 script: 'id -g',
100 returnStdout: true
101 ).trim()
102}
103
104/**
Jakub Josefc8074db2018-01-30 13:33:20 +0100105 * Returns Jenkins user uid and gid in one list (in that order)
106 * Must be run from context of node
107 */
azvyagintsev6bda9422018-08-20 11:57:05 +0300108def getJenkinsUserIds() {
Jakub Josefc8074db2018-01-30 13:33:20 +0100109 return sh(script: "id -u && id -g", returnStdout: true).tokenize("\n")
110}
111
112/**
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100113 *
114 * Find credentials by ID
115 *
azvyagintsev6bda9422018-08-20 11:57:05 +0300116 * @param credsId Credentials ID
117 * @param credsType Credentials type (optional)
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100118 *
119 */
120def getCredentialsById(String credsId, String credsType = 'any') {
121 def credClasses = [ // ordered by class name
azvyagintsev6bda9422018-08-20 11:57:05 +0300122 sshKey : com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.class,
123 cert : com.cloudbees.plugins.credentials.common.CertificateCredentials.class,
124 password : com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
125 any : com.cloudbees.plugins.credentials.impl.BaseStandardCredentials.class,
126 dockerCert: org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials.class,
127 file : org.jenkinsci.plugins.plaincredentials.FileCredentials.class,
128 string : org.jenkinsci.plugins.plaincredentials.StringCredentials.class,
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100129 ]
130 return com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
131 credClasses[credsType],
132 jenkins.model.Jenkins.instance
azvyagintsev6bda9422018-08-20 11:57:05 +0300133 ).findAll { cred -> cred.id == credsId }[0]
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100134}
135
136/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100137 * Get credentials from store
138 *
azvyagintsev6bda9422018-08-20 11:57:05 +0300139 * @param id Credentials name
Jakub Josef79ecec32017-02-17 14:36:28 +0100140 */
Jakub Josef3d9d9ab2017-03-14 15:09:03 +0100141def getCredentials(id, cred_type = "username_password") {
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100142 warningMsg('You are using obsolete function. Please switch to use `getCredentialsById()`')
Jakub Josef79ecec32017-02-17 14:36:28 +0100143
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100144 type_map = [
145 username_password: 'password',
azvyagintsev6bda9422018-08-20 11:57:05 +0300146 key : 'sshKey',
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100147 ]
Jakub Josef79ecec32017-02-17 14:36:28 +0100148
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100149 return getCredentialsById(id, type_map[cred_type])
Jakub Josef79ecec32017-02-17 14:36:28 +0100150}
151
152/**
153 * Abort build, wait for some time and ensure we will terminate
154 */
155def abortBuild() {
156 currentBuild.build().doStop()
157 sleep(180)
158 // just to be sure we will terminate
159 throw new InterruptedException()
160}
161
162/**
Jakub Josefbceaa322017-06-13 18:28:27 +0200163 * Print pretty-printed string representation of given item
164 * @param item item to be pretty-printed (list, map, whatever)
165 */
azvyagintsev6bda9422018-08-20 11:57:05 +0300166def prettyPrint(item) {
Jakub Josefbceaa322017-06-13 18:28:27 +0200167 println prettify(item)
168}
169
170/**
Jakub Josefb41c8d52017-03-24 13:52:24 +0100171 * Return pretty-printed string representation of given item
172 * @param item item to be pretty-printed (list, map, whatever)
173 * @return pretty-printed string
174 */
azvyagintsev6bda9422018-08-20 11:57:05 +0300175def prettify(item) {
Jakub Josefbceaa322017-06-13 18:28:27 +0200176 return groovy.json.JsonOutput.prettyPrint(toJson(item)).replace('\\n', System.getProperty('line.separator'))
Jakub Josefb41c8d52017-03-24 13:52:24 +0100177}
178
179/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100180 * Print informational message
181 *
182 * @param msg
183 * @param color Colorful output or not
184 */
185def infoMsg(msg, color = true) {
186 printMsg(msg, "cyan")
187}
188
189/**
Sergey Galkin7137ad02019-11-07 14:52:13 +0400190 * Print informational message
191 *
192 * @param msg
193 * @param color Colorful output or not
194 */
195def infoSensitivityMsg(msg, color = true, replacing = []) {
196 printSensitivityMsg(msg, "cyan", replacing)
197}
198
199/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100200 * Print error message
201 *
202 * @param msg
203 * @param color Colorful output or not
204 */
205def errorMsg(msg, color = true) {
206 printMsg(msg, "red")
207}
208
209/**
210 * Print success message
211 *
212 * @param msg
213 * @param color Colorful output or not
214 */
215def successMsg(msg, color = true) {
216 printMsg(msg, "green")
217}
218
219/**
220 * Print warning message
221 *
222 * @param msg
223 * @param color Colorful output or not
224 */
225def warningMsg(msg, color = true) {
Jakub Josef0e7bd632017-03-16 16:25:05 +0100226 printMsg(msg, "yellow")
Jakub Josef79ecec32017-02-17 14:36:28 +0100227}
228
229/**
Jakub Josef952ae0b2017-03-14 19:04:21 +0100230 * Print debug message, this message will show only if DEBUG global variable is present
231 * @param msg
232 * @param color Colorful output or not
233 */
azvyagintsev6bda9422018-08-20 11:57:05 +0300234def debugMsg(msg, color = true) {
Jakub Josef9a836ac2017-04-24 12:26:02 +0200235 // if debug property exists on env, debug is enabled
azvyagintsev6bda9422018-08-20 11:57:05 +0300236 if (env.getEnvironment().containsKey('DEBUG') && env['DEBUG'] == "true") {
Jakub Josef74b34692017-03-15 12:10:57 +0100237 printMsg("[DEBUG] ${msg}", "red")
Jakub Josef952ae0b2017-03-14 19:04:21 +0100238 }
239}
240
azvyagintsevf6e77912018-09-07 15:41:09 +0300241def getColorizedString(msg, color) {
242 def colorMap = [
243 'red' : '\u001B[31m',
244 'black' : '\u001B[30m',
245 'green' : '\u001B[32m',
246 'yellow': '\u001B[33m',
247 'blue' : '\u001B[34m',
248 'purple': '\u001B[35m',
249 'cyan' : '\u001B[36m',
250 'white' : '\u001B[37m',
251 'reset' : '\u001B[0m'
252 ]
Vasyl Saienko00ef98b2018-09-05 10:34:32 +0300253
azvyagintsevf6e77912018-09-07 15:41:09 +0300254 return "${colorMap[color]}${msg}${colorMap.reset}"
Vasyl Saienko00ef98b2018-09-05 10:34:32 +0300255}
256
Jakub Josef952ae0b2017-03-14 19:04:21 +0100257/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100258 * Print message
259 *
azvyagintsev6bda9422018-08-20 11:57:05 +0300260 * @param msg Message to be printed
Vasyl Saienko00ef98b2018-09-05 10:34:32 +0300261 * @param color Color to use for output
Jakub Josef79ecec32017-02-17 14:36:28 +0100262 */
Vasyl Saienko00ef98b2018-09-05 10:34:32 +0300263def printMsg(msg, color) {
264 print getColorizedString(msg, color)
Jakub Josef79ecec32017-02-17 14:36:28 +0100265}
266
267/**
Sergey Galkin7137ad02019-11-07 14:52:13 +0400268 * Print sensitivity message
269 *
270 * @param msg Message to be printed
271 * @param color Color to use for output
272 * @param replacing List with maps for deletion (passwords, logins, etc).
273 * The first () matching is mandatory !
274 * Example:
275 * [/ (OS_PASSWORD=)(.*?)+ /,
276 * / (password = )(.*?)+ /,
277 * / (password )(.*?) / ]
278 */
279def printSensitivityMsg(msg, color, replacing = []) {
280 for (i in replacing) {
281 msg = msg.replaceAll(i, ' $1XXXXXX ')
282 }
283 printMsg(msg, color)
284}
285
286/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100287 * Traverse directory structure and return list of files
288 *
289 * @param path Path to search
290 * @param type Type of files to search (groovy.io.FileType.FILES)
291 */
292@NonCPS
azvyagintsev6bda9422018-08-20 11:57:05 +0300293def getFiles(path, type = groovy.io.FileType.FILES) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100294 files = []
295 new File(path).eachFile(type) {
296 files[] = it
297 }
298 return files
299}
300
301/**
302 * Helper method to convert map into form of list of [key,value] to avoid
303 * unserializable exceptions
304 *
305 * @param m Map
306 */
307@NonCPS
308def entries(m) {
azvyagintsev6bda9422018-08-20 11:57:05 +0300309 m.collect { k, v -> [k, v] }
Jakub Josef79ecec32017-02-17 14:36:28 +0100310}
311
312/**
313 * Opposite of build-in parallel, run map of steps in serial
314 *
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200315 * @param steps Map of String<name>: CPSClosure2<step> (or list of closures)
Jakub Josef79ecec32017-02-17 14:36:28 +0100316 */
317def serial(steps) {
318 stepsArray = entries(steps)
azvyagintsev6bda9422018-08-20 11:57:05 +0300319 for (i = 0; i < stepsArray.size; i++) {
Jakub Josefd31de302017-05-15 13:59:18 +0200320 def step = stepsArray[i]
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200321 def dummySteps = [:]
322 def stepKey
azvyagintsev6bda9422018-08-20 11:57:05 +0300323 if (step[1] instanceof List || step[1] instanceof Map) {
324 for (j = 0; j < step[1].size(); j++) {
325 if (step[1] instanceof List) {
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200326 stepKey = j
azvyagintsev6bda9422018-08-20 11:57:05 +0300327 } else if (step[1] instanceof Map) {
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200328 stepKey = step[1].keySet()[j]
329 }
azvyagintsev6bda9422018-08-20 11:57:05 +0300330 dummySteps.put("step-${step[0]}-${stepKey}", step[1][stepKey])
Jakub Josefd31de302017-05-15 13:59:18 +0200331 }
azvyagintsev6bda9422018-08-20 11:57:05 +0300332 } else {
Jakub Josefd31de302017-05-15 13:59:18 +0200333 dummySteps.put(step[0], step[1])
334 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100335 parallel dummySteps
336 }
337}
338
339/**
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200340 * Partition given list to list of small lists
341 * @param inputList input list
342 * @param partitionSize (partition size, optional, default 5)
343 */
azvyagintsev6bda9422018-08-20 11:57:05 +0300344def partitionList(inputList, partitionSize = 5) {
345 List<List<String>> partitions = new ArrayList<>();
346 for (int i = 0; i < inputList.size(); i += partitionSize) {
347 partitions.add(new ArrayList<String>(inputList.subList(i, Math.min(i + partitionSize, inputList.size()))));
348 }
349 return partitions
Jakub Josef7fb8bbd2017-05-15 16:02:44 +0200350}
351
352/**
Jakub Josef79ecec32017-02-17 14:36:28 +0100353 * Get password credentials from store
354 *
azvyagintsev6bda9422018-08-20 11:57:05 +0300355 * @param id Credentials name
Jakub Josef79ecec32017-02-17 14:36:28 +0100356 */
357def getPasswordCredentials(id) {
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100358 return getCredentialsById(id, 'password')
Jakub Josef79ecec32017-02-17 14:36:28 +0100359}
360
361/**
362 * Get SSH credentials from store
363 *
azvyagintsev6bda9422018-08-20 11:57:05 +0300364 * @param id Credentials name
Jakub Josef79ecec32017-02-17 14:36:28 +0100365 */
366def getSshCredentials(id) {
Alexander Evseevbc1fea42017-12-13 10:03:03 +0100367 return getCredentialsById(id, 'sshKey')
Jakub Josef79ecec32017-02-17 14:36:28 +0100368}
Jakub Josef79ecec32017-02-17 14:36:28 +0100369
370/**
371 * Tests Jenkins instance for existence of plugin with given name
372 * @param pluginName plugin short name to test
373 * @return boolean result
374 */
375@NonCPS
azvyagintsev6bda9422018-08-20 11:57:05 +0300376def jenkinsHasPlugin(pluginName) {
377 return Jenkins.instance.pluginManager.plugins.collect { p -> p.shortName }.contains(pluginName)
Jakub Josef79ecec32017-02-17 14:36:28 +0100378}
379
380@NonCPS
381def _needNotification(notificatedTypes, buildStatus, jobName) {
azvyagintsev6bda9422018-08-20 11:57:05 +0300382 if (notificatedTypes && notificatedTypes.contains("onchange")) {
383 if (jobName) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100384 def job = Jenkins.instance.getItem(jobName)
385 def numbuilds = job.builds.size()
azvyagintsev6bda9422018-08-20 11:57:05 +0300386 if (numbuilds > 0) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100387 //actual build is first for some reasons, so last finished build is second
388 def lastBuild = job.builds[1]
azvyagintsev6bda9422018-08-20 11:57:05 +0300389 if (lastBuild) {
390 if (lastBuild.result.toString().toLowerCase().equals(buildStatus)) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100391 println("Build status didn't changed since last build, not sending notifications")
392 return false;
393 }
394 }
395 }
396 }
azvyagintsev6bda9422018-08-20 11:57:05 +0300397 } else if (!notificatedTypes.contains(buildStatus)) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100398 return false;
399 }
400 return true;
401}
402
403/**
404 * Send notification to all enabled notifications services
405 * @param buildStatus message type (success, warning, error), null means SUCCESSFUL
406 * @param msgText message text
407 * @param enabledNotifications list of enabled notification types, types: slack, hipchat, email, default empty
408 * @param notificatedTypes types of notifications will be sent, default onchange - notificate if current build result not equal last result;
409 * otherwise use - ["success","unstable","failed"]
410 * @param jobName optional job name param, if empty env.JOB_NAME will be used
Jakub Josefd0571152017-07-17 14:11:39 +0200411 * @param buildNumber build number param, if empty env.BUILD_NUM will be used
412 * @param buildUrl build url param, if empty env.BUILD_URL will be used
Jakub Josef79ecec32017-02-17 14:36:28 +0100413 * @param mailFrom mail FROM param, if empty "jenkins" will be used, it's mandatory for sending email notifications
Jakub Josefd0571152017-07-17 14:11:39 +0200414 * @param mailTo mail TO param, it's mandatory for sending email notifications, this option enable mail notification
Jakub Josef79ecec32017-02-17 14:36:28 +0100415 */
azvyagintsev6bda9422018-08-20 11:57:05 +0300416def sendNotification(buildStatus, msgText = "", enabledNotifications = [], notificatedTypes = ["onchange"], jobName = null, buildNumber = null, buildUrl = null, mailFrom = "jenkins", mailTo = null) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100417 // Default values
418 def colorName = 'blue'
419 def colorCode = '#0000FF'
420 def buildStatusParam = buildStatus != null && buildStatus != "" ? buildStatus : "SUCCESS"
421 def jobNameParam = jobName != null && jobName != "" ? jobName : env.JOB_NAME
422 def buildNumberParam = buildNumber != null && buildNumber != "" ? buildNumber : env.BUILD_NUMBER
423 def buildUrlParam = buildUrl != null && buildUrl != "" ? buildUrl : env.BUILD_URL
424 def subject = "${buildStatusParam}: Job '${jobNameParam} [${buildNumberParam}]'"
425 def summary = "${subject} (${buildUrlParam})"
426
azvyagintsev6bda9422018-08-20 11:57:05 +0300427 if (msgText != null && msgText != "") {
428 summary += "\n${msgText}"
Jakub Josef79ecec32017-02-17 14:36:28 +0100429 }
azvyagintsev6bda9422018-08-20 11:57:05 +0300430 if (buildStatusParam.toLowerCase().equals("success")) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100431 colorCode = "#00FF00"
432 colorName = "green"
azvyagintsev6bda9422018-08-20 11:57:05 +0300433 } else if (buildStatusParam.toLowerCase().equals("unstable")) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100434 colorCode = "#FFFF00"
435 colorName = "yellow"
azvyagintsev6bda9422018-08-20 11:57:05 +0300436 } else if (buildStatusParam.toLowerCase().equals("failure")) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100437 colorCode = "#FF0000"
438 colorName = "red"
439 }
azvyagintsev6bda9422018-08-20 11:57:05 +0300440 if (_needNotification(notificatedTypes, buildStatusParam.toLowerCase(), jobNameParam)) {
441 if (enabledNotifications.contains("slack") && jenkinsHasPlugin("slack")) {
442 try {
Jakub Josef79ecec32017-02-17 14:36:28 +0100443 slackSend color: colorCode, message: summary
azvyagintsev6bda9422018-08-20 11:57:05 +0300444 } catch (Exception e) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100445 println("Calling slack plugin failed")
446 e.printStackTrace()
447 }
448 }
azvyagintsev6bda9422018-08-20 11:57:05 +0300449 if (enabledNotifications.contains("hipchat") && jenkinsHasPlugin("hipchat")) {
450 try {
Jakub Josef79ecec32017-02-17 14:36:28 +0100451 hipchatSend color: colorName.toUpperCase(), message: summary
azvyagintsev6bda9422018-08-20 11:57:05 +0300452 } catch (Exception e) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100453 println("Calling hipchat plugin failed")
454 e.printStackTrace()
455 }
456 }
azvyagintsev6bda9422018-08-20 11:57:05 +0300457 if (enabledNotifications.contains("email") && mailTo != null && mailTo != "" && mailFrom != null && mailFrom != "") {
458 try {
Jakub Josef79ecec32017-02-17 14:36:28 +0100459 mail body: summary, from: mailFrom, subject: subject, to: mailTo
azvyagintsev6bda9422018-08-20 11:57:05 +0300460 } catch (Exception e) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100461 println("Sending mail plugin failed")
462 e.printStackTrace()
463 }
464 }
465 }
Filip Pytloun49d66302017-03-06 10:26:22 +0100466}
chnyda4e5ac792017-03-14 15:24:18 +0100467
468/**
469 * Execute linux command and catch nth element
470 * @param cmd command to execute
471 * @param index index to retrieve
472 * @return index-th element
473 */
474
azvyagintsev6bda9422018-08-20 11:57:05 +0300475def cutOrDie(cmd, index) {
chnyda4e5ac792017-03-14 15:24:18 +0100476 def common = new com.mirantis.mk.Common()
477 def output
478 try {
azvyagintsev6bda9422018-08-20 11:57:05 +0300479 output = sh(script: cmd, returnStdout: true)
480 def result = output.tokenize(" ")[index]
481 return result;
chnyda4e5ac792017-03-14 15:24:18 +0100482 } catch (Exception e) {
azvyagintsev6bda9422018-08-20 11:57:05 +0300483 common.errorMsg("Failed to execute cmd: ${cmd}\n output: ${output}")
chnyda4e5ac792017-03-14 15:24:18 +0100484 }
Filip Pytloun81c864d2017-03-21 15:19:30 +0100485}
Tomáš Kukrál767dd732017-03-23 10:38:59 +0100486
487/**
488 * Check variable contains keyword
489 * @param variable keywork is searched (contains) here
490 * @param keyword string to look for
491 * @return True if variable contains keyword (case insensitive), False if do not contains or any of input isn't a string
492 */
493
494def checkContains(variable, keyword) {
azvyagintsev6bda9422018-08-20 11:57:05 +0300495 if (env.getEnvironment().containsKey(variable)) {
Jakub Josef7a8dea22017-03-23 19:51:32 +0100496 return env[variable] && env[variable].toLowerCase().contains(keyword.toLowerCase())
Tomáš Kukrál767dd732017-03-23 10:38:59 +0100497 } else {
Tomáš Kukrálc76c1e02017-03-23 19:06:59 +0100498 return false
Tomáš Kukrál767dd732017-03-23 10:38:59 +0100499 }
500}
Jakub Josefa877db52017-04-05 14:22:30 +0200501
502/**
503 * Parse JSON string to hashmap
504 * @param jsonString input JSON string
505 * @return created hashmap
506 */
azvyagintsev6bda9422018-08-20 11:57:05 +0300507def parseJSON(jsonString) {
508 def m = [:]
509 def lazyMap = new JsonSlurperClassic().parseText(jsonString)
510 m.putAll(lazyMap)
511 return m
Jakub Josefa877db52017-04-05 14:22:30 +0200512}
Jakub Josefed239cd2017-05-09 15:27:33 +0200513
514/**
Vasyl Saienko79d6f3b2018-10-19 09:13:46 +0300515 *
516 * Deep merge of Map items. Merges variable number of maps in to onto.
517 * Using the following rules:
518 * - Lists are appended
519 * - Maps are updated
520 * - other object types are replaced.
521 *
522 *
523 * @param onto Map object to merge in
524 * @param overrides Map objects to merge to onto
525*/
526def mergeMaps(Map onto, Map... overrides){
527 if (!overrides){
528 return onto
529 }
530 else if (overrides.length == 1) {
531 overrides[0]?.each { k, v ->
Vasyl Saienkof92cf4f2019-07-10 12:39:25 +0300532 if (k in onto.keySet()) {
533 if (v in Map && onto[k] in Map){
534 mergeMaps((Map) onto[k], (Map) v)
535 } else if (v in List) {
536 onto[k] += v
537 } else {
538 onto[k] = v
539 }
Vasyl Saienko79d6f3b2018-10-19 09:13:46 +0300540 } else {
541 onto[k] = v
542 }
543 }
544 return onto
545 }
546 return overrides.inject(onto, { acc, override -> mergeMaps(acc, override ?: [:]) })
547}
548
549/**
Jakub Josefed239cd2017-05-09 15:27:33 +0200550 * Test pipeline input parameter existence and validity (not null and not empty string)
551 * @param paramName input parameter name (usually uppercase)
azvyagintsevc8ecdfd2018-09-11 12:47:15 +0300552 */
azvyagintsev6bda9422018-08-20 11:57:05 +0300553def validInputParam(paramName) {
azvyagintsevc8ecdfd2018-09-11 12:47:15 +0300554 if (paramName instanceof java.lang.String) {
555 return env.getEnvironment().containsKey(paramName) && env[paramName] != null && env[paramName] != ""
556 }
557 return false
Tomáš Kukráld34fe872017-06-13 10:50:50 +0200558}
559
560/**
561 * Take list of hashmaps and count number of hashmaps with parameter equals eq
562 * @param lm list of hashmaps
563 * @param param define parameter of hashmap to read and compare
564 * @param eq desired value of hashmap parameter
565 * @return count of hashmaps meeting defined condition
566 */
567
568@NonCPS
569def countHashMapEquals(lm, param, eq) {
azvyagintsev6bda9422018-08-20 11:57:05 +0300570 return lm.stream().filter { i -> i[param].equals(eq) }.collect(java.util.stream.Collectors.counting())
Tomáš Kukráld34fe872017-06-13 10:50:50 +0200571}
Vasyl Saienkofb055b12017-11-23 18:15:23 +0200572
573/**
574 * Execute shell command and return stdout, stderr and status
575 *
576 * @param cmd Command to execute
577 * @return map with stdout, stderr, status keys
578 */
579
580def shCmdStatus(cmd) {
azvyagintsev386e94e2019-06-13 13:39:04 +0300581 // Set +x , to hide odd messages about temp file manipulations
Vasyl Saienkofb055b12017-11-23 18:15:23 +0200582 def res = [:]
azvyagintsev386e94e2019-06-13 13:39:04 +0300583 def stderr = sh(script: 'set +x ; mktemp', returnStdout: true).trim()
584 def stdout = sh(script: 'set +x ; mktemp', returnStdout: true).trim()
Vasyl Saienkofb055b12017-11-23 18:15:23 +0200585
586 try {
azvyagintsev6bda9422018-08-20 11:57:05 +0300587 def status = sh(script: "${cmd} 1>${stdout} 2>${stderr}", returnStatus: true)
azvyagintsev386e94e2019-06-13 13:39:04 +0300588 res['stderr'] = sh(script: "set +x; cat ${stderr}", returnStdout: true).trim()
589 res['stdout'] = sh(script: "set +x; cat ${stdout}", returnStdout: true).trim()
Vasyl Saienkofb055b12017-11-23 18:15:23 +0200590 res['status'] = status
591 } finally {
azvyagintsev386e94e2019-06-13 13:39:04 +0300592 sh(script: "set +x; rm ${stderr}")
593 sh(script: "set +x; rm ${stdout}")
Vasyl Saienkofb055b12017-11-23 18:15:23 +0200594 }
595
596 return res
597}
Richard Felkl66a242d2018-01-25 15:27:15 +0100598
Richard Felkl66a242d2018-01-25 15:27:15 +0100599/**
600 * Retry commands passed to body
601 *
Martin Polreich331f2b62019-02-08 10:16:52 +0100602 * Don't use common.retry method for retrying salt.enforceState method. Use retries parameter
603 * built-in the salt.enforceState method instead to ensure correct functionality.
604 *
Richard Felkl66a242d2018-01-25 15:27:15 +0100605 * @param times Number of retries
Jakub Josef61463c72018-02-13 16:10:56 +0100606 * @param delay Delay between retries (in seconds)
Richard Felkl66a242d2018-01-25 15:27:15 +0100607 * @param body Commands to be in retry block
608 * @return calling commands in body
azvyagintsev6bda9422018-08-20 11:57:05 +0300609 * @example retry ( 3 , 5 ) { function body }* retry{ function body }
Richard Felkl66a242d2018-01-25 15:27:15 +0100610 */
611
612def retry(int times = 5, int delay = 0, Closure body) {
613 int retries = 0
azvyagintsev6bda9422018-08-20 11:57:05 +0300614 while (retries++ < times) {
Richard Felkl66a242d2018-01-25 15:27:15 +0100615 try {
616 return body.call()
azvyagintsev6bda9422018-08-20 11:57:05 +0300617 } catch (e) {
Denis Egorenko900a3af2019-01-14 12:54:56 +0400618 errorMsg(e.toString())
Richard Felkl66a242d2018-01-25 15:27:15 +0100619 sleep(delay)
620 }
621 }
Richard Felkl66a242d2018-01-25 15:27:15 +0100622 throw new Exception("Failed after $times retries")
623}
Dmitry Pyzhov0d0d8522018-05-15 22:37:37 +0300624
Dmitry Pyzhov0d0d8522018-05-15 22:37:37 +0300625/**
626 * Wait for user input with timeout
627 *
628 * @param timeoutInSeconds Timeout
629 * @param options Options for input widget
630 */
azvyagintsev6bda9422018-08-20 11:57:05 +0300631def waitForInputThenPass(timeoutInSeconds, options = [message: 'Ready to go?']) {
632 def userInput = true
633 try {
634 timeout(time: timeoutInSeconds, unit: 'SECONDS') {
635 userInput = input options
636 }
637 } catch (err) { // timeout reached or input false
638 def user = err.getCauses()[0].getUser()
639 if ('SYSTEM' == user.toString()) { // SYSTEM means timeout.
640 println("Timeout, proceeding")
641 } else {
642 userInput = false
643 println("Aborted by: [${user}]")
644 throw err
645 }
Dmitry Pyzhov0d0d8522018-05-15 22:37:37 +0300646 }
azvyagintsev6bda9422018-08-20 11:57:05 +0300647 return userInput
Dmitry Pyzhov0d0d8522018-05-15 22:37:37 +0300648}
Oleksii Grudev9e1d97a2018-06-29 16:04:30 +0300649
650/**
651 * Function receives Map variable as input and sorts it
652 * by values ascending. Returns sorted Map
653 * @param _map Map variable
654 */
655@NonCPS
656def SortMapByValueAsc(_map) {
azvyagintsev6bda9422018-08-20 11:57:05 +0300657 def sortedMap = _map.sort { it.value }
Oleksii Grudev9e1d97a2018-06-29 16:04:30 +0300658 return sortedMap
659}
azvyagintsev99d35842018-08-17 20:26:34 +0300660
661/**
662 * Compare 'old' and 'new' dir's recursively
663 * @param diffData =' Only in new/XXX/infra: secrets.yml
664 Files old/XXX/init.yml and new/XXX/init.yml differ
665 Only in old/XXX/infra: secrets11.yml '
666 *
667 * @return
668 * - new:
669 - XXX/secrets.yml
670 - diff:
671 - XXX/init.yml
672 - removed:
673 - XXX/secrets11.yml
674
675 */
676def diffCheckMultidir(diffData) {
azvyagintsevab5637b2018-08-20 18:18:15 +0300677 common = new com.mirantis.mk.Common()
678 // Some global constants. Don't change\move them!
679 keyNew = 'new'
680 keyRemoved = 'removed'
681 keyDiff = 'diff'
682 def output = [
683 new : [],
684 removed: [],
685 diff : [],
686 ]
687 String pathSep = '/'
688 diffData.each { line ->
689 def job_file = ''
690 def job_type = ''
691 if (line.startsWith('Files old/')) {
692 job_file = new File(line.replace('Files old/', '').tokenize()[0])
693 job_type = keyDiff
694 } else if (line.startsWith('Only in new/')) {
695 // get clean normalized filepath, under new/
696 job_file = new File(line.replace('Only in new/', '').replace(': ', pathSep)).toString()
697 job_type = keyNew
698 } else if (line.startsWith('Only in old/')) {
699 // get clean normalized filepath, under old/
700 job_file = new File(line.replace('Only in old/', '').replace(': ', pathSep)).toString()
701 job_type = keyRemoved
702 } else {
703 common.warningMsg("Not parsed diff line: ${line}!")
704 }
705 if (job_file != '') {
706 output[job_type].push(job_file)
707 }
azvyagintsev99d35842018-08-17 20:26:34 +0300708 }
azvyagintsevab5637b2018-08-20 18:18:15 +0300709 return output
azvyagintsev99d35842018-08-17 20:26:34 +0300710}
711
712/**
713 * Compare 2 folder, file by file
714 * Structure should be:
715 * ${compRoot}/
716 └── diff - diff results will be save here
717 ├── new - input folder with data
718 ├── old - input folder with data
719 ├── pillar.diff - globall diff will be saved here
720 * b_url - usual env.BUILD_URL, to be add into description
azvyagintsev3bbcafe2018-08-20 19:36:16 +0300721 * grepOpts - General grep cmdline; Could be used to pass some magic
722 * regexp into after-diff listing file(pillar.diff)
723 * Example: '-Ev infra/secrets.yml'
azvyagintsev99d35842018-08-17 20:26:34 +0300724 * return - html-based string
725 * TODO: allow to specify subdir for results?
azvyagintsev99d35842018-08-17 20:26:34 +0300726 **/
Denis Egorenko4551f372018-09-11 16:36:13 +0400727
azvyagintsev3bbcafe2018-08-20 19:36:16 +0300728def comparePillars(compRoot, b_url, grepOpts) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000729
azvyagintsevab5637b2018-08-20 18:18:15 +0300730 // Some global constants. Don't change\move them!
731 keyNew = 'new'
732 keyRemoved = 'removed'
733 keyDiff = 'diff'
734 def diff_status = 0
735 // FIXME
736 httpWS = b_url + '/artifact/'
737 dir(compRoot) {
azvyagintsev3bbcafe2018-08-20 19:36:16 +0300738 // If diff empty - exit 0
739 diff_status = sh(script: 'diff -q -r old/ new/ > pillar.diff',
azvyagintsevab5637b2018-08-20 18:18:15 +0300740 returnStatus: true,
741 )
azvyagintsev24d49652018-08-21 19:33:51 +0300742 }
azvyagintsevc0fe1442018-08-21 20:01:34 +0300743 // Unfortunately, diff not able to work with dir-based regexp
744 if (diff_status == 1 && grepOpts) {
745 dir(compRoot) {
746 grep_status = sh(script: """
azvyagintsev3bbcafe2018-08-20 19:36:16 +0300747 cp -v pillar.diff pillar_orig.diff
748 grep ${grepOpts} pillar_orig.diff > pillar.diff
749 """,
azvyagintsevc0fe1442018-08-21 20:01:34 +0300750 returnStatus: true
751 )
azvyagintsevf6e77912018-09-07 15:41:09 +0300752 if (grep_status == 1) {
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000753 warningMsg("Grep regexp ${grepOpts} removed all diff!")
azvyagintsevc0fe1442018-08-21 20:01:34 +0300754 diff_status = 0
azvyagintsevb99f87c2018-08-21 19:43:59 +0300755 }
azvyagintsev3bbcafe2018-08-20 19:36:16 +0300756 }
azvyagintsevc0fe1442018-08-21 20:01:34 +0300757 }
758 // Set job description
Denis Egorenko4551f372018-09-11 16:36:13 +0400759 description = ''
azvyagintsevc0fe1442018-08-21 20:01:34 +0300760 if (diff_status == 1) {
azvyagintsevab5637b2018-08-20 18:18:15 +0300761 // Analyse output file and prepare array with results
762 String data_ = readFile file: "${compRoot}/pillar.diff"
763 def diff_list = diffCheckMultidir(data_.split("\\r?\\n"))
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000764 infoMsg(diff_list)
azvyagintsevab5637b2018-08-20 18:18:15 +0300765 dir(compRoot) {
766 if (diff_list[keyDiff].size() > 0) {
767 if (!fileExists('diff')) {
768 sh('mkdir -p diff')
769 }
770 description += '<b>CHANGED</b><ul>'
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +0000771 infoMsg('Changed items:')
Denis Egorenko4551f372018-09-11 16:36:13 +0400772 def stepsForParallel = [:]
773 stepsForParallel.failFast = true
774 diff_list[keyDiff].each {
775 stepsForParallel.put("Differ for:${it}",
776 {
777 // We don't want to handle sub-dirs structure. So, simply make diff 'flat'
778 def item_f = it.toString().replace('/', '_')
779 description += "<li><a href=\"${httpWS}/diff/${item_f}/*view*/\">${it}</a></li>"
780 // Generate diff file
781 def diff_exit_code = sh([
782 script : "diff -U 50 old/${it} new/${it} > diff/${item_f}",
783 returnStdout: false,
784 returnStatus: true,
785 ])
786 // catch normal errors, diff should always return 1
787 if (diff_exit_code != 1) {
788 error 'Error with diff file generation'
789 }
790 })
azvyagintsevab5637b2018-08-20 18:18:15 +0300791 }
Denis Egorenko4551f372018-09-11 16:36:13 +0400792
793 parallel stepsForParallel
azvyagintsevab5637b2018-08-20 18:18:15 +0300794 }
795 if (diff_list[keyNew].size() > 0) {
796 description += '<b>ADDED</b><ul>'
797 for (item in diff_list[keyNew]) {
798 description += "<li><a href=\"${httpWS}/new/${item}/*view*/\">${item}</a></li>"
799 }
800 }
801 if (diff_list[keyRemoved].size() > 0) {
802 description += '<b>DELETED</b><ul>'
803 for (item in diff_list[keyRemoved]) {
804 description += "<li><a href=\"${httpWS}/old/${item}/*view*/\">${item}</a></li>"
805 }
806 }
Denis Egorenko62120962019-03-15 11:24:32 +0400807 def cwd = sh(script: 'basename $(pwd)', returnStdout: true).trim()
808 sh "tar -cf old_${cwd}.tar.gz old/ && rm -rf old/"
809 sh "tar -cf new_${cwd}.tar.gz new/ && rm -rf new/"
azvyagintsevab5637b2018-08-20 18:18:15 +0300810 }
azvyagintsev99d35842018-08-17 20:26:34 +0300811 }
azvyagintsevab5637b2018-08-20 18:18:15 +0300812
813 if (description != '') {
814 dir(compRoot) {
815 archiveArtifacts([
816 artifacts : '**',
817 allowEmptyArchive: true,
818 ])
819 }
820 return description.toString()
821 } else {
azvyagintseva57c82a2018-09-20 12:17:24 +0300822 return '<b>No job changes</b>'
azvyagintsevab5637b2018-08-20 18:18:15 +0300823 }
azvyagintsev99d35842018-08-17 20:26:34 +0300824}
azvyagintsevab5637b2018-08-20 18:18:15 +0300825
826/**
827 * Simple function, to get basename from string.
828 * line - path-string
829 * remove_ext - string, optionl. Drop file extenstion.
830 **/
831def GetBaseName(line, remove_ext) {
832 filename = line.toString().split('/').last()
833 if (remove_ext && filename.endsWith(remove_ext.toString())) {
834 filename = filename.take(filename.lastIndexOf(remove_ext.toString()))
835 }
836 return filename
Vasyl Saienko00ef98b2018-09-05 10:34:32 +0300837}
Vasyl Saienko723cbbc2018-09-05 11:08:52 +0300838
839/**
azvyagintsevf6e77912018-09-07 15:41:09 +0300840 * Return colored string of specific stage in stageMap
841 *
842 * @param stageMap LinkedHashMap object.
843 * @param stageName The name of current stage we are going to execute.
844 * @param color Text color
845 * */
846def getColoredStageView(stageMap, stageName, color) {
847 def stage = stageMap[stageName]
848 def banner = []
849 def currentStageIndex = new ArrayList<String>(stageMap.keySet()).indexOf(stageName)
850 def numberOfStages = stageMap.keySet().size() - 1
Vasyl Saienko723cbbc2018-09-05 11:08:52 +0300851
Vasyl Saienko723cbbc2018-09-05 11:08:52 +0300852 banner.add(getColorizedString(
azvyagintsevf6e77912018-09-07 15:41:09 +0300853 "=========== Stage ${currentStageIndex}/${numberOfStages}: ${stageName} ===========", color))
854 for (stage_item in stage.keySet()) {
855 banner.add(getColorizedString(
856 "${stage_item}: ${stage[stage_item]}", color))
Vasyl Saienko723cbbc2018-09-05 11:08:52 +0300857 }
azvyagintsevf6e77912018-09-07 15:41:09 +0300858 banner.add('\n')
859
860 return banner
861}
862
863/**
864 * Pring stageMap to console with specified color
865 *
866 * @param stageMap LinkedHashMap object with stages information.
867 * @param currentStage The name of current stage we are going to execute.
868 *
869 * */
870def printCurrentStage(stageMap, currentStage) {
871 print getColoredStageView(stageMap, currentStage, "cyan").join('\n')
872}
873
874/**
875 * Pring stageMap to console with specified color
876 *
877 * @param stageMap LinkedHashMap object.
878 * @param baseColor Text color (default white)
879 * */
880def printStageMap(stageMap, baseColor = "white") {
881 def banner = []
882 def index = 0
883 for (stage_name in stageMap.keySet()) {
884 banner.addAll(getColoredStageView(stageMap, stage_name, baseColor))
885 }
886 print banner.join('\n')
887}
888
889/**
890 * Wrap provided code in stage, and do interactive retires if needed.
891 *
892 * @param stageMap LinkedHashMap object with stages information.
893 * @param currentStage The name of current stage we are going to execute.
894 * @param target Target host to execute stage on.
895 * @param interactive Boolean flag to specify if interaction with user is enabled.
896 * @param body Command to be in stage block.
897 * */
898def stageWrapper(stageMap, currentStage, target, interactive = true, Closure body) {
899 def common = new com.mirantis.mk.Common()
900 def banner = []
901
902 printCurrentStage(stageMap, currentStage)
903
904 stage(currentStage) {
Vasyl Saienkobc6debb2018-09-10 14:08:09 +0300905 if (interactive){
azvyagintsevf6e77912018-09-07 15:41:09 +0300906 input message: getColorizedString("We are going to execute stage \'${currentStage}\' on the following target ${target}.\nPlease review stage information above.", "yellow")
Vasyl Saienkobc6debb2018-09-10 14:08:09 +0300907 }
908 try {
Vasyl Saienkobc6debb2018-09-10 14:08:09 +0300909 stageMap[currentStage]['Status'] = "SUCCESS"
Victor Ryzhenkin49d67812019-01-09 15:28:21 +0400910 return body.call()
Vasyl Saienkobc6debb2018-09-10 14:08:09 +0300911 } catch (Exception err) {
912 def msg = "Stage ${currentStage} failed with the following exception:\n${err}"
913 print getColorizedString(msg, "yellow")
914 common.errorMsg(err)
915 if (interactive) {
916 input message: getColorizedString("Please make sure problem is fixed to proceed with retry. Ready to proceed?", "yellow")
917 stageMap[currentStage]['Status'] = "RETRYING"
918 stageWrapper(stageMap, currentStage, target, interactive, body)
919 } else {
920 error(msg)
azvyagintsevf6e77912018-09-07 15:41:09 +0300921 }
Vasyl Saienkobc6debb2018-09-10 14:08:09 +0300922 }
azvyagintsevf6e77912018-09-07 15:41:09 +0300923 }
924}
925
926/**
927 * Ugly transition solution for internal tests.
928 * 1) Check input => transform to static result, based on runtime and input
929 * 2) Check remote-binary repo for exact resource
azvyagintsevf6fce3d2018-12-28 15:30:33 +0200930 * Return: changes each linux_system_* cto false, in case broken url in some of them
931 */
azvyagintsevf6e77912018-09-07 15:41:09 +0300932
933def checkRemoteBinary(LinkedHashMap config, List extraScmExtensions = []) {
934 def common = new com.mirantis.mk.Common()
azvyagintsevf6fce3d2018-12-28 15:30:33 +0200935 def res = [:]
azvyagintsevf6e77912018-09-07 15:41:09 +0300936 res['MirrorRoot'] = config.get('globalMirrorRoot', env["BIN_MIRROR_ROOT"] ? env["BIN_MIRROR_ROOT"] : "http://mirror.mirantis.com/")
937 // Reclass-like format's. To make life eazy!
azvyagintsev603d95b2018-11-09 15:37:10 +0200938 res['mcp_version'] = config.get('mcp_version', env["BIN_APT_MCP_VERSION"] ? env["BIN_APT_MCP_VERSION"] : 'nightly')
azvyagintsevf6fce3d2018-12-28 15:30:33 +0200939 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']}/")
940 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/")
941 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/")
azvyagintsevf6e77912018-09-07 15:41:09 +0300942
943 if (config.get('verify', true)) {
azvyagintsevf6fce3d2018-12-28 15:30:33 +0200944 res.each { key, val ->
945 if (key.toString().startsWith('linux_system_repo')) {
946 def MirrorRootStatus = sh(script: "wget --auth-no-challenge --spider ${val} 2>/dev/null", returnStatus: true)
947 if (MirrorRootStatus != 0) {
948 common.warningMsg("Resource: '${key}' at '${val}' not exist!")
949 res[key] = false
950 }
951 }
azvyagintsevf6e77912018-09-07 15:41:09 +0300952 }
953 }
954 return res
Vasyl Saienko723cbbc2018-09-05 11:08:52 +0300955}
Denis Egorenkoeded0d42018-09-26 13:25:49 +0400956
957/**
958 * Workaround to update env properties, like GERRIT_* vars,
959 * which should be passed from upstream job to downstream.
960 * Will not fail entire job in case any issues.
961 * @param envVar - EnvActionImpl env job
962 * @param extraVars - Multiline YAML text with extra vars
963 */
964def mergeEnv(envVar, extraVars) {
965 try {
966 def extraParams = readYaml text: extraVars
967 for(String key in extraParams.keySet()) {
968 envVar[key] = extraParams[key]
Dmitry Teselkin4badf832020-05-27 17:48:36 +0300969 println("INFO: Parameter ${key} is updated from EXTRA vars.")
Denis Egorenkoeded0d42018-09-26 13:25:49 +0400970 }
971 } catch (Exception e) {
Dmitry Teselkin4badf832020-05-27 17:48:36 +0300972 println("ERR: Can't update env parameteres, because: ${e.toString()}")
Denis Egorenkoeded0d42018-09-26 13:25:49 +0400973 }
974}
Denis Egorenko599bd632018-09-28 15:24:37 +0400975
azvyagintsev898e1742020-05-12 12:40:44 +0300976def setMapDefaults(Object base, Object defaults, Boolean recursive = false) {
977/**
978 * Function to update dict with params, if its not set yet
979 * Will not fail entire job in case any issues.
980 * Those function will not overwrite current options if already passed.
981 * @param base - dict
982 * @param defaults - dict
983 */
984 if (base instanceof Map && defaults instanceof Map) {
985 defaults.inject(base) { result, key, value ->
986 if (result.containsKey(key)) {
987 setMapDefaults(result[key], value, recursive = true)
988 } else {
989 result.put(key, value)
990 }
991 return result
992 }
993 } else if (!recursive) {
994 echo("Can't update map parameters, wrong input data, skipping")
995 }
996}
997
Dmitry Teselkin4badf832020-05-27 17:48:36 +0300998def setEnvDefaults(Object envVar, Object defaults) {
999 /**
1000 * Function to set default values of an environment variables object
1001 * at runtime(patches existing 'env' instance).
1002 * @param env - instance of either EnvActionImpl
1003 * @param defaults - Map with default values
1004 * Example: setEnvDefaults(env, ['ENV_NAME': 'newENV_NAME', 'newvar': 'newval'])
1005 * */
1006
1007 if (!(envVar instanceof EnvActionImpl)) {
1008 error("setEnvDefaults 'env' is not an instance of EnvActionImpl")
1009 } else if (!(defaults instanceof Map)) {
1010 error("setEnvDefaults 'defaults' is not a Map")
1011 }
1012 defaults.each { key, value ->
1013 if (envVar.getEnvironment().containsKey(key)) {
1014 println("INFO:setEnvDefaults env variable ${key} already exist, not overwriting")
1015 } else {
1016 envVar[key] = value
1017 println("INFO:setEnvDefaults env variable ${key} has been added")
1018 }
1019 }
1020}
azvyagintsev898e1742020-05-12 12:40:44 +03001021
Denis Egorenko599bd632018-09-28 15:24:37 +04001022/**
1023 * Wrapper around parallel pipeline function
1024 * with ability to restrict number of parallel threads
1025 * running simultaneously
1026 *
1027 * @param branches - Map with Clousers to be executed
1028 * @param maxParallelJob - Integer number of parallel threads allowed
1029 * to run simultaneously
1030 */
1031def runParallel(branches, maxParallelJob = 10) {
1032 def runningSteps = 0
1033 branches.each { branchName, branchBody ->
1034 if (branchBody instanceof Closure) {
1035 branches[branchName] = {
1036 while (!(runningSteps < maxParallelJob)) {
1037 continue
1038 }
1039 runningSteps += 1
1040 branchBody.call()
1041 runningSteps -= 1
1042 }
1043 }
1044 }
1045 if (branches) {
1046 parallel branches
1047 }
1048}
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +00001049
1050/**
1051 * Ugly processing basic funcs with /etc/apt
Denis Egorenko5cea1412018-10-18 16:40:11 +04001052 * @param repoConfig YAML text or Map
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +00001053 * Example :
Denis Egorenko5cea1412018-10-18 16:40:11 +04001054 repoConfig = '''
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +00001055 ---
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +00001056 aprConfD: |-
Denis Egorenkoe02a1b22018-10-19 17:47:53 +04001057 APT::Get::AllowUnauthenticated 'true';
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +00001058 repo:
Denis Egorenkoe02a1b22018-10-19 17:47:53 +04001059 mcp_saltstack:
1060 source: "deb [arch=amd64] http://mirror.mirantis.com/nightly/saltstack-2017.7/xenial xenial main"
1061 pin:
1062 - package: "libsodium18"
1063 pin: "release o=SaltStack"
1064 priority: 50
1065 - package: "*"
1066 pin: "release o=SaltStack"
1067 priority: "1100"
1068 repo_key: "http://mirror.mirantis.com/public.gpg"
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +00001069 '''
1070 *
1071 */
1072
Denis Egorenko5cea1412018-10-18 16:40:11 +04001073def debianExtraRepos(repoConfig) {
1074 def config = null
1075 if (repoConfig instanceof Map) {
1076 config = repoConfig
1077 } else {
1078 config = readYaml text: repoConfig
1079 }
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +00001080 if (config.get('repo', false)) {
1081 for (String repo in config['repo'].keySet()) {
Denis Egorenko395aa212018-10-11 15:11:28 +04001082 source = config['repo'][repo]['source']
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +00001083 warningMsg("Write ${source} > /etc/apt/sources.list.d/${repo}.list")
1084 sh("echo '${source}' > /etc/apt/sources.list.d/${repo}.list")
Denis Egorenko395aa212018-10-11 15:11:28 +04001085 if (config['repo'][repo].containsKey('repo_key')) {
1086 key = config['repo'][repo]['repo_key']
1087 sh("wget -O - '${key}' | apt-key add -")
1088 }
Denis Egorenkoe02a1b22018-10-19 17:47:53 +04001089 if (config['repo'][repo]['pin']) {
1090 def repoPins = []
1091 for (Map pin in config['repo'][repo]['pin']) {
1092 repoPins.add("Package: ${pin['package']}")
1093 repoPins.add("Pin: ${pin['pin']}")
1094 repoPins.add("Pin-Priority: ${pin['priority']}")
Denis Egorenko1c93d122018-11-02 12:14:05 +04001095 // additional empty line between pins
1096 repoPins.add('\n')
Denis Egorenkoe02a1b22018-10-19 17:47:53 +04001097 }
1098 if (repoPins) {
1099 repoPins.add(0, "### Extra ${repo} repo pin start ###")
1100 repoPins.add("### Extra ${repo} repo pin end ###")
1101 repoPinning = repoPins.join('\n')
1102 warningMsg("Adding pinning \n${repoPinning}\n => /etc/apt/preferences.d/${repo}")
1103 sh("echo '${repoPinning}' > /etc/apt/preferences.d/${repo}")
1104 }
1105 }
Aleksey Zvyagintsevb20bd262018-10-05 15:09:06 +00001106 }
1107 }
1108 if (config.get('aprConfD', false)) {
1109 for (String pref in config['aprConfD'].tokenize('\n')) {
1110 warningMsg("Adding ${pref} => /etc/apt/apt.conf.d/99setupAndTestNode")
1111 sh("echo '${pref}' >> /etc/apt/apt.conf.d/99setupAndTestNode")
1112 }
1113 sh('cat /etc/apt/apt.conf.d/99setupAndTestNode')
1114 }
1115}
Denis Egorenkoeaf78db2019-02-06 17:01:38 +04001116
1117/**
1118 * Parse date from string
1119 * @param String date - date to parse
1120 * @param String format - date format in provided date string value
1121 *
1122 * return new Date() object
1123 */
1124Date parseDate(String date, String format) {
1125 return Date.parse(format, date)
1126}
Denis Egorenko815758d2019-07-08 15:54:08 +04001127
1128/**
1129 * Generate Random Hash string
1130 * @param n Hash length
1131 * @param pool Pool to use for hash generation
1132*/
1133def generateRandomHashString(int n, ArrayList pool = []) {
1134 if (!pool) {
1135 pool = ['a'..'z','A'..'Z',0..9,'_','+','='].flatten()
1136 }
1137 Random rand = new Random(System.currentTimeMillis())
1138 return (1..n).collect { pool[rand.nextInt(pool.size())] }.join()
1139}
Mykyta Karpin70cd3332019-09-16 18:31:03 +03001140
1141/**
1142 * Checks whether string is semver complaint version
1143 * @param string version
1144*/
1145
1146def isSemVer(version){
1147 // Official regex for Semver2 (https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string)
1148 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-]+)*))?$/
1149 return version ==~ semVerRegex
azvyagintsev898e1742020-05-12 12:40:44 +03001150}
azvyagintsevd987eb62020-06-19 15:19:05 +03001151
1152def readYaml2(LinkedHashMap kwargs) {
1153 /**
1154 * readYaml wrapper to workaround case when initial file contains
1155 * yaml data in text field so we need parse it twice.
1156 * * @param file String of filename to read
1157 * * @param text String to be read as Yaml
1158 * Parameters are mutually exclusive
1159 */
1160 if ((kwargs.get('file') && kwargs.get('text'))) {
1161 error('readYaml2 function not able to cover both ["file","text"] opts in same time ')
1162 }
1163 if (kwargs.get('file')) {
1164 data = readYaml(file: kwargs['file'])
1165 if (data instanceof String) {
1166 return readYaml(text: data)
1167 }
azvyagintsev255cadd2020-06-22 14:50:33 +03001168 return data
azvyagintsevd987eb62020-06-19 15:19:05 +03001169 } else if (kwargs.get('text')) {
1170 return readYaml(text: kwargs['text'])
1171 }
azvyagintsev255cadd2020-06-22 14:50:33 +03001172}
Dmitry Teselkinbd57dee2020-09-23 13:05:59 +03001173
1174/**
1175 * withTempDir runs a block of code inside a new temporary directory.
1176 * This temp dir will be removed when finished.
1177 * @param: closure Closure - code block to be executed in a tmp directory
1178 *
1179 * Example:
1180 *
1181 * withTempDir {
1182 * sh "pwd"
1183 * }
1184 *
1185 **/
1186void withTempDir(Closure closure) {
1187 dir(pwd(tmp: true)) {
1188 try {
1189 closure()
1190 } finally {
1191 deleteDir()
1192 }
1193 }
1194}