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") |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 52 | * @param batch Batch param to salt (integer or string with percents) |
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 | |
Jakub Josef | 5f83821 | 2017-04-06 12:43:58 +0200 | [diff] [blame] | 67 | if(batch != null && ( (batch instanceof Integer && batch > 0) || (batch instanceof String && batch.contains("%")))){ |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 68 | data['client']= "local_batch" |
| 69 | data['batch'] = batch |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (args) { |
| 73 | data['arg'] = args |
| 74 | } |
| 75 | |
| 76 | if (kwargs) { |
| 77 | data['kwarg'] = kwargs |
| 78 | } |
| 79 | |
| 80 | headers = [ |
| 81 | 'X-Auth-Token': "${master.authToken}" |
| 82 | ] |
| 83 | |
| 84 | return http.sendHttpPostRequest("${master.url}/", data, headers) |
| 85 | } |
| 86 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 87 | /** |
| 88 | * Return pillar for given master and target |
| 89 | * @param master Salt connection object |
| 90 | * @param target Get pillar target |
| 91 | * @param pillar pillar name (optional) |
| 92 | * @return output of salt command |
| 93 | */ |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 94 | def getPillar(master, target, pillar = null) { |
Tomáš Kukrál | d258970 | 2017-03-10 16:30:46 +0100 | [diff] [blame] | 95 | if (pillar != null) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 96 | 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] | 97 | } else { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 98 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.data') |
Ales Komarek | a3c7e50 | 2017-03-13 11:20:44 +0100 | [diff] [blame] | 99 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 102 | /** |
| 103 | * Return grain for given master and target |
| 104 | * @param master Salt connection object |
| 105 | * @param target Get grain target |
| 106 | * @param grain grain name (optional) |
| 107 | * @return output of salt command |
| 108 | */ |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 109 | def getGrain(master, target, grain = null) { |
| 110 | if(grain != null) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 111 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grain.item', null, [grain]) |
| 112 | } else { |
| 113 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grain.items') |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 114 | } |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 117 | /** |
| 118 | * Enforces state on given master and target |
| 119 | * @param master Salt connection object |
| 120 | * @param target State enforcing target |
| 121 | * @param state Salt state |
| 122 | * @param output print output (optional, default true) |
| 123 | * @param failOnError throw exception on salt state result:false (optional, default true) |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 124 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 125 | * @return output of salt command |
| 126 | */ |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 127 | def enforceState(master, target, state, output = true, failOnError = true, batch = null) { |
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 | |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 139 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', batch, [run_states]) |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 140 | |
Jakub Josef | 374beb7 | 2017-04-27 15:45:09 +0200 | [diff] [blame] | 141 | checkResult(out, failOnError, output) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 142 | return out |
| 143 | } |
| 144 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 145 | /** |
| 146 | * Run command on salt minion (salt cmd.run wrapper) |
| 147 | * @param master Salt connection object |
| 148 | * @param target Get pillar target |
| 149 | * @param cmd command |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 150 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 151 | * @return output of salt command |
| 152 | */ |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 153 | def cmdRun(master, target, cmd, batch=null) { |
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 | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 158 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', batch, [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) |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 177 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 178 | * @return output of salt command |
| 179 | */ |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 180 | def enforceHighstate(master, target, output = false, failOnError = true, batch = null) { |
| 181 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.highstate', batch) |
Jakub Josef | 374beb7 | 2017-04-27 15:45:09 +0200 | [diff] [blame] | 182 | checkResult(out, failOnError, output) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 183 | return out |
| 184 | } |
| 185 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 186 | /** |
Ales Komarek | 5276ebe | 2017-03-16 08:46:34 +0100 | [diff] [blame] | 187 | * Get running minions IDs according to the target |
| 188 | * @param master Salt connection object |
| 189 | * @param target Get minions target |
| 190 | * @return list of active minions fitin |
| 191 | */ |
| 192 | def getMinions(master, target) { |
| 193 | def minionsRaw = runSaltCommand(master, 'local', target, 'test.ping') |
| 194 | return new ArrayList<String>(minionsRaw['return'][0].keySet()) |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /** |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 199 | * Generates node key using key.gen_accept call |
| 200 | * @param master Salt connection object |
| 201 | * @param target Key generating target |
| 202 | * @param host Key generating host |
| 203 | * @param keysize generated key size (optional, default 4096) |
| 204 | * @return output of salt command |
| 205 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 206 | def generateNodeKey(master, target, host, keysize = 4096) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 207 | return runSaltCommand(master, 'wheel', target, 'key.gen_accept', [host], ['keysize': keysize]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 210 | /** |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 211 | * Generates node reclass metadata |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 212 | * @param master Salt connection object |
| 213 | * @param target Metadata generating target |
| 214 | * @param host Metadata generating host |
| 215 | * @param classes Reclass classes |
| 216 | * @param parameters Reclass parameters |
| 217 | * @return output of salt command |
| 218 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 219 | def generateNodeMetadata(master, target, host, classes, parameters) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 220 | 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] | 221 | } |
| 222 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 223 | /** |
| 224 | * Run salt orchestrate on given targets |
| 225 | * @param master Salt connection object |
| 226 | * @param target Orchestration target |
| 227 | * @param orchestrate Salt orchestrate params |
| 228 | * @return output of salt command |
| 229 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 230 | def orchestrateSystem(master, target, orchestrate) { |
| 231 | return runSaltCommand(master, 'runner', target, 'state.orchestrate', [orchestrate]) |
| 232 | } |
| 233 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 234 | /** |
| 235 | * Run salt process step |
| 236 | * @param master Salt connection object |
| 237 | * @param tgt Salt process step target |
| 238 | * @param fun Salt process step function |
| 239 | * @param arg process step arguments (optional, default []) |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 240 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 241 | * @param output print output (optional, default false) |
| 242 | * @return output of salt command |
| 243 | */ |
Tomáš Kukrál | f5dda64 | 2017-03-02 14:22:59 +0100 | [diff] [blame] | 244 | def runSaltProcessStep(master, tgt, fun, arg = [], batch = null, output = false) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 245 | def common = new com.mirantis.mk.Common() |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 246 | def out |
| 247 | |
Tomas Kukral | e90bb34 | 2017-03-02 21:30:35 +0000 | [diff] [blame] | 248 | common.infoMsg("Running step ${fun} on ${tgt}") |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 249 | |
Filip Pytloun | f0435c0 | 2017-03-02 17:48:54 +0100 | [diff] [blame] | 250 | if (batch == true) { |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 251 | out = runSaltCommand(master, 'local_batch', ['expression': tgt, 'type': 'compound'], fun, String.valueOf(batch), arg) |
| 252 | } else { |
| 253 | out = runSaltCommand(master, 'local', ['expression': tgt, 'type': 'compound'], fun, batch, arg) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 254 | } |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 255 | |
Tomáš Kukrál | f5dda64 | 2017-03-02 14:22:59 +0100 | [diff] [blame] | 256 | if (output == true) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 257 | printSaltCommandResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 258 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Check result for errors and throw exception if any found |
| 263 | * |
| 264 | * @param result Parsed response of Salt API |
Jakub Josef | 8021c00 | 2017-03-27 15:41:28 +0200 | [diff] [blame] | 265 | * @param failOnError Do you want to throw exception if salt-call fails (optional, default true) |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 266 | * @param printResults Do you want to print salt results (optional, default true) |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 267 | * @param printOnlyChanges If true (default), print only changed resources |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 268 | */ |
Jakub Josef | 374beb7 | 2017-04-27 15:45:09 +0200 | [diff] [blame] | 269 | def checkResult(result, failOnError = true, printResults = true, printOnlyChanges = true) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 270 | def common = new com.mirantis.mk.Common() |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 271 | if(result != null){ |
| 272 | if(result['return']){ |
| 273 | for (int i=0;i<result['return'].size();i++) { |
| 274 | def entry = result['return'][i] |
| 275 | if (!entry) { |
| 276 | if (failOnError) { |
| 277 | throw new Exception("Salt API returned empty response: ${result}") |
| 278 | } else { |
| 279 | common.errorMsg("Salt API returned empty response: ${result}") |
Jakub Josef | ece32af | 2017-03-14 19:20:08 +0100 | [diff] [blame] | 280 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 281 | } |
| 282 | for (int j=0;j<entry.size();j++) { |
| 283 | def nodeKey = entry.keySet()[j] |
| 284 | def node=entry[nodeKey] |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 285 | def outputResources = [] |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 286 | common.infoMsg("Node ${nodeKey} changes:") |
| 287 | if(node instanceof Map || node instanceof List){ |
| 288 | for (int k=0;k<node.size();k++) { |
| 289 | def resource; |
| 290 | def resKey; |
| 291 | if(node instanceof Map){ |
| 292 | resKey = node.keySet()[k] |
| 293 | }else if(node instanceof List){ |
| 294 | resKey = k |
| 295 | } |
| 296 | resource = node[resKey] |
Jakub Josef | c4c4020 | 2017-04-28 12:04:24 +0200 | [diff] [blame^] | 297 | // print |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 298 | if(printResults){ |
| 299 | if(resource instanceof Map && resource.keySet().contains("result")){ |
| 300 | //clean unnesaccary fields |
| 301 | if(resource.keySet().contains("__run_num__")){ |
| 302 | resource.remove("__run_num__") |
| 303 | } |
| 304 | if(resource.keySet().contains("__id__")){ |
| 305 | resource.remove("__id__") |
| 306 | } |
| 307 | if(resource.keySet().contains("pchanges")){ |
| 308 | resource.remove("pchanges") |
| 309 | } |
| 310 | if(!resource["result"] || (resource["result"] instanceof String && resource["result"] != "true")){ |
| 311 | if(resource["result"] != null){ |
| 312 | outputResources.add(String.format("Resource: %s\n\u001B[31m%s\u001B[0m", resKey, common.prettyPrint(resource))) |
| 313 | }else{ |
| 314 | outputResources.add(String.format("Resource: %s\n\u001B[33m%s\u001B[0m", resKey, common.prettyPrint(resource))) |
| 315 | } |
| 316 | }else{ |
| 317 | if(!printOnlyChanges || resource.changes.size() > 0){ |
| 318 | outputResources.add(String.format("Resource: %s\n\u001B[32m%s\u001B[0m", resKey, common.prettyPrint(resource))) |
| 319 | } |
| 320 | } |
| 321 | }else{ |
| 322 | outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", resKey, common.prettyPrint(resource))) |
| 323 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 324 | } |
Jakub Josef | c4c4020 | 2017-04-28 12:04:24 +0200 | [diff] [blame^] | 325 | common.debugMsg("checkResult: checking resource: ${resource}") |
| 326 | if(resource instanceof String || (resource["result"] != null && !resource["result"]) || (resource["result"] instanceof String && resource["result"] == "false")){ |
| 327 | def prettyResource = common.prettyPrint(resource) |
| 328 | if(env["ASK_ON_ERROR"] && env["ASK_ON_ERROR"] == "true"){ |
| 329 | timeout(time:1, unit:'HOURS') { |
| 330 | input message: "False result on ${nodeKey} found, resource ${prettyResource}. \nDo you want to continue?" |
| 331 | } |
| 332 | }else{ |
| 333 | print(String.format("Resource: %s\n\u001B[33m%s\u001B[0m", resKey, prettyResource)) |
| 334 | def errorMsg = "Salt state on node ${nodeKey} failed: ${prettyResource}. State output: ${node}" |
| 335 | if (failOnError) { |
| 336 | throw new Exception(errorMsg) |
| 337 | } else { |
| 338 | common.errorMsg(errorMsg) |
| 339 | } |
| 340 | } |
| 341 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 342 | } |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 343 | }else if(node!=null && node!=""){ |
| 344 | outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", resKey, common.prettyPrint(node))) |
| 345 | } |
| 346 | if(printResults && !outputResources.isEmpty()){ |
Jakub Josef | b77c081 | 2017-03-27 14:11:01 +0200 | [diff] [blame] | 347 | wrap([$class: 'AnsiColorBuildWrapper']) { |
| 348 | print outputResources.stream().collect(Collectors.joining("\n")) |
| 349 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 350 | } |
| 351 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 352 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +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 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 356 | }else{ |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 357 | common.errorMsg("Cannot check salt result, given result is null") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
Jakub Josef | 7852fe1 | 2017-03-15 16:02:41 +0100 | [diff] [blame] | 362 | * Print salt command run results in human-friendly form |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 363 | * |
| 364 | * @param result Parsed response of Salt API |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 365 | */ |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 366 | def printSaltCommandResult(result) { |
Jakub Josef | 871bf15 | 2017-03-14 20:13:41 +0100 | [diff] [blame] | 367 | def common = new com.mirantis.mk.Common() |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 368 | if(result != null){ |
| 369 | if(result['return']){ |
| 370 | for (int i=0; i<result['return'].size(); i++) { |
| 371 | def entry = result['return'][i] |
| 372 | for (int j=0; j<entry.size(); j++) { |
| 373 | common.debugMsg("printSaltCommandResult: printing salt command entry: ${entry}") |
| 374 | def nodeKey = entry.keySet()[j] |
| 375 | def node=entry[nodeKey] |
Jakub Josef | b41c8d5 | 2017-03-24 13:52:24 +0100 | [diff] [blame] | 376 | 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] | 377 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame] | 378 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 379 | }else{ |
| 380 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 381 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame] | 382 | }else{ |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 383 | common.errorMsg("Cannot print salt command result, given result is null") |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 384 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 385 | } |
Tomáš Kukrál | b12eedd | 2017-04-21 10:45:13 +0200 | [diff] [blame] | 386 | |
| 387 | |
| 388 | /** |
| 389 | * Return content of file target |
| 390 | * |
| 391 | * @param master Salt master object |
| 392 | * @param target Compound target (should target only one host) |
| 393 | * @param file File path to read (/etc/hosts for example) |
| 394 | */ |
| 395 | |
| 396 | def getFileContent(master, target, file) { |
| 397 | result = cmdRun(master, target, "cat ${file}") |
| 398 | return result['return'][0].values()[0] |
| 399 | } |