summaryrefslogtreecommitdiffstats
path: root/openshift-storage-libs/openshiftstoragelibs/command.py
blob: 90b669a722db7ccfda9cb84205bb9e2122f72db2 (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
from glusto.core import Glusto as g


def cmd_run(cmd, hostname, raise_on_error=True):
    """Glusto's command runner wrapper.

    Args:
        cmd (str): Shell command to run on the specified hostname.
        hostname (str): hostname where Glusto should run specified command.
        raise_on_error (bool): defines whether we should raise exception
                               in case command execution failed.
    Returns:
        str: Stripped shell command's stdout value if not None.
    """
    ret, out, err = g.run(hostname, cmd, "root")
    if ("no ssh connection" in err.lower()
            or "tls handshake timeout" in err.lower()):
        g.ssh_close_connection(hostname)
        ret, out, err = g.run(hostname, cmd, "root")
    msg = ("Failed to execute command '%s' on '%s' node. Got non-zero "
           "return code '%s'. Err: %s" % (cmd, hostname, ret, err))
    if int(ret) != 0:
        g.log.error(msg)
    if raise_on_error:
        assert int(ret) == 0, msg

    out = out.strip() if out else out

    return out