X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=yardstick%2Fbenchmark%2Fscenarios%2Favailability%2Futil.py;h=adc20b84446722eeabd6e2f457fefa6021c4f074;hb=d004be4dc2b28617ff95ff71fe228e0209ae5328;hp=2addef8ef79693c0461a60803f324568c5db73a4;hpb=8a4f8df64b8897bf38e77399099099f92266048b;p=yardstick.git diff --git a/yardstick/benchmark/scenarios/availability/util.py b/yardstick/benchmark/scenarios/availability/util.py index 2addef8ef..adc20b844 100644 --- a/yardstick/benchmark/scenarios/availability/util.py +++ b/yardstick/benchmark/scenarios/availability/util.py @@ -6,14 +6,56 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import logging +import subprocess +import traceback +LOG = logging.getLogger(__name__) -def buildshellparams(param): - i = 0 - values = [] - result = '/bin/bash -s' - for key in param.keys(): - values.append(param[key]) - result += " {%d}" % i - i = i + 1 + +def buildshellparams(param, remote=True): + result = '/bin/bash -s' if remote else '' + result += "".join(" {%d}" % i for i in range(len(param))) return result + + +def execute_shell_command(command): + """execute shell script with error handling""" + exitcode = 0 + output = [] + try: + LOG.debug("the command is: %s", command) + output = subprocess.check_output(command, shell=True) + except Exception: + exitcode = -1 + output = traceback.format_exc() + LOG.error("exec command '%s' error:\n ", command) + LOG.error(traceback.format_exc()) + return exitcode, output + +PREFIX = '@' + + +def build_shell_command(param_config, remote=True, intermediate_variables=None): + param_template = '/bin/bash -s' if remote else '' + if intermediate_variables: + for key, val in param_config.items(): + if str(val).startswith(PREFIX): + try: + param_config[key] = intermediate_variables[val] + except KeyError: + pass + result = param_template + "".join(" {}".format(v) for v in param_config.values()) + LOG.debug("THE RESULT OF build_shell_command IS: %s", result) + return result + + +def read_stdout_item(stdout, key): + if key == "all": + return stdout + for item in stdout.splitlines(): + if key in item: + attributes = item.split("|") + if attributes[1].lstrip().startswith(key): + return attributes[2].strip() + return None