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:
@@ -28,7 +28,8 @@ class InstanceClientFactory(object):
|
|||||||
'arch': 'LinuxClient', 'freebsd': 'FreeBSDClient'}
|
'arch': 'LinuxClient', 'freebsd': 'FreeBSDClient'}
|
||||||
|
|
||||||
@classmethod
|
@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
|
@summary: Returns utility class based on the OS type of server
|
||||||
@param ip_address: IP Address of the server
|
@param ip_address: IP Address of the server
|
||||||
@@ -50,7 +51,7 @@ class InstanceClientFactory(object):
|
|||||||
|
|
||||||
return instanceClient(ip_address=ip_address, username=username,
|
return instanceClient(ip_address=ip_address, username=username,
|
||||||
password=password, os_distro=os_distro,
|
password=password, os_distro=os_distro,
|
||||||
server_id=server_id)
|
server_id=server_id, config=config)
|
||||||
|
|
||||||
|
|
||||||
class InstanceClient(object):
|
class InstanceClient(object):
|
||||||
@@ -58,8 +59,14 @@ class InstanceClient(object):
|
|||||||
@summary: Wrapper class around different operating system utilities.
|
@summary: Wrapper class around different operating system utilities.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, ip_address, password, os_distro, username=None, server_id=None):
|
def __init__(self, ip_address=None, password=None, os_distro=None,
|
||||||
self._client = InstanceClientFactory.get_instance_client(ip_address, password, os_distro, username, server_id)
|
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__))
|
self.client_log = cclogging.getLogger(cclogging.get_object_namespace(self.__class__))
|
||||||
|
|
||||||
def can_authenticate(self):
|
def can_authenticate(self):
|
||||||
|
|||||||
@@ -28,10 +28,11 @@ from cloudcafe.compute.common.exceptions import FileNotFoundException, ServerUnr
|
|||||||
|
|
||||||
class LinuxClient(BasePersistentLinuxClient):
|
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 \
|
self.client_log = cclogging.getLogger \
|
||||||
(cclogging.get_object_namespace(self.__class__))
|
(cclogging.get_object_namespace(self.__class__))
|
||||||
ssh_timeout = 600
|
ssh_timeout = config.connection_timeout
|
||||||
if ip_address is None:
|
if ip_address is None:
|
||||||
raise ServerUnreachable("None")
|
raise ServerUnreachable("None")
|
||||||
self.ip_address = ip_address
|
self.ip_address = ip_address
|
||||||
@@ -41,6 +42,15 @@ class LinuxClient(BasePersistentLinuxClient):
|
|||||||
self.password = password
|
self.password = password
|
||||||
self.server_id = server_id
|
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.ssh_client = SSHBaseClient(self.ip_address,
|
||||||
self.username,
|
self.username,
|
||||||
self.password,
|
self.password,
|
||||||
|
|||||||
@@ -59,13 +59,13 @@ class SSHBaseClient(BaseClient):
|
|||||||
try:
|
try:
|
||||||
if not log_attempted:
|
if not log_attempted:
|
||||||
self._log.debug('Attempting to SSH connect to: ')
|
self._log.debug('Attempting to SSH connect to: ')
|
||||||
self._log.debug('host: %s, username: %s' %
|
self._log.debug('host: %s, username: %s, password: %s' %
|
||||||
(self.host, self.username))
|
(self.host, self.username, self.password))
|
||||||
log_attempted = True
|
log_attempted = True
|
||||||
ssh.connect(hostname=self.host,
|
ssh.connect(hostname=self.host,
|
||||||
username=self.username,
|
username=self.username,
|
||||||
password=self.password,
|
password=self.password,
|
||||||
timeout=self.timeout,
|
timeout=20,
|
||||||
key_filename=[],
|
key_filename=[],
|
||||||
look_for_keys=False,
|
look_for_keys=False,
|
||||||
allow_agent=False)
|
allow_agent=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user