Improvements to ssh remote client

* Added a ping check before trying to SSH
* Using servers config to determine timeouts
* (patch) Statically set ssh connect timeout to 20s.
  This should come from the config

Change-Id: Id8c7314079dd0af4b196111de0668ab230c28fde
This commit is contained in:
Daryl Walleck
2013-04-08 02:08:23 -05:00
parent cde272db05
commit 07abc90e19
3 changed files with 26 additions and 9 deletions

View File

@@ -28,7 +28,8 @@ class InstanceClientFactory(object):
'arch': 'LinuxClient', 'freebsd': 'FreeBSDClient'}
@classmethod
def get_instance_client(cls, ip_address, username, password, os_distro, server_id):
def get_instance_client(cls, ip_address=None, username=None, password=None,
os_distro=None, server_id=None, config=None):
"""
@summary: Returns utility class based on the OS type of server
@param ip_address: IP Address of the server
@@ -50,7 +51,7 @@ class InstanceClientFactory(object):
return instanceClient(ip_address=ip_address, username=username,
password=password, os_distro=os_distro,
server_id=server_id)
server_id=server_id, config=config)
class InstanceClient(object):
@@ -58,8 +59,14 @@ class InstanceClient(object):
@summary: Wrapper class around different operating system utilities.
"""
def __init__(self, ip_address, password, os_distro, username=None, server_id=None):
self._client = InstanceClientFactory.get_instance_client(ip_address, password, os_distro, username, server_id)
def __init__(self, ip_address=None, password=None, os_distro=None,
config=None, username=None, server_id=None):
self._client = InstanceClientFactory.get_instance_client(ip_address=ip_address,
password=password,
os_distro=os_distro,
username=username,
server_id=server_id,
config=config)
self.client_log = cclogging.getLogger(cclogging.get_object_namespace(self.__class__))
def can_authenticate(self):

View File

@@ -28,10 +28,11 @@ from cloudcafe.compute.common.exceptions import FileNotFoundException, ServerUnr
class LinuxClient(BasePersistentLinuxClient):
def __init__(self, ip_address, server_id, os_distro, username, password):
def __init__(self, ip_address=None, server_id=None, username=None,
password=None, config=None, os_distro=None):
self.client_log = cclogging.getLogger \
(cclogging.get_object_namespace(self.__class__))
ssh_timeout = 600
ssh_timeout = config.connection_timeout
if ip_address is None:
raise ServerUnreachable("None")
self.ip_address = ip_address
@@ -41,6 +42,15 @@ class LinuxClient(BasePersistentLinuxClient):
self.password = password
self.server_id = server_id
start = int(time.time())
reachable = False
while not reachable:
reachable = PingClient.ping(ip_address,
config.ip_address_version_for_ssh)
time.sleep(config.connection_retry_interval)
if int(time.time()) - start >= config.connection_timeout:
raise ServerUnreachable(ip_address)
self.ssh_client = SSHBaseClient(self.ip_address,
self.username,
self.password,

View File

@@ -59,13 +59,13 @@ class SSHBaseClient(BaseClient):
try:
if not log_attempted:
self._log.debug('Attempting to SSH connect to: ')
self._log.debug('host: %s, username: %s' %
(self.host, self.username))
self._log.debug('host: %s, username: %s, password: %s' %
(self.host, self.username, self.password))
log_attempted = True
ssh.connect(hostname=self.host,
username=self.username,
password=self.password,
timeout=self.timeout,
timeout=20,
key_filename=[],
look_for_keys=False,
allow_agent=False)