示例#1
0
    def __init__(self,
                 ip_address,
                 username='******',
                 password=None,
                 key=None,
                 connection_timeout=600,
                 retry_interval=10):
        self.client_log = cclogging.getLogger(
            cclogging.get_object_namespace(self.__class__))

        # Verify the IP address has a valid format
        try:
            IP(ip_address)
        except ValueError:
            raise ServerUnreachable(ip_address)

        if not self._is_instance_reachable(ip_address=ip_address,
                                           retry_interval=retry_interval,
                                           timeout=connection_timeout):
            raise ServerUnreachable(ip_address)

        self.ip_address = ip_address
        self.username = username
        self.password = password

        self.client = WinRMClient(username=username,
                                  password=password,
                                  host=ip_address)
        self.client.connect_with_retries()
    def __init__(self,
                 ip_address=None,
                 server_id=None,
                 username=None,
                 password=None,
                 config=None,
                 os_distro=None,
                 key=None):
        self.client_log = cclogging.getLogger(
            cclogging.get_object_namespace(self.__class__))

        ssh_timeout = config.connection_timeout
        if ip_address is None:
            raise ServerUnreachable("None")
        self.ip_address = ip_address
        self.username = username
        if self.username is None:
            self.username = '******'
        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)

        if key is not None:
            auth_strategy = SSHAuthStrategy.KEY_STRING
        else:
            auth_strategy = SSHAuthStrategy.PASSWORD

        self.ssh_client = SSHBehaviors(username=self.username,
                                       password=self.password,
                                       host=self.ip_address,
                                       tcp_timeout=20,
                                       auth_strategy=auth_strategy,
                                       look_for_keys=False,
                                       key=key)
        self.ssh_client.connect_with_timeout(cooldown=20, timeout=ssh_timeout)
        if not self.ssh_client.is_connected():
            message = ('SSH timeout after {timeout} seconds: '
                       'Could not connect to {ip_address}.')
            raise SshConnectionException(
                message.format(timeout=ssh_timeout, ip_address=ip_address))
示例#3
0
    def __init__(self,
                 ip_address=None,
                 server_id=None,
                 username=None,
                 password=None,
                 config=None,
                 os_distro=None,
                 key=None):
        self.client_log = cclogging.getLogger(
            cclogging.get_object_namespace(self.__class__))

        ssh_timeout = config.connection_timeout
        if ip_address is None:
            raise ServerUnreachable("None")
        self.ip_address = ip_address
        self.username = username
        if self.username is None:
            self.username = '******'
        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,
                                        timeout=ssh_timeout,
                                        key=key)
        if not self.ssh_client.test_connection_auth():
            self.client_log.error("Ssh connection failed for: IP:{0} \
                    Username:{1} Password: {2}".format(self.ip_address,
                                                       self.username,
                                                       self.password))
            raise SshConnectionException("ssh connection failed")
示例#4
0
    def __init__(self, ip_address=None, username='******', password=None,
                 key=None, connection_timeout=600, retry_interval=10):
        self.client_log = cclogging.getLogger(
            cclogging.get_object_namespace(self.__class__))

        if ip_address is None:
            raise ServerUnreachable("None")
        self.ip_address = ip_address
        self.username = username
        self.password = password
        self.connection_timeout = connection_timeout

        # Verify the server can be pinged before attempting to connect
        start = int(time.time())
        reachable = False
        while not reachable:
            reachable = PingClient.ping(ip_address)
            if reachable:
                break
            time.sleep(retry_interval)
            if int(time.time()) - start >= connection_timeout:
                raise ServerUnreachable(ip_address)

        if key is not None:
            auth_strategy = SSHAuthStrategy.KEY_STRING
        else:
            auth_strategy = SSHAuthStrategy.PASSWORD

        self.ssh_client = SSHClient(
            username=self.username, password=self.password,
            host=self.ip_address, tcp_timeout=20, auth_strategy=auth_strategy,
            look_for_keys=False, key=key)
        self.ssh_client.connect_with_timeout(
            cooldown=20, timeout=connection_timeout)
        if not self.ssh_client.is_connected():
            message = ('SSH timeout after {timeout} seconds: '
                       'Could not connect to {ip_address}.')
            raise SshConnectionException(message.format(
                timeout=connection_timeout, ip_address=ip_address))
示例#5
0
    def verify_server_reachable(cls, ip=None):
        """
        @summary: Verify server connectivity with a basic ping check.
        @param ip: IP address to ping.
                   Defaults to SSH IPv4 address if no IP is provided
        @type ip: string
        """

        if not ip:
            ip = cls.server.addresses.get_by_name(
                cls.servers_config.network_for_ssh).ipv4
        if not PingClient.ping(ip):
            raise ServerUnreachable(
                "Server {id} was not pingable at {ip}".format(id=cls.server.id,
                                                              ip=ip))
示例#6
0
    def get_remote_instance_client(self,
                                   server,
                                   config=None,
                                   ip_address=None,
                                   username=None,
                                   password=None,
                                   key=None,
                                   auth_strategy=None):
        """
        @summary: Gets an client of the server
        @param server: Instance uuid id of the server
        @type server: String
        @param ip_address: IPv4 address of the server
        @type ip_address: String
        @param username: Valid user of the server
        @type username: String
        @param password: Valid user password of the server
        @type password: String
        @return: Either IPv4 or IPv6 address of instance
        @rtype: String
        """
        if ip_address is None:
            network = server.addresses.get_by_name(config.network_for_ssh)
            if config.ip_address_version_for_ssh == 4:
                ip_address = network.ipv4
            elif config.ip_address_version_for_ssh == 6:
                ip_address = network.ipv6

        # Get Server Image ID
        if server.image:
            image_id = server.image.id
        else:
            image_id = self.images_config.primary_image

        # Get the Server Image
        image = self.images_client.get_image(image_id).entity

        if image.metadata.get('os_type', '').lower() == 'windows':
            # Importing just in time in case WinRM plugin is not installed
            # (todo) dwalleck: Handle this more cleanly
            from cloudcafe.compute.common.clients.remote_instance.windows.\
                windows_client import WindowsClient
            client = WindowsClient
        else:
            # Importing just in time in case SSH plugin is not installed
            # (todo) dwalleck: Handle this more cleanly
            from cloudcafe.compute.common.clients.remote_instance.linux.\
                linux_client import LinuxClient
            client = LinuxClient

        user = self.images_config.primary_image_default_user
        strategy = auth_strategy or self.config.instance_auth_strategy.lower()

        try:
            if InstanceAuthStrategies.PASSWORD in strategy:
                if password is None:
                    password = server.admin_pass
                return client(
                    ip_address=ip_address,
                    username=user,
                    password=password,
                    connection_timeout=self.config.connection_timeout)
            else:
                return client(
                    ip_address=ip_address,
                    username=user,
                    key=key,
                    connection_timeout=self.config.connection_timeout)
        except ServerUnreachable:
            raise ServerUnreachable(
                'Unable to ping server {id} at address {address} '
                'within the allowed time of {timeout} seconds. '
                'Test unable to proceed.'.format(
                    id=server.id,
                    address=ip_address,
                    timeout=self.config.connection_timeout))
        except SshConnectionException:
            raise SshConnectionException(
                'Able to ping server {id} at {address}, but unable to '
                'connect via ssh within the allowed time of {timeout} '
                'seconds. Test unable to proceed.'.format(
                    id=server.id,
                    address=ip_address,
                    timeout=self.config.connection_timeout))