aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/availability/util.py
blob: 6fef622bdb836e20ca5bb33e28f0df0eff52267d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
##############################################################################
# Copyright (c) 2016 Juan Qiu
# juan_ qiu@tongji.edu.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# 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, 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):
    for item in stdout.splitlines():
        if key in item:
            attributes = item.split("|")
            if attributes[1].lstrip().startswith(key):
                return attributes[2].strip()
    return None