From 0a71385549cb564b78cde66ecedc8064d7bfd8a0 Mon Sep 17 00:00:00 2001 From: Juan Vidal Date: Tue, 14 Feb 2017 09:48:53 +0000 Subject: Modify run_cmd to return exit codes as well as stdout and stderr The old implementation of run_cmd returned different things in different situations, so the simplified new version broke some functions relying on boolean values instead of stdout. Change-Id: Ie94e529d2abfad171b9707df169925c6f5f47dfc Signed-off-by: Juan Vidal --- sfc/lib/utils.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'sfc/lib') diff --git a/sfc/lib/utils.py b/sfc/lib/utils.py index 9c191462..8bd67e4f 100644 --- a/sfc/lib/utils.py +++ b/sfc/lib/utils.py @@ -25,22 +25,23 @@ FUNCTEST_RESULTS_DIR = os.path.join("home", "opnfv", def run_cmd(cmd): - """run given command locally and return commands output if success""" + """ + Run given command locally + Return a tuple with the return code, stdout, and stderr of the command + """ pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (output, errors) = pipe.communicate() + output, errors = pipe.communicate() logger.debug("running [%s] returns: <%s> - %s " "" % (cmd, pipe.returncode, output)) - if output: - output = output.strip() + if pipe.returncode != 0 or len(errors) > 0: logger.error('FAILED to execute {0}'.format(cmd)) logger.error(errors) - return None - return output + return pipe.returncode, output.strip(), errors.strip() def run_cmd_remote(ip, cmd, username="root", passwd="opnfv"): @@ -163,7 +164,7 @@ def ping(remote, pkt_cnt=1, iface=None, retries=100, timeout=None): cmd = ping_cmd + '|' + grep_cmd while retries > 0: - output = run_cmd(cmd) + _, output, _ = run_cmd(cmd) if not output: return False @@ -213,7 +214,7 @@ def start_http_server(ip): cmd = "\'python -m SimpleHTTPServer 80" cmd = cmd + " > /dev/null 2>&1 &\'" run_cmd_remote(ip, cmd) - output = run_cmd_remote(ip, "ps aux|grep SimpleHTTPServer") + _, output, _ = run_cmd_remote(ip, "ps aux | grep SimpleHTTPServer") if not output: logger.error("Failed to start http server") return False @@ -242,7 +243,7 @@ def netcat(s_ip, c_ip, port="80", timeout=5): cmd = "nc -zv " cmd = cmd + " -w %s %s %s" % (timeout, s_ip, port) cmd = cmd + " 2>&1" - output = run_cmd_remote(c_ip, cmd) + _, output, _ = run_cmd_remote(c_ip, cmd) logger.info("%s" % output) return output @@ -283,7 +284,8 @@ def check_ssh(ips, retries=100): logger.info("Checking SSH connectivity to the SFs with ips %s" % str(ips)) while retries and not all(check): for index, ip in enumerate(ips): - check[index] = run_cmd_remote(ip, "exit") + rc, _, _ = run_cmd_remote(ip, "exit") + check[index] = True if rc == 0 else False if all(check): logger.info("SSH connectivity to the SFs established") @@ -349,7 +351,7 @@ def wait_for_classification_rules(ovs_logger, compute_clients, timeout=200): def setup_compute_node(cidr, compute_nodes): logger.info("bringing up br-int iface") - grep_cidr_routes = ("ip route|grep -o {0} || true".format(cidr)).strip() + grep_cidr_routes = ("ip route | grep -o {0} || true".format(cidr)).strip() add_cidr = "ip route add {0} br-int".format(cidr) for compute in compute_nodes: compute.run_cmd("ifconfig br-int up") @@ -357,4 +359,4 @@ def setup_compute_node(cidr, compute_nodes): logger.info("adding route %s in %s" % (cidr, compute.ip)) compute.run_cmd(add_cidr) else: - logger.info("route %s exists" % cidr) + logger.info("route %s already exists" % cidr) -- cgit 1.2.3-korg