Add isPackageInstalled and getIostatValues methods to Salt.groovy class
- required method for implementation of related pipeline steps
- fix for a method call to use Map
Related: PROD-28135
Change-Id: I615fd90026d3dcd0ada6e6619e9d3302b99886af
(cherry picked from commit 4512e2e2fe11fbb9d2e5bf9f89f2cacd335a32cf)
(cherry picked from commit a7744207889e2f939f66c4a2135b7b25188ddbfc)
diff --git a/src/com/mirantis/mk/Salt.groovy b/src/com/mirantis/mk/Salt.groovy
index 7fd8ad0..21bb4c9 100644
--- a/src/com/mirantis/mk/Salt.groovy
+++ b/src/com/mirantis/mk/Salt.groovy
@@ -1214,3 +1214,96 @@
return false
}
}
+
+/**
+* Finds out IP address of the given node or a list of nodes
+*
+* @param saltId Salt Connection object or pepperEnv (the command will be sent using the selected method)
+* @param nodes Targeted node hostnames to be checked (String or List of strings)
+* @param useGrains If the, the value will be taken from grains. If false, it will be taken from 'hostname' command.
+* @return Map Return result Map in format ['nodeName1': 'ipAdress1', 'nodeName2': 'ipAdress2', ...]
+*/
+
+def getIPAddressesForNodenames(saltId, nodes = [], useGrains = true) {
+ result = [:]
+
+ if (nodes instanceof String) {
+ nodes = [nodes]
+ }
+
+ if (useGrains) {
+ for (String node in nodes) {
+ ip = getReturnValues(getGrain(saltId, node, "fqdn_ip4"))["fqdn_ip4"][0]
+ result[node] = ip
+ }
+ } else {
+ for (String node in nodes) {
+ ip = getReturnValues(cmdRun(saltId, node, "hostname -i")).readLines()[0]
+ result[node] = ip
+ }
+ }
+ return result
+}
+
+/**
+* Checks if required package is installed and returns averaged IO stats for selected disks.
+* Allows getting averaged values of specific parameter for all disks or a specified disk.
+* Interval between checks and its number is parametrized and configurable.
+*
+* @param saltId Salt Connection object or pepperEnv (the command will be sent using the selected method)
+* @param target Node to be targeted (Should only match 1 node)
+* @param parameterName Name of parameter from 'iostat' output (default = '' -- returns all variables)
+* @param interval Interval between checks (default = 1)
+* @param count Number of checks (default = 5)
+* @param disks Disks to be checked (default = '' -- returns all disks)
+* @param output Print Salt command return (default = true)
+* @return Map Map containing desired values in format ['disk':'value']
+*/
+
+def getIostatValues(Map params) {
+ def common = new com.mirantis.mk.Common()
+ def ret = [:]
+ if (isPackageInstalled(['saltId': params.saltId, 'target': params.target, 'packageName': 'sysstat', 'output': false])) {
+ def arg = [params.get('interval', 1), params.get('count', 5), params.get('disks', '')]
+ def res = getReturnValues(runSaltProcessStep(params.saltId, params.target, 'disk.iostat', arg, null, params.output))
+ if (res instanceof Map) {
+ for (int i = 0; i < res.size(); i++) {
+ def key = res.keySet()[i]
+ if (params.containsKey('parameterName')) {
+ if (res[key].containsKey(params.parameterName)){
+ ret[key] = res[key][params.parameterName]
+ } else {
+ common.errorMsg("Parameter '${params.parameterName}' not found for disk '${key}'. Valid parameter for this disk are: '${res[key].keySet()}'")
+ }
+ } else {
+ return res // If no parameterName is defined, return all of them.
+ }
+ }
+ }
+ } else {
+ common.errorMsg("Package 'sysstat' seems not to be installed on at least one of tageted nodes: ${params.target}. Please fix this to be able to check 'iostat' values. Find more in the docs TODO:<Add docs link>")
+ }
+ return ret
+}
+
+/**
+* Checks if defined package is installed on all nodes defined by target parameter.
+*
+* @param saltId Salt Connection object or pepperEnv (the command will be sent using the selected method)
+* @param target Node or nodes to be targeted
+* @param packageName Name of package to be checked
+* @param output Print Salt command return (default = true)
+* @return boolean True if package is installed on all defined nodes. False if not found on at least one of defined nodes.
+*/
+
+def isPackageInstalled(Map params) {
+ def output = params.get('output', true)
+ def res = runSaltProcessStep(params.saltId, params.target, "pkg.info_installed", params.packageName, null, output)['return'][0]
+ for (int i = 0; i < res.size(); i++) {
+ def key = res.keySet()[i]
+ if (!(res[key] instanceof Map && res[key].containsKey(params.packageName))) {
+ return false
+ }
+ }
+ return true
+}