Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 3 | import static groovy.json.JsonOutput.prettyPrint |
| 4 | import static groovy.json.JsonOutput.toJson |
Jakub Josef | 935d2ef | 2017-03-15 17:35:51 +0100 | [diff] [blame] | 5 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 6 | /** |
| 7 | * Salt functions |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * Salt connection and context parameters |
| 13 | * |
| 14 | * @param url Salt API server URL |
| 15 | * @param credentialsID ID of credentials store entry |
| 16 | */ |
| 17 | def connection(url, credentialsId = "salt") { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 18 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 19 | params = [ |
| 20 | "url": url, |
| 21 | "credentialsId": credentialsId, |
| 22 | "authToken": null, |
| 23 | "creds": common.getCredentials(credentialsId) |
| 24 | ] |
| 25 | params["authToken"] = saltLogin(params) |
| 26 | |
| 27 | return params |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Login to Salt API, return auth token |
| 32 | * |
| 33 | * @param master Salt connection object |
| 34 | */ |
| 35 | def saltLogin(master) { |
Tomáš Kukrál | 7bec053 | 2017-02-20 15:39:31 +0100 | [diff] [blame] | 36 | def http = new com.mirantis.mk.Http() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 37 | data = [ |
| 38 | 'username': master.creds.username, |
| 39 | 'password': master.creds.password.toString(), |
| 40 | 'eauth': 'pam' |
| 41 | ] |
Tomáš Kukrál | 7bec053 | 2017-02-20 15:39:31 +0100 | [diff] [blame] | 42 | authToken = http.restGet(master, '/login', data)['return'][0]['token'] |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 43 | return authToken |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Run action using Salt API |
| 48 | * |
| 49 | * @param master Salt connection object |
| 50 | * @param client Client type |
| 51 | * @param target Target specification, eg. for compound matches by Pillar |
| 52 | * data: ['expression': 'I@openssh:server', 'type': 'compound']) |
| 53 | * @param function Function to execute (eg. "state.sls") |
Filip Pytloun | f0435c0 | 2017-03-02 17:48:54 +0100 | [diff] [blame] | 54 | * @param batch |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 55 | * @param args Additional arguments to function |
| 56 | * @param kwargs Additional key-value arguments to function |
| 57 | */ |
| 58 | @NonCPS |
| 59 | def runSaltCommand(master, client, target, function, batch = null, args = null, kwargs = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 60 | def http = new com.mirantis.mk.Http() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 61 | |
| 62 | data = [ |
| 63 | 'tgt': target.expression, |
| 64 | 'fun': function, |
| 65 | 'client': client, |
| 66 | 'expr_form': target.type, |
| 67 | ] |
| 68 | |
Filip Pytloun | f0435c0 | 2017-03-02 17:48:54 +0100 | [diff] [blame] | 69 | if (batch == true) { |
| 70 | data['batch'] = "local_batch" |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (args) { |
| 74 | data['arg'] = args |
| 75 | } |
| 76 | |
| 77 | if (kwargs) { |
| 78 | data['kwarg'] = kwargs |
| 79 | } |
| 80 | |
| 81 | headers = [ |
| 82 | 'X-Auth-Token': "${master.authToken}" |
| 83 | ] |
| 84 | |
| 85 | return http.sendHttpPostRequest("${master.url}/", data, headers) |
| 86 | } |
| 87 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 88 | /** |
| 89 | * Return pillar for given master and target |
| 90 | * @param master Salt connection object |
| 91 | * @param target Get pillar target |
| 92 | * @param pillar pillar name (optional) |
| 93 | * @return output of salt command |
| 94 | */ |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 95 | def getPillar(master, target, pillar = null) { |
Tomáš Kukrál | d258970 | 2017-03-10 16:30:46 +0100 | [diff] [blame] | 96 | if (pillar != null) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 97 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.get', null, [pillar.replace('.', ':')]) |
Tomáš Kukrál | d258970 | 2017-03-10 16:30:46 +0100 | [diff] [blame] | 98 | } else { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 99 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.data') |
Ales Komarek | a3c7e50 | 2017-03-13 11:20:44 +0100 | [diff] [blame] | 100 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 103 | /** |
| 104 | * Return grain for given master and target |
| 105 | * @param master Salt connection object |
| 106 | * @param target Get grain target |
| 107 | * @param grain grain name (optional) |
| 108 | * @return output of salt command |
| 109 | */ |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 110 | def getGrain(master, target, grain = null) { |
| 111 | if(grain != null) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 112 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grain.item', null, [grain]) |
| 113 | } else { |
| 114 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grain.items') |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 115 | } |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 118 | /** |
| 119 | * Enforces state on given master and target |
| 120 | * @param master Salt connection object |
| 121 | * @param target State enforcing target |
| 122 | * @param state Salt state |
| 123 | * @param output print output (optional, default true) |
| 124 | * @param failOnError throw exception on salt state result:false (optional, default true) |
| 125 | * @return output of salt command |
| 126 | */ |
| 127 | def enforceState(master, target, state, output = true, failOnError = true) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 128 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 129 | def run_states |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 130 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 131 | if (state instanceof String) { |
| 132 | run_states = state |
| 133 | } else { |
| 134 | run_states = state.join(',') |
| 135 | } |
| 136 | |
Tomáš Kukrál | dfd4b49 | 2017-03-02 12:08:50 +0100 | [diff] [blame] | 137 | common.infoMsg("Enforcing state ${run_states} on ${target}") |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 138 | |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 139 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', null, [run_states]) |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 140 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 141 | try { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 142 | checkResult(out, failOnError) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 143 | } finally { |
| 144 | if (output == true) { |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 145 | printSaltStateResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | return out |
| 149 | } |
| 150 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 151 | /** |
| 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 |
| 156 | * @return output of salt command |
| 157 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 158 | def cmdRun(master, target, cmd) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 159 | def common = new com.mirantis.mk.Common() |
| 160 | |
Tomáš Kukrál | dfd4b49 | 2017-03-02 12:08:50 +0100 | [diff] [blame] | 161 | common.infoMsg("Running command ${cmd} on ${target}") |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 162 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 163 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', null, [cmd]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 166 | /** |
| 167 | * Perform complete salt sync between master and target |
| 168 | * @param master Salt connection object |
| 169 | * @param target Get pillar target |
| 170 | * @return output of salt command |
| 171 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 172 | def syncAll(master, target) { |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 173 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'saltutil.sync_all') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 176 | /** |
| 177 | * Enforce highstate on given targets |
| 178 | * @param master Salt connection object |
| 179 | * @param target Highstate enforcing target |
| 180 | * @param output print output (optional, default true) |
| 181 | * @param failOnError throw exception on salt state result:false (optional, default true) |
| 182 | * @return output of salt command |
| 183 | */ |
| 184 | def enforceHighstate(master, target, output = false, failOnError = true) { |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 185 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.highstate') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 186 | try { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 187 | checkResult(out, failOnError) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 188 | } finally { |
| 189 | if (output == true) { |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 190 | printSaltStateResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | return out |
| 194 | } |
| 195 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 196 | /** |
| 197 | * Generates node key using key.gen_accept call |
| 198 | * @param master Salt connection object |
| 199 | * @param target Key generating target |
| 200 | * @param host Key generating host |
| 201 | * @param keysize generated key size (optional, default 4096) |
| 202 | * @return output of salt command |
| 203 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 204 | def generateNodeKey(master, target, host, keysize = 4096) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 205 | return runSaltCommand(master, 'wheel', target, 'key.gen_accept', [host], ['keysize': keysize]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 208 | /** |
| 209 | * Generates node reclass metadata |
| 210 | * @param master Salt connection object |
| 211 | * @param target Metadata generating target |
| 212 | * @param host Metadata generating host |
| 213 | * @param classes Reclass classes |
| 214 | * @param parameters Reclass parameters |
| 215 | * @return output of salt command |
| 216 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 217 | def generateNodeMetadata(master, target, host, classes, parameters) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 218 | return runSaltCommand(master, 'local', target, 'reclass.node_create', [host, '_generated'], ['classes': classes, 'parameters': parameters]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 221 | /** |
| 222 | * Run salt orchestrate on given targets |
| 223 | * @param master Salt connection object |
| 224 | * @param target Orchestration target |
| 225 | * @param orchestrate Salt orchestrate params |
| 226 | * @return output of salt command |
| 227 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 228 | def orchestrateSystem(master, target, orchestrate) { |
| 229 | return runSaltCommand(master, 'runner', target, 'state.orchestrate', [orchestrate]) |
| 230 | } |
| 231 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 232 | /** |
| 233 | * Run salt process step |
| 234 | * @param master Salt connection object |
| 235 | * @param tgt Salt process step target |
| 236 | * @param fun Salt process step function |
| 237 | * @param arg process step arguments (optional, default []) |
| 238 | * @param batch using batch (optional, default false) |
| 239 | * @param output print output (optional, default false) |
| 240 | * @return output of salt command |
| 241 | */ |
Tomáš Kukrál | f5dda64 | 2017-03-02 14:22:59 +0100 | [diff] [blame] | 242 | def runSaltProcessStep(master, tgt, fun, arg = [], batch = null, output = false) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 243 | def common = new com.mirantis.mk.Common() |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 244 | def out |
| 245 | |
Tomas Kukral | e90bb34 | 2017-03-02 21:30:35 +0000 | [diff] [blame] | 246 | common.infoMsg("Running step ${fun} on ${tgt}") |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 247 | |
Filip Pytloun | f0435c0 | 2017-03-02 17:48:54 +0100 | [diff] [blame] | 248 | if (batch == true) { |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 249 | out = runSaltCommand(master, 'local_batch', ['expression': tgt, 'type': 'compound'], fun, String.valueOf(batch), arg) |
| 250 | } else { |
| 251 | out = runSaltCommand(master, 'local', ['expression': tgt, 'type': 'compound'], fun, batch, arg) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 252 | } |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 253 | |
Tomáš Kukrál | f5dda64 | 2017-03-02 14:22:59 +0100 | [diff] [blame] | 254 | if (output == true) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 255 | printSaltCommandResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 256 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Check result for errors and throw exception if any found |
| 261 | * |
| 262 | * @param result Parsed response of Salt API |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 263 | * @param failOnError Do you want to throw exception if salt-call fails |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 264 | */ |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 265 | def checkResult(result, failOnError = true) { |
| 266 | def common = new com.mirantis.mk.Common() |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 267 | if(result != null){ |
| 268 | if(result['return']){ |
| 269 | for (int i=0;i<result['return'].size();i++) { |
| 270 | def entry = result['return'][i] |
| 271 | if (!entry) { |
| 272 | if (failOnError) { |
| 273 | throw new Exception("Salt API returned empty response: ${result}") |
| 274 | } else { |
| 275 | common.errorMsg("Salt API returned empty response: ${result}") |
Jakub Josef | ece32af | 2017-03-14 19:20:08 +0100 | [diff] [blame] | 276 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 277 | } |
| 278 | for (int j=0;j<entry.size();j++) { |
| 279 | def nodeKey = entry.keySet()[j] |
| 280 | def node=entry[nodeKey] |
| 281 | for (int k=0;k<node.size();k++) { |
| 282 | def resource; |
| 283 | def resKey; |
| 284 | if(node instanceof Map){ |
| 285 | resKey = node.keySet()[k] |
| 286 | }else if(node instanceof List){ |
| 287 | resKey = k |
| 288 | } |
| 289 | resource = node[resKey] |
| 290 | common.debugMsg("checkResult: checking resource: ${resource}") |
| 291 | if(resource instanceof String || !resource["result"] || (resource["result"] instanceof String && resource["result"] != "true")){ |
| 292 | if (failOnError) { |
| 293 | throw new Exception("Salt state on node ${nodeKey} failed: ${resource}. State output: ${node}") |
| 294 | } else { |
| 295 | common.errorMsg("Salt state on node ${nodeKey} failed: ${resource}. State output: ${node}") |
| 296 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 297 | } |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 298 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 299 | } |
| 300 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 301 | }else{ |
| 302 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 303 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 304 | }else{ |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 305 | common.errorMsg("Cannot check salt result, given result is null") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Print Salt state run results in human-friendly form |
| 311 | * |
| 312 | * @param result Parsed response of Salt API |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 313 | * @param onlyChanges If true (default), print only changed resources |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 314 | */ |
| 315 | def printSaltStateResult(result, onlyChanges = true) { |
Tomáš Kukrál | 6eb45f8 | 2017-03-08 18:26:16 +0100 | [diff] [blame] | 316 | def common = new com.mirantis.mk.Common() |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 317 | if(result != null){ |
| 318 | if(result['return']){ |
| 319 | for (int i=0; i<result['return'].size(); i++) { |
| 320 | def entry = result['return'][i] |
| 321 | for (int j=0; j<entry.size(); j++) { |
| 322 | common.debugMsg("printSaltStateResult: printing salt command entry: ${entry}") |
| 323 | def nodeKey = entry.keySet()[j] |
| 324 | def node=entry[nodeKey] |
| 325 | common.infoMsg("Node ${nodeKey} changes:") |
| 326 | if(node instanceof Map || node instanceof List){ |
| 327 | for (int k=0;k<node.size();k++) { |
| 328 | def resource; |
| 329 | def resKey; |
| 330 | if(node instanceof Map){ |
| 331 | resKey = node.keySet()[k] |
| 332 | }else if(node instanceof List){ |
| 333 | resKey = k |
| 334 | } |
| 335 | resource = node[resKey] |
| 336 | //clean unnesaccary fields |
| 337 | resource.remove("__run_num__") |
| 338 | resource.remove("__id__") |
| 339 | if(resource instanceof Map && resource.keySet().contains("result")){ |
| 340 | if(!resource["result"] || (resource["result"] instanceof String && resource["result"] != "true")){ |
| 341 | common.errorMsg(String.format("Resource: %s\n%s", resKey, prettyPrint(toJson(resource)).replace('\\n', System.getProperty('line.separator')))) |
| 342 | }else{ |
| 343 | if(!onlyChanges || resource.changes.size() > 0){ |
| 344 | common.successMsg(String.format("Resource: %s\n%s", resKey, prettyPrint(toJson(resource)).replace('\\n', System.getProperty('line.separator')))) |
| 345 | } |
| 346 | } |
| 347 | }else{ |
| 348 | common.infoMsg(String.format("Resource: %s\n%s", resKey, prettyPrint(toJson(resource)).replace('\\n', System.getProperty('line.separator')))) |
| 349 | } |
| 350 | } |
| 351 | }else{ |
| 352 | common.infoMsg(prettyPrint(toJson(node))) |
| 353 | } |
| 354 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 355 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 356 | }else{ |
| 357 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 358 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 359 | }else{ |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 360 | common.errorMsg("Cannot print salt state result, given result is null") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
Jakub Josef | 7852fe1 | 2017-03-15 16:02:41 +0100 | [diff] [blame] | 365 | * Print salt command run results in human-friendly form |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 366 | * |
| 367 | * @param result Parsed response of Salt API |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 368 | */ |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 369 | def printSaltCommandResult(result) { |
Jakub Josef | 871bf15 | 2017-03-14 20:13:41 +0100 | [diff] [blame] | 370 | def common = new com.mirantis.mk.Common() |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 371 | if(result != null){ |
| 372 | if(result['return']){ |
| 373 | for (int i=0; i<result['return'].size(); i++) { |
| 374 | def entry = result['return'][i] |
| 375 | for (int j=0; j<entry.size(); j++) { |
| 376 | common.debugMsg("printSaltCommandResult: printing salt command entry: ${entry}") |
| 377 | def nodeKey = entry.keySet()[j] |
| 378 | def node=entry[nodeKey] |
| 379 | common.infoMsg(String.format("Node %s changes:\n%s",nodeKey,prettyPrint(toJson(node)).replace('\\n', System.getProperty('line.separator')))) |
| 380 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame] | 381 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 382 | }else{ |
| 383 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 384 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame] | 385 | }else{ |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame^] | 386 | common.errorMsg("Cannot print salt command result, given result is null") |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 387 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 388 | } |