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") |
| 51 | * @param batch |
| 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 | |
| 66 | if (batch) { |
| 67 | data['batch'] = batch |
| 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 | |
| 85 | def pillarGet(master, target, pillar) { |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 86 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.get', null, [pillar.replace('.', ':')]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 87 | return out |
| 88 | } |
| 89 | |
| 90 | def enforceState(master, target, state, output = false) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame^] | 91 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 92 | def run_states |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame^] | 93 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 94 | if (state instanceof String) { |
| 95 | run_states = state |
| 96 | } else { |
| 97 | run_states = state.join(',') |
| 98 | } |
| 99 | |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame^] | 100 | common.infoMsg('Enforcing state ${run_states} on ${target}') |
| 101 | |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 102 | 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] | 103 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 104 | try { |
| 105 | checkResult(out) |
| 106 | } finally { |
| 107 | if (output == true) { |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 108 | printSaltStateResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | return out |
| 112 | } |
| 113 | |
| 114 | def cmdRun(master, target, cmd) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame^] | 115 | def common = new com.mirantis.mk.Common() |
| 116 | |
| 117 | common.infoMsg('Running command ${cmd} on ${target}') |
| 118 | |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 119 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', null, [cmd]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 120 | return out |
| 121 | } |
| 122 | |
| 123 | def syncAll(master, target) { |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 124 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'saltutil.sync_all') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | def enforceHighstate(master, target, output = false) { |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 128 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.highstate') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 129 | try { |
| 130 | checkResult(out) |
| 131 | } finally { |
| 132 | if (output == true) { |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 133 | printSaltStateResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | return out |
| 137 | } |
| 138 | |
| 139 | def generateNodeKey(master, target, host, keysize = 4096) { |
| 140 | args = [host] |
| 141 | kwargs = ['keysize': keysize] |
| 142 | return runSaltCommand(master, 'wheel', target, 'key.gen_accept', args, kwargs) |
| 143 | } |
| 144 | |
| 145 | def generateNodeMetadata(master, target, host, classes, parameters) { |
| 146 | args = [host, '_generated'] |
| 147 | kwargs = ['classes': classes, 'parameters': parameters] |
| 148 | return runSaltCommand(master, 'local', target, 'reclass.node_create', args, kwargs) |
| 149 | } |
| 150 | |
| 151 | def orchestrateSystem(master, target, orchestrate) { |
| 152 | return runSaltCommand(master, 'runner', target, 'state.orchestrate', [orchestrate]) |
| 153 | } |
| 154 | |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 155 | def runSaltProcessStep(master, tgt, fun, arg = [], batch = null, output = true) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame^] | 156 | def common = new com.mirantis.mk.Common() |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 157 | def out |
| 158 | |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame^] | 159 | common.infoMsg('Running step ${fun} on ${tgt}') |
| 160 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 161 | if (batch) { |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 162 | out = runSaltCommand(master, 'local_batch', ['expression': tgt, 'type': 'compound'], fun, String.valueOf(batch), arg) |
| 163 | } else { |
| 164 | out = runSaltCommand(master, 'local', ['expression': tgt, 'type': 'compound'], fun, batch, arg) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 165 | } |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 166 | |
| 167 | try { |
| 168 | checkResult(out) |
| 169 | } finally { |
| 170 | if (output == true) { |
| 171 | printSaltCommandResult(out) |
| 172 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 173 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Check result for errors and throw exception if any found |
| 178 | * |
| 179 | * @param result Parsed response of Salt API |
| 180 | */ |
| 181 | def checkResult(result) { |
| 182 | for (entry in result['return']) { |
| 183 | if (!entry) { |
| 184 | throw new Exception("Salt API returned empty response: ${result}") |
| 185 | } |
| 186 | for (node in entry) { |
| 187 | for (resource in node.value) { |
| 188 | if (resource instanceof String || resource.value.result.toString().toBoolean() != true) { |
| 189 | throw new Exception("Salt state on node ${node.key} failed: ${node.value}") |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Print Salt state run results in human-friendly form |
| 198 | * |
| 199 | * @param result Parsed response of Salt API |
| 200 | * @param onlyChanges If true (default), print only changed resources |
| 201 | * parsing |
| 202 | */ |
| 203 | def printSaltStateResult(result, onlyChanges = true) { |
| 204 | def out = [:] |
| 205 | for (entry in result['return']) { |
| 206 | for (node in entry) { |
| 207 | out[node.key] = [:] |
| 208 | for (resource in node.value) { |
| 209 | if (resource instanceof String) { |
| 210 | out[node.key] = node.value |
| 211 | } else if (resource.value.result.toString().toBoolean() == false || resource.value.changes || onlyChanges == false) { |
| 212 | out[node.key][resource.key] = resource.value |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | for (node in out) { |
| 219 | if (node.value) { |
| 220 | println "Node ${node.key} changes:" |
| 221 | print new groovy.json.JsonBuilder(node.value).toPrettyString() |
| 222 | } else { |
| 223 | println "No changes for node ${node.key}" |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Print Salt state run results in human-friendly form |
| 230 | * |
| 231 | * @param result Parsed response of Salt API |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 232 | */ |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 233 | def printSaltCommandResult(result) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 234 | def out = [:] |
| 235 | for (entry in result['return']) { |
| 236 | for (node in entry) { |
| 237 | out[node.key] = [:] |
| 238 | for (resource in node.value) { |
| 239 | out[node.key] = node.value |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | for (node in out) { |
| 245 | if (node.value) { |
| 246 | println "Node ${node.key} changes:" |
| 247 | print new groovy.json.JsonBuilder(node.value).toPrettyString() |
| 248 | } else { |
| 249 | println "No changes for node ${node.key}" |
| 250 | } |
| 251 | } |
| 252 | } |