Exemple #1
0
    def _delete_volume(self, volume):
        try:
            self.cinderclient.volumes.delete(volume)
        except cinder_exception.NotFound:
            return
        except cinder_exception.ClientException as e:
            LOG.error(
                _LE("Error happened when delete volume from Cinder."
                    " Error: %s"), e)
            raise

        start_time = time.time()
        # Wait until the volume is not there or until the operation timeout
        while (time.time() - start_time < consts.DESTROY_VOLUME_TIMEOUT):
            try:
                self.cinderclient.volumes.get(volume.id)
            except cinder_exception.NotFound:
                return
            time.sleep(consts.VOLUME_SCAN_TIME_DELAY)

        # If the volume is not deleted, raise an exception
        msg_ft = _LE("Timed out while waiting for volume. "
                     "Expected Volume: {0}, "
                     "Expected State: {1}, "
                     "Elapsed Time: {2}").format(volume, None,
                                                 time.time() - start_time)
        raise exceptions.TimeoutException(msg_ft)
Exemple #2
0
    def connect_volume(self, volume, **connect_opts):
        mountpoint = connect_opts.get('mountpoint', None)
        host_name = utils.get_hostname()

        try:
            self.cinderclient.volumes.reserve(volume)
        except cinder_exception.ClientException:
            LOG.error(_LE("Reserve volume %s failed"), volume)
            raise

        try:
            device_info = self._connect_volume(volume)
            self.cinderclient.volumes.attach(volume=volume,
                                             instance_uuid=None,
                                             mountpoint=mountpoint,
                                             host_name=host_name)
            LOG.info(_LI("Attach volume to this server successfully"))
        except Exception:
            LOG.error(_LE("Attach volume %s to this server failed"), volume)
            with excutils.save_and_reraise_exception():
                try:
                    self._disconnect_volume(volume)
                except Exception:
                    pass
                self.cinderclient.volumes.unreserve(volume)

        return device_info
    def connect_volume(self, volume, **connect_opts):
        mountpoint = connect_opts.get('mountpoint', None)
        host_name = utils.get_hostname()

        try:
            self.cinderclient.volumes.reserve(volume)
        except cinder_exception.ClientException:
            LOG.error(_LE("Reserve volume %s failed"), volume)
            raise

        try:
            device_info = self._connect_volume(volume)
            self.cinderclient.volumes.attach(volume=volume,
                                             instance_uuid=None,
                                             mountpoint=mountpoint,
                                             host_name=host_name)
            LOG.info(_LI("Attach volume to this server successfully"))
        except Exception:
            LOG.error(_LE("Attach volume %s to this server failed"), volume)
            with excutils.save_and_reraise_exception():
                try:
                    self._disconnect_volume(volume)
                except Exception:
                    pass
                self.cinderclient.volumes.unreserve(volume)

        return device_info
Exemple #4
0
    def disconnect_volume(self, volume, **disconnect_opts):
        try:
            volume = self.cinderclient.volumes.get(volume.id)
        except cinder_exception.ClientException as e:
            LOG.error(_LE("Get Volume %s from Cinder failed"), volume.id)
            raise

        try:
            link_path = self.get_device_path(volume)
            utils.execute('rm', '-f', link_path, run_as_root=True)
        except processutils.ProcessExecutionError as e:
            LOG.warning(
                _LE("Error happened when remove docker volume"
                    " mountpoint directory. Error: %s"), e)

        try:
            self.novaclient.volumes.delete_server_volume(
                utils.get_instance_uuid(), volume.id)
        except nova_exception.ClientException as e:
            LOG.error(_LE("Detaching volume %(vol)s failed. Err: %(err)s"), {
                'vol': volume.id,
                'err': e
            })
            raise

        volume_monitor = state_monitor.StateMonitor(self.cinderclient, volume,
                                                    'available', (
                                                        'in-use',
                                                        'detaching',
                                                    ))
        return volume_monitor.monitor_cinder_volume()
Exemple #5
0
    def _create_from_existing_share(self, docker_volume_name, share_id,
                                    share_opts):
        try:
            share = self.manilaclient.shares.get(share_id)
        except manila_exception.NotFound:
            LOG.error(_LE("Could not find share %s"), share_id)
            raise

        if share.status != 'available':
            raise exceptions.UnexpectedStateException(
                "Manila share is unavailable")

        if share.name != docker_volume_name:
            LOG.error(
                _LE("Provided volume name %(d_name)s does not match "
                    "with existing share name %(s_name)s"), {
                        'd_name': docker_volume_name,
                        's_name': share.name
                    })
            raise exceptions.InvalidInput('Volume name does not match')

        metadata = {consts.VOLUME_FROM: CONF.volume_from}
        self.manilaclient.shares.update_all_metadata(share, metadata)

        return share
Exemple #6
0
    def _delete_share(self, share):
        try:
            share_access_list = self.manilaclient.shares.access_list(share)
            if len(share_access_list) > 0:
                LOG.warning(
                    _LE("Share %s is still used by other server, so "
                        "should not delete it."), share)
                return

            self.manilaclient.shares.delete(share)
        except manila_exception.ClientException as e:
            LOG.error(
                _LE("Error happened when delete Volume %(vol)s (Manila "
                    "share: %(share)s). Error: %(err)s"), {
                        'vol': share.name,
                        'share': share,
                        'err': e
                    })
            raise

        start_time = time.time()
        while True:
            try:
                self.manilaclient.shares.get(share.id)
            except manila_exception.NotFound:
                break

            if time.time() - start_time > consts.DESTROY_SHARE_TIMEOUT:
                raise exceptions.TimeoutException

            time.sleep(consts.SHARE_SCAN_INTERVAL)

        LOG.debug("Delete share %s from Manila successfully", share)
Exemple #7
0
    def _delete_volume(self, volume):
        try:
            self.cinderclient.volumes.delete(volume)
        except cinder_exception.NotFound:
            return
        except cinder_exception.ClientException as e:
            msg = _LE("Error happened when delete volume from Cinder. "
                      "Error: {0}").format(e)
            LOG.error(msg)
            raise

        start_time = time.time()
        # Wait until the volume is not there or until the operation timeout
        while(time.time() - start_time < consts.DESTROY_VOLUME_TIMEOUT):
            try:
                self.cinderclient.volumes.get(volume.id)
            except cinder_exception.NotFound:
                return
            time.sleep(consts.VOLUME_SCAN_TIME_DELAY)

        # If the volume is not deleted, raise an exception
        msg_ft = _LE("Timed out while waiting for volume. "
                     "Expected Volume: {0}, "
                     "Expected State: {1}, "
                     "Elapsed Time: {2}").format(volume,
                                                 None,
                                                 time.time() - start_time)
        raise exceptions.TimeoutException(msg_ft)
