def __init__(self, cluster_id, rdx_ip, password, chap_user, chap_password):
        logger.info("ReduxioStorageDriverAPI Initializing")

        self._cluster_id = cluster_id
        self._rdx_ip = rdx_ip
        self._password = password
        self._chap_user = chap_user
        self._chap_password = chap_password

        logger.debug("Initializing {}; {};".format(self._cluster_id, self._rdx_ip))

        self._rdxapi = ReduxioAPI(host=rdx_ip, user="******", password=password)
        self._rdxhelper = RdxHelper()

        self.initiator_name = get_initiator_name()
        logger.debug("Initiator name is {} .".format(self.initiator_name))
    def detach_volume(self, blockdevice_id):
        """Detach ``blockdevice_id`` from whatever host it is attached to.

        :param unicode blockdevice_id: The unique identifier for the block
            device being detached.
        :raises UnknownVolume: If the supplied ``blockdevice_id`` does not
            exist.
        :raises UnattachedVolume: If the supplied ``blockdevice_id`` is
            not attached to anything.
        :returns: ``None``
        """
        LOG.info("Detaching %s", blockdevice_id)

        with self._client.open_connection() as api:
            # Check that we have that volume
            scvolume = api.find_volume(blockdevice_id)
            if not scvolume:
                raise blockdevice.UnknownVolume(blockdevice_id)

            # First check if we are mapped
            mappings = api.find_mapping_profiles(scvolume)
            if not mappings:
                raise blockdevice.UnattachedVolume(blockdevice_id)

            device_id = scvolume["deviceId"]
            paths = iscsi_utils.find_paths(device_id)
            paths.reverse()
            for path in paths:
                iscsi_utils.remove_device(path)

            # Make sure we have a server defined for this host
            iqn = iscsi_utils.get_initiator_name()
            host = api.find_server(iqn)
            LOG.info("Search for server returned: %s", host)
            if not host:
                # Try to create a new host
                host = api.create_server(self.compute_instance_id(), iqn)
            LOG.info("Created server %s", host)

            # Make sure we were able to find something
            if not host:
                raise BlockDriverAPIException("Unable to locate server.")

            api.unmap_volume(scvolume, host)
        self._do_rescan("detach")
    def detach_volume(self, blockdevice_id):
        """Detach ``blockdevice_id`` from whatever host it is attached to.

        :param unicode blockdevice_id: The unique identifier for the block
            device being detached.
        :raises UnknownVolume: If the supplied ``blockdevice_id`` does not
            exist.
        :raises UnattachedVolume: If the supplied ``blockdevice_id`` is
            not attached to anything.
        :returns: ``None``
        """
        LOG.info('Detaching %s', blockdevice_id)

        with self._client.open_connection() as api:
            # Check that we have that volume
            scvolume = api.find_volume(blockdevice_id)
            if not scvolume:
                raise blockdevice.UnknownVolume(blockdevice_id)

            # First check if we are mapped
            mappings = api.find_mapping_profiles(scvolume)
            if not mappings:
                raise blockdevice.UnattachedVolume(blockdevice_id)

            device_id = scvolume['deviceId']
            paths = iscsi_utils.find_paths(device_id)
            paths.reverse()
            for path in paths:
                iscsi_utils.remove_device(path)

            # Make sure we have a server defined for this host
            iqn = iscsi_utils.get_initiator_name()
            host = api.find_server(iqn)
            LOG.info("Search for server returned: %s", host)
            if not host:
                # Try to create a new host
                host = api.create_server(self.compute_instance_id(), iqn)
            LOG.info("Created server %s", host)

            # Make sure we were able to find something
            if not host:
                raise BlockDriverAPIException('Unable to locate server.')

            api.unmap_volume(scvolume, host)
        self._do_rescan('detach')
Exemple #4
0
    rdx_password = kwargs[u'password']

    chap_password = None
    chap_user = None
    if Validations()._is_chap_enabled(args=kwargs):
        chap_user = kwargs[u'chap_user']
        chap_password = kwargs[u'chap_password']
    return reduxio_init_from_configuration(cluster_id=cluster_id,
                                           rdx_ip=rdx_ip,
                                           password=rdx_password,
                                           chap_user=chap_user,
                                           chap_password=chap_password)


try:
    get_initiator_name()
except:
    logger.error(
        'Unable to get initiator-name, please ensure the relevant package is installed.'
    )
    raise Exception('Unable to get initiator-name.')

try:
    is_multipath_tools_installed()
