Exemplo n.º 1
0
def my_first_test_method(ip=None, username=None, password=None):
    """
    Description: This description will be displayed in the test run output and should
    explain this test's objectives, progression, any artifacts it creates/removes, dependencies,
    etc..
    This test attempts to ssh into a remote device (in practice this might be a VM/instance)
     and verify the end host is alive by executing 2 commands; 'hostname' and 'uptime', verifies
     the return code of the commands to be '0', and prints the output out at debug level.
    """
    ip = ip or  testcase.args.test_ip # this is the arg we added above
    password = password or testcase.args.instance_password # instance_password provided
    username = username or testcase.args.instance_user or 'root' # so is instance_user
    if not ip or not password:
        raise ValueError('Need ip and password to run this ssh test! ip={0}, password={1}'
                         .format(ip, password))
    # Create an ssh connection using the ip. Using a short timeout and no retry since this
    # is an example, otherwise defaults are usually good.
    ssh = SshConnection(host=ip, username=username, password=password, timeout=10, retry=0,
                        verbose=True)

    # SshConnection.sys() will execute a cmd on the remote system, 'code' will check the return
    # code of the command and throw an exception if it does not match the value provided.
    # By default the returned output is a list of lines, but can also be returned as a single
    # string buffer using 'listformat=False')
    # The command is monitored and the session will be torn down and a timeout exception will be
    # thrown if it does not return by 'timeout' seconds.
    output = ssh.sys('hostname && uptime', listformat=False, code=0, timeout=20)


    # default logger writes to stdout and it's debug level method can be used such as...
    testcase.debug('my_first_test_method passed yay!!')
    testcase.debug('Heres the output of our two commands:\n{0}'.format(output))
Exemplo n.º 2
0
    def do_ssh(self, q, lock, name, command):
        empty = False
        q = q or None
        while not empty:
            try:
                ssh = None
                logger = None
                self.logger.debug('Thread: {0}, in Q loop...'.format(name))
                host = None
                try:
                    host = q.get(timeout=self.maxwait)
                except Empty:
                    empty = True
                    break
                start = time.time()
                try:
                    self.logger.debug('Connecting to new host:' + str(host))
                    logger = Eulogger(str(host))
                    ssh = SshConnection(host=host, username=self.username, password=self.password,
                                        keypath=self.keypath, debug_connect=True,
                                        timeout=self.args.timeout, verbose=True, logger=logger)
                    logger.debug('host: {0} running command:{1} '.format(host, command))
                    out = ssh.cmd(str(command), listformat=True, timeout=self.args.timeout,
                                  get_pty=not(self.args.no_pty))
                    logger.debug('Done with host: {0}'.format(host))

                    with lock:
                        self.results[host] = {'status': out.get('status'),
                                              'output': out.get('output'),
                                              'elapsed': int(time.time() - start)}
                except Exception as E:
                    err = "{0}\n{1}".format(get_traceback(), E)
                    with lock:
                        self.results[host] = {'status': -1,
                                              'output': [err],
                                              'elapsed': int(time.time() - start)}
                finally:
                    logger.debug('Closing ssh to host: {0}'.format(host))
                    if ssh:
                        ssh.connection.close()
                        logger.debug('Closed ssh to host: {0}'.format(host))
                    try:
                        if logger:
                            logger.close()
                    except:
                        pass
            except Exception as SE:
                self.logger.error('{0}\nError in do_ssh:{0}'.format(get_traceback(), SE))
            finally:
                if q is not None and not empty:
                    q.task_done()
                self.logger.debug('Finished task in thread:{0}'.format(name))
        self.logger.debug('{0}: Done with thread'.format(name))
Exemplo n.º 3
0
 def test3_download(self):
     """
     Attempts to download the SOS reports from each host in the cloud and store in a local
     directory
     """
     error_msg = ""
     count = 0
     err_count = 0
     host_count = len(self.ip_list)
     for ip in self.ip_list:
         if self.tc:
             if ip in self.tc.sysadmin.eucahosts.keys():
                 host = self.tc.sysadmin.eucahosts.get(ip)
                 ssh = host.ssh
             else:
                 ssh = SshConnection(host=ip, password=self.args.password)
         try:
             remote_tarball_path = ssh.sys(
                 "ls -1 {0}/*.xz | grep {1}".format(self.remote_dir,
                                                    self.ticket_number),
                 code=0)[0]
             tarball_name = os.path.basename(remote_tarball_path)
             local_name = "sosreport-{0}.{1}{2}".format(
                 ip, self.ticket_number,
                 tarball_name.split(str(self.ticket_number))[1])
             local_tarball_path = os.path.join(self.args.local_dir,
                                               local_name)
             self.log.debug("Downloading file to: " + local_tarball_path)
             ssh.sftp_get(localfilepath=local_tarball_path,
                          remotefilepath=remote_tarball_path)
         except Exception, e:
             err_count += 1
             msg = '\nError Downloading from: {0}. Error:"{0}"\n'.format(
                 ip, e)
             self.log.error("{0}\n{1}".format(get_traceback(), msg))
             error_msg += msg
         else:
             count += 1
             self.log.info(
                 markup('Downloaded SOS report {0}/{1} to:{2}'.format(
                     count, host_count, local_tarball_path),
                        markups=[
                            ForegroundColor.WHITE, BackGroundColor.BG_GREEN
                        ]))
