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