Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 3 | import com.cloudbees.groovy.cps.NonCPS |
Jakub Josef | b77c081 | 2017-03-27 14:11:01 +0200 | [diff] [blame] | 4 | import java.util.stream.Collectors |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 5 | /** |
| 6 | * Salt functions |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Salt connection and context parameters |
| 12 | * |
| 13 | * @param url Salt API server URL |
| 14 | * @param credentialsID ID of credentials store entry |
| 15 | */ |
| 16 | def connection(url, credentialsId = "salt") { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 17 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 18 | params = [ |
| 19 | "url": url, |
| 20 | "credentialsId": credentialsId, |
| 21 | "authToken": null, |
| 22 | "creds": common.getCredentials(credentialsId) |
| 23 | ] |
| 24 | params["authToken"] = saltLogin(params) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 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 | /** |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame^] | 45 | * Run action using Salt API (using plain HTTP request from Jenkins master) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 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 |
Jiri Broulik | 48544be | 2017-06-14 18:33:54 +0200 | [diff] [blame] | 55 | * @param timeout Additional argument salt api timeout |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 56 | * @param read_timeout http session read timeout |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 57 | */ |
| 58 | @NonCPS |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 59 | def runSaltCommand(master, client, target, function, batch = null, args = null, kwargs = null, timeout = -1, read_timeout = -1) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 60 | def http = new com.mirantis.mk.Http() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 61 | |
| 62 | data = [ |
| 63 | 'tgt': target.expression, |
| 64 | 'fun': function, |
| 65 | 'client': client, |
| 66 | 'expr_form': target.type, |
| 67 | ] |
Jakub Josef | 5f83821 | 2017-04-06 12:43:58 +0200 | [diff] [blame] | 68 | 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] | 69 | data['client']= "local_batch" |
| 70 | data['batch'] = batch |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (args) { |
| 74 | data['arg'] = args |
| 75 | } |
| 76 | |
| 77 | if (kwargs) { |
| 78 | data['kwarg'] = kwargs |
| 79 | } |
| 80 | |
Jiri Broulik | 48544be | 2017-06-14 18:33:54 +0200 | [diff] [blame] | 81 | if (timeout != -1) { |
| 82 | data['timeout'] = timeout |
| 83 | } |
| 84 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 85 | headers = [ |
| 86 | 'X-Auth-Token': "${master.authToken}" |
| 87 | ] |
| 88 | |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 89 | return http.sendHttpPostRequest("${master.url}/", data, headers, read_timeout) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 92 | /** |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame^] | 93 | * Run action using Salt API (using salt pepper from slave shell) |
| 94 | * |
| 95 | * @param master Salt connection object |
| 96 | * @param client Client type |
| 97 | * @param target Target specification, eg. for compound matches by Pillar |
| 98 | * data: ['expression': 'I@openssh:server', 'type': 'compound']) |
| 99 | * @param function Function to execute (eg. "state.sls") |
| 100 | * @param batch Batch param to salt (integer or string with percents) |
| 101 | * @param args Additional arguments to function |
| 102 | * @param kwargs Additional key-value arguments to function |
| 103 | * @param timeout Additional argument salt api timeout |
| 104 | * @param read_timeout http session read timeout |
| 105 | */ |
| 106 | @NonCPS |
| 107 | def runSaltCommandPepper(pepperVenv, client, target, function, batch = null, args = null, kwargs = null, timeout = -1, read_timeout = -1) { |
| 108 | def http = new com.mirantis.mk.Http() |
| 109 | |
| 110 | data = [ |
| 111 | 'tgt': target.expression, |
| 112 | 'fun': function, |
| 113 | 'client': client, |
| 114 | 'expr_form': target.type, |
| 115 | ] |
| 116 | if(batch != null && ( (batch instanceof Integer && batch > 0) || (batch instanceof String && batch.contains("%")))){ |
| 117 | data['client']= "local_batch" |
| 118 | data['batch'] = batch |
| 119 | } |
| 120 | |
| 121 | if (args) { |
| 122 | data['arg'] = args |
| 123 | } |
| 124 | |
| 125 | if (kwargs) { |
| 126 | data['kwarg'] = kwargs |
| 127 | } |
| 128 | |
| 129 | if (timeout != -1) { |
| 130 | data['timeout'] = timeout |
| 131 | } |
| 132 | |
| 133 | headers = [ |
| 134 | 'X-Auth-Token': "${master.authToken}" |
| 135 | ] |
| 136 | |
| 137 | return runPepperCommand(data, pepperVenv) |
| 138 | } |
| 139 | |
| 140 | /** |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 141 | * Return pillar for given master and target |
| 142 | * @param master Salt connection object |
| 143 | * @param target Get pillar target |
| 144 | * @param pillar pillar name (optional) |
| 145 | * @return output of salt command |
| 146 | */ |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 147 | def getPillar(master, target, pillar = null) { |
Tomáš Kukrál | d258970 | 2017-03-10 16:30:46 +0100 | [diff] [blame] | 148 | if (pillar != null) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 149 | 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] | 150 | } else { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 151 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'pillar.data') |
Ales Komarek | a3c7e50 | 2017-03-13 11:20:44 +0100 | [diff] [blame] | 152 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 155 | /** |
| 156 | * Return grain for given master and target |
| 157 | * @param master Salt connection object |
| 158 | * @param target Get grain target |
| 159 | * @param grain grain name (optional) |
| 160 | * @return output of salt command |
| 161 | */ |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 162 | def getGrain(master, target, grain = null) { |
| 163 | if(grain != null) { |
Jiri Broulik | 852dfd5 | 2017-05-16 13:38:55 +0200 | [diff] [blame] | 164 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grains.item', null, [grain]) |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 165 | } else { |
Jiri Broulik | 852dfd5 | 2017-05-16 13:38:55 +0200 | [diff] [blame] | 166 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'grains.items') |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 167 | } |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 170 | /** |
| 171 | * Enforces state on given master and target |
| 172 | * @param master Salt connection object |
| 173 | * @param target State enforcing target |
| 174 | * @param state Salt state |
| 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) |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 178 | * @param read_timeout http session read timeout |
| 179 | * @param retries Retry count for salt state. |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 180 | * @return output of salt command |
| 181 | */ |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 182 | def enforceState(master, target, state, output = true, failOnError = true, batch = null, optional = false, read_timeout=-1, retries=-1) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 183 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 184 | def run_states |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 185 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 186 | if (state instanceof String) { |
| 187 | run_states = state |
| 188 | } else { |
| 189 | run_states = state.join(',') |
| 190 | } |
| 191 | |
Marek Celoud | 6336611 | 2017-07-25 17:27:24 +0200 | [diff] [blame] | 192 | common.infoMsg("Running state ${run_states} on ${target}") |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 193 | def out |
| 194 | |
| 195 | if (optional == false || testTarget(master, target)){ |
| 196 | if (retries != -1){ |
| 197 | retry(retries){ |
| 198 | out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', batch, [run_states], null, -1, read_timeout) |
| 199 | } |
| 200 | } |
| 201 | else { |
| 202 | out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', batch, [run_states], null, -1, read_timeout) |
| 203 | } |
Martin Polreich | 1c77afa | 2017-07-18 11:27:02 +0200 | [diff] [blame] | 204 | checkResult(out, failOnError, output) |
| 205 | return out |
Martin Polreich | 1c77afa | 2017-07-18 11:27:02 +0200 | [diff] [blame] | 206 | } else { |
| 207 | common.infoMsg("No Minions matched the target given, but 'optional' param was set to true - Pipeline continues. ") |
| 208 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 211 | /** |
| 212 | * Run command on salt minion (salt cmd.run wrapper) |
| 213 | * @param master Salt connection object |
| 214 | * @param target Get pillar target |
| 215 | * @param cmd command |
Jakub Josef | 053df39 | 2017-05-03 15:51:05 +0200 | [diff] [blame] | 216 | * @param checkResponse test command success execution (default true) |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 217 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 218 | * @param output do you want to print output |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 219 | * @return output of salt command |
| 220 | */ |
Jiri Broulik | 16e9ce7 | 2017-05-17 13:28:31 +0200 | [diff] [blame] | 221 | def cmdRun(master, target, cmd, checkResponse = true, batch=null, output = true) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 222 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 053df39 | 2017-05-03 15:51:05 +0200 | [diff] [blame] | 223 | def originalCmd = cmd |
Tomáš Kukrál | dfd4b49 | 2017-03-02 12:08:50 +0100 | [diff] [blame] | 224 | common.infoMsg("Running command ${cmd} on ${target}") |
Jakub Josef | 053df39 | 2017-05-03 15:51:05 +0200 | [diff] [blame] | 225 | if (checkResponse) { |
| 226 | cmd = cmd + " && echo Salt command execution success" |
| 227 | } |
Jiri Broulik | 16e9ce7 | 2017-05-17 13:28:31 +0200 | [diff] [blame] | 228 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', batch, [cmd]) |
Jakub Josef | 053df39 | 2017-05-03 15:51:05 +0200 | [diff] [blame] | 229 | if (checkResponse) { |
| 230 | // iterate over all affected nodes and check success return code |
Jiri Broulik | 16e9ce7 | 2017-05-17 13:28:31 +0200 | [diff] [blame] | 231 | if (out["return"]){ |
| 232 | for(int i=0;i<out["return"].size();i++){ |
| 233 | def node = out["return"][i]; |
Jakub Josef | 053df39 | 2017-05-03 15:51:05 +0200 | [diff] [blame] | 234 | for(int j=0;j<node.size();j++){ |
| 235 | def nodeKey = node.keySet()[j] |
| 236 | if (!node[nodeKey].contains("Salt command execution success")) { |
| 237 | throw new Exception("Execution of cmd ${originalCmd} failed. Server returns: ${node[nodeKey]}") |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | }else{ |
| 242 | throw new Exception("Salt Api response doesn't have return param!") |
| 243 | } |
| 244 | } |
Jiri Broulik | 16e9ce7 | 2017-05-17 13:28:31 +0200 | [diff] [blame] | 245 | if (output == true) { |
| 246 | printSaltCommandResult(out) |
| 247 | } |
| 248 | return out |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 251 | /** |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 252 | * Checks if salt minion is in a list of salt master's accepted keys |
| 253 | * @usage minionPresent(saltMaster, 'I@salt:master', 'ntw', true, null, true, 200, 3) |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 254 | * @param master Salt connection object |
| 255 | * @param target Get pillar target |
| 256 | * @param minion_name unique identification of a minion in salt-key command output |
| 257 | * @param waitUntilPresent return after the minion becomes present (default true) |
| 258 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 259 | * @param output print salt command (default true) |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 260 | * @param maxRetries finite number of iterations to check status of a command (default 200) |
| 261 | * @param answers how many minions should return (optional, default 1) |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 262 | * @return output of salt command |
| 263 | */ |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 264 | def minionPresent(master, target, minion_name, waitUntilPresent = true, batch=null, output = true, maxRetries = 200, answers = 1) { |
| 265 | minion_name = minion_name.replace("*", "") |
| 266 | def common = new com.mirantis.mk.Common() |
| 267 | def cmd = 'salt-key | grep ' + minion_name |
| 268 | if (waitUntilPresent){ |
| 269 | def count = 0 |
| 270 | while(count < maxRetries) { |
| 271 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.shell', batch, [cmd], null, 5) |
| 272 | if (output) { |
| 273 | printSaltCommandResult(out) |
| 274 | } |
| 275 | def valueMap = out["return"][0] |
| 276 | def result = valueMap.get(valueMap.keySet()[0]) |
| 277 | def resultsArray = result.tokenize("\n") |
| 278 | def size = resultsArray.size() |
| 279 | if (size >= answers) { |
| 280 | return out |
| 281 | } |
| 282 | count++ |
| 283 | sleep(time: 500, unit: 'MILLISECONDS') |
| 284 | common.infoMsg("Waiting for ${cmd} on ${target} to be in correct state") |
| 285 | } |
| 286 | } else { |
| 287 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.shell', batch, [cmd], null, 5) |
| 288 | if (output) { |
| 289 | printSaltCommandResult(out) |
| 290 | } |
| 291 | return out |
| 292 | } |
| 293 | // otherwise throw exception |
| 294 | common.errorMsg("Status of command ${cmd} on ${target} failed, please check it.") |
| 295 | throw new Exception("${cmd} signals failure of status check!") |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * You can call this function when salt-master already contains salt keys of the target_nodes |
| 300 | * @param master Salt connection object |
| 301 | * @param target Should always be salt-master |
| 302 | * @param target_nodes unique identification of a minion or group of salt minions |
| 303 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 304 | * @param wait timeout for the salt command if minions do not return (default 10) |
| 305 | * @param maxRetries finite number of iterations to check status of a command (default 200) |
| 306 | * @return output of salt command |
| 307 | */ |
| 308 | def minionsReachable(master, target, target_nodes, batch=null, wait = 10, maxRetries = 200) { |
| 309 | def common = new com.mirantis.mk.Common() |
| 310 | def cmd = "salt -t${wait} -C '${target_nodes}' test.ping" |
| 311 | common.infoMsg("Checking if all ${target_nodes} minions are reachable") |
| 312 | def count = 0 |
| 313 | while(count < maxRetries) { |
| 314 | Calendar timeout = Calendar.getInstance(); |
| 315 | timeout.add(Calendar.SECOND, wait); |
| 316 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.shell', batch, [cmd], null, wait) |
| 317 | Calendar current = Calendar.getInstance(); |
| 318 | if (current.getTime().before(timeout.getTime())) { |
| 319 | printSaltCommandResult(out) |
| 320 | return out |
| 321 | } |
| 322 | common.infoMsg("Not all of the targeted '${target_nodes}' minions returned yet. Waiting ...") |
| 323 | count++ |
| 324 | sleep(time: 500, unit: 'MILLISECONDS') |
| 325 | } |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Run command on salt minion (salt cmd.run wrapper) |
| 330 | * @param master Salt connection object |
| 331 | * @param target Get pillar target |
| 332 | * @param cmd name of a service |
| 333 | * @param correct_state string that command must contain if status is in correct state (optional, default 'running') |
Jiri Broulik | cf1f233 | 2017-07-25 11:30:03 +0200 | [diff] [blame] | 334 | * @param find bool value if it is suppose to find some string in the output or the cmd should return empty string (optional, default true) |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 335 | * @param waitUntilOk return after the minion becomes present (optional, default true) |
| 336 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 337 | * @param output print salt command (default true) |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 338 | * @param maxRetries finite number of iterations to check status of a command (default 200) |
| 339 | * @param answers how many minions should return (optional, default 0) |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 340 | * @return output of salt command |
| 341 | */ |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 342 | def commandStatus(master, target, cmd, correct_state='running', find = true, waitUntilOk = true, batch=null, output = true, maxRetries = 200, answers = 0) { |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 343 | def common = new com.mirantis.mk.Common() |
| 344 | common.infoMsg("Checking if status of verification command ${cmd} on ${target} is in correct state") |
| 345 | if (waitUntilOk){ |
| 346 | def count = 0 |
| 347 | while(count < maxRetries) { |
Jiri Broulik | d0c2757 | 2017-07-24 20:01:10 +0200 | [diff] [blame] | 348 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.shell', batch, [cmd], null, 5) |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 349 | if (output) { |
| 350 | printSaltCommandResult(out) |
| 351 | } |
Jakub Josef | 115a78f | 2017-07-18 15:04:00 +0200 | [diff] [blame] | 352 | def resultMap = out["return"][0] |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 353 | def success = 0 |
| 354 | if (answers == 0){ |
| 355 | answers = resultMap.size() |
| 356 | } |
| 357 | for (int i=0;i<answers;i++) { |
| 358 | result = resultMap.get(resultMap.keySet()[i]) |
| 359 | // if the goal is to find some string in output of the command |
| 360 | if (find) { |
| 361 | if(result == null || result instanceof Boolean || result.isEmpty()) { result='' } |
| 362 | if (result.toLowerCase().contains(correct_state.toLowerCase())) { |
| 363 | success++ |
| 364 | if (success == answers) { |
| 365 | return out |
| 366 | } |
Jiri Broulik | d0c2757 | 2017-07-24 20:01:10 +0200 | [diff] [blame] | 367 | } |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 368 | // else the goal is to not find any string in output of the command |
| 369 | } else { |
| 370 | if(result instanceof String && result.isEmpty()) { |
| 371 | success++ |
| 372 | if (success == answers) { |
| 373 | return out |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | count++ |
| 379 | sleep(time: 500, unit: 'MILLISECONDS') |
| 380 | common.infoMsg("Waiting for ${cmd} on ${target} to be in correct state") |
| 381 | } |
| 382 | } else { |
| 383 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.shell', batch, [cmd], null, 5) |
| 384 | def resultMap = out["return"][0] |
| 385 | if (output) { |
| 386 | printSaltCommandResult(out) |
| 387 | } |
| 388 | for (int i=0;i<resultMap.size();i++) { |
| 389 | result = resultMap.get(resultMap.keySet()[i]) |
| 390 | // if the goal is to find some string in output of the command |
| 391 | if (find) { |
| 392 | if(result == null || result instanceof Boolean || result.isEmpty()) { result='' } |
| 393 | if (result.toLowerCase().contains(correct_state.toLowerCase())) { |
Jiri Broulik | d0c2757 | 2017-07-24 20:01:10 +0200 | [diff] [blame] | 394 | return out |
| 395 | } |
| 396 | |
| 397 | // else the goal is to not find any string in output of the command |
| 398 | } else { |
| 399 | if(result instanceof String && result.isEmpty()) { |
| 400 | return out |
| 401 | } |
| 402 | } |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | // otherwise throw exception |
Jiri Broulik | d0c2757 | 2017-07-24 20:01:10 +0200 | [diff] [blame] | 406 | common.errorMsg("Status of command ${cmd} on ${target} failed, please check it.") |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 407 | throw new Exception("${cmd} signals failure of status check!") |
| 408 | } |
| 409 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 410 | /** |
| 411 | * Perform complete salt sync between master and target |
| 412 | * @param master Salt connection object |
| 413 | * @param target Get pillar target |
| 414 | * @return output of salt command |
| 415 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 416 | def syncAll(master, target) { |
Filip Pytloun | 5a7f7fd | 2017-02-27 18:50:25 +0100 | [diff] [blame] | 417 | return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'saltutil.sync_all') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 420 | /** |
| 421 | * Enforce highstate on given targets |
| 422 | * @param master Salt connection object |
| 423 | * @param target Highstate enforcing target |
| 424 | * @param output print output (optional, default true) |
| 425 | * @param failOnError throw exception on salt state result:false (optional, default true) |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 426 | * @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] | 427 | * @return output of salt command |
| 428 | */ |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 429 | def enforceHighstate(master, target, output = false, failOnError = true, batch = null) { |
| 430 | def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.highstate', batch) |
Alexander Noskov | 657ccfc | 2017-07-14 11:35:52 +0000 | [diff] [blame] | 431 | def common = new com.mirantis.mk.Common() |
| 432 | |
Marek Celoud | 6336611 | 2017-07-25 17:27:24 +0200 | [diff] [blame] | 433 | common.infoMsg("Running state highstate on ${target}") |
Alexander Noskov | 657ccfc | 2017-07-14 11:35:52 +0000 | [diff] [blame] | 434 | |
Jakub Josef | 374beb7 | 2017-04-27 15:45:09 +0200 | [diff] [blame] | 435 | checkResult(out, failOnError, output) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 436 | return out |
| 437 | } |
| 438 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 439 | /** |
Ales Komarek | 5276ebe | 2017-03-16 08:46:34 +0100 | [diff] [blame] | 440 | * Get running minions IDs according to the target |
| 441 | * @param master Salt connection object |
| 442 | * @param target Get minions target |
| 443 | * @return list of active minions fitin |
| 444 | */ |
| 445 | def getMinions(master, target) { |
Tomáš Kukrál | 18ac50f | 2017-07-13 10:22:09 +0200 | [diff] [blame] | 446 | def minionsRaw = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'test.ping') |
Ales Komarek | 5276ebe | 2017-03-16 08:46:34 +0100 | [diff] [blame] | 447 | return new ArrayList<String>(minionsRaw['return'][0].keySet()) |
| 448 | } |
| 449 | |
| 450 | |
| 451 | /** |
Tomáš Kukrál | b12ff9f | 2017-07-12 12:32:34 +0200 | [diff] [blame] | 452 | * Test if there are any minions to target |
| 453 | * @param master Salt connection object |
| 454 | * @param target Target to test |
vrovachev | 1c4770b | 2017-07-05 13:25:21 +0400 | [diff] [blame] | 455 | * @return bool indicating if target was succesful |
Tomáš Kukrál | b12ff9f | 2017-07-12 12:32:34 +0200 | [diff] [blame] | 456 | */ |
| 457 | |
| 458 | def testTarget(master, target) { |
| 459 | return getMinions(master, target).size() > 0 |
| 460 | } |
| 461 | |
| 462 | /** |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 463 | * Generates node key using key.gen_accept call |
| 464 | * @param master Salt connection object |
| 465 | * @param target Key generating target |
| 466 | * @param host Key generating host |
| 467 | * @param keysize generated key size (optional, default 4096) |
| 468 | * @return output of salt command |
| 469 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 470 | def generateNodeKey(master, target, host, keysize = 4096) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 471 | return runSaltCommand(master, 'wheel', target, 'key.gen_accept', [host], ['keysize': keysize]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 474 | /** |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 475 | * Generates node reclass metadata |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 476 | * @param master Salt connection object |
| 477 | * @param target Metadata generating target |
| 478 | * @param host Metadata generating host |
| 479 | * @param classes Reclass classes |
| 480 | * @param parameters Reclass parameters |
| 481 | * @return output of salt command |
| 482 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 483 | def generateNodeMetadata(master, target, host, classes, parameters) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 484 | 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] | 485 | } |
| 486 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 487 | /** |
| 488 | * Run salt orchestrate on given targets |
| 489 | * @param master Salt connection object |
| 490 | * @param target Orchestration target |
| 491 | * @param orchestrate Salt orchestrate params |
| 492 | * @return output of salt command |
| 493 | */ |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 494 | def orchestrateSystem(master, target, orchestrate) { |
| 495 | return runSaltCommand(master, 'runner', target, 'state.orchestrate', [orchestrate]) |
| 496 | } |
| 497 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 498 | /** |
| 499 | * Run salt process step |
| 500 | * @param master Salt connection object |
| 501 | * @param tgt Salt process step target |
| 502 | * @param fun Salt process step function |
| 503 | * @param arg process step arguments (optional, default []) |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 504 | * @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] | 505 | * @param output print output (optional, default false) |
Jiri Broulik | 48544be | 2017-06-14 18:33:54 +0200 | [diff] [blame] | 506 | * @param timeout Additional argument salt api timeout |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 507 | * @return output of salt command |
| 508 | */ |
Kalynovskyi | 1928212 | 2017-08-31 10:45:33 +0300 | [diff] [blame] | 509 | def runSaltProcessStep(master, tgt, fun, arg = [], batch = null, output = false, timeout = -1, kwargs = null) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 510 | def common = new com.mirantis.mk.Common() |
Jiri Broulik | 48544be | 2017-06-14 18:33:54 +0200 | [diff] [blame] | 511 | def salt = new com.mirantis.mk.Salt() |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 512 | def out |
| 513 | |
Marek Celoud | 6336611 | 2017-07-25 17:27:24 +0200 | [diff] [blame] | 514 | common.infoMsg("Running step ${fun} ${arg} on ${tgt}") |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 515 | |
Filip Pytloun | f0435c0 | 2017-03-02 17:48:54 +0100 | [diff] [blame] | 516 | if (batch == true) { |
Kalynovskyi | 1928212 | 2017-08-31 10:45:33 +0300 | [diff] [blame] | 517 | out = runSaltCommand(master, 'local_batch', ['expression': tgt, 'type': 'compound'], fun, String.valueOf(batch), arg, kwargs, timeout) |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 518 | } else { |
Kalynovskyi | 1928212 | 2017-08-31 10:45:33 +0300 | [diff] [blame] | 519 | out = runSaltCommand(master, 'local', ['expression': tgt, 'type': 'compound'], fun, batch, arg, kwargs, timeout) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 520 | } |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 521 | |
Tomáš Kukrál | f5dda64 | 2017-03-02 14:22:59 +0100 | [diff] [blame] | 522 | if (output == true) { |
Jiri Broulik | 48544be | 2017-06-14 18:33:54 +0200 | [diff] [blame] | 523 | salt.printSaltCommandResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 524 | } |
Jiri Broulik | ae19c26 | 2017-05-16 19:06:52 +0200 | [diff] [blame] | 525 | return out |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Check result for errors and throw exception if any found |
| 530 | * |
| 531 | * @param result Parsed response of Salt API |
Jakub Josef | 8021c00 | 2017-03-27 15:41:28 +0200 | [diff] [blame] | 532 | * @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] | 533 | * @param printResults Do you want to print salt results (optional, default true) |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 534 | * @param printOnlyChanges If true (default), print only changed resources |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 535 | */ |
Jakub Josef | 374beb7 | 2017-04-27 15:45:09 +0200 | [diff] [blame] | 536 | def checkResult(result, failOnError = true, printResults = true, printOnlyChanges = true) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 537 | def common = new com.mirantis.mk.Common() |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 538 | if(result != null){ |
| 539 | if(result['return']){ |
| 540 | for (int i=0;i<result['return'].size();i++) { |
| 541 | def entry = result['return'][i] |
| 542 | if (!entry) { |
| 543 | if (failOnError) { |
| 544 | throw new Exception("Salt API returned empty response: ${result}") |
| 545 | } else { |
| 546 | common.errorMsg("Salt API returned empty response: ${result}") |
Jakub Josef | ece32af | 2017-03-14 19:20:08 +0100 | [diff] [blame] | 547 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 548 | } |
| 549 | for (int j=0;j<entry.size();j++) { |
| 550 | def nodeKey = entry.keySet()[j] |
| 551 | def node=entry[nodeKey] |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 552 | def outputResources = [] |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 553 | common.infoMsg("Node ${nodeKey} changes:") |
| 554 | if(node instanceof Map || node instanceof List){ |
| 555 | for (int k=0;k<node.size();k++) { |
| 556 | def resource; |
| 557 | def resKey; |
| 558 | if(node instanceof Map){ |
| 559 | resKey = node.keySet()[k] |
| 560 | }else if(node instanceof List){ |
| 561 | resKey = k |
| 562 | } |
| 563 | resource = node[resKey] |
Jakub Josef | c4c4020 | 2017-04-28 12:04:24 +0200 | [diff] [blame] | 564 | // print |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 565 | if(printResults){ |
| 566 | if(resource instanceof Map && resource.keySet().contains("result")){ |
| 567 | //clean unnesaccary fields |
| 568 | if(resource.keySet().contains("__run_num__")){ |
| 569 | resource.remove("__run_num__") |
| 570 | } |
| 571 | if(resource.keySet().contains("__id__")){ |
| 572 | resource.remove("__id__") |
| 573 | } |
| 574 | if(resource.keySet().contains("pchanges")){ |
| 575 | resource.remove("pchanges") |
| 576 | } |
| 577 | if(!resource["result"] || (resource["result"] instanceof String && resource["result"] != "true")){ |
| 578 | if(resource["result"] != null){ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 579 | outputResources.add(String.format("Resource: %s\n\u001B[31m%s\u001B[0m", resKey, common.prettify(resource))) |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 580 | }else{ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 581 | outputResources.add(String.format("Resource: %s\n\u001B[33m%s\u001B[0m", resKey, common.prettify(resource))) |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 582 | } |
| 583 | }else{ |
| 584 | if(!printOnlyChanges || resource.changes.size() > 0){ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 585 | outputResources.add(String.format("Resource: %s\n\u001B[32m%s\u001B[0m", resKey, common.prettify(resource))) |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | }else{ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 589 | outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", resKey, common.prettify(resource))) |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 590 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 591 | } |
Jakub Josef | c4c4020 | 2017-04-28 12:04:24 +0200 | [diff] [blame] | 592 | common.debugMsg("checkResult: checking resource: ${resource}") |
| 593 | if(resource instanceof String || (resource["result"] != null && !resource["result"]) || (resource["result"] instanceof String && resource["result"] == "false")){ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 594 | def prettyResource = common.prettify(resource) |
Jakub Josef | c4c4020 | 2017-04-28 12:04:24 +0200 | [diff] [blame] | 595 | if(env["ASK_ON_ERROR"] && env["ASK_ON_ERROR"] == "true"){ |
| 596 | timeout(time:1, unit:'HOURS') { |
| 597 | input message: "False result on ${nodeKey} found, resource ${prettyResource}. \nDo you want to continue?" |
| 598 | } |
| 599 | }else{ |
Jakub Josef | d97d7db | 2017-05-11 19:11:53 +0200 | [diff] [blame] | 600 | common.errorMsg(String.format("Resource: %s\n%s", resKey, prettyResource)) |
Jakub Josef | d9001df | 2017-05-11 16:45:28 +0200 | [diff] [blame] | 601 | def errorMsg = "Salt state on node ${nodeKey} failed: ${prettyResource}." |
Jakub Josef | c4c4020 | 2017-04-28 12:04:24 +0200 | [diff] [blame] | 602 | if (failOnError) { |
| 603 | throw new Exception(errorMsg) |
| 604 | } else { |
| 605 | common.errorMsg(errorMsg) |
| 606 | } |
| 607 | } |
| 608 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 609 | } |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 610 | }else if(node!=null && node!=""){ |
Jakub Josef | 62f6c84 | 2017-08-04 16:36:35 +0200 | [diff] [blame] | 611 | outputResources.add(String.format("Resource: %s\n\u001B[36m%s\u001B[0m", nodeKey, common.prettify(node))) |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 612 | } |
| 613 | if(printResults && !outputResources.isEmpty()){ |
Jakub Josef | e6c562e | 2017-08-09 14:41:03 +0200 | [diff] [blame] | 614 | print outputResources.stream().collect(Collectors.joining("\n")) |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 615 | } |
| 616 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 617 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 618 | }else{ |
| 619 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 620 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 621 | }else{ |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 622 | common.errorMsg("Cannot check salt result, given result is null") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | /** |
Jakub Josef | 7852fe1 | 2017-03-15 16:02:41 +0100 | [diff] [blame] | 627 | * Print salt command run results in human-friendly form |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 628 | * |
| 629 | * @param result Parsed response of Salt API |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 630 | */ |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 631 | def printSaltCommandResult(result) { |
Jakub Josef | 871bf15 | 2017-03-14 20:13:41 +0100 | [diff] [blame] | 632 | def common = new com.mirantis.mk.Common() |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 633 | if(result != null){ |
| 634 | if(result['return']){ |
| 635 | for (int i=0; i<result['return'].size(); i++) { |
| 636 | def entry = result['return'][i] |
| 637 | for (int j=0; j<entry.size(); j++) { |
| 638 | common.debugMsg("printSaltCommandResult: printing salt command entry: ${entry}") |
| 639 | def nodeKey = entry.keySet()[j] |
| 640 | def node=entry[nodeKey] |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 641 | common.infoMsg(String.format("Node %s changes:\n%s",nodeKey, common.prettify(node))) |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 642 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame] | 643 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 644 | }else{ |
| 645 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 646 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame] | 647 | }else{ |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 648 | common.errorMsg("Cannot print salt command result, given result is null") |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 649 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 650 | } |
Tomáš Kukrál | b12eedd | 2017-04-21 10:45:13 +0200 | [diff] [blame] | 651 | |
| 652 | |
| 653 | /** |
| 654 | * Return content of file target |
| 655 | * |
| 656 | * @param master Salt master object |
| 657 | * @param target Compound target (should target only one host) |
| 658 | * @param file File path to read (/etc/hosts for example) |
| 659 | */ |
| 660 | |
| 661 | def getFileContent(master, target, file) { |
| 662 | result = cmdRun(master, target, "cat ${file}") |
Tomáš Kukrál | f1a692a | 2017-08-11 13:29:28 +0200 | [diff] [blame] | 663 | return result['return'][0].values()[0].replaceAll('Salt command execution success','') |
Tomáš Kukrál | b12eedd | 2017-04-21 10:45:13 +0200 | [diff] [blame] | 664 | } |
Matthew Mosesohn | 9e88085 | 2017-07-04 21:17:53 +0300 | [diff] [blame] | 665 | |
| 666 | /** |
| 667 | * Set override parameters in Salt cluster metadata |
| 668 | * |
| 669 | * @param master Salt master object |
| 670 | * @param salt_overrides YAML formatted string containing key: value, one per line |
Matthew Mosesohn | e564684 | 2017-07-19 16:54:57 +0300 | [diff] [blame] | 671 | * @param reclass_dir Directory where Reclass git repo is located |
Matthew Mosesohn | 9e88085 | 2017-07-04 21:17:53 +0300 | [diff] [blame] | 672 | */ |
| 673 | |
Matthew Mosesohn | e564684 | 2017-07-19 16:54:57 +0300 | [diff] [blame] | 674 | def setSaltOverrides(master, salt_overrides, reclass_dir="/srv/salt/reclass") { |
Tomáš Kukrál | f178f05 | 2017-07-11 11:31:00 +0200 | [diff] [blame] | 675 | def common = new com.mirantis.mk.Common() |
Mykyta Karpin | 1c165e2 | 2017-08-22 18:27:01 +0300 | [diff] [blame] | 676 | def salt_overrides_map = readYaml text: salt_overrides |
Tomáš Kukrál | 243cf84 | 2017-07-11 13:11:56 +0200 | [diff] [blame] | 677 | for (entry in common.entries(salt_overrides_map)) { |
Matthew Mosesohn | 9e88085 | 2017-07-04 21:17:53 +0300 | [diff] [blame] | 678 | def key = entry[0] |
| 679 | def value = entry[1] |
| 680 | |
| 681 | common.debugMsg("Set salt override ${key}=${value}") |
Matthew Mosesohn | 29c9e33 | 2017-07-25 14:00:17 +0300 | [diff] [blame] | 682 | runSaltProcessStep(master, 'I@salt:master', 'reclass.cluster_meta_set', ["${key}", "${value}"], false) |
Matthew Mosesohn | 9e88085 | 2017-07-04 21:17:53 +0300 | [diff] [blame] | 683 | } |
Matthew Mosesohn | e564684 | 2017-07-19 16:54:57 +0300 | [diff] [blame] | 684 | runSaltProcessStep(master, 'I@salt:master', 'cmd.run', ["git -C ${reclass_dir} update-index --skip-worktree classes/cluster/overrides.yml"]) |
Matthew Mosesohn | 9e88085 | 2017-07-04 21:17:53 +0300 | [diff] [blame] | 685 | } |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame^] | 686 | |
| 687 | /** |
| 688 | * Execute salt commands via salt-api with |
| 689 | * CLI client salt-pepper |
| 690 | * |
| 691 | * @param data Salt command map |
| 692 | * @param venv Path to virtualenv with |
| 693 | */ |
| 694 | |
| 695 | def runPepperCommand(data, venv) { |
| 696 | def python = new com.mirantis.mk.Python() |
| 697 | def dataStr = new groovy.json.JsonBuilder(data).toString() |
| 698 | |
| 699 | def pepperCmd = "pepper -c ${venv}/pepperrc --make-token --json \'${dataStr}\'" |
| 700 | |
| 701 | if (venv) { |
| 702 | output = python.runVirtualenvCommand(venv, pepperCmd) |
| 703 | } else { |
| 704 | echo("[Command]: ${pepperCmd}") |
| 705 | output = sh ( |
| 706 | script: pepperCmd, |
| 707 | returnStdout: true |
| 708 | ).trim() |
| 709 | } |
| 710 | |
| 711 | return new groovy.json.JsonSlurperClassic().parseText(output) |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Create config file for pepper |
| 716 | * |
| 717 | * @param url SALT_MASTER URL |
| 718 | * @param credentialsId credentials to SALT_API |
| 719 | * @param path path to pepper env |
| 720 | */ |
| 721 | def createPepperEnv(url, credentialsId, path) { |
| 722 | def common = new com.mirantis.mk.Common() |
| 723 | rcFile = "${path}/pepperrc" |
| 724 | creds = common.getPasswordCredentials(credentialsId) |
| 725 | rc = """\ |
| 726 | [main] |
| 727 | SALTAPI_EAUTH=pam |
| 728 | SALTAPI_URL=${url} |
| 729 | SALTAPI_USER=${creds.username} |
| 730 | SALTAPI_PASS=${creds.password.toString()} |
| 731 | """ |
| 732 | writeFile file: rcFile, text: rc |
| 733 | return rcFile |
| 734 | } |