blob: 8183252cc6a0dd706d9ebb9f05a883434fc1d528 [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package com.mirantis.mk
2
Jakub Josefbceaa322017-06-13 18:28:27 +02003import com.cloudbees.groovy.cps.NonCPS
Jakub Josefb77c0812017-03-27 14:11:01 +02004import java.util.stream.Collectors
Jakub Josef79ecec32017-02-17 14:36:28 +01005/**
6 * Salt functions
7 *
8*/
9
10/**
11 * Salt connection and context parameters
12 *
13 * @param url Salt API server URL
14 * @param credentialsID ID of credentials store entry
15 */
16def connection(url, credentialsId = "salt") {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +010017 def common = new com.mirantis.mk.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +010018 params = [
19 "url": url,
20 "credentialsId": credentialsId,
21 "authToken": null,
22 "creds": common.getCredentials(credentialsId)
23 ]
24 params["authToken"] = saltLogin(params)
25
26 return params
27}
28
29/**
30 * Login to Salt API, return auth token
31 *
32 * @param master Salt connection object
33 */
34def saltLogin(master) {
Tomáš Kukrál7bec0532017-02-20 15:39:31 +010035 def http = new com.mirantis.mk.Http()
Jakub Josef79ecec32017-02-17 14:36:28 +010036 data = [
37 'username': master.creds.username,
38 'password': master.creds.password.toString(),
39 'eauth': 'pam'
40 ]
Tomáš Kukrál7bec0532017-02-20 15:39:31 +010041 authToken = http.restGet(master, '/login', data)['return'][0]['token']
Jakub Josef79ecec32017-02-17 14:36:28 +010042 return authToken
43}
44
45/**
46 * Run action using Salt API
47 *
48 * @param master Salt connection object
49 * @param client Client type
50 * @param target Target specification, eg. for compound matches by Pillar
51 * data: ['expression': 'I@openssh:server', 'type': 'compound'])
52 * @param function Function to execute (eg. "state.sls")
Jakub Josef2f25cf22017-03-28 13:34:57 +020053 * @param batch Batch param to salt (integer or string with percents)
Jakub Josef79ecec32017-02-17 14:36:28 +010054 * @param args Additional arguments to function
55 * @param kwargs Additional key-value arguments to function
Jiri Broulik48544be2017-06-14 18:33:54 +020056 * @param timeout Additional argument salt api timeout
Jakub Josef79ecec32017-02-17 14:36:28 +010057 */
58@NonCPS
Jiri Broulik48544be2017-06-14 18:33:54 +020059def runSaltCommand(master, client, target, function, batch = null, args = null, kwargs = null, timeout = -1) {
iberezovskiyd4240b52017-02-20 17:18:28 +040060 def http = new com.mirantis.mk.Http()
Jakub Josef79ecec32017-02-17 14:36:28 +010061
62 data = [
63 'tgt': target.expression,
64 'fun': function,
65 'client': client,
66 'expr_form': target.type,
67 ]
68
Jakub Josef5f838212017-04-06 12:43:58 +020069 if(batch != null && ( (batch instanceof Integer && batch > 0) || (batch instanceof String && batch.contains("%")))){
Jakub Josef2f25cf22017-03-28 13:34:57 +020070 data['client']= "local_batch"
71 data['batch'] = batch
Jakub Josef79ecec32017-02-17 14:36:28 +010072 }
73
74 if (args) {
75 data['arg'] = args
76 }
77
78 if (kwargs) {
79 data['kwarg'] = kwargs
80 }
81
Jiri Broulik48544be2017-06-14 18:33:54 +020082 if (timeout != -1) {
83 data['timeout'] = timeout
84 }
85
Jakub Josef79ecec32017-02-17 14:36:28 +010086 headers = [
87 'X-Auth-Token': "${master.authToken}"
88 ]
89
90 return http.sendHttpPostRequest("${master.url}/", data, headers)
91}
92
Jakub Josef5ade54c2017-03-10 16:14:01 +010093/**
94 * Return pillar for given master and target
95 * @param master Salt connection object
96 * @param target Get pillar target
97 * @param pillar pillar name (optional)
98 * @return output of salt command
99 */
Ales Komarekcec24d42017-03-08 10:25:45 +0100100def getPillar(master, target, pillar = null) {
Tomáš Kukráld2589702017-03-10 16:30:46 +0100101 if (pillar != null) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100102 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.get', null, [pillar.replace('.', ':')])
Tomáš Kukráld2589702017-03-10 16:30:46 +0100103 } else {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100104 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.data')
Ales Komareka3c7e502017-03-13 11:20:44 +0100105 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100106}
107
Jakub Josef5ade54c2017-03-10 16:14:01 +0100108/**
109 * Return grain for given master and target
110 * @param master Salt connection object
111 * @param target Get grain target
112 * @param grain grain name (optional)
113 * @return output of salt command
114 */
Ales Komarekcec24d42017-03-08 10:25:45 +0100115def getGrain(master, target, grain = null) {
116 if(grain != null) {
Jiri Broulik852dfd52017-05-16 13:38:55 +0200117 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grains.item', null, [grain])
Jakub Josef5ade54c2017-03-10 16:14:01 +0100118 } else {
Jiri Broulik852dfd52017-05-16 13:38:55 +0200119 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grains.items')
Ales Komarekcec24d42017-03-08 10:25:45 +0100120 }
Ales Komarekcec24d42017-03-08 10:25:45 +0100121}
122
Jakub Josef5ade54c2017-03-10 16:14:01 +0100123/**
124 * Enforces state on given master and target
125 * @param master Salt connection object
126 * @param target State enforcing target
127 * @param state Salt state
128 * @param output print output (optional, default true)
129 * @param failOnError throw exception on salt state result:false (optional, default true)
Jakub Josef2f25cf22017-03-28 13:34:57 +0200130 * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch)
Jakub Josef5ade54c2017-03-10 16:14:01 +0100131 * @return output of salt command
132 */
Jakub Josef2f25cf22017-03-28 13:34:57 +0200133def enforceState(master, target, state, output = true, failOnError = true, batch = null) {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100134 def common = new com.mirantis.mk.Common()
Jakub Josef79ecec32017-02-17 14:36:28 +0100135 def run_states
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100136
Jakub Josef79ecec32017-02-17 14:36:28 +0100137 if (state instanceof String) {
138 run_states = state
139 } else {
140 run_states = state.join(',')
141 }
142
Tomáš Kukráldfd4b492017-03-02 12:08:50 +0100143 common.infoMsg("Enforcing state ${run_states} on ${target}")
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100144
Jakub Josef2f25cf22017-03-28 13:34:57 +0200145 def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', batch, [run_states])
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100146
Jakub Josef374beb72017-04-27 15:45:09 +0200147 checkResult(out, failOnError, output)
Jakub Josef79ecec32017-02-17 14:36:28 +0100148 return out
149}
150
Jakub Josef5ade54c2017-03-10 16:14:01 +0100151/**
152 * Run command on salt minion (salt cmd.run wrapper)
153 * @param master Salt connection object
154 * @param target Get pillar target
155 * @param cmd command
Jakub Josef053df392017-05-03 15:51:05 +0200156 * @param checkResponse test command success execution (default true)
Jakub Josef2f25cf22017-03-28 13:34:57 +0200157 * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch)
Jakub Josef5ade54c2017-03-10 16:14:01 +0100158 * @return output of salt command
159 */
Jiri Broulik16e9ce72017-05-17 13:28:31 +0200160def cmdRun(master, target, cmd, checkResponse = true, batch=null, output = true) {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100161 def common = new com.mirantis.mk.Common()
Jakub Josef053df392017-05-03 15:51:05 +0200162 def originalCmd = cmd
Tomáš Kukráldfd4b492017-03-02 12:08:50 +0100163 common.infoMsg("Running command ${cmd} on ${target}")
Jakub Josef053df392017-05-03 15:51:05 +0200164 if (checkResponse) {
165 cmd = cmd + " && echo Salt command execution success"
166 }
Jiri Broulik16e9ce72017-05-17 13:28:31 +0200167 def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', batch, [cmd])
Jakub Josef053df392017-05-03 15:51:05 +0200168 if (checkResponse) {
169 // iterate over all affected nodes and check success return code
Jiri Broulik16e9ce72017-05-17 13:28:31 +0200170 if (out["return"]){
171 for(int i=0;i<out["return"].size();i++){
172 def node = out["return"][i];
Jakub Josef053df392017-05-03 15:51:05 +0200173 for(int j=0;j<node.size();j++){
174 def nodeKey = node.keySet()[j]
175 if (!node[nodeKey].contains("Salt command execution success")) {
176 throw new Exception("Execution of cmd ${originalCmd} failed. Server returns: ${node[nodeKey]}")
177 }
178 }
179 }
180 }else{
181 throw new Exception("Salt Api response doesn't have return param!")
182 }
183 }
Jiri Broulik16e9ce72017-05-17 13:28:31 +0200184 if (output == true) {
185 printSaltCommandResult(out)
186 }
187 return out
Jakub Josef79ecec32017-02-17 14:36:28 +0100188}
189
Jakub Josef5ade54c2017-03-10 16:14:01 +0100190/**
191 * Perform complete salt sync between master and target
192 * @param master Salt connection object
193 * @param target Get pillar target
194 * @return output of salt command
195 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100196def syncAll(master, target) {
Filip Pytloun5a7f7fd2017-02-27 18:50:25 +0100197 return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'saltutil.sync_all')
Jakub Josef79ecec32017-02-17 14:36:28 +0100198}
199
Jakub Josef5ade54c2017-03-10 16:14:01 +0100200/**
201 * Enforce highstate on given targets
202 * @param master Salt connection object
203 * @param target Highstate enforcing target
204 * @param output print output (optional, default true)
205 * @param failOnError throw exception on salt state result:false (optional, default true)
Jakub Josef2f25cf22017-03-28 13:34:57 +0200206 * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch)
Jakub Josef5ade54c2017-03-10 16:14:01 +0100207 * @return output of salt command
208 */
Jakub Josef2f25cf22017-03-28 13:34:57 +0200209def enforceHighstate(master, target, output = false, failOnError = true, batch = null) {
210 def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.highstate', batch)
Jakub Josef374beb72017-04-27 15:45:09 +0200211 checkResult(out, failOnError, output)
Jakub Josef79ecec32017-02-17 14:36:28 +0100212 return out
213}
214
Jakub Josef5ade54c2017-03-10 16:14:01 +0100215/**
Ales Komarek5276ebe2017-03-16 08:46:34 +0100216 * Get running minions IDs according to the target
217 * @param master Salt connection object
218 * @param target Get minions target
219 * @return list of active minions fitin
220 */
221def getMinions(master, target) {
Tomáš Kukrál18ac50f2017-07-13 10:22:09 +0200222 def minionsRaw = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'test.ping')
Ales Komarek5276ebe2017-03-16 08:46:34 +0100223 return new ArrayList<String>(minionsRaw['return'][0].keySet())
224}
225
226
227/**
Tomáš Kukrálb12ff9f2017-07-12 12:32:34 +0200228 * Test if there are any minions to target
229 * @param master Salt connection object
230 * @param target Target to test
vrovachev1c4770b2017-07-05 13:25:21 +0400231 * @return bool indicating if target was succesful
Tomáš Kukrálb12ff9f2017-07-12 12:32:34 +0200232 */
233
234def testTarget(master, target) {
235 return getMinions(master, target).size() > 0
236}
237
238/**
Jakub Josef5ade54c2017-03-10 16:14:01 +0100239 * Generates node key using key.gen_accept call
240 * @param master Salt connection object
241 * @param target Key generating target
242 * @param host Key generating host
243 * @param keysize generated key size (optional, default 4096)
244 * @return output of salt command
245 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100246def generateNodeKey(master, target, host, keysize = 4096) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100247 return runSaltCommand(master, 'wheel', target, 'key.gen_accept', [host], ['keysize': keysize])
Jakub Josef79ecec32017-02-17 14:36:28 +0100248}
249
Jakub Josef5ade54c2017-03-10 16:14:01 +0100250/**
Jakub Josef2f25cf22017-03-28 13:34:57 +0200251 * Generates node reclass metadata
Jakub Josef5ade54c2017-03-10 16:14:01 +0100252 * @param master Salt connection object
253 * @param target Metadata generating target
254 * @param host Metadata generating host
255 * @param classes Reclass classes
256 * @param parameters Reclass parameters
257 * @return output of salt command
258 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100259def generateNodeMetadata(master, target, host, classes, parameters) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100260 return runSaltCommand(master, 'local', target, 'reclass.node_create', [host, '_generated'], ['classes': classes, 'parameters': parameters])
Jakub Josef79ecec32017-02-17 14:36:28 +0100261}
262
Jakub Josef5ade54c2017-03-10 16:14:01 +0100263/**
264 * Run salt orchestrate on given targets
265 * @param master Salt connection object
266 * @param target Orchestration target
267 * @param orchestrate Salt orchestrate params
268 * @return output of salt command
269 */
Jakub Josef79ecec32017-02-17 14:36:28 +0100270def orchestrateSystem(master, target, orchestrate) {
271 return runSaltCommand(master, 'runner', target, 'state.orchestrate', [orchestrate])
272}
273
Jakub Josef5ade54c2017-03-10 16:14:01 +0100274/**
275 * Run salt process step
276 * @param master Salt connection object
277 * @param tgt Salt process step target
278 * @param fun Salt process step function
279 * @param arg process step arguments (optional, default [])
Jakub Josef2f25cf22017-03-28 13:34:57 +0200280 * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch)
Jakub Josef5ade54c2017-03-10 16:14:01 +0100281 * @param output print output (optional, default false)
Jiri Broulik48544be2017-06-14 18:33:54 +0200282 * @param timeout Additional argument salt api timeout
Jakub Josef5ade54c2017-03-10 16:14:01 +0100283 * @return output of salt command
284 */
Jiri Broulik48544be2017-06-14 18:33:54 +0200285def runSaltProcessStep(master, tgt, fun, arg = [], batch = null, output = false, timeout = -1) {
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100286 def common = new com.mirantis.mk.Common()
Jiri Broulik48544be2017-06-14 18:33:54 +0200287 def salt = new com.mirantis.mk.Salt()
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100288 def out
289
Tomas Kukrale90bb342017-03-02 21:30:35 +0000290 common.infoMsg("Running step ${fun} on ${tgt}")
Tomáš Kukrál6c04bd02017-03-01 22:18:52 +0100291
Filip Pytlounf0435c02017-03-02 17:48:54 +0100292 if (batch == true) {
Jiri Broulik48544be2017-06-14 18:33:54 +0200293 out = runSaltCommand(master, 'local_batch', ['expression': tgt, 'type': 'compound'], fun, String.valueOf(batch), arg, null, timeout)
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100294 } else {
Jiri Broulik48544be2017-06-14 18:33:54 +0200295 out = runSaltCommand(master, 'local', ['expression': tgt, 'type': 'compound'], fun, batch, arg, null, timeout)
Jakub Josef79ecec32017-02-17 14:36:28 +0100296 }
Tomáš Kukráladb4ecd2017-03-02 10:06:36 +0100297
Tomáš Kukrálf5dda642017-03-02 14:22:59 +0100298 if (output == true) {
Jiri Broulik48544be2017-06-14 18:33:54 +0200299 salt.printSaltCommandResult(out)
Jakub Josef79ecec32017-02-17 14:36:28 +0100300 }
Jiri Broulikae19c262017-05-16 19:06:52 +0200301 return out
Jakub Josef79ecec32017-02-17 14:36:28 +0100302}
303
304/**
305 * Check result for errors and throw exception if any found
306 *
307 * @param result Parsed response of Salt API
Jakub Josef8021c002017-03-27 15:41:28 +0200308 * @param failOnError Do you want to throw exception if salt-call fails (optional, default true)
Jakub Josefa87941c2017-04-20 17:14:58 +0200309 * @param printResults Do you want to print salt results (optional, default true)
Jakub Josefa87941c2017-04-20 17:14:58 +0200310 * @param printOnlyChanges If true (default), print only changed resources
Jakub Josef79ecec32017-02-17 14:36:28 +0100311 */
Jakub Josef374beb72017-04-27 15:45:09 +0200312def checkResult(result, failOnError = true, printResults = true, printOnlyChanges = true) {
Jakub Josef5ade54c2017-03-10 16:14:01 +0100313 def common = new com.mirantis.mk.Common()
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100314 if(result != null){
315 if(result['return']){
316 for (int i=0;i<result['return'].size();i++) {
317 def entry = result['return'][i]
318 if (!entry) {
319 if (failOnError) {
320 throw new Exception("Salt API returned empty response: ${result}")
321 } else {
322 common.errorMsg("Salt API returned empty response: ${result}")
Jakub Josefece32af2017-03-14 19:20:08 +0100323 }
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100324 }
325 for (int j=0;j<entry.size();j++) {
326 def nodeKey = entry.keySet()[j]
327 def node=entry[nodeKey]
Jakub Josefa87941c2017-04-20 17:14:58 +0200328 def outputResources = []
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100329 common.infoMsg("Node ${nodeKey} changes:")
330 if(node instanceof Map || node instanceof List){
331 for (int k=0;k<node.size();k++) {
332 def resource;
333 def resKey;
334 if(node instanceof Map){
335 resKey = node.keySet()[k]
336 }else if(node instanceof List){
337 resKey = k
338 }
339 resource = node[resKey]
Jakub Josefc4c40202017-04-28 12:04:24 +0200340 // print
Jakub Josefa87941c2017-04-20 17:14:58 +0200341 if(printResults){
342 if(resource instanceof Map && resource.keySet().contains("result")){
343 //clean unnesaccary fields
344 if(resource.keySet().contains("__run_num__")){
345 resource.remove("__run_num__")
346 }
347 if(resource.keySet().contains("__id__")){
348 resource.remove("__id__")
349 }
350 if(resource.keySet().contains("pchanges")){
351 resource.remove("pchanges")
352 }
353 if(!resource["result"] || (resource["result"] instanceof String && resource["result"] != "true")){
354 if(resource["result"] != null){
Jakub Josefbceaa322017-06-13 18:28:27 +0200355 outputResources.add(String.format("Resource: %s\n\u001B[31m%s\u001B[0m", resKey, common.prettify(resource)))
Jakub Josefa87941c2017-04-20 17:14:58 +0200356 }else{
Jakub Josefbceaa322017-06-13 18:28:27 +0200357 outputResources.add(String.format("Resource: %s\n\u001B[33m%s\u001B[0m", resKey, common.prettify(resource)))
Jakub Josefa87941c2017-04-20 17:14:58 +0200358 }
359 }else{
360 if(!printOnlyChanges || resource.changes.size() > 0){
Jakub Josefbceaa322017-06-13 18:28:27 +0200361 outputResources.add(String.format("Resource: %s\n\u001B[32m%s\u001B[0m", resKey, common.prettify(resource)))
Jakub Josefa87941c2017-04-20 17:14:58 +0200362 }
363 }
364 }else{
Jakub Josefbceaa322017-06-13 18:28:27 +0200365 outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", resKey, common.prettify(resource)))
Jakub Josefa87941c2017-04-20 17:14:58 +0200366 }
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100367 }
Jakub Josefc4c40202017-04-28 12:04:24 +0200368 common.debugMsg("checkResult: checking resource: ${resource}")
369 if(resource instanceof String || (resource["result"] != null && !resource["result"]) || (resource["result"] instanceof String && resource["result"] == "false")){
Jakub Josefbceaa322017-06-13 18:28:27 +0200370 def prettyResource = common.prettify(resource)
Jakub Josefc4c40202017-04-28 12:04:24 +0200371 if(env["ASK_ON_ERROR"] && env["ASK_ON_ERROR"] == "true"){
372 timeout(time:1, unit:'HOURS') {
373 input message: "False result on ${nodeKey} found, resource ${prettyResource}. \nDo you want to continue?"
374 }
375 }else{
Jakub Josefd97d7db2017-05-11 19:11:53 +0200376 common.errorMsg(String.format("Resource: %s\n%s", resKey, prettyResource))
Jakub Josefd9001df2017-05-11 16:45:28 +0200377 def errorMsg = "Salt state on node ${nodeKey} failed: ${prettyResource}."
Jakub Josefc4c40202017-04-28 12:04:24 +0200378 if (failOnError) {
379 throw new Exception(errorMsg)
380 } else {
381 common.errorMsg(errorMsg)
382 }
383 }
384 }
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100385 }
Jakub Josefa87941c2017-04-20 17:14:58 +0200386 }else if(node!=null && node!=""){
Jakub Josefbceaa322017-06-13 18:28:27 +0200387 outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", resKey, common.prettify(node)))
Jakub Josefa87941c2017-04-20 17:14:58 +0200388 }
389 if(printResults && !outputResources.isEmpty()){
Jakub Josefb77c0812017-03-27 14:11:01 +0200390 wrap([$class: 'AnsiColorBuildWrapper']) {
391 print outputResources.stream().collect(Collectors.joining("\n"))
392 }
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100393 }
394 }
Jakub Josef52f69f72017-03-14 15:18:08 +0100395 }
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100396 }else{
397 common.errorMsg("Salt result hasn't return attribute! Result: ${result}")
Jakub Josef79ecec32017-02-17 14:36:28 +0100398 }
Jakub Josef52f69f72017-03-14 15:18:08 +0100399 }else{
Jakub Josefa87941c2017-04-20 17:14:58 +0200400 common.errorMsg("Cannot check salt result, given result is null")
Jakub Josef79ecec32017-02-17 14:36:28 +0100401 }
402}
403
404/**
Jakub Josef7852fe12017-03-15 16:02:41 +0100405 * Print salt command run results in human-friendly form
Jakub Josef79ecec32017-02-17 14:36:28 +0100406 *
407 * @param result Parsed response of Salt API
Jakub Josef79ecec32017-02-17 14:36:28 +0100408 */
Filip Pytlound2f1bbe2017-02-27 19:03:51 +0100409def printSaltCommandResult(result) {
Jakub Josef871bf152017-03-14 20:13:41 +0100410 def common = new com.mirantis.mk.Common()
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100411 if(result != null){
412 if(result['return']){
413 for (int i=0; i<result['return'].size(); i++) {
414 def entry = result['return'][i]
415 for (int j=0; j<entry.size(); j++) {
416 common.debugMsg("printSaltCommandResult: printing salt command entry: ${entry}")
417 def nodeKey = entry.keySet()[j]
418 def node=entry[nodeKey]
Jakub Josefbceaa322017-06-13 18:28:27 +0200419 common.infoMsg(String.format("Node %s changes:\n%s",nodeKey, common.prettify(node)))
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100420 }
Jakub Josef8a715bf2017-03-14 21:39:01 +0100421 }
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100422 }else{
423 common.errorMsg("Salt result hasn't return attribute! Result: ${result}")
Jakub Josef79ecec32017-02-17 14:36:28 +0100424 }
Jakub Josef8a715bf2017-03-14 21:39:01 +0100425 }else{
Jakub Josefd9afd0e2017-03-15 19:19:23 +0100426 common.errorMsg("Cannot print salt command result, given result is null")
Jakub Josef52f69f72017-03-14 15:18:08 +0100427 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100428}
Tomáš Kukrálb12eedd2017-04-21 10:45:13 +0200429
430
431/**
432 * Return content of file target
433 *
434 * @param master Salt master object
435 * @param target Compound target (should target only one host)
436 * @param file File path to read (/etc/hosts for example)
437 */
438
439def getFileContent(master, target, file) {
440 result = cmdRun(master, target, "cat ${file}")
441 return result['return'][0].values()[0]
442}
Matthew Mosesohn9e880852017-07-04 21:17:53 +0300443
444/**
445 * Set override parameters in Salt cluster metadata
446 *
447 * @param master Salt master object
448 * @param salt_overrides YAML formatted string containing key: value, one per line
449 */
450
451def setSaltOverrides(master, salt_overrides, debug=false) {
452 def mcpcommon = new com.mirantis.mcp.Common()
Tomáš Kukrálf178f052017-07-11 11:31:00 +0200453 def common = new com.mirantis.mk.Common()
Matthew Mosesohn9e880852017-07-04 21:17:53 +0300454
455 def salt_overrides_map = mcpcommon.loadYAML(salt_overrides)
Tomáš Kukrál243cf842017-07-11 13:11:56 +0200456 for (entry in common.entries(salt_overrides_map)) {
Matthew Mosesohn9e880852017-07-04 21:17:53 +0300457 def key = entry[0]
458 def value = entry[1]
459
460 common.debugMsg("Set salt override ${key}=${value}")
461 runSaltProcessStep(master, 'I@salt:master', 'reclass.cluster_meta_set', ["${key}", "${value}"], false, debug)
462 }
463}