Skip to content

Commit

Permalink
removing obsolete code from utils.py, ssh.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekkumac committed Jan 17, 2017
1 parent 2788748 commit 7d18e44
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 557 deletions.
5 changes: 2 additions & 3 deletions acktools/tests/acktools_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ class MockProcess:

def __init__(self, output, err=None):
self.output = output
if err:
self.returncode = 1 if err else 0
self.__stderr = err
self.returncode = 1 if err else 0
self.__stderr = err

def stderr(self):
return self.__stderr
Expand Down
326 changes: 0 additions & 326 deletions autocertkit/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,6 @@

SSHPORT = 22

# Symbols we want to export from the package.
__all__ = ["SSHSession",
"SFTPSession",
"SSHCommand",
"SSH",
"SSHread",
"getPublicKey"]


def getPublicKey():
filename = ".ssh/id_dsa.pub"
f = file(filename, "r")
data = f.read()
f.close()
return string.strip(data)


class SSHSession:

Expand Down Expand Up @@ -169,230 +153,6 @@ def __del__(self):
self.close()


class SFTPSession(SSHSession):
"""An SFTP session guarded for target lockups."""

def __init__(self,
ip,
log,
username="root",
timeout=300,
password=None,
nowarn=False):
self.log = log
self.log.debug("SFTP session to %s@%s" % (username, ip))
self.ip = ip
self.username = username
self.timeout = timeout
self.password = password
self.nowarn = nowarn
SSHSession.__init__(self,
ip,
log,
username=username,
timeout=timeout,
password=password,
nowarn=nowarn)
try:
# We do this rather than the simple trans.open_sftp_client() because
# if we don't then we don't get a timeout set so we can hang
# forever
c = self.trans.open_channel("session")
c.settimeout(timeout)
c.invoke_subsystem("sftp")
self.client = paramiko.SFTPClient(c)
except:
self.reply = "SFTP connection failed"
self.toreply = 1
self.close()

def getClient(self):
# This is UNSAFE - the client object may change if we auto reconnect!
return self.client

def check(self):
# Check if the connection is still active, if not, try and re-open the
# connection (this handles the case where the connection has dropped
# due to a transient network error)...

alive = True

# First see if the transport is alive
if not self.trans.is_active():
alive = False
else:
try:
d = self.client.listdir()
except:
alive = False

if not alive:
log.warn(
"SFTP session appears to have gone away, attempting to reconnect...")
self.__init__(self.ip,
self.log,
username=self.username,
timeout=self.timeout,
password=self.password,
nowarn=self.nowarn)

def close(self):
if self.client:
try:
self.client.close()
except Exception, e:
log.debug("SFTP close exception %s" % (str(e)))
if self.trans:
try:
self.trans.close()
except Exception, e:
log.debug("SFTP trans close exception %s" % (str(e)))

def copyTo(self, source, dest, preserve=True):
log.debug("SFTP local:%s to remote:%s" % (source, dest))
self.client.put(source, dest)
if preserve:
st = os.lstat(source)
if preserve == True:
self.client.chmod(dest, st.st_mode)
self.client.utime(dest, (st.st_atime, st.st_mtime))

def copyFrom(self, source, dest, preserve=True, threshold=None,
sizethresh=None):
log.debug("SFTP remote:%s to local:%s" % (source, dest))
self.check()
st = self.client.stat(source)
if threshold and st.st_mtime < threshold:
log.debug("Skipping %s, too old" % (source))
return
elif sizethresh and st.st_size > long(sizethresh):
log.debug("Skipping %s, too big (%u)" %
(source, st.st_size))
return
self.client.get(source, dest)
if preserve:
if preserve == True:
os.chmod(dest, st.st_mode)
os.utime(dest, (st.st_atime, st.st_mtime))

def copyTreeTo(self, source, dest, preserve=True):
"""Recursive copy to the remote host
source: local directory being root of the tree
dest: remote directory to be the new root of the tree
"""
log.debug("SFTP recursive local:%s to remote:%s" %
(source, dest))
self.check()
source = os.path.normpath(source)
dirs = os.walk(source)
for dir in dirs:
(dirname, dirnames, filenames) = dir
# Create the remote directory
dirname = os.path.normpath(dirname)
relpath = dirname[len(source):]
if len(relpath) > 0 and relpath[0] == "/":
relpath = relpath[1:]
targetpath = os.path.normpath(os.path.join(dest, relpath))
try:
self.client.lstat(targetpath)
# Already exists
if preserve == True:
self.client.chmod(targetpath, os.lstat(dirname).st_mode)
except IOError, e:
self.client.mkdir(targetpath, os.lstat(dirname).st_mode)
# Copy all the files in
for file in filenames:
srcfile = os.path.join(dirname, file)
dstfile = os.path.join(targetpath, file)
st = os.lstat(srcfile)
self.client.put(srcfile, dstfile)
if preserve:
if preserve == True:
self.client.chmod(dstfile, st.st_mode)
self.client.utime(dstfile, (st.st_atime, st.st_mtime))

