Add shCmdStatus command
There is no buil-in command that allows to get both stdout,
stderr and status. And sometimes it is needed to get all
of them.
This patch adds such ability.
Change-Id: Iac016c2753d4dee87246f1a44cd1441a791e1665
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index 15676a9..1732da4 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -448,3 +448,28 @@
def countHashMapEquals(lm, param, eq) {
return lm.stream().filter{i -> i[param].equals(eq)}.collect(java.util.stream.Collectors.counting())
}
+
+/**
+ * Execute shell command and return stdout, stderr and status
+ *
+ * @param cmd Command to execute
+ * @return map with stdout, stderr, status keys
+ */
+
+def shCmdStatus(cmd) {
+ def res = [:]
+ def stderr = sh(script: 'mktemp', returnStdout: true).trim()
+ def stdout = sh(script: 'mktemp', returnStdout: true).trim()
+
+ try {
+ def status = sh(script:"${cmd} 1>${stdout} 2>${stderr}", returnStatus: true)
+ res['stderr'] = sh(script: "cat ${stderr}", returnStdout: true)
+ res['stdout'] = sh(script: "cat ${stdout}", returnStdout: true)
+ res['status'] = status
+ } finally {
+ sh(script: "rm ${stderr}", returnStdout: true)
+ sh(script: "rm ${stdout}", returnStdout: true)
+ }
+
+ return res
+}