Exemple #8
0
    def disconnect_volume(self, volume, **disconnect_opts):
        try:
            volume = self.cinderclient.volumes.get(volume.id)
        except cinder_exception.ClientException as e:
            msg = _LE("Get Volume {0} from Cinder failed").format(volume.id)
            LOG.error(msg)
            raise

        try:
            link_path = self.get_device_path(volume)
            utils.execute('rm', '-f', link_path, run_as_root=True)
        except processutils.ProcessExecutionError as e:
            msg = _LE("Error happened when remove docker volume "
                      "mountpoint directory. Error: {0}").format(e)
            LOG.warn(msg)

        try:
            self.novaclient.volumes.delete_server_volume(
                utils.get_instance_uuid(),
                volume.id)
        except nova_exception.ClientException as e:
            msg = _LE("Detaching volume {0} failed. "
                      "Err: {1}").format(volume.id, e)
            LOG.error(msg)
            raise

        volume_monitor = state_monitor.StateMonitor(self.cinderclient,
                                                    volume,
                                                    'available',
                                                    ('in-use', 'detaching',))
        return volume_monitor.monitor_cinder_volume()
Exemple #9
0
    def create(self, docker_volume_name, volume_opts):
        if not volume_opts:
            volume_opts = {}

        connector = self._get_connector()
        cinder_volume, state = self._get_docker_volume(docker_volume_name)
        LOG.info(
            _LI("Get docker volume {0} {1} with state "
                "{2}").format(docker_volume_name, cinder_volume, state))

        device_info = {}
        if state == ATTACH_TO_THIS:
            LOG.warn(
                _LW("The volume {0} {1} already exists and attached to "
                    "this server").format(docker_volume_name, cinder_volume))
            device_info = {'path': connector.get_device_path(cinder_volume)}
        elif state == NOT_ATTACH:
            LOG.warn(
                _LW("The volume {0} {1} is already exists but not "
                    "attached").format(docker_volume_name, cinder_volume))
            device_info = connector.connect_volume(cinder_volume)
        elif state == ATTACH_TO_OTHER:
            if cinder_volume.multiattach:
                fstype = volume_opts.get('fstype', cinder_conf.fstype)
                vol_fstype = cinder_volume.metadata.get(
                    'fstype', cinder_conf.fstype)
                if fstype != vol_fstype:
                    msg = _LE("Volume already exists with fstype: {0}, but "
                              "currently provided fstype is {1}, not "
                              "match").format(vol_fstype, fstype)
                    LOG.error(msg)
                    raise exceptions.FuxiException('FSType Not Match')
                device_info = connector.connect_volume(cinder_volume)
            else:
                msg = _LE("The volume {0} {1} is already attached to another "
                          "server").format(docker_volume_name, cinder_volume)
                LOG.error(msg)
                raise exceptions.FuxiException(msg)
        elif state == UNKNOWN:
            if 'volume_id' in volume_opts:
                cinder_volume = self._create_from_existing_volume(
                    docker_volume_name, volume_opts.pop('volume_id'),
                    volume_opts)
                if self._check_attached_to_this(cinder_volume):
                    device_info = {
                        'path': connector.get_device_path(cinder_volume)
                    }
                else:
                    device_info = connector.connect_volume(cinder_volume)
            else:
                cinder_volume = self._create_volume(docker_volume_name,
                                                    volume_opts)
                device_info = connector.connect_volume(cinder_volume)

        return device_info
Exemple #10
0
    def _get_docker_volume(self, docker_volume_name):
        search_opts = {
            'name': docker_volume_name,
            'metadata': {
                consts.VOLUME_FROM: CONF.volume_from
            }
        }
        try:
            docker_shares = self.manilaclient.shares.list(
                search_opts=search_opts)
        except manila_exception.ClientException as e:
            LOG.error(_LE("Could not retrieve Manila share list. Error: %s"),
                      e)
            raise

        if not docker_shares:
            raise exceptions.NotFound("Could not find share with "
                                      "search_opts: {0}".format(search_opts))
        elif len(docker_shares) > 1:
            raise exceptions.TooManyResources(
                "Find too many shares with search_opts: {0}, while "
                "for Fuxi, should get only one share with provided "
                "search_opts".format(docker_shares))

        docker_share = docker_shares[0]
        if self.connector.check_access_allowed(docker_share):
            return docker_share, ATTACH_TO_THIS
        else:
            return docker_share, NOT_ATTACH
Exemple #11
0
    def show(self, docker_volume_name):
        cinder_volume, state = self._get_docker_volume(docker_volume_name)
        LOG.info(_LI("Get docker volume {0} {1} with state "
                     "{2}").format(docker_volume_name, cinder_volume, state))

        if state == ATTACH_TO_THIS:
            devpath = os.path.realpath(
                self._get_connector().get_device_path(cinder_volume))
            mp = self._get_mountpoint(docker_volume_name)
            LOG.info("Expected devpath: {0} and mountpoint: {1} for volume: "
                     "{2} {3}".format(devpath, mp, docker_volume_name,
                                      cinder_volume))
            mounter = mount.Mounter()
            return {"Name": docker_volume_name,
                    "Mountpoint": mp if mp in mounter.get_mps_by_device(
                        	      devpath) else ''}
        elif state in (NOT_ATTACH, ATTACH_TO_OTHER):
            return {'Name': docker_volume_name, 'Mountpoint': ''}
        elif state == UNKNOWN:
            msg = _LW("Can't find this volume '{0}' in "
                      "OpenSDS").format(docker_volume_name)
            LOG.warning(msg)
            raise exceptions.NotFound(msg)
        else:
            msg = _LE("Volume '{0}' exists, but not attached to this volume,"
                      "and current state is {1}").format(docker_volume_name,
                                                         state)
            raise exceptions.NotMatchedState(msg)
