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