X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=doctor_tests%2Fcommon%2Futils.py;h=67ca4f4b764e1cf8b972274966ac32ae44897085;hb=d8eb12f4200c21f569df5bc01d378a846b4c0db0;hp=2e823acba12ab96f4b3eade4e993639f889a1b25;hpb=079ac9a481fcc1baa53cb5ab2896bbe037585f3b;p=doctor.git diff --git a/doctor_tests/common/utils.py b/doctor_tests/common/utils.py index 2e823acb..67ca4f4b 100644 --- a/doctor_tests/common/utils.py +++ b/doctor_tests/common/utils.py @@ -10,6 +10,7 @@ import json import os import paramiko import re +import subprocess def load_json_file(full_path): @@ -47,13 +48,17 @@ def match_rep_in_file(regex, full_path): return None +def get_doctor_test_root_dir(): + current_dir = os.path.split(os.path.realpath(__file__))[0] + return os.path.dirname(current_dir) + + class SSHClient(object): def __init__(self, ip, username, password=None, pkey=None, key_filename=None, log=None, look_for_keys=False, allow_agent=False): self.client = paramiko.SSHClient() - self.client.load_system_host_keys() - self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.client.set_missing_host_key_policy(paramiko.WarningPolicy()) self.client.connect(ip, username=username, password=password, pkey=pkey, key_filename=key_filename, look_for_keys=look_for_keys, @@ -63,7 +68,7 @@ class SSHClient(object): def __del__(self): self.client.close() - def ssh(self, command): + def ssh(self, command, raise_enabled=True): if self.log: self.log.info("Executing: %s" % command) stdin, stdout, stderr = self.client.exec_command(command) @@ -71,9 +76,10 @@ class SSHClient(object): output = list() for line in stdout.read().splitlines(): output.append(line.decode('utf-8')) - if ret: + if ret and raise_enabled: if self.log: - self.log.info("*** FAILED to run command %s (%s)" % (command, ret)) + self.log.info("*** FAILED to run command %s (%s)" + % (command, ret)) raise Exception( "Unable to run \ncommand: %s\nret: %s" % (command, ret)) @@ -92,6 +98,27 @@ class SSHClient(object): ftp.close() +class LocalSSH(object): + + def __init__(self, log): + self.log = log + self.log.info('Init local ssh client') + + def ssh(self, cmd): + ret = 0 + output = "%s failed!!!" % cmd + try: + output = subprocess.check_output((cmd), shell=True, + universal_newlines=True) + except subprocess.CalledProcessError: + ret = 1 + return ret, output + + def scp(self, src_file, dst_file): + return subprocess.check_output("cp %s %s" % (src_file, dst_file), + shell=True) + + def run_async(func): from threading import Thread from functools import wraps