Improved salt cmd.run ability
Change-Id: I52b0bf90b04faf86395d6474d20c8ef114abbf8f
diff --git a/src/com/mirantis/mk/Salt.groovy b/src/com/mirantis/mk/Salt.groovy
index 3e870fa..53b2629 100644
--- a/src/com/mirantis/mk/Salt.groovy
+++ b/src/com/mirantis/mk/Salt.groovy
@@ -147,15 +147,35 @@
* @param master Salt connection object
* @param target Get pillar target
* @param cmd command
+ * @param checkResponse test command success execution (default true)
* @param batch salt batch parameter integer or string with percents (optional, default null - disable batch)
* @return output of salt command
*/
-def cmdRun(master, target, cmd, batch=null) {
+def cmdRun(master, target, cmd, checkResponse = true, batch=null) {
def common = new com.mirantis.mk.Common()
-
+ def originalCmd = cmd
common.infoMsg("Running command ${cmd} on ${target}")
-
- return runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', batch, [cmd])
+ if (checkResponse) {
+ cmd = cmd + " && echo Salt command execution success"
+ }
+ def output = salt.runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', batch, [cmd])
+ if (checkResponse) {
+ // iterate over all affected nodes and check success return code
+ if (output["return"]){
+ for(int i=0;i<output["return"].size();i++){
+ def node = output["return"][i];
+ for(int j=0;j<node.size();j++){
+ def nodeKey = node.keySet()[j]
+ if (!node[nodeKey].contains("Salt command execution success")) {
+ throw new Exception("Execution of cmd ${originalCmd} failed. Server returns: ${node[nodeKey]}")
+ }
+ }
+ }
+ }else{
+ throw new Exception("Salt Api response doesn't have return param!")
+ }
+ }
+ return output
}
/**