Exemple #12
0
    def _get_docker_volume(self, docker_volume_name):
        LOG.info(
            _LI("Retrieve docker volume {0} from "
                "Cinder").format(docker_volume_name))

        try:
            host_id = get_host_id()

            volume_connector = cinder_conf.volume_connector
            search_opts = {
                'name': docker_volume_name,
                'metadata': {
                    consts.VOLUME_FROM: CONF.volume_from
                }
            }
            for vol in self.cinderclient.volumes.list(search_opts=search_opts):
                if vol.name == docker_volume_name:
                    if vol.attachments:
                        for am in vol.attachments:
                            if volume_connector == OPENSTACK:
                                if am['server_id'] == host_id:
                                    return vol, ATTACH_TO_THIS
                            elif volume_connector == OSBRICK:
                                if (am['host_name'] or '').lower() == host_id:
                                    return vol, ATTACH_TO_THIS
                        return vol, ATTACH_TO_OTHER
                    else:
                        return vol, NOT_ATTACH
            return None, UNKNOWN
        except cinder_exception.ClientException as ex:
            LOG.error(
                _LE("Error happened while getting volume list "
                    "information from cinder. Error: {0}").format(ex))
            raise
Exemple #13
0
    def list(self):
        LOG.info(_LI("Start to retrieve all docker volumes from Cinder"))

        docker_volumes = []
        try:
            search_opts = {'metadata': {consts.VOLUME_FROM: CONF.volume_from}}
            for vol in self.cinderclient.volumes.list(search_opts=search_opts):
                docker_volume_name = vol.name
                if not docker_volume_name:
                    continue

                mountpoint = self._get_mountpoint(vol.name)
                devpath = os.path.realpath(
                    self._get_connector().get_device_path(vol))
                mps = mount.Mounter().get_mps_by_device(devpath)
                mountpoint = mountpoint if mountpoint in mps else ''
                docker_vol = {
                    'Name': docker_volume_name,
                    'Mountpoint': mountpoint
                }
                docker_volumes.append(docker_vol)
        except cinder_exception.ClientException as e:
            LOG.error(_LE("Retrieve volume list failed. Error: %s"), e)
            raise

        LOG.info(_LI("Retrieve docker volumes %s from Cinder "
                     "successfully"), docker_volumes)
        return docker_volumes
Exemple #14
0
 def _get_connector(self):
     connector = cinder_conf.volume_connector
     if not connector or connector not in volume_connector_conf:
         msg = _LE("Must provide an valid volume connector")
         LOG.error(msg)
         raise exceptions.FuxiException(msg)
     return importutils.import_class(volume_connector_conf[connector])()
Exemple #15
0
    def _create_volume(self, docker_volume_name, volume_opts):
        LOG.info(_LI("Start to create docker volume %s from Cinder"),
                 docker_volume_name)

        cinder_volume_kwargs = get_cinder_volume_kwargs(
            docker_volume_name, volume_opts)

        try:
            volume = self.cinderclient.volumes.create(**cinder_volume_kwargs)
        except cinder_exception.ClientException as e:
            LOG.error(
                _LE("Error happened when create an volume %(vol)s from"
                    " Cinder. Error: %(err)s"), {
                        'vol': docker_volume_name,
                        'err': e
                    })
            raise

        LOG.info(_LI("Waiting volume %s to be available"), volume)
        volume_monitor = state_monitor.StateMonitor(
            self.cinderclient,
            volume,
            'available', ('creating', ),
            time_delay=consts.VOLUME_SCAN_TIME_DELAY)
        volume = volume_monitor.monitor_cinder_volume()

        LOG.info(
            _LI("Create docker volume %(d_v)s %(vol)s from Cinder "
                "successfully"), {
                    'd_v': docker_volume_name,
                    'vol': volume
                })
        return volume
Exemple #16
0
    def _create_volume(self, docker_volume_name, volume_opts):
        LOG.info(_LI("Start to create docker volume {0} from "
                     "Cinder").format(docker_volume_name))

        cinder_volume_kwargs = get_cinder_volume_kwargs(docker_volume_name,
                                                        volume_opts)

        try:
            volume = self.cinderclient.volumes.create(**cinder_volume_kwargs)
        except cinder_exception.ClientException as e:
            msg = _LE("Error happened when create an volume {0} from Cinder. "
                      "Error: {1}").format(docker_volume_name, e)
            LOG.error(msg)
            raise

        LOG.info(_LI("Waiting volume {0} to be available").format(volume))
        volume_monitor = state_monitor.StateMonitor(
            self.cinderclient,
            volume,
            'available',
            ('creating',),
            time_delay=consts.VOLUME_SCAN_TIME_DELAY)
        volume = volume_monitor.monitor_cinder_volume()

        LOG.info(_LI("Create docker volume {0} {1} from Cinder "
                     "successfully").format(docker_volume_name, volume))
        return volume
Exemple #17
0
    def _create_volume(self, docker_volume_name, volume_opts):
        LOG.info(
            _LI("Start to create docker volume {0} from "
                "Cinder").format(docker_volume_name))

        cinder_volume_kwargs = get_cinder_volume_kwargs(
            docker_volume_name, volume_opts)

        try:
            volume = self.cinderclient.volumes.create(**cinder_volume_kwargs)
        except cinder_exception.ClientException as e:
            msg = _LE("Error happened when create an volume {0} from Cinder. "
                      "Error: {1}").format(docker_volume_name, e)
            LOG.error(msg)
            raise

        LOG.info(_LI("Waiting volume {0} to be available").format(volume))
        volume_monitor = state_monitor.StateMonitor(
            self.cinderclient,
            volume,
            'available', ('creating', ),
            time_delay=consts.VOLUME_SCAN_TIME_DELAY)
        volume = volume_monitor.monitor_cinder_volume()

        LOG.info(
            _LI("Create docker volume {0} {1} from Cinder "
                "successfully").format(docker_volume_name, volume))
        return volume
