diff options
Diffstat (limited to 'sfc/lib')
-rw-r--r-- | sfc/lib/utils.py | 62 |
1 files changed, 30 insertions, 32 deletions
diff --git a/sfc/lib/utils.py b/sfc/lib/utils.py index 67dbff22..c7247dbe 100644 --- a/sfc/lib/utils.py +++ b/sfc/lib/utils.py @@ -27,22 +27,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"): @@ -181,7 +182,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 @@ -231,7 +232,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 @@ -255,32 +256,28 @@ def vxlan_tool_stop(sf): run_cmd_remote(sf, cmd) -def netcat(s_ip, c_ip, port="80", timeout=5): - """Run netcat on a give machine, Can be VM""" - cmd = "nc -zv " - cmd = cmd + " -w %s %s %s" % (timeout, s_ip, port) - cmd = cmd + " 2>&1" - output = run_cmd_remote(c_ip, cmd) +def netcat(source_ip, destination_ip, port, timeout=5): + """ + SSH into source_ip, and check the connectivity from there to destination_ip + on the specified port, using the netcat command. + Returns 0 on successful execution, != 0 on failure + """ + cmd = "nc -zv -w %s %s %s 2>&1" % (timeout, destination_ip, port) + rc, output, _ = run_cmd_remote(source_ip, cmd) + logger.info("Running netcat from [%s] - connecting to [%s] on port [%s]" % + (source_ip, destination_ip, port)) logger.info("%s" % output) - return output + return rc -def is_ssh_blocked(srv_prv_ip, client_ip): - res = netcat(srv_prv_ip, client_ip, port="22") - match = re.search("nc:.*timed out:.*", res, re.M) - if match: - return True - - return False +def is_ssh_blocked(source_ip, destination_ip): + rc = netcat(source_ip, destination_ip, port="22") + return rc != 0 -def is_http_blocked(srv_prv_ip, client_ip): - res = netcat(srv_prv_ip, client_ip, port="80") - match = re.search(".* 80 port.* succeeded!", res, re.M) - if match: - return False - - return True +def is_http_blocked(source_ip, destination_ip): + rc = netcat(source_ip, destination_ip, port="80") + return rc != 0 def capture_ovs_logs(ovs_logger, controller_clients, compute_clients, error): @@ -301,7 +298,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") @@ -367,7 +365,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") @@ -375,4 +373,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) |