blob: ab00eb5b8c109f2b0581d87cb76cfee1c8c6a4a9 [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')
Ales Komareka3c7e502017-03-13 11:20:44 +010097 }
Jakub Josef79ecec32017-02-17 14:36:28 +010098}
99
Jakub Josef5ade54c2017-03-10 16:14:01 +0100100/**
101 * Return grain for given master and target
102 * @param master Salt connection object
103 * @param target Get grain target
104 * @param grain grain name (optional)
105 * @return output of salt command
106 */
Ales Komarekcec24d42017-03-08 10:25:45 +0100107def getGrain(master, target, grain = null) {
108 if(grain != null) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100109 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grain.item', null, [grain])
110 } else {
111 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grain.items')
Ales Komarekcec24d42017-03-08 10:25:45 +0100112 }
Ales Komarekcec24d42017-03-08 10:25:45 +0100113}
114
Jakub Josef5ade54c2017-03-10 16:14:01 +0100115/**
116 * Enforces state on given master and target
117 * @param master Salt connection object
118 * @param target State enforcing target
119 * @param state Salt state
120 * @param output print output (optional, default true)
121 * @param failOnError throw exception on salt state result:false (optional, default true)
122 * @return output of salt command
123 */
124def enforceState(master, target, state, output = true, failOnError = true) {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100125 def common = new com.mirantis.mk.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +0100126 def run_states
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100127
Jakub Josef79ecec32017-02-17 14:36:28 +0100128 if (state instanceof String) {
129 run_states = state
130 } else {
131 run_states = state.join(',')
132 }
133
Tomáš Kukráldfd4b492017-03-02 12:08:50 +0100134 common.infoMsg("Enforcing state ${run_states} on ${target}")
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100135
Filip Pytloun5a7f7fd2017-02-27 18:50:25 +0100136 def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', null, [run_states])
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100137
Jakub Josef79ecec32017-02-17 14:36:28 +0100138 try {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100139 checkResult(out, failOnError)
Jakub Josef79ecec32017-02-17 14:36:28 +0100140 } finally {
141 if (output == true) {
Filip Pytlound2f1bbe2017-02-27 19:03:51 +0100142 printSaltStateResult(out)
Jakub Josef79ecec32017-02-17 14:36:28 +0100143 }
144 }
145 return out
146}
147
Jakub Josef5ade54c2017-03-10 16:14:01 +0100148/**
149 * Run command on salt minion (salt cmd.run wrapper)
150 * @param master Salt connection object
151 * @param target Get pillar target
152 * @param cmd command
153 * @return output of salt command
154 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100155def cmdRun(master, target, cmd) {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100156 def common = new com.mirantis.mk.Common()
157
Tomáš Kukráldfd4b492017-03-02 12:08:50 +0100158 common.infoMsg("Running command ${cmd} on ${target}")
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100159
Jakub Josef5ade54c2017-03-10 16:14:01 +0100160 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', null, [cmd])
Jakub Josef79ecec32017-02-17 14:36:28 +0100161}
162
Jakub Josef5ade54c2017-03-10 16:14:01 +0100163/**
164 * Perform complete salt sync between master and target
165 * @param master Salt connection object
166 * @param target Get pillar target
167 * @return output of salt command
168 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100169def syncAll(master, target) {
Filip Pytloun5a7f7fd2017-02-27 18:50:25 +0100170 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'saltutil.sync_all')
Jakub Josef79ecec32017-02-17 14:36:28 +0100171}
172
Jakub Josef5ade54c2017-03-10 16:14:01 +0100173/**
174 * Enforce highstate on given targets
175 * @param master Salt connection object
176 * @param target Highstate enforcing target
177 * @param output print output (optional, default true)
178 * @param failOnError throw exception on salt state result:false (optional, default true)
179 * @return output of salt command
180 */
181def enforceHighstate(master, target, output = false, failOnError = true) {
Filip Pytloun5a7f7fd2017-02-27 18:50:25 +0100182 def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.highstate')
Jakub Josef79ecec32017-02-17 14:36:28 +0100183 try {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100184 checkResult(out, failOnError)
Jakub Josef79ecec32017-02-17 14:36:28 +0100185 } finally {
186 if (output == true) {
Filip Pytlound2f1bbe2017-02-27 19:03:51 +0100187 printSaltStateResult(out)
Jakub Josef79ecec32017-02-17 14:36:28 +0100188 }
189 }
190 return out
191}
192
Jakub Josef5ade54c2017-03-10 16:14:01 +0100193/**
194 * Generates node key using key.gen_accept call
195 * @param master Salt connection object
196 * @param target Key generating target
197 * @param host Key generating host
198 * @param keysize generated key size (optional, default 4096)
199 * @return output of salt command
200 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100201def generateNodeKey(master, target, host, keysize = 4096) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100202 return runSaltCommand(master, 'wheel', target, 'key.gen_accept', [host], ['keysize': keysize])
Jakub Josef79ecec32017-02-17 14:36:28 +0100203}
204
Jakub Josef5ade54c2017-03-10 16:14:01 +0100205/**
206 * Generates node reclass metadata
207 * @param master Salt connection object
208 * @param target Metadata generating target
209 * @param host Metadata generating host
210 * @param classes Reclass classes
211 * @param parameters Reclass parameters
212 * @return output of salt command
213 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100214def generateNodeMetadata(master, target, host, classes, parameters) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100215 return runSaltCommand(master, 'local', target, 'reclass.node_create', [host, '_generated'], ['classes': classes, 'parameters': parameters])
Jakub Josef79ecec32017-02-17 14:36:28 +0100216}
217
Jakub Josef5ade54c2017-03-10 16:14:01 +0100218/**
219 * Run salt orchestrate on given targets
220 * @param master Salt connection object
221 * @param target Orchestration target
222 * @param orchestrate Salt orchestrate params
223 * @return output of salt command
224 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100225def orchestrateSystem(master, target, orchestrate) {
226 return runSaltCommand(master, 'runner', target, 'state.orchestrate', [orchestrate])
227}
228
Jakub Josef5ade54c2017-03-10 16:14:01 +0100229/**
230 * Run salt process step
231 * @param master Salt connection object
232 * @param tgt Salt process step target
233 * @param fun Salt process step function
234 * @param arg process step arguments (optional, default [])
235 * @param batch using batch (optional, default false)
236 * @param output print output (optional, default false)
237 * @return output of salt command
238 */
Tomáš Kukrálf5dda642017-03-02 14:22:59 +0100239def runSaltProcessStep(master, tgt, fun, arg = [], batch = null, output = false) {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100240 def common = new com.mirantis.mk.Common()
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100241 def out
242
Tomas Kukrale90bb342017-03-02 21:30:35 +0000243 common.infoMsg("Running step ${fun} on ${tgt}")
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100244
Filip Pytlounf0435c02017-03-02 17:48:54 +0100245 if (batch == true) {
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100246 out = runSaltCommand(master, 'local_batch', ['expression': tgt, 'type': 'compound'], fun, String.valueOf(batch), arg)
247 } else {
248 out = runSaltCommand(master, 'local', ['expression': tgt, 'type': 'compound'], fun, batch, arg)
Jakub Josef79ecec32017-02-17 14:36:28 +0100249 }
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100250
Tomáš Kukrálf5dda642017-03-02 14:22:59 +0100251 if (output == true) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100252 printSaltCommandResult(out)
Jakub Josef79ecec32017-02-17 14:36:28 +0100253 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100254}
255
256/**
257 * Check result for errors and throw exception if any found
258 *
259 * @param result Parsed response of Salt API
Jakub Josef5ade54c2017-03-10 16:14:01 +0100260 * @param failOnError Do you want to throw exception if salt-call fails
Jakub Josef79ecec32017-02-17 14:36:28 +0100261 */
Jakub Josef5ade54c2017-03-10 16:14:01 +0100262def checkResult(result, failOnError = true) {
263 def common = new com.mirantis.mk.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +0100264 for (entry in result['return']) {
265 if (!entry) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100266 if (failOnError) {
267 throw new Exception("Salt API returned empty response: ${result}")
268 } else {
269 common.errorMsg("Salt API returned empty response: ${result}")
270 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100271 }
272 for (node in entry) {
273 for (resource in node.value) {
Jakub Josef1aeb6e32017-03-13 11:57:26 +0100274 if(!resource["result"] || resource["result"] != "true") {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100275 if (failOnError) {
276 throw new Exception("Salt state on node ${node.key} failed: ${node.value}")
277 } else {
278 common.errorMsg("Salt state on node ${node.key} failed: ${node.value}")
279 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100280 }
281 }
282 }
283 }
284}
285
286/**
287 * Print Salt state run results in human-friendly form
288 *
289 * @param result Parsed response of Salt API
290 * @param onlyChanges If true (default), print only changed resources
291 * parsing
292 */
293def printSaltStateResult(result, onlyChanges = true) {
Tomáš Kukrál6eb45f82017-03-08 18:26:16 +0100294 def common = new com.mirantis.mk.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +0100295 def out = [:]
296 for (entry in result['return']) {
297 for (node in entry) {
298 out[node.key] = [:]
299 for (resource in node.value) {
300 if (resource instanceof String) {
301 out[node.key] = node.value
302 } else if (resource.value.result.toString().toBoolean() == false || resource.value.changes || onlyChanges == false) {
303 out[node.key][resource.key] = resource.value
Tomáš Kukrál6eb45f82017-03-08 18:26:16 +0100304
Tomáš Kukrál9a195612017-03-09 09:40:53 +0100305 //if (resource.value.result.toString().toBoolean() == false && resource.key instanceof String && node.key instanceof String) {
306 // common.warningMsg("Resource ${resource.key} failed on node ${node.key}!")
307 //}
Jakub Josef79ecec32017-02-17 14:36:28 +0100308 }
309 }
310 }
311 }
312
313 for (node in out) {
314 if (node.value) {
315 println "Node ${node.key} changes:"
Tomáš Kukrál2a9b7122017-03-04 00:17:18 +0100316 print new groovy.json.JsonBuilder(node.value).toPrettyString().replace('\\n', System.getProperty('line.separator'))
Jakub Josef79ecec32017-02-17 14:36:28 +0100317 } else {
318 println "No changes for node ${node.key}"
319 }
320 }
321}
322
323/**
324 * Print Salt state run results in human-friendly form
325 *
326 * @param result Parsed response of Salt API
Jakub Josef79ecec32017-02-17 14:36:28 +0100327 */
Filip Pytlound2f1bbe2017-02-27 19:03:51 +0100328def printSaltCommandResult(result) {
Jakub Josef79ecec32017-02-17 14:36:28 +0100329 def out = [:]
330 for (entry in result['return']) {
331 for (node in entry) {
332 out[node.key] = [:]
333 for (resource in node.value) {
334 out[node.key] = node.value
335 }
336 }
337 }
338
339 for (node in out) {
340 if (node.value) {
341 println "Node ${node.key} changes:"
342 print new groovy.json.JsonBuilder(node.value).toPrettyString()
343 } else {
344 println "No changes for node ${node.key}"
345 }
346 }
347}