Exemple #18
0
    def _get_docker_volume(self, docker_volume_name):
        LOG.info(_LI("Retrieve docker volume {0} from "
                     "Cinder").format(docker_volume_name))

        try:
            host_id = get_host_id()

            volume_connector = cinder_conf.volume_connector
            search_opts = {'name': docker_volume_name,
                           'metadata': {consts.VOLUME_FROM: CONF.volume_from}}
            for vol in self.cinderclient.volumes.list(search_opts=search_opts):
                if vol.name == docker_volume_name:
                    if vol.attachments:
                        for am in vol.attachments:
                            if volume_connector == OPENSTACK:
                                if am['server_id'] == host_id:
                                    return vol, ATTACH_TO_THIS
                            elif volume_connector == OSBRICK:
                                if (am['host_name'] or '').lower() == host_id:
                                    return vol, ATTACH_TO_THIS
                        return vol, ATTACH_TO_OTHER
                    else:
                        return vol, NOT_ATTACH
            return None, UNKNOWN
        except cinder_exception.ClientException as ex:
            LOG.error(_LE("Error happened while getting volume list "
                          "information from cinder. Error: {0}").format(ex))
            raise
Exemple #19
0
    def list(self):
        LOG.info(_LI("Start to retrieve all docker volumes from OpenSDS"))

        docker_volumes = []
        try:
            for vol in self.opensdsclient.list():
		# LOG.info(_LI("Retrieve docker volumes {0} from OpenSDS "
                #      "successfully").format(vol))
                docker_volume_name = vol['name']
                if not docker_volume_name:
                    continue

                mountpoint = self._get_mountpoint(docker_volume_name)
		vol = APIDictWrapper(vol)
                devpath = os.path.realpath(
                    self._get_connector().get_device_path(vol))
                mps = mount.Mounter().get_mps_by_device(devpath)
                mountpoint = mountpoint if mountpoint in mps else ''
                docker_vol = {'Name': docker_volume_name,
                              'Mountpoint': mountpoint}
                docker_volumes.append(docker_vol)
        except cinder_exception.ClientException as e:
            LOG.error(_LE("Retrieve volume list failed. Error: {0}").format(e))
            raise

        LOG.info(_LI("Retrieve docker volumes {0} from OpenSDS "
                     "successfully").format(docker_volumes))
        return docker_volumes
Exemple #20
0
    def list(self):
        LOG.info(_LI("Start to retrieve all docker volumes from Cinder"))

        docker_volumes = []
        try:
            search_opts = {'metadata': {consts.VOLUME_FROM: 'fuxi'}}
            for vol in self.cinderclient.volumes.list(search_opts=search_opts):
                docker_volume_name = vol.name
                if not docker_volume_name or not vol.attachments:
                    continue

                mountpoint = self._get_mountpoint(vol.name)
                if self._check_attached_to_this(vol):
                    devpath = os.path.realpath(
                        self._get_connector().get_device_path(vol))
                    mps = mount.Mounter().get_mps_by_device(devpath)
                    mountpoint = mountpoint if mountpoint in mps else ''
                    docker_vol = {'Name': docker_volume_name,
                                  'Mountpoint': mountpoint}
                    docker_volumes.append(docker_vol)
        except cinder_exception.ClientException as e:
            LOG.error(_LE("Retrieve volume list failed. Error: {0}").format(e))
            raise

        LOG.info(_LI("Retrieve docker volumes {0} from Cinder "
                     "successfully").format(docker_volumes))
        return docker_volumes
Exemple #21
0
    def show(self, docker_volume_name):
        cinder_volume, state = self._get_docker_volume(docker_volume_name)
        LOG.info(_LI("Get docker volume {0} {1} with state "
                     "{2}").format(docker_volume_name, cinder_volume, state))

        if state == ATTACH_TO_THIS:
            devpath = os.path.realpath(
                self._get_connector().get_device_path(cinder_volume))
            mp = self._get_mountpoint(docker_volume_name)
            LOG.info("Expected devpath: {0} and mountpoint: {1} for volume: "
                     "{2} {3}".format(devpath, mp, docker_volume_name,
                                      cinder_volume))
            mounter = mount.Mounter()
            return {"Name": docker_volume_name,
                    "Mountpoint": mp if mp in mounter.get_mps_by_device(
                        devpath) else ''}
        elif state == UNKNOWN:
            msg = _LW("Can't find this volume '{0}' in "
                      "Cinder").format(docker_volume_name)
            LOG.warn(msg)
            raise exceptions.NotFound(msg)
        else:
            msg = _LE("Volume '{0}' exists, but not attached to this volume,"
                      "and current state is {1}").format(docker_volume_name,
                                                         state)
            raise exceptions.NotMatchedState(msg)
Exemple #22
0
    def _access_allow(self, share):
        share_proto = share.share_proto
        if share_proto not in self.proto_access_type_map.keys():
            raise exceptions.InvalidProtocol("Not enabled share protocol %s" %
                                             share_proto)

        try:
            if self.check_access_allowed(share):
                return

            access_type = self.proto_access_type_map[share_proto]
            access_to = self._get_access_to(access_type)
            LOG.info(
                _LI("Allow machine to access share %(shr)s with "
                    "access_type %(type)s and access_to %(to)s"), {
                        'shr': share,
                        'type': access_type,
                        'to': access_to
                    })
            self.manilaclient.shares.allow(share, access_type, access_to, 'rw')
        except manila_exception.ClientException as e:
            LOG.error(_LE("Failed to grant access for server, %s"), e)
            raise

        LOG.info(_LI("Waiting share %s access to be active"), share)
        state_monitor.StateMonitor(self.manilaclient, share, 'active',
                                   ('new', )).monitor_share_access(
                                       access_type, access_to)
Exemple #23
0
 def _get_connector(self):
     connector = cinder_conf.volume_connector
     if not connector or connector not in volume_connector_conf:
         msg = _LE("Must provide an valid volume connector")
         LOG.error(msg)
         raise exceptions.FuxiException(msg)
     return importutils.import_class(volume_connector_conf[connector])()
Exemple #24
0
 def get_device_size(self, device):
     try:
         nr_sectors = open(device + '/size').read().rstrip('\n')
         sect_size = open(device + '/queue/hw_sector_size')\
             .read().rstrip('\n')
         return (float(nr_sectors) * float(sect_size)) / units.Gi
     except IOError as e:
         LOG.error(_LE("Failed to read device size. {0}").format(e))
         raise exceptions.FuxiException(e.message)