except:
    logger.error(
        'Error running multipath, please ensure the relevant package is installed.'
    )
    raise Exception('Error running multipath.')

try:
    def attach_volume(self, blockdevice_id, attach_to):
        """Attach an existing volume to an initiator.

        :param blockdevice_id: The unique identifier for the volume.
        :param attach_to: An identifier like the one returned by the
            ``compute_instance_id`` method indicating the node to which to
            attach the volume.

        :raises UnknownVolume: If the supplied ``blockdevice_id`` does not
            exist.
        :returns: A ``BlockDeviceVolume`` with a ``attached_to`` attribute set
            to ``attach_to``.
        """
        LOG.info("Attaching %s to %s", blockdevice_id, attach_to)

        # Functional tests expect a failure if it's already
        # attached, even if we're being asked to attach to
        # the same host.
        # not_local = attach_to != self.compute_instance_id()
        not_local = True

        with self._client.open_connection() as api:
            # Check that we have that volume
            scvolume = api.find_volume(blockdevice_id)
            if not scvolume:
                raise blockdevice.UnknownVolume(blockdevice_id)

            # Make sure we have a server defined for this host
            iqn = iscsi_utils.get_initiator_name()
            host = api.find_server(iqn)
            LOG.info("Search for server returned: %s", host)
            if not host:
                # Try to create a new host
                host = api.create_server(attach_to, iqn)
                LOG.info("Created server %s", host)
            # Make sure the server is logged in to the array
            ports = api.get_iscsi_ports()
            for port in ports:
                iscsi_utils.iscsi_login(port[0], port[1])

            # Make sure we were able to find something
            if not host:
                raise BlockDriverAPIException()

            # First check if we are already mapped
            mappings = api.find_mapping_profiles(scvolume)
            if mappings:
                # See if it is to this server
                if not_local:
                    raise blockdevice.AlreadyAttachedVolume(blockdevice_id)
                for mapping in mappings:
                    if mapping["server"]["instanceName"] != host["instanceName"]:
                        raise blockdevice.AlreadyAttachedVolume(blockdevice_id)

            mapping = api.map_volume(scvolume, host)
            if not mapping:
                raise BlockDriverAPIException("Unable to map volume to server.")

            self._do_rescan("attach")

            return self._to_blockdevicevolume(scvolume, attach_to)
    def attach_volume(self, blockdevice_id, attach_to):
        """Attach an existing volume to an initiator.

        :param blockdevice_id: The unique identifier for the volume.
        :param attach_to: An identifier like the one returned by the
            ``compute_instance_id`` method indicating the node to which to
            attach the volume.

        :raises UnknownVolume: If the supplied ``blockdevice_id`` does not
            exist.
        :returns: A ``BlockDeviceVolume`` with a ``attached_to`` attribute set
            to ``attach_to``.
        """
        LOG.info('Attaching %s to %s', blockdevice_id, attach_to)

        # Functional tests expect a failure if it's already
        # attached, even if we're being asked to attach to
        # the same host.
        # not_local = attach_to != self.compute_instance_id()
        not_local = True

        with self._client.open_connection() as api:
            # Check that we have that volume
            scvolume = api.find_volume(blockdevice_id)
            if not scvolume:
                raise blockdevice.UnknownVolume(blockdevice_id)

            # Make sure we have a server defined for this host
            iqn = iscsi_utils.get_initiator_name()
            host = api.find_server(iqn)
            LOG.info("Search for server returned: %s", host)
            if not host:
                # Try to create a new host
                host = api.create_server(attach_to, iqn)
                LOG.info("Created server %s", host)
            # Make sure the server is logged in to the array
            ports = api.get_iscsi_ports()
            for port in ports:
                iscsi_utils.iscsi_login(port[0], port[1])

            # Make sure we were able to find something
            if not host:
                raise BlockDriverAPIException()

            # First check if we are already mapped
            mappings = api.find_mapping_profiles(scvolume)
            if mappings:
                # See if it is to this server
                if not_local:
                    raise blockdevice.AlreadyAttachedVolume(blockdevice_id)
                for mapping in mappings:
                    if (mapping['server']['instanceName'] !=
                            host['instanceName']):
                        raise blockdevice.AlreadyAttachedVolume(blockdevice_id)

            mapping = api.map_volume(scvolume, host)
            if not mapping:
                raise BlockDriverAPIException(
                    'Unable to map volume to server.')

            self._do_rescan('attach')

            return self._to_blockdevicevolume(scvolume, attach_to)