Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 1 | package 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 | */ |
| 14 | def connection(url, credentialsId = "salt") { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 15 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 16 | 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 | */ |
| 32 | def saltLogin(master) { |
Tomáš Kukrál | 7bec053 | 2017-02-20 15:39:31 +0100 | [diff] [blame] | 33 | def http = new com.mirantis.mk.Http() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 34 | data = [ |
| 35 | 'username': master.creds.username, |
| 36 | 'password': master.creds.password.toString(), |
| 37 | 'eauth': 'pam' |
| 38 | ] |
Tomáš Kukrál | 7bec053 | 2017-02-20 15:39:31 +0100 | [diff] [blame] | 39 | authToken = http.restGet(master, '/login', data)['return'][0]['token'] |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 40 | 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 Pytloun | f0435c0 | 2017-03-02 17:48:54 +0100 | [diff] [blame] | 51 | * @param batch |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 52 | * @param args Additional arguments to function |
| 53 | * @param kwargs Additional key-value arguments to function |
| 54 | */ |
| 55 | @NonCPS |
| 56 | def runSaltCommand(master, client, target, function, batch = null, args = null, kwargs = null) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 57 | def http = new com.mirantis.mk.Http() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 58 | |
| 59 | data = [ |
| 60 | 'tgt': target.expression, |
| 61 | 'fun': function, |
| 62 | 'client': client, |
| 63 | 'expr_form': target.type, |
| 64 | ] |
| 65 | |
Filip Pytloun | f0435c0 | 2017-03-02 17:48:54 +0100 | [diff] [blame] | 66 | if (batch == true) { |
| 67 | data['batch'] = "local_batch" |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 68 | } |
| 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 Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 85 | /** |
| 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 Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 92 | def getPillar(master, target, pillar = null) { |
Tomáš Kukrál | d258970 | 2017-03-10 16:30:46 +0100 | [diff] [blame] | 93 | if (pillar != null) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 94 | 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] | 95 | } else { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 96 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.data') |
Ales Komarek | a3c7e50 | 2017-03-13 11:20:44 +0100 | [diff] [blame] | 97 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 100 | /** |
| 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 Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 107 | def getGrain(master, target, grain = null) { |
| 108 | if(grain != null) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 109 | 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 Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 112 | } |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 115 | /** |
| 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 | */ |
| 124 | def enforceState(master, target, state, output = true, failOnError = true) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 125 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 126 | def run_states |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 127 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 128 | if (state instanceof String) { |
| 129 | run_states = state |
| 130 | } else { |
| 131 | run_states = state.join(',') |
| 132 | } |
| 133 | |
Tomáš Kukrál | dfd4b49 | 2017-03-02 12:08:50 +0100 | [diff] [blame] | 134 | common.infoMsg("Enforcing state ${run_states} on ${target}") |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 135 | |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 136 | 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] | 137 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 138 | try { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 139 | checkResult(out, failOnError) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 140 | } finally { |
| 141 | if (output == true) { |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 142 | printSaltStateResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | return out |
| 146 | } |
| 147 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 148 | /** |
| 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 Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 155 | def cmdRun(master, target, cmd) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 156 | def common = new com.mirantis.mk.Common() |
| 157 | |
Tomáš Kukrál | dfd4b49 | 2017-03-02 12:08:50 +0100 | [diff] [blame] | 158 | common.infoMsg("Running command ${cmd} on ${target}") |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 159 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 160 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', null, [cmd]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 163 | /** |
| 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 Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 169 | def syncAll(master, target) { |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 170 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'saltutil.sync_all') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 173 | /** |
| 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 | */ |
| 181 | def enforceHighstate(master, target, output = false, failOnError = true) { |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 182 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.highstate') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 183 | try { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 184 | checkResult(out, failOnError) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 185 | } finally { |
| 186 | if (output == true) { |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 187 | printSaltStateResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | return out |
| 191 | } |
| 192 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 193 | /** |
| 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 Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 201 | def generateNodeKey(master, target, host, keysize = 4096) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 202 | return runSaltCommand(master, 'wheel', target, 'key.gen_accept', [host], ['keysize': keysize]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 205 | /** |
| 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 Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 214 | def generateNodeMetadata(master, target, host, classes, parameters) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 215 | 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] | 216 | } |
| 217 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 218 | /** |
| 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 Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 225 | def orchestrateSystem(master, target, orchestrate) { |
| 226 | return runSaltCommand(master, 'runner', target, 'state.orchestrate', [orchestrate]) |
| 227 | } |
| 228 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 229 | /** |
| 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ál | f5dda64 | 2017-03-02 14:22:59 +0100 | [diff] [blame] | 239 | def runSaltProcessStep(master, tgt, fun, arg = [], batch = null, output = false) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 240 | def common = new com.mirantis.mk.Common() |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 241 | def out |
| 242 | |
Tomas Kukral | e90bb34 | 2017-03-02 21:30:35 +0000 | [diff] [blame] | 243 | common.infoMsg("Running step ${fun} on ${tgt}") |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 244 | |
Filip Pytloun | f0435c0 | 2017-03-02 17:48:54 +0100 | [diff] [blame] | 245 | if (batch == true) { |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 246 | 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 Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 249 | } |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 250 | |
Tomáš Kukrál | f5dda64 | 2017-03-02 14:22:59 +0100 | [diff] [blame] | 251 | if (output == true) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 252 | printSaltCommandResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 253 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Check result for errors and throw exception if any found |
| 258 | * |
| 259 | * @param result Parsed response of Salt API |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 260 | * @param failOnError Do you want to throw exception if salt-call fails |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 261 | */ |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 262 | def checkResult(result, failOnError = true) { |
| 263 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 264 | if(result['return']){ |
| 265 | for (int i=0;i<result['return'].size();i++) { |
| 266 | def entry = result['return'][i] |
| 267 | if (!entry) { |
| 268 | if (failOnError) { |
| 269 | throw new Exception("Salt API returned empty response: ${result}") |
| 270 | } else { |
| 271 | common.errorMsg("Salt API returned empty response: ${result}") |
Jakub Josef | a532efc | 2017-03-13 14:51:40 +0100 | [diff] [blame] | 272 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 273 | } |
| 274 | for (int j=0;j<entry.size();j++) { |
Jakub Josef | 26546bd | 2017-03-14 18:38:00 +0100 | [diff] [blame] | 275 | def nodeKey = entry.keySet()[j] |
| 276 | def node=entry[nodeKey] |
| 277 | for (int k=0;k<node.size();k++) { |
Jakub Josef | ece32af | 2017-03-14 19:20:08 +0100 | [diff] [blame] | 278 | def resource; |
| 279 | def resKey; |
| 280 | if(node instanceof Map){ |
| 281 | resKey = node.keySet()[k] |
| 282 | }else if(node instanceof List){ |
| 283 | resKey = k |
| 284 | } |
| 285 | resource = node[resKey] |
Jakub Josef | 9873dae | 2017-03-14 19:23:19 +0100 | [diff] [blame] | 286 | common.errorMsg("Checking resource: ${resource}") |
Jakub Josef | 871bf15 | 2017-03-14 20:13:41 +0100 | [diff] [blame] | 287 | if(resource instanceof String || !resource["result"] || (resource["result"] instanceof String && resource["result"] != "true")){ |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 288 | if (failOnError) { |
Jakub Josef | 9873dae | 2017-03-14 19:23:19 +0100 | [diff] [blame] | 289 | throw new Exception("Salt state on node ${nodeKey} failed: ${resource}. State output: ${node}") |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 290 | } else { |
Jakub Josef | 9873dae | 2017-03-14 19:23:19 +0100 | [diff] [blame] | 291 | common.errorMsg("Salt state on node ${nodeKey} failed: ${resource}. State output: ${node}") |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 292 | } |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 293 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 297 | }else{ |
| 298 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Print Salt state run results in human-friendly form |
| 304 | * |
| 305 | * @param result Parsed response of Salt API |
| 306 | * @param onlyChanges If true (default), print only changed resources |
| 307 | * parsing |
| 308 | */ |
| 309 | def printSaltStateResult(result, onlyChanges = true) { |
Tomáš Kukrál | 6eb45f8 | 2017-03-08 18:26:16 +0100 | [diff] [blame] | 310 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 311 | def out = [:] |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 312 | if(result['return']){ |
| 313 | for (int i=0; i<result['return'].size(); i++) { |
Jakub Josef | f17c534 | 2017-03-14 18:06:50 +0100 | [diff] [blame] | 314 | def entry = result['return'][i] |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 315 | for (int j=0; j<entry.size(); j++) { |
Jakub Josef | b22c65a | 2017-03-14 15:42:52 +0100 | [diff] [blame] | 316 | common.errorMsg("Entry is: ${entry}") |
Jakub Josef | ce90b10 | 2017-03-14 16:22:55 +0100 | [diff] [blame] | 317 | def nodeKey = entry.keySet()[j] |
| 318 | def node=entry[nodeKey] |
| 319 | out[nodeKey] = [:] |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame^] | 320 | if(node instanceof Iterable){ |
| 321 | for (int k=0; k<node.size(); k++) { |
| 322 | def resource; |
| 323 | def resKey; |
| 324 | if(node instanceof Map){ |
| 325 | resKey = node.keySet()[k] |
| 326 | }else if(node instanceof List){ |
| 327 | resKey=k |
| 328 | } |
| 329 | resource = node[resKey] |
| 330 | if (resource instanceof String) { |
| 331 | //ORIGINAL??out[node.key] = node.value |
| 332 | out[nodeKey][resKey] = resource |
| 333 | } else if (resource.result.toString().toBoolean() == false || resource.changes || onlyChanges == false) { |
| 334 | out[nodeKey][resKey] = resource.value |
| 335 | } |
Jakub Josef | 4eb2e6a | 2017-03-14 18:53:35 +0100 | [diff] [blame] | 336 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame^] | 337 | } else { |
| 338 | out[nodeKey] = node.toString(); |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 342 | |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 343 | for (int i=0; i<out.size(); i++) { |
Jakub Josef | ce90b10 | 2017-03-14 16:22:55 +0100 | [diff] [blame] | 344 | def nodeKey = out.keySet()[i] |
| 345 | def node=out[nodeKey] |
| 346 | if (node) { |
| 347 | println "Node ${nodeKey} changes:" |
| 348 | print new groovy.json.JsonBuilder(node).toPrettyString().replace('\\n', System.getProperty('line.separator')) |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 349 | } else { |
Jakub Josef | ce90b10 | 2017-03-14 16:22:55 +0100 | [diff] [blame] | 350 | println "No changes for node ${nodeKey}" |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 351 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 352 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 353 | }else{ |
| 354 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Print Salt state run results in human-friendly form |
| 360 | * |
| 361 | * @param result Parsed response of Salt API |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 362 | */ |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 363 | def printSaltCommandResult(result) { |
Jakub Josef | 871bf15 | 2017-03-14 20:13:41 +0100 | [diff] [blame] | 364 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 365 | def out = [:] |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 366 | if(result['return']){ |
| 367 | for (int i=0; i<result['return'].size(); i++) { |
Jakub Josef | f17c534 | 2017-03-14 18:06:50 +0100 | [diff] [blame] | 368 | def entry = result['return'][i] |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 369 | for (int j=0; j<entry.size(); j++) { |
Jakub Josef | ce90b10 | 2017-03-14 16:22:55 +0100 | [diff] [blame] | 370 | common.errorMsg("Entry is: ${entry}") |
| 371 | def nodeKey = entry.keySet()[j] |
| 372 | def node=entry[nodeKey] |
| 373 | out[nodeKey] = [:] |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame^] | 374 | if(node instanceof Iterable){ |
| 375 | for (int k=0; k<node.size(); k++) { |
| 376 | def resource; |
| 377 | def resKey; |
| 378 | if(node instanceof Map){ |
| 379 | resKey = node.keySet()[k] |
| 380 | }else if(node instanceof List){ |
| 381 | resKey=k |
| 382 | } |
| 383 | resource = node[resKey] |
| 384 | //ORIGINAL??out[node.key] = node.value |
| 385 | out[nodeKey][resKey] = resource |
Jakub Josef | 871bf15 | 2017-03-14 20:13:41 +0100 | [diff] [blame] | 386 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame^] | 387 | } else { |
| 388 | out[nodeKey] = node.toString(); |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 389 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 390 | } |
| 391 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 392 | |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame^] | 393 | for (int i=0; i<out.size(); i++) { |
| 394 | def nodeKey = out.keySet()[i] |
| 395 | def node = out[nodeKey] |
| 396 | if (node) { |
| 397 | common.infoMsg("Node ${nodeKey} changes:") |
| 398 | common.infoMsg(new groovy.json.JsonBuilder(node).toPrettyString()) |
| 399 | } else { |
| 400 | common.infoMsg("No changes for node ${nodeKey}") |
| 401 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 402 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame^] | 403 | }else{ |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 404 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
| 405 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 406 | } |