Exemplo n.º 4
0
 def test3_download(self):
     """
     Attempts to download the SOS reports from each host in the cloud and store in a local
     directory
     """
     error_msg = ""
     count = 0
     err_count = 0
     host_count = len(self.ip_list)
     for ip in self.ip_list:
         if self.tc:
             if ip in self.tc.sysadmin.eucahosts.keys():
                 host = self.tc.sysadmin.eucahosts.get(ip)
                 ssh = host.ssh
             else:
                 ssh = SshConnection(host=ip, password=self.args.password)
         try:
             remote_tarball_path = ssh.sys("ls -1 {0}/*.xz | grep {1}"
                                            .format(self.remote_dir, self.ticket_number),
                                            code=0)[0]
             tarball_name = os.path.basename(remote_tarball_path)
             local_name = "sosreport-{0}.{1}{2}".format(ip, self.ticket_number,
                                              tarball_name.split(str(self.ticket_number))[1])
             local_tarball_path = os.path.join(self.args.local_dir, local_name)
             self.log.debug("Downloading file to: " + local_tarball_path)
             ssh.sftp_get(localfilepath=local_tarball_path,
                               remotefilepath=remote_tarball_path)
         except Exception, e:
             err_count += 1
             msg = '\nError Downloading from: {0}. Error:"{0}"\n'.format(ip, e)
             self.log.error("{0}\n{1}".format(get_traceback(), msg))
             error_msg += msg
         else:
             count += 1
             self.log.info(markup('Downloaded SOS report {0}/{1} to:{2}'
                                  .format(count, host_count, local_tarball_path),
                                  markups=[ForegroundColor.WHITE, BackGroundColor.BG_GREEN]))
Exemplo n.º 5
0
    def do_ssh(self, q, lock, name, command):
        empty = False
        q = q or None
        while not empty:
            try:
                ssh = None
                logger = None
                self.logger.debug('Thread: {0}, in Q loop...'.format(name))
                host = None
                try:
                    host = q.get(timeout=self.maxwait)
                except Empty:
                    empty = True
                    break
                start = time.time()
                try:
                    self.logger.debug('Connecting to new host:' + str(host))
                    logger = Eulogger(str(host))
                    ssh = SshConnection(host=host,
                                        username=self.username,
                                        password=self.password,
                                        keypath=self.keypath,
                                        debug_connect=True,
                                        timeout=self.args.timeout,
                                        verbose=True,
                                        logger=logger)
                    logger.debug('host: {0} running command:{1} '.format(
                        host, command))
                    out = ssh.cmd(str(command),
                                  listformat=True,
                                  timeout=self.args.timeout)
                    logger.debug('Done with host: {0}'.format(host))

                    with lock:
                        self.results[host] = {
                            'status': out.get('status'),
                            'output': out.get('output'),
                            'elapsed': int(time.time() - start)
                        }
                except Exception as E:
                    err = "{0}\n{1}".format(get_traceback(), E)
                    with lock:
                        self.results[host] = {
                            'status': -1,
                            'output': [err],
                            'elapsed': int(time.time() - start)
                        }
                finally:
                    logger.debug('Closing ssh to host: {0}'.format(host))
                    if ssh:
                        ssh.connection.close()
                        logger.debug('Closed ssh to host: {0}'.format(host))
                    try:
                        if logger:
                            logger.close()
                    except:
                        pass
            except Exception as SE:
                self.logger.error('{0}\nError in do_ssh:{0}'.format(
                    get_traceback(), SE))
            finally:
                if q is not None and not empty:
                    q.task_done()
                self.logger.debug('Finished task in thread:{0}'.format(name))
        self.logger.debug('{0}: Done with thread'.format(name))
Exemplo n.º 6
0
    log_level = 'DEBUG'
logger = logging.getLogger('args.host')
logger.setLevel(getattr(logging, log_level.upper()))
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
    "%(asctime)s - %(levelname)s - %(name)s - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)

venv_dest = None
if args.virtualenv:
    venv_dest = os.path.join(args.destdir, args.virtualenv)
ssh = SshConnection(host=args.host,
                    password=args.password,
                    username=args.username,
                    logger=logger,
                    verbose=args.debug)


def remote_sys(cmd,
               code=0,
               timeout=60,
               list_format=True,
               verbose=args.debug,
               enable_debug=args.debug):
    return ssh.sys(cmd,
                   code=code,
                   timeout=timeout,
                   listformat=list_format,
                   verbose=verbose)