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 | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 264 | for (entry in result['return']) { |
| 265 | if (!entry) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 266 | if (failOnError) { |
| 267 | throw new Exception("Salt API returned empty response: ${result}") |
| 268 | } else { |
| 269 | common.errorMsg("Salt API returned empty response: ${result}") |
| 270 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 271 | } |
| 272 | for (node in entry) { |
| 273 | for (resource in node.value) { |
Jakub Josef | 1aeb6e3 | 2017-03-13 11:57:26 +0100 | [diff] [blame^] | 274 | if(!resource["result"] || resource["result"] != "true") { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 275 | if (failOnError) { |
| 276 | throw new Exception("Salt state on node ${node.key} failed: ${node.value}") |
| 277 | } else { |
| 278 | common.errorMsg("Salt state on node ${node.key} failed: ${node.value}") |
| 279 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Print Salt state run results in human-friendly form |
| 288 | * |
| 289 | * @param result Parsed response of Salt API |
| 290 | * @param onlyChanges If true (default), print only changed resources |
| 291 | * parsing |
| 292 | */ |
| 293 | def printSaltStateResult(result, onlyChanges = true) { |
Tomáš Kukrál | 6eb45f8 | 2017-03-08 18:26:16 +0100 | [diff] [blame] | 294 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 295 | def out = [:] |
| 296 | for (entry in result['return']) { |
| 297 | for (node in entry) { |
| 298 | out[node.key] = [:] |
| 299 | for (resource in node.value) { |
| 300 | if (resource instanceof String) { |
| 301 | out[node.key] = node.value |
| 302 | } else if (resource.value.result.toString().toBoolean() == false || resource.value.changes || onlyChanges == false) { |
| 303 | out[node.key][resource.key] = resource.value |
Tomáš Kukrál | 6eb45f8 | 2017-03-08 18:26:16 +0100 | [diff] [blame] | 304 | |
Tomáš Kukrál | 9a19561 | 2017-03-09 09:40:53 +0100 | [diff] [blame] | 305 | //if (resource.value.result.toString().toBoolean() == false && resource.key instanceof String && node.key instanceof String) { |
| 306 | // common.warningMsg("Resource ${resource.key} failed on node ${node.key}!") |
| 307 | //} |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | for (node in out) { |
| 314 | if (node.value) { |
| 315 | println "Node ${node.key} changes:" |
Tomáš Kukrál | 2a9b712 | 2017-03-04 00:17:18 +0100 | [diff] [blame] | 316 | print new groovy.json.JsonBuilder(node.value).toPrettyString().replace('\\n', System.getProperty('line.separator')) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 317 | } else { |
| 318 | println "No changes for node ${node.key}" |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Print Salt state run results in human-friendly form |
| 325 | * |
| 326 | * @param result Parsed response of Salt API |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 327 | */ |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 328 | def printSaltCommandResult(result) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 329 | def out = [:] |
| 330 | for (entry in result['return']) { |
| 331 | for (node in entry) { |
| 332 | out[node.key] = [:] |
| 333 | for (resource in node.value) { |
| 334 | out[node.key] = node.value |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | for (node in out) { |
| 340 | if (node.value) { |
| 341 | println "Node ${node.key} changes:" |
| 342 | print new groovy.json.JsonBuilder(node.value).toPrettyString() |
| 343 | } else { |
| 344 | println "No changes for node ${node.key}" |
| 345 | } |
| 346 | } |
| 347 | } |