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