Exemple #25
0
    def create(self, docker_volume_name, volume_opts):
        if not volume_opts:
            volume_opts = {}

        connector = self._get_connector()
        cinder_volume, state = self._get_docker_volume(docker_volume_name)
        LOG.info(_LI("Get docker volume {0} {1} with state "
                     "{2}").format(docker_volume_name, cinder_volume, state))

        device_info = {}
        if state == ATTACH_TO_THIS:
            LOG.warn(_LW("The volume {0} {1} already exists and attached to "
                         "this server").format(docker_volume_name,
                                               cinder_volume))
            device_info = {'path': connector.get_device_path(cinder_volume)}
        elif state == NOT_ATTACH:
            LOG.warn(_LW("The volume {0} {1} is already exists but not "
                         "attached").format(docker_volume_name,
                                            cinder_volume))
            device_info = connector.connect_volume(cinder_volume)
        elif state == ATTACH_TO_OTHER:
            if cinder_volume.multiattach:
                fstype = volume_opts.get('fstype', cinder_conf.fstype)
                vol_fstype = cinder_volume.metadata.get('fstype',
                                                        cinder_conf.fstype)
                if fstype != vol_fstype:
                    msg = _LE("Volume already exists with fstype: {0}, but "
                              "currently provided fstype is {1}, not "
                              "match").format(vol_fstype, fstype)
                    LOG.error(msg)
                    raise exceptions.FuxiException('FSType Not Match')
                device_info = connector.connect_volume(cinder_volume)
            else:
                msg = _LE("The volume {0} {1} is already attached to another "
                          "server").format(docker_volume_name, cinder_volume)
                LOG.error(msg)
                raise exceptions.FuxiException(msg)
        elif state == UNKNOWN:
            volume_opts['name'] = docker_volume_name
            cinder_volume = self._create_volume(docker_volume_name,
                                                volume_opts)
            device_info = connector.connect_volume(cinder_volume)

        return device_info
Exemple #26
0
 def make_json_error(ex):
     app.logger.error(_LE("Unexpected error happened: %s"),
                      traceback.format_exc())
     response = flask.jsonify({"Err": str(ex)})
     response.status_code = w_exceptions.InternalServerError.code
     if isinstance(ex, w_exceptions.HTTPException):
         response.status_code = ex.code
     content_type = 'application/vnd.docker.plugins.v1+json; charset=utf-8'
     response.headers['Content-Type'] = content_type
     return response
Exemple #27
0
def get_cinder_volume_kwargs(docker_volume_name, docker_volume_opt):
    """Retrieve parameters for creating Cinder volume.

    Retrieve required parameters and remove unsupported arguments from
    client input. These parameters are used to create a Cinder volume.

    :param docker_volume_name: Name for Cinder volume
    :type docker_volume_name: str
    :param docker_volume_opt: Optional parameters for Cinder volume
    :type docker_volume_opt: dict
    :rtype: dict
    """
    options = [
        'size', 'consistencygroup_id', 'snapshot_id', 'source_volid',
        'description', 'volume_type', 'user_id', 'project_id',
        'availability_zone', 'scheduler_hints', 'source_replica', 'multiattach'
    ]
    kwargs = {}

    if 'size' in docker_volume_opt:
        try:
            size = int(docker_volume_opt.pop('size'))
        except ValueError:
            msg = _LE("Volume size must be able to convert to int type")
            LOG.error(msg)
            raise exceptions.InvalidInput(msg)
    else:
        size = CONF.default_volume_size
        LOG.info(
            _LI("Volume size doesn't provide from command, so use"
                " default size %sG"), size)
    kwargs['size'] = size

    for key, value in docker_volume_opt.items():
        if key in options:
            kwargs[key] = value

    if not kwargs.get('availability_zone', None):
        kwargs['availability_zone'] = cinder_conf.availability_zone

    if not kwargs.get('volume_type', None):
        kwargs['volume_type'] = cinder_conf.volume_type

    kwargs['name'] = docker_volume_name
    kwargs['metadata'] = {
        consts.VOLUME_FROM: CONF.volume_from,
        'fstype': kwargs.pop('fstype', cinder_conf.fstype)
    }

    req_multiattach = kwargs.pop('multiattach', cinder_conf.multiattach)
    kwargs['multiattach'] = strutils.bool_from_string(req_multiattach,
                                                      strict=True)

    return kwargs
Exemple #28
0
    def __init__(self):
        super(Manila, self).__init__()
        self.manilaclient = utils.get_manilaclient()

        conn_conf = manila_conf.volume_connector
        if not conn_conf or conn_conf not in volume_connector_conf:
            msg = _LE("Must provide a valid volume connector")
            LOG.error(msg)
            raise exceptions.InvalidInput(msg)
        self.connector = importutils.import_object(
            volume_connector_conf[conn_conf], manilaclient=self.manilaclient)
