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 | ] |
Richard Felkl | d9476ac | 2018-07-12 19:01:33 +0200 | [diff] [blame] | 67 | |
| 68 | if(batch != null){ |
| 69 | batch = batch.toString() |
| 70 | if( (batch.isInteger() && batch.toInteger() > 0) || (batch.contains("%"))){ |
| 71 | data['client']= "local_batch" |
| 72 | data['batch'] = batch |
| 73 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (args) { |
| 77 | data['arg'] = args |
| 78 | } |
| 79 | |
| 80 | if (kwargs) { |
| 81 | data['kwarg'] = kwargs |
| 82 | } |
| 83 | |
Jiri Broulik | 48544be | 2017-06-14 18:33:54 +0200 | [diff] [blame] | 84 | if (timeout != -1) { |
| 85 | data['timeout'] = timeout |
| 86 | } |
| 87 | |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 88 | // Command will be sent using HttpRequest |
| 89 | if (saltId instanceof HashMap && saltId.containsKey("authToken") ) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 90 | |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 91 | def headers = [ |
| 92 | 'X-Auth-Token': "${saltId.authToken}" |
| 93 | ] |
| 94 | |
| 95 | def http = new com.mirantis.mk.Http() |
| 96 | return http.sendHttpPostRequest("${saltId.url}/", data, headers, read_timeout) |
| 97 | } else if (saltId instanceof HashMap) { |
| 98 | throw new Exception("Invalid saltId") |
| 99 | } |
| 100 | |
| 101 | // Command will be sent using Pepper |
| 102 | return runPepperCommand(data, saltId) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 105 | /** |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 106 | * Return pillar for given saltId and target |
| 107 | * @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] | 108 | * @param target Get pillar target |
| 109 | * @param pillar pillar name (optional) |
| 110 | * @return output of salt command |
| 111 | */ |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 112 | def getPillar(saltId, target, pillar = null) { |
Tomáš Kukrál | d258970 | 2017-03-10 16:30:46 +0100 | [diff] [blame] | 113 | if (pillar != null) { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 114 | 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] | 115 | } else { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 116 | return runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'pillar.data') |
Ales Komarek | a3c7e50 | 2017-03-13 11:20:44 +0100 | [diff] [blame] | 117 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 120 | /** |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 121 | * Return grain for given saltId and target |
| 122 | * @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] | 123 | * @param target Get grain target |
| 124 | * @param grain grain name (optional) |
| 125 | * @return output of salt command |
| 126 | */ |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 127 | def getGrain(saltId, target, grain = null) { |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 128 | if(grain != null) { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 129 | return runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'grains.item', null, [grain]) |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 130 | } else { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 131 | return runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'grains.items') |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 132 | } |
Ales Komarek | cec24d4 | 2017-03-08 10:25:45 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 135 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 136 | /** |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 137 | * Enforces state on given saltId and target |
| 138 | * @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] | 139 | * @param target State enforcing target |
| 140 | * @param state Salt state |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 141 | * @param excludedStates states which will be excluded from main state (default empty string) |
| 142 | * @param output print output (optional, default true) |
| 143 | * @param failOnError throw exception on salt state result:false (optional, default true) |
| 144 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 145 | * @param optional Optional flag (if true pipeline will continue even if no minions for target found) |
| 146 | * @param read_timeout http session read timeout (optional, default -1 - disabled) |
| 147 | * @param retries Retry count for salt state. (optional, default -1 - no retries) |
| 148 | * @param queue salt queue parameter for state.sls calls (optional, default true) - CANNOT BE USED WITH BATCH |
| 149 | * @param saltArgs additional salt args eq. ["runas=aptly"] |
| 150 | * @return output of salt command |
| 151 | */ |
| 152 | def enforceStateWithExclude(saltId, target, state, excludedStates = "", output = true, failOnError = true, batch = null, optional = false, read_timeout=-1, retries=-1, queue=true, saltArgs=[]) { |
| 153 | saltArgs << "exclude=${excludedStates}" |
| 154 | return enforceState(saltId, target, state, output, failOnError, batch, optional, read_timeout, retries, queue, saltArgs) |
| 155 | } |
| 156 | |
| 157 | /* Enforces state on given saltId and target |
| 158 | * @param saltId Salt Connection object or pepperEnv (the command will be sent using the selected method) |
| 159 | * @param target State enforcing target |
| 160 | * @param state Salt state |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 161 | * @param output print output (optional, default true) |
| 162 | * @param failOnError throw exception on salt state result:false (optional, default true) |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 163 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 164 | * @param optional Optional flag (if true pipeline will continue even if no minions for target found) |
Petr Michalec | de0ff32 | 2017-10-04 09:32:14 +0200 | [diff] [blame] | 165 | * @param read_timeout http session read timeout (optional, default -1 - disabled) |
| 166 | * @param retries Retry count for salt state. (optional, default -1 - no retries) |
| 167 | * @param queue salt queue parameter for state.sls calls (optional, default true) - CANNOT BE USED WITH BATCH |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 168 | * @param saltArgs additional salt args eq. ["runas=aptly", exclude="opencontrail.database"] |
Vasyl Saienko | 6a39621 | 2018-06-08 09:20:08 +0300 | [diff] [blame] | 169 | * @param minionRestartWaitTimeout specifies timeout that we should wait after minion restart. |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 170 | * @return output of salt command |
| 171 | */ |
Vasyl Saienko | 6a39621 | 2018-06-08 09:20:08 +0300 | [diff] [blame] | 172 | def enforceState(saltId, target, state, output = true, failOnError = true, batch = null, optional = false, read_timeout=-1, retries=-1, queue=true, saltArgs = [], minionRestartWaitTimeout=10) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 173 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 174 | // add state to salt args |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 175 | if (state instanceof String) { |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 176 | saltArgs << state |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 177 | } else { |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 178 | saltArgs << state.join(',') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Jakub Josef | 84f0168 | 2018-02-07 14:26:19 +0100 | [diff] [blame] | 181 | common.infoMsg("Running state ${state} on ${target}") |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 182 | def out |
Petr Michalec | de0ff32 | 2017-10-04 09:32:14 +0200 | [diff] [blame] | 183 | def kwargs = [:] |
| 184 | |
| 185 | if (queue && batch == null) { |
| 186 | kwargs["queue"] = true |
| 187 | } |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 188 | |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 189 | if (optional == false || testTarget(saltId, target)){ |
Richard Felkl | 03203d6 | 2017-11-01 17:57:32 +0100 | [diff] [blame] | 190 | if (retries > 0){ |
Jakub Josef | 962ba91 | 2018-04-04 17:39:19 +0200 | [diff] [blame] | 191 | def retriesCounter = 0 |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 192 | retry(retries){ |
Jakub Josef | 962ba91 | 2018-04-04 17:39:19 +0200 | [diff] [blame] | 193 | retriesCounter++ |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 194 | // we have to reverse order in saltArgs because salt state have to be first |
| 195 | out = runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'state.sls', batch, saltArgs.reverse(), kwargs, -1, read_timeout) |
| 196 | // failOnError should be passed as true because we need to throw exception for retry block handler |
Jakub Josef | 962ba91 | 2018-04-04 17:39:19 +0200 | [diff] [blame] | 197 | checkResult(out, true, output, true, retriesCounter < retries) //disable ask on error for every interation except last one |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 198 | } |
Petr Michalec | de0ff32 | 2017-10-04 09:32:14 +0200 | [diff] [blame] | 199 | } else { |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 200 | // we have to reverse order in saltArgs because salt state have to be first |
| 201 | out = runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'state.sls', batch, saltArgs.reverse(), kwargs, -1, read_timeout) |
Richard Felkl | 03203d6 | 2017-11-01 17:57:32 +0100 | [diff] [blame] | 202 | checkResult(out, failOnError, output) |
Vasyl Saienko | e36ab7c | 2017-07-17 14:35:48 +0300 | [diff] [blame] | 203 | } |
Vasyl Saienko | 6a39621 | 2018-06-08 09:20:08 +0300 | [diff] [blame] | 204 | waitForMinion(out, minionRestartWaitTimeout) |
Martin Polreich | 1c77afa | 2017-07-18 11:27:02 +0200 | [diff] [blame] | 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) |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 213 | * @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] | 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 |
chnyda | 205a92b | 2018-01-11 17:07:32 +0100 | [diff] [blame] | 219 | * @param saltArgs additional salt args eq. ["runas=aptly"] |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 220 | * @return output of salt command |
| 221 | */ |
chnyda | 205a92b | 2018-01-11 17:07:32 +0100 | [diff] [blame] | 222 | def cmdRun(saltId, target, cmd, checkResponse = true, batch=null, output = true, saltArgs = []) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 223 | def common = new com.mirantis.mk.Common() |
Jakub Josef | 053df39 | 2017-05-03 15:51:05 +0200 | [diff] [blame] | 224 | def originalCmd = cmd |
Tomáš Kukrál | dfd4b49 | 2017-03-02 12:08:50 +0100 | [diff] [blame] | 225 | common.infoMsg("Running command ${cmd} on ${target}") |
Jakub Josef | 053df39 | 2017-05-03 15:51:05 +0200 | [diff] [blame] | 226 | if (checkResponse) { |
| 227 | cmd = cmd + " && echo Salt command execution success" |
| 228 | } |
chnyda | 205a92b | 2018-01-11 17:07:32 +0100 | [diff] [blame] | 229 | |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 230 | // add cmd name to salt args list |
chnyda | 205a92b | 2018-01-11 17:07:32 +0100 | [diff] [blame] | 231 | saltArgs << cmd |
| 232 | |
| 233 | def out = runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', batch, saltArgs.reverse()) |
Jakub Josef | 053df39 | 2017-05-03 15:51:05 +0200 | [diff] [blame] | 234 | if (checkResponse) { |
| 235 | // iterate over all affected nodes and check success return code |
Jiri Broulik | 16e9ce7 | 2017-05-17 13:28:31 +0200 | [diff] [blame] | 236 | if (out["return"]){ |
| 237 | for(int i=0;i<out["return"].size();i++){ |
| 238 | def node = out["return"][i]; |
Jakub Josef | 053df39 | 2017-05-03 15:51:05 +0200 | [diff] [blame] | 239 | for(int j=0;j<node.size();j++){ |
| 240 | def nodeKey = node.keySet()[j] |
| 241 | if (!node[nodeKey].contains("Salt command execution success")) { |
| 242 | throw new Exception("Execution of cmd ${originalCmd} failed. Server returns: ${node[nodeKey]}") |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | }else{ |
| 247 | throw new Exception("Salt Api response doesn't have return param!") |
| 248 | } |
| 249 | } |
Jiri Broulik | 16e9ce7 | 2017-05-17 13:28:31 +0200 | [diff] [blame] | 250 | if (output == true) { |
| 251 | printSaltCommandResult(out) |
| 252 | } |
| 253 | return out |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 256 | /** |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 257 | * 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] | 258 | * @usage minionPresent(saltId, 'I@salt:master', 'ntw', true, null, true, 200, 3) |
| 259 | * @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] | 260 | * @param target Get pillar target |
| 261 | * @param minion_name unique identification of a minion in salt-key command output |
| 262 | * @param waitUntilPresent return after the minion becomes present (default true) |
| 263 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 264 | * @param output print salt command (default true) |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 265 | * @param maxRetries finite number of iterations to check status of a command (default 200) |
| 266 | * @param answers how many minions should return (optional, default 1) |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 267 | * @return output of salt command |
| 268 | */ |
lmercl | 9418927 | 2018-06-01 11:03:46 +0200 | [diff] [blame] | 269 | def minionPresent(saltId, target, minion_name, waitUntilPresent = true, batch=null, output = true, maxRetries = 180, answers = 1) { |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 270 | minion_name = minion_name.replace("*", "") |
| 271 | def common = new com.mirantis.mk.Common() |
Dzmitry Stremkouski | 8a8d56b | 2018-05-01 12:20:04 +0200 | [diff] [blame] | 272 | common.infoMsg("Looking for minion: " + minion_name) |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 273 | def cmd = 'salt-key | grep ' + minion_name |
| 274 | if (waitUntilPresent){ |
| 275 | def count = 0 |
| 276 | while(count < maxRetries) { |
Dzmitry Stremkouski | 5425d23 | 2018-06-07 00:46:00 +0200 | [diff] [blame] | 277 | try { |
| 278 | def out = runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'cmd.shell', batch, [cmd], null, 5) |
| 279 | if (output) { |
| 280 | printSaltCommandResult(out) |
| 281 | } |
| 282 | def valueMap = out["return"][0] |
| 283 | def result = valueMap.get(valueMap.keySet()[0]) |
| 284 | def resultsArray = result.tokenize("\n") |
| 285 | def size = resultsArray.size() |
| 286 | if (size >= answers) { |
| 287 | return out |
| 288 | } |
| 289 | count++ |
| 290 | sleep(time: 1000, unit: 'MILLISECONDS') |
| 291 | common.infoMsg("Waiting for ${cmd} on ${target} to be in correct state") |
| 292 | } catch (Exception er) { |
| 293 | common.infoMsg('[WARNING]: runSaltCommand command read timeout within 5 seconds. You have very slow or broken environment') |
| 294 | } |
| 295 | } |
| 296 | } else { |
| 297 | try { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 298 | 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] | 299 | if (output) { |
| 300 | printSaltCommandResult(out) |
| 301 | } |
Dzmitry Stremkouski | 5425d23 | 2018-06-07 00:46:00 +0200 | [diff] [blame] | 302 | return out |
| 303 | } catch (Exception er) { |
| 304 | common.infoMsg('[WARNING]: runSaltCommand command read timeout within 5 seconds. You have very slow or broken environment') |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 305 | } |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 306 | } |
| 307 | // otherwise throw exception |
| 308 | common.errorMsg("Status of command ${cmd} on ${target} failed, please check it.") |
| 309 | throw new Exception("${cmd} signals failure of status check!") |
| 310 | } |
| 311 | |
| 312 | /** |
Dzmitry Stremkouski | 8a8d56b | 2018-05-01 12:20:04 +0200 | [diff] [blame] | 313 | * Checks if salt minions are in a list of salt master's accepted keys by matching compound |
| 314 | * @usage minionsPresent(saltId, 'I@salt:master', 'I@salt:minion', true, null, true, 200, 3) |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 315 | * @param saltId Salt Connection object or pepperEnv (the command will be sent using the selected method) |
| 316 | * @param target Performs tests on this target node |
| 317 | * @param target_minions all targeted minions to test (for ex. I@salt:minion) |
| 318 | * @param waitUntilPresent return after the minion becomes present (default true) |
| 319 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 320 | * @param output print salt command (default true) |
| 321 | * @param maxRetries finite number of iterations to check status of a command (default 200) |
| 322 | * @param answers how many minions should return (optional, default 1) |
| 323 | * @return output of salt command |
| 324 | */ |
| 325 | def minionsPresent(saltId, target = 'I@salt:master', target_minions = '', waitUntilPresent = true, batch=null, output = true, maxRetries = 200, answers = 1) { |
Dzmitry Stremkouski | 8a8d56b | 2018-05-01 12:20:04 +0200 | [diff] [blame] | 326 | def target_hosts = getMinionsSorted(saltId, target_minions) |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 327 | for (t in target_hosts) { |
Dzmitry Stremkouski | 8a8d56b | 2018-05-01 12:20:04 +0200 | [diff] [blame] | 328 | def tgt = stripDomainName(t) |
| 329 | minionPresent(saltId, target, tgt, waitUntilPresent, batch, output, maxRetries, answers) |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Checks if salt minions are in a list of salt master's accepted keys by matching a list |
| 335 | * @usage minionsPresentFromList(saltId, 'I@salt:master', ["cfg01.example.com", "bmk01.example.com"], true, null, true, 200, 3) |
| 336 | * @param saltId Salt Connection object or pepperEnv (the command will be sent using the selected method) |
| 337 | * @param target Performs tests on this target node |
| 338 | * @param target_minions list to test (for ex. ["cfg01.example.com", "bmk01.example.com"]) |
| 339 | * @param waitUntilPresent return after the minion becomes present (default true) |
| 340 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 341 | * @param output print salt command (default true) |
| 342 | * @param maxRetries finite number of iterations to check status of a command (default 200) |
| 343 | * @param answers how many minions should return (optional, default 1) |
| 344 | * @return output of salt command |
| 345 | */ |
| 346 | def minionsPresentFromList(saltId, target = 'I@salt:master', target_minions = [], waitUntilPresent = true, batch=null, output = true, maxRetries = 200, answers = 1) { |
| 347 | def common = new com.mirantis.mk.Common() |
| 348 | for (tgt in target_minions) { |
| 349 | common.infoMsg("Checking if minion " + tgt + " is present") |
| 350 | minionPresent(saltId, target, tgt, waitUntilPresent, batch, output, maxRetries, answers) |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 355 | * 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] | 356 | * @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] | 357 | * @param target Should always be salt-master |
| 358 | * @param target_nodes unique identification of a minion or group of salt minions |
| 359 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 360 | * @param wait timeout for the salt command if minions do not return (default 10) |
| 361 | * @param maxRetries finite number of iterations to check status of a command (default 200) |
| 362 | * @return output of salt command |
| 363 | */ |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 364 | def minionsReachable(saltId, target, target_nodes, batch=null, wait = 10, maxRetries = 200) { |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 365 | def common = new com.mirantis.mk.Common() |
| 366 | def cmd = "salt -t${wait} -C '${target_nodes}' test.ping" |
| 367 | common.infoMsg("Checking if all ${target_nodes} minions are reachable") |
| 368 | def count = 0 |
| 369 | while(count < maxRetries) { |
| 370 | Calendar timeout = Calendar.getInstance(); |
| 371 | timeout.add(Calendar.SECOND, wait); |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 372 | 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] | 373 | Calendar current = Calendar.getInstance(); |
| 374 | if (current.getTime().before(timeout.getTime())) { |
| 375 | printSaltCommandResult(out) |
| 376 | return out |
| 377 | } |
| 378 | common.infoMsg("Not all of the targeted '${target_nodes}' minions returned yet. Waiting ...") |
| 379 | count++ |
| 380 | sleep(time: 500, unit: 'MILLISECONDS') |
| 381 | } |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Run command on salt minion (salt cmd.run wrapper) |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 386 | * @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] | 387 | * @param target Get pillar target |
| 388 | * @param cmd name of a service |
| 389 | * @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] | 390 | * @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] | 391 | * @param waitUntilOk return after the minion becomes present (optional, default true) |
| 392 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 393 | * @param output print salt command (default true) |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 394 | * @param maxRetries finite number of iterations to check status of a command (default 200) |
| 395 | * @param answers how many minions should return (optional, default 0) |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 396 | * @return output of salt command |
| 397 | */ |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 398 | 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] | 399 | def common = new com.mirantis.mk.Common() |
| 400 | common.infoMsg("Checking if status of verification command ${cmd} on ${target} is in correct state") |
| 401 | if (waitUntilOk){ |
| 402 | def count = 0 |
| 403 | while(count < maxRetries) { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 404 | 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] | 405 | if (output) { |
| 406 | printSaltCommandResult(out) |
| 407 | } |
Jakub Josef | 115a78f | 2017-07-18 15:04:00 +0200 | [diff] [blame] | 408 | def resultMap = out["return"][0] |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 409 | def success = 0 |
| 410 | if (answers == 0){ |
| 411 | answers = resultMap.size() |
| 412 | } |
| 413 | for (int i=0;i<answers;i++) { |
| 414 | result = resultMap.get(resultMap.keySet()[i]) |
| 415 | // if the goal is to find some string in output of the command |
| 416 | if (find) { |
| 417 | if(result == null || result instanceof Boolean || result.isEmpty()) { result='' } |
| 418 | if (result.toLowerCase().contains(correct_state.toLowerCase())) { |
| 419 | success++ |
| 420 | if (success == answers) { |
| 421 | return out |
| 422 | } |
Jiri Broulik | d0c2757 | 2017-07-24 20:01:10 +0200 | [diff] [blame] | 423 | } |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 424 | // else the goal is to not find any string in output of the command |
| 425 | } else { |
| 426 | if(result instanceof String && result.isEmpty()) { |
| 427 | success++ |
| 428 | if (success == answers) { |
| 429 | return out |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 430 | } |
Jiri Broulik | 71512bc | 2017-08-04 10:00:18 +0200 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | count++ |
| 435 | sleep(time: 500, unit: 'MILLISECONDS') |
| 436 | common.infoMsg("Waiting for ${cmd} on ${target} to be in correct state") |
| 437 | } |
| 438 | } else { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 439 | 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] | 440 | def resultMap = out["return"][0] |
| 441 | if (output) { |
| 442 | printSaltCommandResult(out) |
| 443 | } |
| 444 | for (int i=0;i<resultMap.size();i++) { |
| 445 | result = resultMap.get(resultMap.keySet()[i]) |
| 446 | // if the goal is to find some string in output of the command |
| 447 | if (find) { |
| 448 | if(result == null || result instanceof Boolean || result.isEmpty()) { result='' } |
| 449 | if (result.toLowerCase().contains(correct_state.toLowerCase())) { |
Jiri Broulik | d0c2757 | 2017-07-24 20:01:10 +0200 | [diff] [blame] | 450 | return out |
| 451 | } |
| 452 | |
| 453 | // else the goal is to not find any string in output of the command |
| 454 | } else { |
| 455 | if(result instanceof String && result.isEmpty()) { |
| 456 | return out |
| 457 | } |
| 458 | } |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | // otherwise throw exception |
Jiri Broulik | d0c2757 | 2017-07-24 20:01:10 +0200 | [diff] [blame] | 462 | common.errorMsg("Status of command ${cmd} on ${target} failed, please check it.") |
Jiri Broulik | 2c69f3d | 2017-07-18 14:23:58 +0200 | [diff] [blame] | 463 | throw new Exception("${cmd} signals failure of status check!") |
| 464 | } |
| 465 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 466 | /** |
| 467 | * Perform complete salt sync between master and target |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 468 | * @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] | 469 | * @param target Get pillar target |
| 470 | * @return output of salt command |
| 471 | */ |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 472 | def syncAll(saltId, target) { |
| 473 | return runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'saltutil.sync_all') |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 476 | /** |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 477 | * Perform complete salt refresh between master and target |
| 478 | * Method will call saltutil.refresh_pillar, saltutil.refresh_grains and saltutil.sync_all |
| 479 | * @param saltId Salt Connection object or pepperEnv (the command will be sent using the selected method) |
| 480 | * @param target Get pillar target |
| 481 | * @return output of salt command |
| 482 | */ |
| 483 | def fullRefresh(saltId, target){ |
| 484 | runSaltProcessStep(saltId, target, 'saltutil.refresh_pillar', [], null, true) |
| 485 | runSaltProcessStep(saltId, target, 'saltutil.refresh_grains', [], null, true) |
| 486 | runSaltProcessStep(saltId, target, 'saltutil.sync_all', [], null, true) |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Enforce highstate on given targets |
| 491 | * @param saltId Salt Connection object or pepperEnv (the command will be sent using the selected method) |
| 492 | * @param target Highstate enforcing target |
| 493 | * @param excludedStates states which will be excluded from main state (default empty string) |
| 494 | * @param output print output (optional, default true) |
| 495 | * @param failOnError throw exception on salt state result:false (optional, default true) |
| 496 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
| 497 | * @param saltArgs additional salt args eq. ["runas=aptly", exclude="opencontrail.database"] |
| 498 | * @return output of salt command |
| 499 | */ |
| 500 | def enforceHighstateWithExclude(saltId, target, excludedStates = "", output = false, failOnError = true, batch = null, saltArgs = []) { |
| 501 | saltArgs << "exclude=${excludedStates}" |
| 502 | return enforceHighstate(saltId, target, output, failOnError, batch, saltArgs) |
| 503 | } |
| 504 | /** |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 505 | * Enforce highstate on given targets |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 506 | * @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] | 507 | * @param target Highstate enforcing target |
| 508 | * @param output print output (optional, default true) |
| 509 | * @param failOnError throw exception on salt state result:false (optional, default true) |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 510 | * @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] | 511 | * @return output of salt command |
| 512 | */ |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 513 | def enforceHighstate(saltId, target, output = false, failOnError = true, batch = null, saltArgs = []) { |
Petr Jediný | 30be703 | 2018-05-29 18:22:46 +0200 | [diff] [blame] | 514 | def out = runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'state.highstate', batch, saltArgs) |
Alexander Noskov | 657ccfc | 2017-07-14 11:35:52 +0000 | [diff] [blame] | 515 | def common = new com.mirantis.mk.Common() |
| 516 | |
Marek Celoud | 6336611 | 2017-07-25 17:27:24 +0200 | [diff] [blame] | 517 | common.infoMsg("Running state highstate on ${target}") |
Alexander Noskov | 657ccfc | 2017-07-14 11:35:52 +0000 | [diff] [blame] | 518 | |
Jakub Josef | 374beb7 | 2017-04-27 15:45:09 +0200 | [diff] [blame] | 519 | checkResult(out, failOnError, output) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 520 | return out |
| 521 | } |
| 522 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 523 | /** |
Ales Komarek | 5276ebe | 2017-03-16 08:46:34 +0100 | [diff] [blame] | 524 | * Get running minions IDs according to the target |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 525 | * @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] | 526 | * @param target Get minions target |
| 527 | * @return list of active minions fitin |
| 528 | */ |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 529 | def getMinions(saltId, target) { |
| 530 | def minionsRaw = runSaltCommand(saltId, 'local', ['expression': target, 'type': 'compound'], 'test.ping') |
Ales Komarek | 5276ebe | 2017-03-16 08:46:34 +0100 | [diff] [blame] | 531 | return new ArrayList<String>(minionsRaw['return'][0].keySet()) |
| 532 | } |
| 533 | |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 534 | /** |
| 535 | * Get sorted running minions IDs according to the target |
| 536 | * @param saltId Salt Connection object or pepperEnv |
| 537 | * @param target Get minions target |
| 538 | * @return list of sorted active minions fitin |
| 539 | */ |
| 540 | def getMinionsSorted(saltId, target) { |
| 541 | return getMinions(saltId, target).sort() |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Get first out of running minions IDs according to the target |
| 546 | * @param saltId Salt Connection object or pepperEnv |
| 547 | * @param target Get minions target |
| 548 | * @return first of active minions fitin |
| 549 | */ |
| 550 | def getFirstMinion(saltId, target) { |
| 551 | def minionsSorted = getMinionsSorted(saltId, target) |
| 552 | return minionsSorted[0].split("\\.")[0] |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Get running salt minions IDs without it's domain name part and its numbering identifications |
| 557 | * @param saltId Salt Connection object or pepperEnv |
| 558 | * @param target Get minions target |
| 559 | * @return list of active minions fitin without it's domain name part name numbering |
| 560 | */ |
| 561 | def getMinionsGeneralName(saltId, target) { |
| 562 | def minionsSorted = getMinionsSorted(saltId, target) |
| 563 | return stripDomainName(minionsSorted[0]).replaceAll('\\d+$', "") |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Get domain name of the env |
| 568 | * @param saltId Salt Connection object or pepperEnv |
| 569 | * @return domain name |
| 570 | */ |
| 571 | def getDomainName(saltId) { |
| 572 | return getReturnValues(getPillar(saltId, 'I@salt:master', '_param:cluster_domain')) |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Remove domain name from Salt minion ID |
| 577 | * @param name String of Salt minion ID |
| 578 | * @return Salt minion ID without its domain name |
| 579 | */ |
| 580 | def stripDomainName(name) { |
| 581 | return name.split("\\.")[0] |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Gets return values of a salt command |
| 586 | * @param output String of Salt minion ID |
| 587 | * @return Return values of a salt command |
| 588 | */ |
| 589 | def getReturnValues(output) { |
| 590 | if(output.containsKey("return") && !output.get("return").isEmpty()) { |
| 591 | return output['return'][0].values()[0] |
| 592 | } |
| 593 | def common = new com.mirantis.mk.Common() |
| 594 | common.errorMsg('output does not contain return key') |
| 595 | return '' |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Get minion ID of one of KVM nodes |
| 600 | * @param saltId Salt Connection object or pepperEnv (the command will be sent using the selected method) |
| 601 | * @return Salt minion ID of one of KVM nodes in env |
| 602 | */ |
| 603 | def getKvmMinionId(saltId) { |
| 604 | return getReturnValues(getGrain(saltId, 'I@salt:control', 'id')).values()[0] |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Get Salt minion ID of KVM node hosting 'name' VM |
| 609 | * @param saltId Salt Connection object or pepperEnv |
| 610 | * @param name Name of the VM (for ex. ctl01) |
| 611 | * @return Salt minion ID of KVM node hosting 'name' VM |
| 612 | */ |
Jiri Broulik | d2a5055 | 2018-04-25 17:17:59 +0200 | [diff] [blame] | 613 | def getNodeProvider(saltId, nodeName) { |
| 614 | def salt = new com.mirantis.mk.Salt() |
| 615 | def common = new com.mirantis.mk.Common() |
| 616 | def kvms = salt.getMinions(saltId, 'I@salt:control') |
| 617 | for (kvm in kvms) { |
| 618 | try { |
| 619 | vms = salt.getReturnValues(salt.runSaltProcessStep(saltId, kvm, 'virt.list_domains', [], null, true)) |
| 620 | if (vms.toString().contains(nodeName)) { |
| 621 | return kvm |
| 622 | } |
| 623 | } catch (Exception er) { |
| 624 | common.infoMsg("${nodeName} not present on ${kvm}") |
| 625 | } |
| 626 | } |
Jiri Broulik | f8f9694 | 2018-02-15 10:03:42 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Ales Komarek | 5276ebe | 2017-03-16 08:46:34 +0100 | [diff] [blame] | 629 | /** |
Tomáš Kukrál | b12ff9f | 2017-07-12 12:32:34 +0200 | [diff] [blame] | 630 | * Test if there are any minions to target |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 631 | * @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] | 632 | * @param target Target to test |
vrovachev | 1c4770b | 2017-07-05 13:25:21 +0400 | [diff] [blame] | 633 | * @return bool indicating if target was succesful |
Tomáš Kukrál | b12ff9f | 2017-07-12 12:32:34 +0200 | [diff] [blame] | 634 | */ |
| 635 | |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 636 | def testTarget(saltId, target) { |
| 637 | return getMinions(saltId, target).size() > 0 |
Tomáš Kukrál | b12ff9f | 2017-07-12 12:32:34 +0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 641 | * Generates node key using key.gen_accept call |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 642 | * @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] | 643 | * @param target Key generating target |
| 644 | * @param host Key generating host |
| 645 | * @param keysize generated key size (optional, default 4096) |
| 646 | * @return output of salt command |
| 647 | */ |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 648 | def generateNodeKey(saltId, target, host, keysize = 4096) { |
| 649 | return runSaltCommand(saltId, 'wheel', target, 'key.gen_accept', [host], ['keysize': keysize]) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 652 | /** |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 653 | * Generates node reclass metadata |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 654 | * @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] | 655 | * @param target Metadata generating target |
| 656 | * @param host Metadata generating host |
| 657 | * @param classes Reclass classes |
| 658 | * @param parameters Reclass parameters |
| 659 | * @return output of salt command |
| 660 | */ |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 661 | def generateNodeMetadata(saltId, target, host, classes, parameters) { |
| 662 | 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] | 663 | } |
| 664 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 665 | /** |
| 666 | * Run salt orchestrate on given targets |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 667 | * @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] | 668 | * @param target Orchestration target |
| 669 | * @param orchestrate Salt orchestrate params |
Dzmitry Stremkouski | dd020d9 | 2018-07-22 12:01:07 +0200 | [diff] [blame^] | 670 | * @param kwargs Salt orchestrate params |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 671 | * @return output of salt command |
| 672 | */ |
Dzmitry Stremkouski | dd020d9 | 2018-07-22 12:01:07 +0200 | [diff] [blame^] | 673 | def orchestrateSystem(saltId, target, orchestrate=[], kwargs = null) { |
| 674 | return runSaltCommand(saltId, 'runner', target, 'state.orchestrate', true, orchestrate, kwargs, -1, -1) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 677 | /** |
| 678 | * Run salt process step |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 679 | * @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] | 680 | * @param tgt Salt process step target |
| 681 | * @param fun Salt process step function |
| 682 | * @param arg process step arguments (optional, default []) |
Jakub Josef | 2f25cf2 | 2017-03-28 13:34:57 +0200 | [diff] [blame] | 683 | * @param batch salt batch parameter integer or string with percents (optional, default null - disable batch) |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 684 | * @param output print output (optional, default true) |
Jiri Broulik | 48544be | 2017-06-14 18:33:54 +0200 | [diff] [blame] | 685 | * @param timeout Additional argument salt api timeout |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 686 | * @return output of salt command |
| 687 | */ |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 688 | def runSaltProcessStep(saltId, tgt, fun, arg = [], batch = null, output = true, timeout = -1, kwargs = null) { |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 689 | def common = new com.mirantis.mk.Common() |
Jiri Broulik | 48544be | 2017-06-14 18:33:54 +0200 | [diff] [blame] | 690 | def salt = new com.mirantis.mk.Salt() |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 691 | def out |
| 692 | |
Marek Celoud | 6336611 | 2017-07-25 17:27:24 +0200 | [diff] [blame] | 693 | common.infoMsg("Running step ${fun} ${arg} on ${tgt}") |
Tomáš Kukrál | 6c04bd0 | 2017-03-01 22:18:52 +0100 | [diff] [blame] | 694 | |
Filip Pytloun | f0435c0 | 2017-03-02 17:48:54 +0100 | [diff] [blame] | 695 | if (batch == true) { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 696 | 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] | 697 | } else { |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 698 | 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] | 699 | } |
Tomáš Kukrál | adb4ecd | 2017-03-02 10:06:36 +0100 | [diff] [blame] | 700 | |
Tomáš Kukrál | f5dda64 | 2017-03-02 14:22:59 +0100 | [diff] [blame] | 701 | if (output == true) { |
Jiri Broulik | 48544be | 2017-06-14 18:33:54 +0200 | [diff] [blame] | 702 | salt.printSaltCommandResult(out) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 703 | } |
Jiri Broulik | ae19c26 | 2017-05-16 19:06:52 +0200 | [diff] [blame] | 704 | return out |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Check result for errors and throw exception if any found |
| 709 | * |
| 710 | * @param result Parsed response of Salt API |
Jakub Josef | 8021c00 | 2017-03-27 15:41:28 +0200 | [diff] [blame] | 711 | * @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] | 712 | * @param printResults Do you want to print salt results (optional, default true) |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 713 | * @param printOnlyChanges If true (default), print only changed resources |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 714 | * @param disableAskOnError Flag for disabling ASK_ON_ERROR feature (optional, default false) |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 715 | */ |
Jakub Josef | 432e9d9 | 2018-02-06 18:28:37 +0100 | [diff] [blame] | 716 | def checkResult(result, failOnError = true, printResults = true, printOnlyChanges = true, disableAskOnError = false) { |
Jakub Josef | 5ade54c | 2017-03-10 16:14:01 +0100 | [diff] [blame] | 717 | def common = new com.mirantis.mk.Common() |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 718 | if(result != null){ |
| 719 | if(result['return']){ |
| 720 | for (int i=0;i<result['return'].size();i++) { |
| 721 | def entry = result['return'][i] |
| 722 | if (!entry) { |
| 723 | if (failOnError) { |
| 724 | throw new Exception("Salt API returned empty response: ${result}") |
| 725 | } else { |
| 726 | common.errorMsg("Salt API returned empty response: ${result}") |
Jakub Josef | ece32af | 2017-03-14 19:20:08 +0100 | [diff] [blame] | 727 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 728 | } |
| 729 | for (int j=0;j<entry.size();j++) { |
| 730 | def nodeKey = entry.keySet()[j] |
| 731 | def node=entry[nodeKey] |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 732 | def outputResources = [] |
Jakub Josef | 4714594 | 2018-04-04 17:30:38 +0200 | [diff] [blame] | 733 | def errorResources = [] |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 734 | common.infoMsg("Node ${nodeKey} changes:") |
| 735 | if(node instanceof Map || node instanceof List){ |
| 736 | for (int k=0;k<node.size();k++) { |
| 737 | def resource; |
| 738 | def resKey; |
| 739 | if(node instanceof Map){ |
| 740 | resKey = node.keySet()[k] |
Richard Felkl | d9476ac | 2018-07-12 19:01:33 +0200 | [diff] [blame] | 741 | if (resKey == "retcode") |
| 742 | continue |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 743 | }else if(node instanceof List){ |
| 744 | resKey = k |
| 745 | } |
| 746 | resource = node[resKey] |
Jakub Josef | c4c4020 | 2017-04-28 12:04:24 +0200 | [diff] [blame] | 747 | // print |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 748 | if(printResults){ |
| 749 | if(resource instanceof Map && resource.keySet().contains("result")){ |
| 750 | //clean unnesaccary fields |
| 751 | if(resource.keySet().contains("__run_num__")){ |
| 752 | resource.remove("__run_num__") |
| 753 | } |
| 754 | if(resource.keySet().contains("__id__")){ |
| 755 | resource.remove("__id__") |
| 756 | } |
| 757 | if(resource.keySet().contains("pchanges")){ |
| 758 | resource.remove("pchanges") |
| 759 | } |
| 760 | if(!resource["result"] || (resource["result"] instanceof String && resource["result"] != "true")){ |
| 761 | if(resource["result"] != null){ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 762 | 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] | 763 | }else{ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 764 | 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] | 765 | } |
| 766 | }else{ |
| 767 | if(!printOnlyChanges || resource.changes.size() > 0){ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 768 | 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] | 769 | } |
| 770 | } |
| 771 | }else{ |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 772 | 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] | 773 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 774 | } |
Jakub Josef | c4c4020 | 2017-04-28 12:04:24 +0200 | [diff] [blame] | 775 | common.debugMsg("checkResult: checking resource: ${resource}") |
| 776 | if(resource instanceof String || (resource["result"] != null && !resource["result"]) || (resource["result"] instanceof String && resource["result"] == "false")){ |
Jakub Josef | 4714594 | 2018-04-04 17:30:38 +0200 | [diff] [blame] | 777 | errorResources.add(resource) |
Jakub Josef | c4c4020 | 2017-04-28 12:04:24 +0200 | [diff] [blame] | 778 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 779 | } |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 780 | }else if(node!=null && node!=""){ |
Jakub Josef | 62f6c84 | 2017-08-04 16:36:35 +0200 | [diff] [blame] | 781 | 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] | 782 | } |
| 783 | if(printResults && !outputResources.isEmpty()){ |
Jakub Josef | 4714594 | 2018-04-04 17:30:38 +0200 | [diff] [blame] | 784 | println outputResources.stream().collect(Collectors.joining("\n")) |
| 785 | } |
| 786 | if(!errorResources.isEmpty()){ |
| 787 | for(resource in errorResources){ |
| 788 | def prettyResource = common.prettify(resource) |
| 789 | if (!disableAskOnError && env["ASK_ON_ERROR"] && env["ASK_ON_ERROR"] == "true") { |
| 790 | timeout(time:1, unit:'HOURS') { |
| 791 | input message: "False result on ${nodeKey} found, resource ${prettyResource}. \nDo you want to continue?" |
| 792 | } |
| 793 | } else { |
| 794 | def errorMsg = "Salt state on node ${nodeKey} failed. Resource: ${prettyResource}" |
| 795 | if (failOnError) { |
| 796 | throw new Exception(errorMsg) |
| 797 | } else { |
| 798 | common.errorMsg(errorMsg) |
| 799 | } |
| 800 | } |
| 801 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 802 | } |
| 803 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 804 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 805 | }else{ |
| 806 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 807 | } |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 808 | }else{ |
Jakub Josef | a87941c | 2017-04-20 17:14:58 +0200 | [diff] [blame] | 809 | common.errorMsg("Cannot check salt result, given result is null") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | /** |
Oleg Iurchenko | 7eb2150 | 2017-11-28 18:53:43 +0200 | [diff] [blame] | 814 | * Parse salt API output to check minion restart and wait some time to be sure minion is up. |
| 815 | * See https://mirantis.jira.com/browse/PROD-16258 for more details |
| 816 | * TODO: change sleep to more tricky procedure. |
| 817 | * |
| 818 | * @param result Parsed response of Salt API |
| 819 | */ |
Vasyl Saienko | 6a39621 | 2018-06-08 09:20:08 +0300 | [diff] [blame] | 820 | def waitForMinion(result, minionRestartWaitTimeout=10) { |
Oleg Iurchenko | 7eb2150 | 2017-11-28 18:53:43 +0200 | [diff] [blame] | 821 | def common = new com.mirantis.mk.Common() |
Oleg Iurchenko | 3eedc78 | 2017-12-12 11:49:29 +0200 | [diff] [blame] | 822 | //In order to prevent multiple sleeps use bool variable to catch restart for any minion. |
Oleg Iurchenko | 7eb2150 | 2017-11-28 18:53:43 +0200 | [diff] [blame] | 823 | def isMinionRestarted = false |
Oleg Iurchenko | 3eedc78 | 2017-12-12 11:49:29 +0200 | [diff] [blame] | 824 | if(result != null){ |
| 825 | if(result['return']){ |
| 826 | for (int i=0;i<result['return'].size();i++) { |
| 827 | def entry = result['return'][i] |
| 828 | // exit in case of empty response. |
| 829 | if (!entry) { |
| 830 | return |
| 831 | } |
| 832 | // Loop for nodes |
| 833 | for (int j=0;j<entry.size();j++) { |
| 834 | def nodeKey = entry.keySet()[j] |
| 835 | def node=entry[nodeKey] |
| 836 | if(node instanceof Map || node instanceof List){ |
| 837 | // Loop for node resources |
| 838 | for (int k=0;k<node.size();k++) { |
| 839 | def resource; |
| 840 | def resKey; |
| 841 | if(node instanceof Map){ |
| 842 | resKey = node.keySet()[k] |
| 843 | }else if(node instanceof List){ |
| 844 | resKey = k |
| 845 | } |
| 846 | resource = node[resKey] |
Jakub Josef | fb9996d | 2018-04-10 14:05:31 +0200 | [diff] [blame] | 847 | // try to find if salt_minion service was restarted |
| 848 | if(resKey instanceof String && resKey.contains("salt_minion_service_restart") && resource instanceof Map && resource.keySet().contains("result")){ |
Oleg Iurchenko | 3eedc78 | 2017-12-12 11:49:29 +0200 | [diff] [blame] | 849 | if((resource["result"] instanceof Boolean && resource["result"]) || (resource["result"] instanceof String && resource["result"] == "true")){ |
| 850 | if(resource.changes.size() > 0){ |
| 851 | isMinionRestarted=true |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | } |
Oleg Iurchenko | 7eb2150 | 2017-11-28 18:53:43 +0200 | [diff] [blame] | 859 | } |
| 860 | } |
Oleg Iurchenko | 7eb2150 | 2017-11-28 18:53:43 +0200 | [diff] [blame] | 861 | if (isMinionRestarted){ |
Vasyl Saienko | 6a39621 | 2018-06-08 09:20:08 +0300 | [diff] [blame] | 862 | common.infoMsg("Salt minion service restart detected. Sleep ${minionRestartWaitTimeout} seconds to wait minion restart") |
| 863 | sleep(minionRestartWaitTimeout) |
Oleg Iurchenko | 7eb2150 | 2017-11-28 18:53:43 +0200 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
| 867 | /** |
Jakub Josef | 7852fe1 | 2017-03-15 16:02:41 +0100 | [diff] [blame] | 868 | * Print salt command run results in human-friendly form |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 869 | * |
| 870 | * @param result Parsed response of Salt API |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 871 | */ |
Filip Pytloun | d2f1bbe | 2017-02-27 19:03:51 +0100 | [diff] [blame] | 872 | def printSaltCommandResult(result) { |
Jakub Josef | 871bf15 | 2017-03-14 20:13:41 +0100 | [diff] [blame] | 873 | def common = new com.mirantis.mk.Common() |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 874 | if(result != null){ |
| 875 | if(result['return']){ |
| 876 | for (int i=0; i<result['return'].size(); i++) { |
| 877 | def entry = result['return'][i] |
| 878 | for (int j=0; j<entry.size(); j++) { |
| 879 | common.debugMsg("printSaltCommandResult: printing salt command entry: ${entry}") |
| 880 | def nodeKey = entry.keySet()[j] |
| 881 | def node=entry[nodeKey] |
Jakub Josef | bceaa32 | 2017-06-13 18:28:27 +0200 | [diff] [blame] | 882 | 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] | 883 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame] | 884 | } |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 885 | }else{ |
| 886 | common.errorMsg("Salt result hasn't return attribute! Result: ${result}") |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 887 | } |
Jakub Josef | 8a715bf | 2017-03-14 21:39:01 +0100 | [diff] [blame] | 888 | }else{ |
Jakub Josef | d9afd0e | 2017-03-15 19:19:23 +0100 | [diff] [blame] | 889 | common.errorMsg("Cannot print salt command result, given result is null") |
Jakub Josef | 52f69f7 | 2017-03-14 15:18:08 +0100 | [diff] [blame] | 890 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 891 | } |
Tomáš Kukrál | b12eedd | 2017-04-21 10:45:13 +0200 | [diff] [blame] | 892 | |
| 893 | |
| 894 | /** |
| 895 | * Return content of file target |
| 896 | * |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 897 | * @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] | 898 | * @param target Compound target (should target only one host) |
| 899 | * @param file File path to read (/etc/hosts for example) |
| 900 | */ |
| 901 | |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 902 | def getFileContent(saltId, target, file) { |
| 903 | result = cmdRun(saltId, target, "cat ${file}") |
Tomáš Kukrál | f1a692a | 2017-08-11 13:29:28 +0200 | [diff] [blame] | 904 | return result['return'][0].values()[0].replaceAll('Salt command execution success','') |
Tomáš Kukrál | b12eedd | 2017-04-21 10:45:13 +0200 | [diff] [blame] | 905 | } |
Matthew Mosesohn | 9e88085 | 2017-07-04 21:17:53 +0300 | [diff] [blame] | 906 | |
| 907 | /** |
| 908 | * Set override parameters in Salt cluster metadata |
| 909 | * |
chnyda | a0dbb25 | 2017-10-05 10:46:09 +0200 | [diff] [blame] | 910 | * @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] | 911 | * @param salt_overrides YAML formatted string containing key: value, one per line |
Matthew Mosesohn | e564684 | 2017-07-19 16:54:57 +0300 | [diff] [blame] | 912 | * @param reclass_dir Directory where Reclass git repo is located |
Matthew Mosesohn | 9e88085 | 2017-07-04 21:17:53 +0300 | [diff] [blame] | 913 | */ |
| 914 | |
Oleh Hryhorov | 5f96e09 | 2018-06-22 18:37:08 +0300 | [diff] [blame] | 915 | def setSaltOverrides(saltId, salt_overrides, reclass_dir="/srv/salt/reclass", extra_tgt = '') { |
Tomáš Kukrál | f178f05 | 2017-07-11 11:31:00 +0200 | [diff] [blame] | 916 | def common = new com.mirantis.mk.Common() |
Mykyta Karpin | 1c165e2 | 2017-08-22 18:27:01 +0300 | [diff] [blame] | 917 | def salt_overrides_map = readYaml text: salt_overrides |
Tomáš Kukrál | 243cf84 | 2017-07-11 13:11:56 +0200 | [diff] [blame] | 918 | for (entry in common.entries(salt_overrides_map)) { |
Matthew Mosesohn | 9e88085 | 2017-07-04 21:17:53 +0300 | [diff] [blame] | 919 | def key = entry[0] |
| 920 | def value = entry[1] |
| 921 | |
| 922 | common.debugMsg("Set salt override ${key}=${value}") |
Oleh Hryhorov | 5f96e09 | 2018-06-22 18:37:08 +0300 | [diff] [blame] | 923 | runSaltProcessStep(saltId, "I@salt:master ${extra_tgt}", 'reclass.cluster_meta_set', [key, value], false) |
Matthew Mosesohn | 9e88085 | 2017-07-04 21:17:53 +0300 | [diff] [blame] | 924 | } |
Oleh Hryhorov | 5f96e09 | 2018-06-22 18:37:08 +0300 | [diff] [blame] | 925 | runSaltProcessStep(saltId, "I@salt:master ${extra_tgt}", '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] | 926 | } |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame] | 927 | |
| 928 | /** |
| 929 | * Execute salt commands via salt-api with |
| 930 | * CLI client salt-pepper |
| 931 | * |
| 932 | * @param data Salt command map |
| 933 | * @param venv Path to virtualenv with |
| 934 | */ |
| 935 | |
| 936 | def runPepperCommand(data, venv) { |
Jakub Josef | 03d4d5a | 2017-12-20 16:35:09 +0100 | [diff] [blame] | 937 | def common = new com.mirantis.mk.Common() |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame] | 938 | def python = new com.mirantis.mk.Python() |
| 939 | def dataStr = new groovy.json.JsonBuilder(data).toString() |
chnyda | 4901a04 | 2017-11-16 12:14:56 +0100 | [diff] [blame] | 940 | |
Jakub Josef | a2491ad | 2018-01-15 16:26:27 +0100 | [diff] [blame] | 941 | def pepperCmdFile = "${venv}/pepper-cmd.json" |
| 942 | writeFile file: pepperCmdFile, text: dataStr |
| 943 | def pepperCmd = "pepper -c ${venv}/pepperrc --make-token -x ${venv}/.peppercache --json-file ${pepperCmdFile}" |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame] | 944 | |
| 945 | if (venv) { |
Jakub Josef | e2f4ebb | 2018-01-15 16:11:51 +0100 | [diff] [blame] | 946 | output = python.runVirtualenvCommand(venv, pepperCmd, true) |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame] | 947 | } else { |
| 948 | echo("[Command]: ${pepperCmd}") |
| 949 | output = sh ( |
| 950 | script: pepperCmd, |
| 951 | returnStdout: true |
| 952 | ).trim() |
| 953 | } |
| 954 | |
Jakub Josef | 37cd497 | 2018-02-01 16:25:25 +0100 | [diff] [blame] | 955 | def outputObj |
| 956 | try { |
| 957 | outputObj = new groovy.json.JsonSlurperClassic().parseText(output) |
| 958 | } catch(Exception e) { |
| 959 | common.errorMsg("Parsing Salt API JSON response failed! Response: " + output) |
| 960 | throw e |
| 961 | } |
| 962 | return outputObj |
Oleg Grigorov | bec4558 | 2017-09-12 20:29:24 +0300 | [diff] [blame] | 963 | } |