blob: f64b9db80b16601ce9bfa5ee9d836bac3348a628 [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package com.mirantis.mk
2
3/**
4 *
5 * Common functions
6 *
7 */
8
9/**
10 * Generate current timestamp
11 *
12 * @param format Defaults to yyyyMMddHHmmss
13 */
14def getDatetime(format="yyyyMMddHHmmss") {
15 def now = new Date();
16 return now.format(format, TimeZone.getTimeZone('UTC'));
17}
18
19/**
20 * Abort build, wait for some time and ensure we will terminate
21 */
22def abortBuild() {
23 currentBuild.build().doStop()
24 sleep(180)
25 // just to be sure we will terminate
26 throw new InterruptedException()
27}
28
29/**
30 * Print informational message
31 *
32 * @param msg
33 * @param color Colorful output or not
34 */
35def infoMsg(msg, color = true) {
36 printMsg(msg, "cyan")
37}
38
39/**
40 * Print error message
41 *
42 * @param msg
43 * @param color Colorful output or not
44 */
45def errorMsg(msg, color = true) {
46 printMsg(msg, "red")
47}
48
49/**
50 * Print success message
51 *
52 * @param msg
53 * @param color Colorful output or not
54 */
55def successMsg(msg, color = true) {
56 printMsg(msg, "green")
57}
58
59/**
60 * Print warning message
61 *
62 * @param msg
63 * @param color Colorful output or not
64 */
65def warningMsg(msg, color = true) {
66 printMsg(msg, "blue")
67}
68
69/**
70 * Print message
71 *
72 * @param msg Message to be printed
73 * @param level Level of message (default INFO)
74 * @param color Color to use for output or false (default)
75 */
76def printMsg(msg, color = false) {
77 colors = [
78 'red' : '\u001B[31m',
79 'black' : '\u001B[30m',
80 'green' : '\u001B[32m',
81 'yellow': '\u001B[33m',
82 'blue' : '\u001B[34m',
83 'purple': '\u001B[35m',
84 'cyan' : '\u001B[36m',
85 'white' : '\u001B[37m',
86 'reset' : '\u001B[0m'
87 ]
88 if (color != false) {
89 wrap([$class: 'AnsiColorBuildWrapper']) {
90 print "${colors[color]}${msg}${colors.reset}"
91 }
92 } else {
93 print "[${level}] ${msg}"
94 }
95}
96
97/**
98 * Traverse directory structure and return list of files
99 *
100 * @param path Path to search
101 * @param type Type of files to search (groovy.io.FileType.FILES)
102 */
103@NonCPS
104def getFiles(path, type=groovy.io.FileType.FILES) {
105 files = []
106 new File(path).eachFile(type) {
107 files[] = it
108 }
109 return files
110}
111
112/**
113 * Helper method to convert map into form of list of [key,value] to avoid
114 * unserializable exceptions
115 *
116 * @param m Map
117 */
118@NonCPS
119def entries(m) {
120 m.collect {k, v -> [k, v]}
121}
122
123/**
124 * Opposite of build-in parallel, run map of steps in serial
125 *
126 * @param steps Map of String<name>: CPSClosure2<step>
127 */
128def serial(steps) {
129 stepsArray = entries(steps)
130 for (i=0; i < stepsArray.size; i++) {
131 s = stepsArray[i]
132 dummySteps = ["${s[0]}": s[1]]
133 parallel dummySteps
134 }
135}
136
137/**
138 * Get password credentials from store
139 *
140 * @param id Credentials name
141 */
142def getPasswordCredentials(id) {
143 def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
144 com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
145 jenkins.model.Jenkins.instance
146 )
147
148 for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
149 c = credsIter.next();
150 if ( c.id == id ) {
151 return c;
152 }
153 }
154
155 throw new Exception("Could not find credentials for ID ${id}")
156}
157
158/**
159 * Get SSH credentials from store
160 *
161 * @param id Credentials name
162 */
163def getSshCredentials(id) {
164 def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
165 com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
166 jenkins.model.Jenkins.instance
167 )
168
169 for (Iterator<String> credsIter = creds.iterator(); credsIter.hasNext();) {
170 c = credsIter.next();
171 if ( c.id == id ) {
172 return c;
173 }
174 }
175
176 throw new Exception("Could not find credentials for ID ${id}")
177}