Exemple #29
0
    def _delete_volume(self, volume):
        try:
            self.opensdsclient.delete(volume.id)
        except cinder_exception.NotFound:
            return
        except cinder_exception.ClientException as e:
            msg = _LE("Error happened when delete volume from OpenSDS. "
                      "Error: {0}").format(e)
            LOG.error(msg)
            raise

        """start_time = time.time()
Exemple #30
0
    def _clear_mountpoint(self, mountpoint):
        """Clear mount point directory if it wouldn't used any more.

        :param mountpoint: The path of Docker volume.
        """
        if os.path.exists(mountpoint) and os.path.isdir(mountpoint):
            try:
                utils.execute('rm', '-r', mountpoint, run_as_root=True)
                LOG.info(_LI("Clear mountpoint %s successfully"), mountpoint)
            except processutils.ProcessExecutionError as e:
                LOG.error(_LE("Error happened when clear mountpoint {0}"), e)
                raise
Exemple #31
0
    def _create_from_existing_volume(self, docker_volume_name,
                                     cinder_volume_id, volume_opts):
        try:
            cinder_volume = self.cinderclient.volumes.get(cinder_volume_id)
        except cinder_exception.ClientException as e:
            msg = _LE("Failed to get volume %(vol_id)s from Cinder. "
                      "Error: %(err)s")
            LOG.error(msg, {'vol_id': cinder_volume_id, 'err': e})
            raise

        status = cinder_volume.status
        if status not in ('available', 'in-use'):
            LOG.error(
                _LE("Current volume %(vol)s status %(status)s not in "
                    "desired states"), {
                        'vol': cinder_volume,
                        'status': status
                    })
            raise exceptions.NotMatchedState('Cinder volume is unavailable')
        elif status == 'in-use' and not cinder_volume.multiattach:
            if not self._check_attached_to_this(cinder_volume):
                msg = _LE("Current volume %(vol)s status %(status)s not "
                          "in desired states")
                LOG.error(msg, {'vol': cinder_volume, 'status': status})
                raise exceptions.NotMatchedState(
                    'Cinder volume is unavailable')

        if cinder_volume.name != docker_volume_name:
            LOG.error(
                _LE("Provided volume name %(d_name)s does not match "
                    "with existing Cinder volume name %(c_name)s"), {
                        'd_name': docker_volume_name,
                        'c_name': cinder_volume.name
                    })
            raise exceptions.InvalidInput('Volume name does not match')

        fstype = volume_opts.pop('fstype', cinder_conf.fstype)
        vol_fstype = cinder_volume.metadata.get('fstype', cinder_conf.fstype)
        if fstype != vol_fstype:
            LOG.error(
                _LE("Volume already exists with fstype %(c_fstype)s, "
                    "but currently provided fstype is %(fstype)s, not "
                    "match"), {
                        'c_fstype': vol_fstype,
                        'fstype': fstype
                    })
            raise exceptions.InvalidInput('FSType does not match')

        try:
            metadata = {consts.VOLUME_FROM: CONF.volume_from, 'fstype': fstype}
            self.cinderclient.volumes.set_metadata(cinder_volume, metadata)
        except cinder_exception.ClientException as e:
            LOG.error(
                _LE("Failed to update volume %(vol)s information. "
                    "Error: %(err)s"), {
                        'vol': cinder_volume_id,
                        'err': e
                    })
            raise
        return cinder_volume
Exemple #32
0
def get_cinder_volume_kwargs(docker_volume_name, docker_volume_opt):
    """Retrieve parameters for creating Cinder volume.

    Retrieve required parameters and remove unsupported arguments from
    client input. These parameters are used to create a Cinder volume.

    :param docker_volume_name: Name for Cinder volume
    :type docker_volume_name: str
    :param docker_volume_opt: Optional parameters for Cinder volume
    :type docker_volume_opt: dict
    :rtype: dict
    """
    options = ['size', 'consistencygroup_id', 'snapshot_id', 'source_volid',
               'description', 'volume_type', 'user_id', 'project_id',
               'availability_zone', 'scheduler_hints', 'source_replica',
               'multiattach']
    kwargs = {}

    if 'size' in docker_volume_opt:
        try:
            size = int(docker_volume_opt.pop('size'))
        except ValueError:
            msg = _LE("Volume size must be able to convert to int type")
            LOG.error(msg)
            raise exceptions.InvalidInput(msg)
    else:
        size = CONF.default_volume_size
        msg = _LI("Volume size doesn't provide from command, so use "
                  "default size {0}G").format(size)
        LOG.info(msg)
    kwargs['size'] = size

    for key, value in docker_volume_opt.items():
        if key in options:
            kwargs[key] = value

    if not kwargs.get('availability_zone', None):
        kwargs['availability_zone'] = cinder_conf.availability_zone

    if not kwargs.get('volume_type', None):
        kwargs['volume_type'] = cinder_conf.volume_type

    kwargs['name'] = docker_volume_name
    kwargs['metadata'] = {consts.VOLUME_FROM: CONF.volume_from,
                          'fstype': kwargs.pop('fstype', cinder_conf.fstype)}

    req_multiattach = kwargs.pop('multiattach', cinder_conf.multiattach)
    kwargs['multiattach'] = strutils.bool_from_string(req_multiattach,
                                                      strict=True)

    return kwargs
Exemple #33
0
    def _disconnect_volume(self, volume):
        try:
            link_path = self.get_device_path(volume)
            utils.execute('rm', '-f', link_path, run_as_root=True)
        except processutils.ProcessExecutionError as e:
            msg = _LE("Error happened when remove docker volume "
                      "mountpoint directory. Error: {0}").format(e)
            LOG.warn(msg)

        conn_info = self._get_connection_info(volume.id)

        protocol = conn_info['driver_volume_type']
        brick_get_connector(protocol).disconnect_volume(conn_info['data'],
                                                        None)
    def _disconnect_volume(self, volume):
        try:
            link_path = self.get_device_path(volume)
            utils.execute('rm', '-f', link_path, run_as_root=True)
        except processutils.ProcessExecutionError as e:
            msg = _LE("Error happened when remove docker volume "
                      "mountpoint directory. Error: {0}").format(e)
            LOG.warning(msg)

        conn_info = self._get_connection_info(volume.id)

        protocol = conn_info['driver_volume_type']
        brick_get_connector(protocol).disconnect_volume(conn_info['data'],
                                                        None)
Exemple #35
0
    def _create_mountpoint(self, mountpoint):
        """Create mount point directory for Docker volume.

        :param mountpoint: The path of Docker volume.
        """
        try:
            if not os.path.exists(mountpoint) or not os.path.isdir(mountpoint):
                utils.execute('mkdir', '-p', '-m=755', mountpoint,
                              run_as_root=True)
                LOG.info(_LI("Create mountpoint %s successfully"), mountpoint)
        except processutils.ProcessExecutionError as e:
            LOG.error(_LE("Error happened when create volume "
                          "directory. Error: %s"), e)
            raise
Exemple #36
0
 def _get_connection_info(self, volume_id):
     LOG.info(
         _LI("Get connection info for osbrick connector and use it to "
             "connect to volume"))
     try:
         conn_info = self.cinderclient.volumes.initialize_connection(
             volume_id, brick_get_connector_properties())
         LOG.info(_LI("Get connection information %s"), conn_info)
         return conn_info
     except cinder_exception.ClientException as e:
         LOG.error(
             _LE("Error happened when initialize connection"
                 " for volume. Error: %s"), e)
         raise
Exemple #37
0
 def _get_connection_info(self, volume_id):
     LOG.info(_LI("Get connection info for osbrick connector and use it to "
                  "connect to volume"))
     try:
         conn_info = self.cinderclient.volumes.initialize_connection(
             volume_id,
             brick_get_connector_properties())
         msg = _LI("Get connection information {0}").format(conn_info)
         LOG.info(msg)
         return conn_info
     except cinder_exception.ClientException as e:
         msg = _LE("Error happened when initialize connection for volume. "
                   "Error: {0}").format(e)
         LOG.error(msg)
         raise
 def _get_connection_info(self, volume_id):
     LOG.info(_LI("Get connection info for osbrick connector and use it to "
                  "connect to volume"))
     try:
         conn_info = self.cinderclient.volumes.initialize_connection(
             volume_id,
             brick_get_connector_properties())
         msg = _LI("Get connection information {0}").format(conn_info)
         LOG.info(msg)
         return conn_info
     except cinder_exception.ClientException as e:
         msg = _LE("Error happened when initialize connection for volume. "
                   "Error: {0}").format(e)
         LOG.error(msg)
         raise
Exemple #39
0
    def _create_share(self, docker_volume_name, share_opts):
        share_kwargs = extract_share_kwargs(docker_volume_name, share_opts)

        try:
            LOG.debug("Start to create share from Manila")
            share = self.manilaclient.shares.create(**share_kwargs)
        except manila_exception.ClientException as e:
            LOG.error(_LE("Create Manila share failed. Error: {0}"), e)
            raise

        LOG.info(_LI("Waiting for share %s status to be available"), share)
        share_monitor = state_monitor.StateMonitor(self.manilaclient, share,
                                                   'available', ('creating', ))
        share = share_monitor.monitor_manila_share()
        LOG.info(_LI("Creating share %s successfully"), share)
        return share
Exemple #40
0
    def _get_mountpoint(self, docker_volume_name):
        """Generate a mount point for volume.

        :param docker_volume_name:
        :rtype: str
        """
        if not docker_volume_name:
            LOG.error(_LE("Volume name could not be None"))
            raise exceptions.FuxiException("Volume name could not be None")
        if self.volume_provider_type:
            return os.path.join(CONF.volume_dir,
                                self.volume_provider_type,
                                docker_volume_name)
        else:
            return os.path.join(CONF.volume_dir,
                                docker_volume_name)
Exemple #41
0
    def _create_volume(self, docker_volume_name, volume_opts):
        LOG.info(_LI("Start to create docker volume {0} from "
                     "OpenSDS").format(docker_volume_name))

        try:
            volume = self.opensdsclient.create(docker_volume_name, volume_opts['size'])
        except cinder_exception.ClientException as e:
            msg = _LE("Error happened when create an volume {0} from OpenSDS. "
                      "Error: {1}").format(docker_volume_name, e)
            LOG.error(msg)
            raise
	volume = APIDictWrapper(volume)
        time.sleep(5)

        LOG.info(_LI("Create docker volume {0} {1} from OpenSDS "
                     "successfully").format(docker_volume_name, volume))
        return volume
Exemple #42
0
 def _reached_desired_state(self, current_state):
     if current_state == self.desired_state:
         return True
     elif current_state in self.transient_states:
         idx = self.transient_states.index(current_state)
         if idx > 0:
             self.transient_states = self.transient_states[idx:]
         return False
     else:
         msg = _LE("Unexpected state while waiting for volume. "
                   "Expected Volume: {0}, "
                   "Expected State: {1}, "
                   "Reached State: {2}").format(self.expected_obj,
                                                self.desired_state,
                                                current_state)
         LOG.error(msg)
         raise exceptions.UnexpectedStateException(msg)
Exemple #43
0
    def _connect_volume(self, volume):
        conn_info = self._get_connection_info(volume.id)

        protocol = conn_info['driver_volume_type']
        brick_connector = brick_get_connector(protocol)
        device_info = brick_connector.connect_volume(conn_info['data'])
        LOG.info(_LI("Get device_info after connect to "
                     "volume %s") % device_info)
        try:
            link_path = os.path.join(consts.VOLUME_LINK_DIR, volume.id)
            utils.execute('ln', '-s', os.path.realpath(device_info['path']),
                          link_path,
                          run_as_root=True)
        except processutils.ProcessExecutionError as e:
            LOG.error(_LE("Failed to create link for device. %s"), e)
            raise
        return {'path': link_path, 'iscsi_path': device_info['path']}
Exemple #44
0
 def _reached_desired_state(self, current_state):
     if current_state == self.desired_state:
         return True
     elif current_state in self.transient_states:
         idx = self.transient_states.index(current_state)
         if idx > 0:
             self.transient_states = self.transient_states[idx:]
         return False
     else:
         msg = _LE("Unexpected state while waiting for volume. "
                   "Expected Volume: {0}, "
                   "Expected State: {1}, "
                   "Reached State: {2}").format(self.expected_obj,
                                                self.desired_state,
                                                current_state)
         LOG.error(msg)
         raise exceptions.UnexpectedStateException(msg)
Exemple #45
0
    def disconnect_volume(self, volume, **disconnect_opts):
        self._disconnect_volume(volume)

        attachments = volume.attachments
        attachment_uuid = None
        for am in attachments:
            if am['host_name'].lower() == utils.get_hostname().lower():
                attachment_uuid = am['attachment_id']
                break
        try:
            self.cinderclient.volumes.detach(volume.id,
                                             attachment_uuid=attachment_uuid)
            LOG.info(_LI("Disconnect volume successfully"))
        except cinder_exception.ClientException as e:
            msg = _LE("Error happened when detach volume {0} {1} from this "
                      "server. Error: {2}").format(volume.name, volume, e)
            LOG.error(msg)
            raise
Exemple #46
0
def get_instance_uuid():
    try:
        inst_uuid = ''
        inst_uuid_count = 0
        dirs = os.listdir(cloud_init_conf)
        for uuid_dir in dirs:
            if uuidutils.is_uuid_like(uuid_dir):
                inst_uuid = uuid_dir
                inst_uuid_count += 1

        # If not or not only get on instance_uuid, then search
        # it from metadata server.
        if inst_uuid_count == 1:
            return inst_uuid
    except Exception:
        LOG.warning(_LW("Get instance_uuid from cloud-init failed"))

    try:
        resp = requests.get('http://169.254.169.254/openstack',
                            timeout=constants.CURL_MD_TIMEOUT)
        metadata_api_versions = resp.text.split()
        metadata_api_versions.sort(reverse=True)
    except Exception as e:
        LOG.error(_LE("Get metadata apis failed. Error: {}").format(e))
        raise exceptions.FuxiException("Metadata API Not Found")

    for api_version in metadata_api_versions:
        metadata_url = ''.join(['http://169.254.169.254/openstack/',
                                api_version,
                                '/meta_data.json'])
        try:
            resp = requests.get(metadata_url,
                                timeout=constants.CURL_MD_TIMEOUT)
            metadata = resp.json()
            if metadata.get('uuid', None):
                return metadata['uuid']
        except Exception as e:
            LOG.warning(_LW("Get instance_uuid from metadata server {0} "
                            "failed. Error: {1}").format(metadata_url, e))
            continue

    raise exceptions.FuxiException("Instance UUID Not Found")
Exemple #47
0
    def monitor_cinder_volume(self):
        while True:
            try:
                volume = self.client.volumes.get(self.expected_obj.id)
            except cinder_exception.ClientException:
                elapsed_time = time.time() - self.start_time
                if elapsed_time > self.time_limit:
                    msg = _LE("Timed out while waiting for volume. "
                              "Expected Volume: {0}, "
                              "Expected State: {1}, "
                              "Elapsed Time: {2}").format(self.expected_obj,
                                                          self.desired_state,
                                                          elapsed_time)
                    LOG.error(msg)
                    raise exceptions.TimeoutException(msg)
                raise

            if self._reached_desired_state(volume.status):
                return volume

            time.sleep(self.time_delay)
Exemple #48
0
    def connect_volume(self, volume, **connect_opts):
        bdm = blockdevice.BlockerDeviceManager()
        ori_devices = bdm.device_scan()

        # Do volume-attach
        try:
            server_id = connect_opts.get('server_id', None)
            if not server_id:
                server_id = utils.get_instance_uuid()

            LOG.info(_LI("Start to connect to volume {0}").format(volume))
            nova_volume = self.novaclient.volumes.create_server_volume(
                server_id=server_id,
                volume_id=volume.id,
                device=None)

            volume_monitor = state_monitor.StateMonitor(
                self.cinderclient,
                nova_volume,
                'in-use',
                ('available', 'attaching',))
            attached_volume = volume_monitor.monitor_cinder_volume()
        except nova_exception.ClientException as ex:
            LOG.error(_LE("Attaching volume {0} to server {1} "
                          "failed. Error: {2}").format(volume.id,
                                                       server_id, ex))
            raise

        # Get all devices on host after do volume-attach,
        # and then find attached device.
        LOG.info(_LI("After connected to volume, scan the added "
                     "block device on host"))
        curr_devices = bdm.device_scan()
        start_time = time.time()
        delta_devices = list(set(curr_devices) - set(ori_devices))
        while not delta_devices:
            time.sleep(consts.DEVICE_SCAN_TIME_DELAY)
            curr_devices = bdm.device_scan()
            delta_devices = list(set(curr_devices) - set(ori_devices))
            if time.time() - start_time > consts.DEVICE_SCAN_TIMEOUT:
                msg = _("Could not detect added device with "
                        "limited time")
                raise exceptions.FuxiException(msg)
        LOG.info(_LI("Get extra added block device {0}"
                     "").format(delta_devices))

        for device in delta_devices:
            if bdm.get_device_size(device) == volume.size:
                device = device.replace('/sys/block', '/dev')
                msg = _LI("Find attached device {0} for volume {1} "
                          "{2}").format(device,
                                        attached_volume.name,
                                        volume)
                LOG.info(msg)

                link_path = os.path.join(consts.VOLUME_LINK_DIR, volume.id)
                try:
                    utils.execute('ln', '-s', device,
                                  link_path,
                                  run_as_root=True)
                except processutils.ProcessExecutionError as e:
                    msg = _LE("Error happened when create link file for "
                              "block device attached by Nova. "
                              "Error: {0}").format(e)
                    LOG.error(msg)
                    raise
                return {'path': link_path}

        LOG.warm(_LW("Could not find matched device"))
        raise exceptions.NotFound("Not Found Matched Device")
Exemple #49
0
    def delete(self, docker_volume_name):
        cinder_volume, state = self._get_docker_volume(docker_volume_name)
        LOG.info(_LI("Get docker volume {0} {1} with state "
                     "{2}").format(docker_volume_name, cinder_volume, state))

        if state == ATTACH_TO_THIS:
            link_path = self._get_connector().get_device_path(cinder_volume)
            if not link_path or not os.path.exists(link_path):
                msg = _LE(
                    "Could not find device link path for volume {0} {1} "
                    "in host").format(docker_volume_name, cinder_volume)
                LOG.error(msg)
                raise exceptions.FuxiException(msg)

            devpath = os.path.realpath(link_path)
            if not os.path.exists(devpath):
                msg = _LE("Could not find device path for volume {0} {1} in "
                          "host").format(docker_volume_name, cinder_volume)
                LOG.error(msg)
                raise exceptions.FuxiException(msg)

            mounter = mount.Mounter()
            mps = mounter.get_mps_by_device(devpath)
            ref_count = len(mps)
            if ref_count > 0:
                mountpoint = self._get_mountpoint(docker_volume_name)
                if mountpoint in mps:
                    mounter.unmount(mountpoint)

                    self._clear_mountpoint(mountpoint)

                    # If this volume is still mounted on other mount point,
                    # then return.
                    if ref_count > 1:
                        return True
                else:
                    return True

            # Detach device from this server.
            self._get_connector().disconnect_volume(cinder_volume)

            available_volume = self.cinderclient.volumes.get(cinder_volume.id)
            # If this volume is not used by other server any more,
            # than delete it from Cinder.
            if not available_volume.attachments:
                msg = _LW("No other servers still use this volume {0} "
                          "{1} any more, so delete it from Cinder"
                          "").format(docker_volume_name, cinder_volume)
                LOG.warn(msg)
                self._delete_volume(available_volume)
            return True
        elif state == UNKNOWN:
            return False
        else:
            msg = _LE("The volume {0} {1} state must be {2} when "
                      "remove it from this server, but current state "
                      "is {3}").format(docker_volume_name,
                                       cinder_volume,
                                       ATTACH_TO_THIS,
                                       state)
            LOG.error(msg)
            raise exceptions.NotMatchedState(msg)