Add parameter to allow enforceState continue in case 'No minions matched the target'
Change-Id: I0a5b7caec7102ac823887f2b4546a3041c210452
diff --git a/src/com/mirantis/mk/Salt.groovy b/src/com/mirantis/mk/Salt.groovy
index a63865f..f0d0cc8 100644
--- a/src/com/mirantis/mk/Salt.groovy
+++ b/src/com/mirantis/mk/Salt.groovy
@@ -128,9 +128,10 @@
* @param output print output (optional, default true)
* @param failOnError throw exception on salt state result:false (optional, default true)
* @param batch salt batch parameter integer or string with percents (optional, default null - disable batch)
+ * @param optional don't fail on empty response from salt caused by 'No minions matched the targed' if set to true (default false)
* @return output of salt command
*/
-def enforceState(master, target, state, output = true, failOnError = true, batch = null) {
+def enforceState(master, target, state, output = true, failOnError = true, batch = null, optional = false) {
def common = new com.mirantis.mk.Common()
def run_states
@@ -141,11 +142,17 @@
}
common.infoMsg("Enforcing state ${run_states} on ${target}")
-
- def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', batch, [run_states])
-
- checkResult(out, failOnError, output)
- return out
+ if (optional==false){
+ def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', batch, [run_states])
+ checkResult(out, failOnError, output)
+ return out
+ } else if (testTarget(master, target)) {
+ def out = runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'state.sls', batch, [run_states])
+ checkResult(out, failOnError, output)
+ return out
+ } else {
+ common.infoMsg("No Minions matched the target given, but 'optional' param was set to true - Pipeline continues. ")
+ }
}
/**