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 ]))
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))
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)