Refactoring remote client to use new ping methods
* Removed obsolete _is_instance_reachable method * Updated connectivity checks in Windows and Linux remote client code to use new ping_until_reachable method * Added InvalidAddressFormat exception Change-Id: I588e26a16e8f9b829c48177a35ff8d816f97bcf9
This commit is contained in:
		| @@ -1,10 +1,7 @@ | |||||||
| import abc | import abc | ||||||
| import time |  | ||||||
|  |  | ||||||
| from cafe.engine.clients.base import BaseClient | from cafe.engine.clients.base import BaseClient | ||||||
|  |  | ||||||
| from cloudcafe.compute.common.clients.ping import PingClient |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class RemoteInstanceClient(BaseClient): | class RemoteInstanceClient(BaseClient): | ||||||
|     __metaclass__ = abc.ABCMeta |     __metaclass__ = abc.ABCMeta | ||||||
| @@ -60,16 +57,3 @@ class RemoteInstanceClient(BaseClient): | |||||||
|     def can_authenticate(self): |     def can_authenticate(self): | ||||||
|         """Verifies a remote connection can be made to the server.""" |         """Verifies a remote connection can be made to the server.""" | ||||||
|         raise NotImplementedError |         raise NotImplementedError | ||||||
|  |  | ||||||
|     @staticmethod |  | ||||||
|     def _is_instance_reachable(ip_address, retry_interval, timeout): |  | ||||||
|         """Verify the server can be pinged.""" |  | ||||||
|         start = int(time.time()) |  | ||||||
|         reachable = False |  | ||||||
|         while not reachable: |  | ||||||
|             reachable = PingClient.ping(ip_address) |  | ||||||
|             if reachable: |  | ||||||
|                 return True |  | ||||||
|             time.sleep(retry_interval) |  | ||||||
|             if int(time.time()) - start >= timeout: |  | ||||||
|                 return False |  | ||||||
|   | |||||||
| @@ -18,8 +18,7 @@ import os | |||||||
| import re | import re | ||||||
| import time | import time | ||||||
|  |  | ||||||
| from cloudcafe.common.tools import datagen | from IPy import IP | ||||||
|  |  | ||||||
| from cafe.common.reporting import cclogging | from cafe.common.reporting import cclogging | ||||||
| from cafe.engine.clients.remote_instance.exceptions \ | from cafe.engine.clients.remote_instance.exceptions \ | ||||||
|     import DirectoryNotFoundException |     import DirectoryNotFoundException | ||||||
| @@ -28,11 +27,12 @@ from cafe.engine.clients.remote_instance.models.dir_details \ | |||||||
| from cafe.engine.clients.remote_instance.models.file_details \ | from cafe.engine.clients.remote_instance.models.file_details \ | ||||||
|     import FileDetails |     import FileDetails | ||||||
| from cafe.engine.ssh.client import SSHAuthStrategy, SSHClient | from cafe.engine.ssh.client import SSHAuthStrategy, SSHClient | ||||||
|  | from cloudcafe.common.tools import datagen | ||||||
| from cloudcafe.compute.common.clients.ping import PingClient | from cloudcafe.compute.common.clients.ping import PingClient | ||||||
| from cloudcafe.compute.common.clients.remote_instance.base_client import \ | from cloudcafe.compute.common.clients.remote_instance.base_client import \ | ||||||
|     RemoteInstanceClient |     RemoteInstanceClient | ||||||
| from cloudcafe.compute.common.exceptions import FileNotFoundException, \ | from cloudcafe.compute.common.exceptions import FileNotFoundException, \ | ||||||
|     ServerUnreachable, SshConnectionException |     SshConnectionException, InvalidAddressFormat | ||||||
| from cloudcafe.common.tools.md5hash import get_md5_hash | from cloudcafe.common.tools.md5hash import get_md5_hash | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -43,25 +43,21 @@ class LinuxClient(RemoteInstanceClient): | |||||||
|         self.client_log = cclogging.getLogger( |         self.client_log = cclogging.getLogger( | ||||||
|             cclogging.get_object_namespace(self.__class__)) |             cclogging.get_object_namespace(self.__class__)) | ||||||
|  |  | ||||||
|         if ip_address is None: |  | ||||||
|             raise ServerUnreachable("None") |  | ||||||
|         self.ip_address = ip_address |         self.ip_address = ip_address | ||||||
|         self.username = username |         self.username = username | ||||||
|         self.password = password |         self.password = password | ||||||
|         self.connection_timeout = connection_timeout |         self.connection_timeout = connection_timeout | ||||||
|  |  | ||||||
|  |         # Verify the IP address has a valid format | ||||||
|  |         try: | ||||||
|  |             IP(ip_address) | ||||||
|  |         except ValueError: | ||||||
|  |             raise InvalidAddressFormat(ip_address) | ||||||
|  |  | ||||||
|         # Verify the server can be pinged before attempting to connect |         # Verify the server can be pinged before attempting to connect | ||||||
|         start = int(time.time()) |         PingClient.ping_until_reachable(ip_address, | ||||||
|         reachable = False |                                         timeout=connection_timeout, | ||||||
|         while not reachable: |                                         interval_time=retry_interval) | ||||||
|             reachable = PingClient.ping(ip_address) |  | ||||||
|             if reachable: |  | ||||||
|                 break |  | ||||||
|             time.sleep(retry_interval) |  | ||||||
|             if int(time.time()) - start >= connection_timeout: |  | ||||||
|                 raise ServerUnreachable( |  | ||||||
|                     'Could not reach the server at {ip_address}'.format( |  | ||||||
|                         ip_address=ip_address)) |  | ||||||
|  |  | ||||||
|         if key is not None: |         if key is not None: | ||||||
|             auth_strategy = SSHAuthStrategy.KEY_STRING |             auth_strategy = SSHAuthStrategy.KEY_STRING | ||||||
|   | |||||||
| @@ -18,17 +18,18 @@ from dateutil.parser import parse | |||||||
| import re | import re | ||||||
|  |  | ||||||
| from IPy import IP | from IPy import IP | ||||||
|  |  | ||||||
| from cafe.engine.clients.winrm_client import WinRMClient | from cafe.engine.clients.winrm_client import WinRMClient | ||||||
| from cafe.common.reporting import cclogging | from cafe.common.reporting import cclogging | ||||||
| from cafe.engine.clients.remote_instance.models.dir_details \ | from cafe.engine.clients.remote_instance.models.dir_details \ | ||||||
|     import DirectoryDetails |     import DirectoryDetails | ||||||
| from cafe.engine.clients.remote_instance.models.file_details \ | from cafe.engine.clients.remote_instance.models.file_details \ | ||||||
|     import FileDetails |     import FileDetails | ||||||
|  |  | ||||||
|  | from cloudcafe.compute.common.clients.ping import PingClient | ||||||
| from cloudcafe.compute.common.clients.remote_instance.base_client import \ | from cloudcafe.compute.common.clients.remote_instance.base_client import \ | ||||||
|     RemoteInstanceClient |     RemoteInstanceClient | ||||||
| from cloudcafe.compute.common.exceptions import ServerUnreachable, \ | from cloudcafe.compute.common.exceptions import WinRMConnectionException, \ | ||||||
|     WinRMConnectionException |     InvalidAddressFormat | ||||||
|  |  | ||||||
|  |  | ||||||
| class WindowsClient(RemoteInstanceClient): | class WindowsClient(RemoteInstanceClient): | ||||||
| @@ -45,14 +46,12 @@ class WindowsClient(RemoteInstanceClient): | |||||||
|         try: |         try: | ||||||
|             IP(ip_address) |             IP(ip_address) | ||||||
|         except ValueError: |         except ValueError: | ||||||
|             raise ServerUnreachable(ip_address) |             raise InvalidAddressFormat(ip_address) | ||||||
|  |  | ||||||
|         if not self._is_instance_reachable( |         # Verify the server can be pinged before attempting to connect | ||||||
|                 ip_address=ip_address, retry_interval=retry_interval, |         PingClient.ping_until_reachable(ip_address, | ||||||
|                 timeout=connection_timeout): |                                         timeout=connection_timeout, | ||||||
|             raise ServerUnreachable( |                                         interval_time=retry_interval) | ||||||
|                 'Could not reach the server at {ip_address}'.format( |  | ||||||
|                     ip_address=ip_address)) |  | ||||||
|  |  | ||||||
|         self.ip_address = ip_address |         self.ip_address = ip_address | ||||||
|         self.username = username |         self.username = username | ||||||
|   | |||||||
| @@ -180,6 +180,16 @@ class ServerUnreachable(Exception): | |||||||
|         return repr(self.message) |         return repr(self.message) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class InvalidAddressFormat(Exception): | ||||||
|  |     def __init__(self, ip_address): | ||||||
|  |         self.message = ( | ||||||
|  |             'Invalid IP address provided: {ip_address}').format( | ||||||
|  |                 ip_address=ip_address) | ||||||
|  |  | ||||||
|  |     def __str__(self): | ||||||
|  |         return repr(self.message) | ||||||
|  |  | ||||||
|  |  | ||||||
| class InvalidJSON(Exception): | class InvalidJSON(Exception): | ||||||
|     def __init__(self, message, expected_response): |     def __init__(self, message, expected_response): | ||||||
|         self.message = ('Unexpected JSON response. ' |         self.message = ('Unexpected JSON response. ' | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Levi Blackstone
					Levi Blackstone