def copyTreeFromRecurse(self, source, dest, preserve=True, threshold=None,
sizethresh=None):
# make sure local destination exists
if not os.path.exists(dest):
os.makedirs(dest)
if preserve:
os.chmod(dest, self.client.lstat(source).st_mode)
d = self.client.listdir(source)
for i in d:
try:
dummy = self.client.listdir("%s/%s" % (source, i))
isdir = True
except:
isdir = False
if isdir:
self.copyTreeFromRecurse("%s/%s" % (source, i),
"%s/%s" % (dest, i),
preserve=preserve,
threshold=threshold,
sizethresh=sizethresh)
else:
log.debug("About to copy %s/%s" % (source, i))
st = self.client.stat("%s/%s" % (source, i))
if threshold and st.st_mtime < threshold:
log.debug("Skipping %s/%s, too old" %
(source, i))
elif sizethresh and st.st_size > long(sizethresh):
log.debug("Skipping %s/%s, too big (%u)" %
(source, i, st.st_size))
else:
self.client.get("%s/%s" % (source, i),
"%s/%s" % (dest, i))
if preserve:
if preserve == True:
os.chmod("%s/%s" % (dest, i), st.st_mode)
os.utime("%s/%s" % (dest, i),
(st.st_atime, st.st_mtime))

def copyTreeFrom(self, source, dest, preserve=True, threshold=None,
sizethresh=None):
"""Recursive copy from the remote host
source: remote directory being root of the tree
dest: local directory to be the new root of the tree
"""
log.debug("SFTP recursive remote:%s to local:%s" %
(source, dest))
self.check()
self.copyTreeFromRecurse(source,
dest,
preserve=preserve,
threshold=threshold,
sizethresh=sizethresh)

def copyLogsFrom(self, pathlist, dest, threshold=None, sizethresh=None):
"""Copy any files or directory trees from pathlist remotely to
dest locally"""
log.debug("SFTP log fetch of %s to local:%s" %
(`pathlist`, dest))
for p in pathlist:
# Directory?
log.debug("Trying to fetch %s." % (p))
try:
d = self.client.listdir(p)
self.copyTreeFrom(p, "%s/%s" % (dest, os.path.basename(p)),
preserve="utime", threshold=threshold,
sizethresh=sizethresh)
except:
# File?
try:
s = self.client.lstat(p)
self.copyFrom(p, "%s/%s" % (dest, os.path.basename(p)),
preserve="utime", threshold=threshold,
sizethresh=sizethresh)
except:
pass

def __del__(self):
SSHSession.__del__(self)


class SSHCommand(SSHSession):
"""An SSH session guarded for target lockups."""

Expand Down Expand Up @@ -486,89 +246,3 @@ def read(self, retval="code", fh=None):

def __del__(self):
SSHSession.__del__(self)


def SSH(ip,
command,
username="root",
timeout=300,
retval="code",
password=None,
idempotent=False,
nowarn=False,
newlineok=False,
getreply=True,
nolog=False,
outfile=None):
tries = 0
while True:
tries = tries + 1
log.debug("SSH %s@%s %s (attempt %u)" %
(username, ip, command, tries))
try:
s = SSHCommand(ip,
command,
log,
username=username,
timeout=timeout,
password=password,
nowarn=nowarn,
newlineok=newlineok,
nolog=nolog)
if outfile:
try:
f = file(outfile, 'w')
reply = s.read(retval="code", fh=f)
finally:
f.close()
return reply
elif getreply:
reply = s.read(retval=retval)
return reply
else:
return None
except Exception, e:
if tries >= 3 or not idempotent:
raise e
if string.find(str(e), "SSH command exited with error") > -1:
raise e
if not nowarn:
log.debug("Retrying ssh connection %s@%s %s after %s"
% (username, ip, command, str(e)))
time.sleep(5)


def SSHread(ip,
command,
log,
username="root",
timeout=300,
password=None,
idempotent=False,
nowarn=False,
newlineok=False):
tries = 0
while True:
tries = tries + 1
log.debug("SSH %s@%s %s (attempt %u)" %
(username, ip, command, tries))
try:
s = SSHCommand(ip,
command,
log,
username=username,
timeout=timeout,
password=password,
nowarn=nowarn,
newlineok=newlineok)
reply = s.read(retval="string")
return reply
except Exception, e:
if tries >= 3 or not idempotent:
raise e
if string.find(str(e), "SSH command exited with error") > -1:
raise e
if not nowarn:
log.debug("Retrying ssh connection %s@%s %s after %s"
% (username, ip, command, str(e)))
time.sleep(5)
3 changes: 2 additions & 1 deletion autocertkit/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def wrap_text(string, width):

def print_system_info(stream):
""" Retrieve system information from SMBIOS and write to given stream. """
sys_info = search_dmidecode("System Information")
session = get_local_xapi_session()
sys_info = search_dmidecode(session, "System Information")
stream.write("#########################\n")
stream.write("System Information from SMBIOS\n")
stream.write('\n'.join(sys_info))
Expand Down
Loading

0 comments on commit 7d18e44

Please sign in to comment.