blob: 32b55a7ab859eed87931cdb3abf597e048835d2a [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package com.mirantis.mk
2
3/**
4 * Salt functions
5 *
6*/
7
8/**
9 * Salt connection and context parameters
10 *
11 * @param url Salt API server URL
12 * @param credentialsID ID of credentials store entry
13 */
14def connection(url, credentialsId = "salt") {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +010015 def common = new com.mirantis.mk.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +010016 params = [
17 "url": url,
18 "credentialsId": credentialsId,
19 "authToken": null,
20 "creds": common.getCredentials(credentialsId)
21 ]
22 params["authToken"] = saltLogin(params)
23
24 return params
25}
26
27/**
28 * Login to Salt API, return auth token
29 *
30 * @param master Salt connection object
31 */
32def saltLogin(master) {
Tomáš Kukrál7bec0532017-02-20 15:39:31 +010033 def http = new com.mirantis.mk.Http()
Jakub Josef79ecec32017-02-17 14:36:28 +010034 data = [
35 'username': master.creds.username,
36 'password': master.creds.password.toString(),
37 'eauth': 'pam'
38 ]
Tomáš Kukrál7bec0532017-02-20 15:39:31 +010039 authToken = http.restGet(master, '/login', data)['return'][0]['token']
Jakub Josef79ecec32017-02-17 14:36:28 +010040 return authToken
41}
42
43/**
44 * Run action using Salt API
45 *
46 * @param master Salt connection object
47 * @param client Client type
48 * @param target Target specification, eg. for compound matches by Pillar
49 * data: ['expression': 'I@openssh:server', 'type': 'compound'])
50 * @param function Function to execute (eg. "state.sls")
Filip Pytlounf0435c02017-03-02 17:48:54 +010051 * @param batch
Jakub Josef79ecec32017-02-17 14:36:28 +010052 * @param args Additional arguments to function
53 * @param kwargs Additional key-value arguments to function
54 */
55@NonCPS
56def runSaltCommand(master, client, target, function, batch = null, args = null, kwargs = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +040057 def http = new com.mirantis.mk.Http()
Jakub Josef79ecec32017-02-17 14:36:28 +010058
59 data = [
60 'tgt': target.expression,
61 'fun': function,
62 'client': client,
63 'expr_form': target.type,
64 ]
65
Filip Pytlounf0435c02017-03-02 17:48:54 +010066 if (batch == true) {
67 data['batch'] = "local_batch"
Jakub Josef79ecec32017-02-17 14:36:28 +010068 }
69
70 if (args) {
71 data['arg'] = args
72 }
73
74 if (kwargs) {
75 data['kwarg'] = kwargs
76 }
77
78 headers = [
79 'X-Auth-Token': "${master.authToken}"
80 ]
81
82 return http.sendHttpPostRequest("${master.url}/", data, headers)
83}
84
Jakub Josef5ade54c2017-03-10 16:14:01 +010085/**
86 * Return pillar for given master and target
87 * @param master Salt connection object
88 * @param target Get pillar target
89 * @param pillar pillar name (optional)
90 * @return output of salt command
91 */
Ales Komarekcec24d42017-03-08 10:25:45 +010092def getPillar(master, target, pillar = null) {
Tomáš Kukráld2589702017-03-10 16:30:46 +010093 if (pillar != null) {
Jakub Josef5ade54c2017-03-10 16:14:01 +010094 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.get', null, [pillar.replace('.', ':')])
Tomáš Kukráld2589702017-03-10 16:30:46 +010095 } else {
Jakub Josef5ade54c2017-03-10 16:14:01 +010096 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.data')
Jakub Josef79ecec32017-02-17 14:36:28 +010097}
98
Jakub Josef5ade54c2017-03-10 16:14:01 +010099/**
100 * Return grain for given master and target
101 * @param master Salt connection object
102 * @param target Get grain target
103 * @param grain grain name (optional)
104 * @return output of salt command
105 */
Ales Komarekcec24d42017-03-08 10:25:45 +0100106def getGrain(master, target, grain = null) {
107 if(grain != null) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100108 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grain.item', null, [grain])
109 } else {
110 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grain.items')
Ales Komarekcec24d42017-03-08 10:25:45 +0100111 }
Ales Komarekcec24d42017-03-08 10:25:45 +0100112}
113
Jakub Josef5ade54c2017-03-10 16:14:01 +0100114/**
115 * Enforces state on given master and target
116 * @param master Salt connection object
117 * @param target State enforcing target
118 * @param state Salt state
119 * @param output print output (optional, default true)
120 * @param failOnError throw exception on salt state result:false (optional, default true)
121 * @return output of salt command
122 */
123def enforceState(master, target, state, output = true, failOnError = true) {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100124 def common = new com.mirantis.mk.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +0100125 def run_states
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100126
Jakub Josef79ecec32017-02-17 14:36:28 +0100127 if (state instanceof String) {
128 run_states = state
129 } else {
130 run_states = state.join(',')
131 }
132
Tomáš Kukráldfd4b492017-03-02 12:08:50 +0100133 common.infoMsg("Enforcing state ${run_states} on ${target}")
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100134
Filip Pytloun5a7f7fd2017-02-27 18:50:25 +0100135 def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', null, [run_states])
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100136
Jakub Josef79ecec32017-02-17 14:36:28 +0100137 try {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100138 checkResult(out, failOnError)
Jakub Josef79ecec32017-02-17 14:36:28 +0100139 } finally {
140 if (output == true) {
Filip Pytlound2f1bbe2017-02-27 19:03:51 +0100141 printSaltStateResult(out)
Jakub Josef79ecec32017-02-17 14:36:28 +0100142 }
143 }
144 return out
145}
146
Jakub Josef5ade54c2017-03-10 16:14:01 +0100147/**
148 * Run command on salt minion (salt cmd.run wrapper)
149 * @param master Salt connection object
150 * @param target Get pillar target
151 * @param cmd command
152 * @return output of salt command
153 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100154def cmdRun(master, target, cmd) {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100155 def common = new com.mirantis.mk.Common()
156
Tomáš Kukráldfd4b492017-03-02 12:08:50 +0100157 common.infoMsg("Running command ${cmd} on ${target}")
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100158
Jakub Josef5ade54c2017-03-10 16:14:01 +0100159 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', null, [cmd])
Jakub Josef79ecec32017-02-17 14:36:28 +0100160}
161
Jakub Josef5ade54c2017-03-10 16:14:01 +0100162/**
163 * Perform complete salt sync between master and target
164 * @param master Salt connection object
165 * @param target Get pillar target
166 * @return output of salt command
167 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100168def syncAll(master, target) {
Filip Pytloun5a7f7fd2017-02-27 18:50:25 +0100169 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'saltutil.sync_all')
Jakub Josef79ecec32017-02-17 14:36:28 +0100170}
171
Jakub Josef5ade54c2017-03-10 16:14:01 +0100172/**
173 * Enforce highstate on given targets
174 * @param master Salt connection object
175 * @param target Highstate enforcing target
176 * @param output print output (optional, default true)
177 * @param failOnError throw exception on salt state result:false (optional, default true)
178 * @return output of salt command
179 */
180def enforceHighstate(master, target, output = false, failOnError = true) {
Filip Pytloun5a7f7fd2017-02-27 18:50:25 +0100181 def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.highstate')
Jakub Josef79ecec32017-02-17 14:36:28 +0100182 try {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100183 checkResult(out, failOnError)
Jakub Josef79ecec32017-02-17 14:36:28 +0100184 } finally {
185 if (output == true) {
Filip Pytlound2f1bbe2017-02-27 19:03:51 +0100186 printSaltStateResult(out)
Jakub Josef79ecec32017-02-17 14:36:28 +0100187 }
188 }
189 return out
190}
191
Jakub Josef5ade54c2017-03-10 16:14:01 +0100192/**
193 * Generates node key using key.gen_accept call
194 * @param master Salt connection object
195 * @param target Key generating target
196 * @param host Key generating host
197 * @param keysize generated key size (optional, default 4096)
198 * @return output of salt command
199 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100200def generateNodeKey(master, target, host, keysize = 4096) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100201 return runSaltCommand(master, 'wheel', target, 'key.gen_accept', [host], ['keysize': keysize])
Jakub Josef79ecec32017-02-17 14:36:28 +0100202}
203
Jakub Josef5ade54c2017-03-10 16:14:01 +0100204/**
205 * Generates node reclass metadata
206 * @param master Salt connection object
207 * @param target Metadata generating target
208 * @param host Metadata generating host
209 * @param classes Reclass classes
210 * @param parameters Reclass parameters
211 * @return output of salt command
212 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100213def generateNodeMetadata(master, target, host, classes, parameters) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100214 return runSaltCommand(master, 'local', target, 'reclass.node_create', [host, '_generated'], ['classes': classes, 'parameters': parameters])
Jakub Josef79ecec32017-02-17 14:36:28 +0100215}
216
Jakub Josef5ade54c2017-03-10 16:14:01 +0100217/**
218 * Run salt orchestrate on given targets
219 * @param master Salt connection object
220 * @param target Orchestration target
221 * @param orchestrate Salt orchestrate params
222 * @return output of salt command
223 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100224def orchestrateSystem(master, target, orchestrate) {
225 return runSaltCommand(master, 'runner', target, 'state.orchestrate', [orchestrate])
226}
227
Jakub Josef5ade54c2017-03-10 16:14:01 +0100228/**
229 * Run salt process step
230 * @param master Salt connection object
231 * @param tgt Salt process step target
232 * @param fun Salt process step function
233 * @param arg process step arguments (optional, default [])
234 * @param batch using batch (optional, default false)
235 * @param output print output (optional, default false)
236 * @return output of salt command
237 */
Tomáš Kukrálf5dda642017-03-02 14:22:59 +0100238def runSaltProcessStep(master, tgt, fun, arg = [], batch = null, output = false) {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100239 def common = new com.mirantis.mk.Common()
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100240 def out
241
Tomas Kukrale90bb342017-03-02 21:30:35 +0000242 common.infoMsg("Running step ${fun} on ${tgt}")
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100243
Filip Pytlounf0435c02017-03-02 17:48:54 +0100244 if (batch == true) {
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100245 out = runSaltCommand(master, 'local_batch', ['expression': tgt, 'type': 'compound'], fun, String.valueOf(batch), arg)
246 } else {
247 out = runSaltCommand(master, 'local', ['expression': tgt, 'type': 'compound'], fun, batch, arg)
Jakub Josef79ecec32017-02-17 14:36:28 +0100248 }
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100249
Tomáš Kukrálf5dda642017-03-02 14:22:59 +0100250 if (output == true) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100251 printSaltCommandResult(out)
Jakub Josef79ecec32017-02-17 14:36:28 +0100252 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100253}
254
255/**
256 * Check result for errors and throw exception if any found
257 *
258 * @param result Parsed response of Salt API
Jakub Josef5ade54c2017-03-10 16:14:01 +0100259 * @param failOnError Do you want to throw exception if salt-call fails
Jakub Josef79ecec32017-02-17 14:36:28 +0100260 */
Jakub Josef5ade54c2017-03-10 16:14:01 +0100261def checkResult(result, failOnError = true) {
262 def common = new com.mirantis.mk.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +0100263 for (entry in result['return']) {
264 if (!entry) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100265 if (failOnError) {
266 throw new Exception("Salt API returned empty response: ${result}")
267 } else {
268 common.errorMsg("Salt API returned empty response: ${result}")
269 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100270 }
271 for (node in entry) {
272 for (resource in node.value) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100273 if( resource[value] && resource[value][result] && resource[value][result] != true)
274 if (failOnError) {
275 throw new Exception("Salt state on node ${node.key} failed: ${node.value}")
276 } else {
277 common.errorMsg("Salt state on node ${node.key} failed: ${node.value}")
278 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100279 }
280 }
281 }
282 }
283}
284
285/**
286 * Print Salt state run results in human-friendly form
287 *
288 * @param result Parsed response of Salt API
289 * @param onlyChanges If true (default), print only changed resources
290 * parsing
291 */
292def printSaltStateResult(result, onlyChanges = true) {
Tomáš Kukrál6eb45f82017-03-08 18:26:16 +0100293 def common = new com.mirantis.mk.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +0100294 def out = [:]
295 for (entry in result['return']) {
296 for (node in entry) {
297 out[node.key] = [:]
298 for (resource in node.value) {
299 if (resource instanceof String) {
300 out[node.key] = node.value
301 } else if (resource.value.result.toString().toBoolean() == false || resource.value.changes || onlyChanges == false) {
302 out[node.key][resource.key] = resource.value
Tomáš Kukrál6eb45f82017-03-08 18:26:16 +0100303
Tomáš Kukrál9a195612017-03-09 09:40:53 +0100304 //if (resource.value.result.toString().toBoolean() == false && resource.key instanceof String && node.key instanceof String) {
305 // common.warningMsg("Resource ${resource.key} failed on node ${node.key}!")
306 //}
Jakub Josef79ecec32017-02-17 14:36:28 +0100307 }
308 }
309 }
310 }
311
312 for (node in out) {
313 if (node.value) {
314 println "Node ${node.key} changes:"
Tomáš Kukrál2a9b7122017-03-04 00:17:18 +0100315 print new groovy.json.JsonBuilder(node.value).toPrettyString().replace('\\n', System.getProperty('line.separator'))
Jakub Josef79ecec32017-02-17 14:36:28 +0100316 } else {
317 println "No changes for node ${node.key}"
318 }
319 }
320}
321
322/**
323 * Print Salt state run results in human-friendly form
324 *
325 * @param result Parsed response of Salt API
Jakub Josef79ecec32017-02-17 14:36:28 +0100326 */
Filip Pytlound2f1bbe2017-02-27 19:03:51 +0100327def printSaltCommandResult(result) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100328 def out = [:]
329 for (entry in result['return']) {
330 for (node in entry) {
331 out[node.key] = [:]
332 for (resource in node.value) {
333 out[node.key] = node.value
334 }
335 }
336 }
337
338 for (node in out) {
339 if (node.value) {
340 println "Node ${node.key} changes:"
341 print new groovy.json.JsonBuilder(node.value).toPrettyString()
342 } else {
343 println "No changes for node ${node.key}"
344 }
345 }
346}