Ejemplo n.º 1
0
def get_dst_user_from_src_user_id(src_keystone, dst_keystone, src_user_id,
                                  fallback_to_admin=True):
    """Returns user from destination with the same name as on source. None if
    user does not exist"""
    try:
        with proxy_client.expect_exception(ks_exceptions.NotFound):
            src_user = src_keystone.keystone_client.users.get(src_user_id)
        src_user_name = src_user.name
    except ks_exceptions.NotFound:
        LOG.warning("User '%s' not found on SRC!", src_user_id)
        if fallback_to_admin:
            LOG.warning("Replacing user '%s' with SRC admin", src_user_id)
            src_user_name = cfglib.CONF.src.user
        else:
            return

    if fallback_to_admin:
        default_user_name = cfglib.CONF.dst.user
    else:
        default_user_name = None
    try:
        with proxy_client.expect_exception(ks_exceptions.NotFound):
            return dst_keystone.try_get_user_by_name(
                src_user_name, default_user_name)
    except ks_exceptions.NotFound:
        return None
Ejemplo n.º 2
0
def get_dst_user_from_src_user_id(src_keystone,
                                  dst_keystone,
                                  src_user_id,
                                  fallback_to_admin=True):
    """Returns user from destination with the same name as on source. None if
    user does not exist"""
    try:
        with proxy_client.expect_exception(ks_exceptions.NotFound):
            src_user = src_keystone.keystone_client.users.get(src_user_id)
        src_user_name = src_user.name
    except ks_exceptions.NotFound:
        LOG.warning("User '%s' not found on SRC!", src_user_id)
        if fallback_to_admin:
            LOG.warning("Replacing user '%s' with SRC admin", src_user_id)
            src_user_name = cfglib.CONF.src.user
        else:
            return

    if fallback_to_admin:
        default_user_name = cfglib.CONF.dst.user
    else:
        default_user_name = None
    try:
        with proxy_client.expect_exception(ks_exceptions.NotFound):
            return dst_keystone.try_get_user_by_name(src_user_name,
                                                     default_user_name)
    except ks_exceptions.NotFound:
        return None
Ejemplo n.º 3
0
def get_dst_tenant_from_src_tenant_id(src_keystone, dst_keystone,
                                      src_tenant_id):
    try:
        with proxy_client.expect_exception(ks_exceptions.NotFound):
            client = src_keystone.keystone_client
            src_tenant = client.tenants.find(id=src_tenant_id)
    except ks_exceptions.NotFound:
        return None

    try:
        with proxy_client.expect_exception(ks_exceptions.NotFound):
            client = dst_keystone.keystone_client
            return client.tenants.find(name=src_tenant.name)
    except ks_exceptions.NotFound:
        return None
Ejemplo n.º 4
0
def get_dst_tenant_from_src_tenant_id(src_keystone, dst_keystone,
                                      src_tenant_id):
    try:
        with proxy_client.expect_exception(ks_exceptions.NotFound):
            client = src_keystone.keystone_client
            src_tenant = client.tenants.find(id=src_tenant_id)
    except ks_exceptions.NotFound:
        return None

    try:
        with proxy_client.expect_exception(ks_exceptions.NotFound):
            client = dst_keystone.keystone_client
            return client.tenants.find(name=src_tenant.name)
    except ks_exceptions.NotFound:
        return None
Ejemplo n.º 5
0
 def image_exists(self, image_id):
     with proxy_client.expect_exception(glance_exceptions.HTTPNotFound):
         try:
             self.get_image_raw(image_id)
             return True
         except glance_exceptions.HTTPNotFound:
             return False
Ejemplo n.º 6
0
    def _check_opts_img(self, opts):
        image_resource = self.cloud.resources[utl.IMAGE_RESOURCE]
        if opts and \
                opts.get('images_list') and opts.get('exclude_images_list'):
            raise exception.AbortMigrationError(
                "In the filter config file was specified "
                "'images_list' and 'exclude_images_list'. "
                "Must either specify - 'images_list' or "
                "'exclude_images_list'.")

        if opts and opts.get('images_list'):
            images_list = opts['images_list']
            for img_id in images_list:
                LOG.debug('Filtered image id: %s', img_id)
                try:
                    with proxy_client.expect_exception(glance_exc.NotFound):
                        img = image_resource.glance_client.images.get(img_id)
                    if img:
                        LOG.debug('Filter config check: Image ID %s is OK',
                                  img_id)
                except glance_exc.HTTPNotFound:
                    LOG.error(
                        'Filter config check: Image ID %s '
                        'is not present in source cloud, '
                        'please update your filter config. Aborting.', img_id)
                    raise
Ejemplo n.º 7
0
    def run(self, **kwargs):
        """
        Check filter file.

        Check and make sure all entries are present in source cloud.

        """
        search_opts = kwargs.get('search_opts', {})
        self._check_opts_img(kwargs.get('search_opts_img', {}))
        self._check_opts_vol(kwargs.get('search_opts_vol', {}))
        self._check_opts_vol_date(kwargs.get('search_opts_vol', {}))
        self._check_opts_tenant(kwargs.get('search_opts_tenant', {}))

        compute_resource = self.cloud.resources[utl.COMPUTE_RESOURCE]
        if search_opts and search_opts.get('id'):
            instances = search_opts['id']
            for instance_id in instances:
                LOG.debug('Filtered instance id: %s', instance_id)
                try:
                    with proxy_client.expect_exception(nova_exc.NotFound):
                        instance = \
                            compute_resource.nova_client.servers.get(
                                instance_id)
                    if instance:
                        LOG.debug('Filter config check: Instance ID %s is OK',
                                  instance_id)
                except nova_exc.NotFound:
                    LOG.error(
                        'Filter config check: Instance ID %s '
                        'is not present in source cloud, '
                        'please update your filter config. Aborting.',
                        instance_id)
                    raise
Ejemplo n.º 8
0
 def get_availability_zone(self, az_name):
     try:
         with proxy_client.expect_exception(nova_exc.NotFound):
             self.nova_client.availability_zones.find(zoneName=az_name)
             return az_name
     except nova_exc.NotFound:
         return None
Ejemplo n.º 9
0
    def _read_info_resources(self):
        """
        Read info about compute resources except instances from the cloud.
        """

        info = {'flavors': {},
                'default_quotas': {},
                'user_quotas': [],
                'project_quotas': []}

        for flavor in self.get_flavor_list(is_public=None):
            try:
                with proxy_client.expect_exception(nova_exc.NotFound):
                    internal_flavor = self.convert(flavor, cloud=self.cloud)
                if internal_flavor is None:
                    continue
                info['flavors'][flavor.id] = internal_flavor
                LOG.info("Got flavor '%s'", flavor.name)
                LOG.debug("%s", pprint.pformat(internal_flavor))
            except nova_exc.NotFound:
                # In case Nova failed with flavor-access-list obtaining
                # particular flavor it crashes with NotFound exception
                LOG.warning('Skipping invalid flavor %s', flavor.name)

        if self.config.migrate.migrate_quotas:
            info['default_quotas'] = self.get_default_quotas()
            info['project_quotas'], info['user_quotas'] = \
                self._read_info_quotas()

        return info
Ejemplo n.º 10
0
 def image_exists(self, image_id):
     with proxy_client.expect_exception(glance_exceptions.HTTPNotFound):
         try:
             self.get_image_raw(image_id)
             return True
         except glance_exceptions.HTTPNotFound:
             return False
Ejemplo n.º 11
0
    def _ensure_instance_flavor_exists(self, instance):
        flavor_id = instance['flavor_id']
        flavor_details = instance['flav_details']
        new_flavor_id = None
        try:
            with proxy_client.expect_exception(nova_exc.NotFound):
                flavor = self.get_flavor_from_id(flavor_id)
            need_new_flavor = (
                flavor.vcpus != flavor_details['vcpus'] or
                flavor.ram != flavor_details['memory_mb'] or
                flavor.disk != flavor_details['root_gb'] or
                flavor.ephemeral != flavor_details['ephemeral_gb'])
        except nova_exc.NotFound:
            need_new_flavor = True

        if need_new_flavor:
            new_flavor_id = str(uuid.uuid4())
            instance['flavor_id'] = new_flavor_id
            self.create_flavor(name='deleted_' + flavor_id,
                               flavorid=new_flavor_id,
                               ram=flavor_details['memory_mb'],
                               vcpus=flavor_details['vcpus'],
                               disk=flavor_details['root_gb'],
                               ephemeral=flavor_details['ephemeral_gb'])
        try:
            yield
        finally:
            if new_flavor_id is not None:
                self.delete_flavor(new_flavor_id)
Ejemplo n.º 12
0
    def _check_opts_img(self, opts):
        image_resource = self.cloud.resources[utl.IMAGE_RESOURCE]
        if opts and \
                opts.get('images_list') and opts.get('exclude_images_list'):
            raise exception.AbortMigrationError(
                "In the filter config file was specified "
                "'images_list' and 'exclude_images_list'. "
                "Must either specify - 'images_list' or "
                "'exclude_images_list'.")

        if opts and opts.get('images_list'):
            images_list = opts['images_list']
            for img_id in images_list:
                LOG.debug('Filtered image id: %s', img_id)
                try:
                    with proxy_client.expect_exception(glance_exc.NotFound):
                        img = image_resource.glance_client.images.get(img_id)
                    if img:
                        LOG.debug('Filter config check: Image ID %s is OK',
                                  img_id)
                except glance_exc.HTTPNotFound:
                    LOG.error('Filter config check: Image ID %s '
                              'is not present in source cloud, '
                              'please update your filter config. Aborting.',
                              img_id)
                    raise
Ejemplo n.º 13
0
    def _ensure_instance_flavor_exists(self, instance):
        flavor_id = instance['flavor_id']
        flavor_details = instance['flav_details']
        new_flavor_id = None
        try:
            with proxy_client.expect_exception(nova_exc.NotFound):
                flavor = self.get_flavor_from_id(flavor_id)
            need_new_flavor = (
                flavor.vcpus != flavor_details['vcpus']
                or flavor.ram != flavor_details['memory_mb']
                or flavor.disk != flavor_details['root_gb']
                or flavor.ephemeral != flavor_details['ephemeral_gb'])
        except nova_exc.NotFound:
            need_new_flavor = True

        if need_new_flavor:
            new_flavor_id = str(uuid.uuid4())
            instance['flavor_id'] = new_flavor_id
            self.create_flavor(name='deleted_' + flavor_id,
                               flavorid=new_flavor_id,
                               ram=flavor_details['memory_mb'],
                               vcpus=flavor_details['vcpus'],
                               disk=flavor_details['root_gb'],
                               ephemeral=flavor_details['ephemeral_gb'])
        try:
            yield
        finally:
            if new_flavor_id is not None:
                self.delete_flavor(new_flavor_id)
Ejemplo n.º 14
0
    def run(self, **kwargs):
        """
        Check filter file.

        Check and make sure all entries are present in source cloud.

        """
        search_opts = kwargs.get('search_opts', {})
        self._check_opts_img(kwargs.get('search_opts_img', {}))
        self._check_opts_vol(kwargs.get('search_opts_vol', {}))
        self._check_opts_vol_date(kwargs.get('search_opts_vol', {}))
        self._check_opts_tenant(kwargs.get('search_opts_tenant', {}))

        compute_resource = self.cloud.resources[utl.COMPUTE_RESOURCE]
        if search_opts and search_opts.get('id'):
            instances = search_opts['id']
            for instance_id in instances:
                LOG.debug('Filtered instance id: %s', instance_id)
                try:
                    with proxy_client.expect_exception(nova_exc.NotFound):
                        instance = \
                            compute_resource.nova_client.servers.get(
                                instance_id)
                    if instance:
                        LOG.debug('Filter config check: Instance ID %s is OK',
                                  instance_id)
                except nova_exc.NotFound:
                    LOG.error('Filter config check: Instance ID %s '
                              'is not present in source cloud, '
                              'please update your filter config. Aborting.',
                              instance_id)
                    raise
Ejemplo n.º 15
0
    def _read_info_resources(self):
        """
        Read info about compute resources except instances from the cloud.
        """

        info = {
            'flavors': {},
            'default_quotas': {},
            'user_quotas': [],
            'project_quotas': []
        }

        for flavor in self.get_flavor_list(is_public=None):
            try:
                with proxy_client.expect_exception(nova_exc.NotFound):
                    internal_flavor = self.convert(flavor, cloud=self.cloud)
                if internal_flavor is None:
                    continue
                info['flavors'][flavor.id] = internal_flavor
                LOG.info("Got flavor '%s'", flavor.name)
                LOG.debug("%s", pprint.pformat(internal_flavor))
            except nova_exc.NotFound:
                # In case Nova failed with flavor-access-list obtaining
                # particular flavor it crashes with NotFound exception
                LOG.warning('Skipping invalid flavor %s', flavor.name)

        if self.config.migrate.migrate_quotas:
            info['default_quotas'] = self.get_default_quotas()
            info['project_quotas'], info['user_quotas'] = \
                self._read_info_quotas()

        return info
Ejemplo n.º 16
0
 def get_availability_zone(self, az_name):
     try:
         with proxy_client.expect_exception(nova_exc.NotFound):
             self.nova_client.availability_zones.find(zoneName=az_name)
             return az_name
     except nova_exc.NotFound:
         return None
Ejemplo n.º 17
0
    def get_instances_list(self, detailed=True, search_opts=None, marker=None,
                           limit=None):
        """
        Get a list of servers.

        :param detailed: Whether to return detailed server info (optional).
        :param search_opts: Search options to filter out servers (optional).
        :param marker: Begin returning servers that appear later in the server
                       list than that represented by this server id (optional).
        :param limit: Maximum number of servers to return (optional).

        :rtype: list of :class:`Server`
        """
        ids = search_opts.get('id', None) if search_opts else None
        if not ids:
            servers = self.nova_client.servers.list(
                detailed=detailed, search_opts=search_opts, marker=marker,
                limit=limit)
        else:
            ids = ids if isinstance(ids, list) else [ids]
            servers = []
            for i in ids:
                try:
                    with proxy_client.expect_exception(nova_exc.NotFound):
                        servers.append(self.nova_client.servers.get(i))
                except nova_exc.NotFound:
                    LOG.warning("No server with ID of '%s' exists.", i)

        active_computes = self.get_compute_hosts()
        servers = [i for i in servers
                   if getattr(i, INSTANCE_HOST_ATTRIBUTE) in active_computes]

        return servers
Ejemplo n.º 18
0
def check_affinity_api(cloud):
    compute_resource = cloud.resources[utils.COMPUTE_RESOURCE]
    with proxy_client.expect_exception(nova_exceptions.NotFound):
        try:
            compute_resource.nova_client.server_groups.list()
        except nova_exceptions.NotFound:
            raise cf_exceptions.AbortMigrationError(
                "'%s' cloud does not support affinity/anti-affinity "
                "(Nova server groups) API." % cloud.position)
Ejemplo n.º 19
0
def check_affinity_api(cloud):
    compute_resource = cloud.resources[utils.COMPUTE_RESOURCE]
    with proxy_client.expect_exception(nova_exceptions.NotFound):
        try:
            compute_resource.nova_client.server_groups.list()
        except nova_exceptions.NotFound:
            raise cf_exceptions.AbortMigrationError(
                "'%s' cloud does not support affinity/anti-affinity "
                "(Nova server groups) API." % cloud.position)
Ejemplo n.º 20
0
 def _add_flavor_access_for_tenants(self, flavor_id, tenant_ids):
     for t in tenant_ids:
         LOG.debug("Adding access for tenant '%s' to flavor '%s'", t,
                   flavor_id)
         try:
             with proxy_client.expect_exception(nova_exc.Conflict):
                 self.add_flavor_access(flavor_id, t)
         except nova_exc.Conflict:
             LOG.debug("Tenant '%s' already has access to flavor '%s'", t,
                       flavor_id)
Ejemplo n.º 21
0
 def _add_flavor_access_for_tenants(self, flavor_id, tenant_ids):
     for t in tenant_ids:
         LOG.debug("Adding access for tenant '%s' to flavor '%s'", t,
                   flavor_id)
         try:
             with proxy_client.expect_exception(nova_exc.Conflict):
                 self.add_flavor_access(flavor_id, t)
         except nova_exc.Conflict:
             LOG.debug("Tenant '%s' already has access to flavor '%s'", t,
                       flavor_id)
Ejemplo n.º 22
0
 def try_get_tenant_name_by_id(self, tenant_id, default=None):
     """ Same as `get_tenant_by_id` but returns `default` in case tenant
     ID is not present """
     try:
         with proxy_client.expect_exception(ks_exceptions.NotFound):
             return self.keystone_client.tenants.get(tenant_id).name
     except ks_exceptions.NotFound:
         LOG.warning("Tenant '%s' not found, returning default value = "
                     "'%s'", tenant_id, default)
         return default
Ejemplo n.º 23
0
def is_vm_deleted(client, instance_id):
    """
    Returns True when there is no VM with ID provided in first argument.
    """
    try:
        with proxy_client.expect_exception(nova_exc.NotFound):
            client.servers.get(instance_id)
        return False
    except nova_exc.NotFound:
        return True
Ejemplo n.º 24
0
    def create_tenant(self, tenant_name, description=None, enabled=True):
        """ Create new tenant in keystone. """

        try:
            with proxy_client.expect_exception(ks_exceptions.Conflict):
                return self.keystone_client.tenants.create(
                    tenant_name=tenant_name, description=description,
                    enabled=enabled)
        except ks_exceptions.Conflict:
            return self.get_tenant_by_name(tenant_name)
Ejemplo n.º 25
0
 def try_get_tenant_name_by_id(self, tenant_id, default=None):
     """ Same as `get_tenant_by_id` but returns `default` in case tenant
     ID is not present """
     try:
         with proxy_client.expect_exception(ks_exceptions.NotFound):
             return self.keystone_client.tenants.get(tenant_id).name
     except ks_exceptions.NotFound:
         LOG.warning(
             "Tenant '%s' not found, returning default value = "
             "'%s'", tenant_id, default)
         return default
Ejemplo n.º 26
0
 def get_ref_image(self, image_id):
     try:
         # ssl.ZeroReturnError happens because a size of an image is zero
         with proxy_client.expect_exception(
                 glance_exceptions.NotFound,
                 glance_exceptions.HTTPInternalServerError,
                 ssl.ZeroReturnError):
             return self.get_resp(self.glance_client.images.data(image_id))
     except (glance_exceptions.HTTPInternalServerError,
             glance_exceptions.HTTPNotFound, ssl.ZeroReturnError):
         raise exception.ImageDownloadError
Ejemplo n.º 27
0
 def try_get_user_by_id(self, user_id, default=None):
     if default is None:
         admin_usr = self.try_get_user_by_name(self.config.cloud.user)
         default = admin_usr.id
     try:
         with proxy_client.expect_exception(ks_exceptions.NotFound):
             return self.keystone_client.users.find(id=user_id)
     except ks_exceptions.NotFound:
         LOG.warning("User '%s' has not been found, returning default "
                     "value = '%s'", user_id, default)
         return self.keystone_client.users.find(id=default)
Ejemplo n.º 28
0
    def create_tenant(self, tenant_name, description=None, enabled=True):
        """ Create new tenant in keystone. """

        try:
            with proxy_client.expect_exception(ks_exceptions.Conflict):
                return self.keystone_client.tenants.create(
                    tenant_name=tenant_name,
                    description=description,
                    enabled=enabled)
        except ks_exceptions.Conflict:
            return self.get_tenant_by_name(tenant_name)
Ejemplo n.º 29
0
 def wait_for_instance_to_be_deleted(nv_client, instance):
     retryer = retrying.Retry(max_time=300,
                              retry_on_return_value=True,
                              return_value=instance,
                              expected_exceptions=[nova_exc.NotFound],
                              retry_message="Instance still exists")
     try:
         with proxy_client.expect_exception(nova_exc.NotFound):
             retryer.run(nv_client.nova_client.servers.get, instance.id)
     except nova_exc.NotFound:
         LOG.info("Instance '%s' has been successfully deleted.",
                  instance.name)
Ejemplo n.º 30
0
 def check_quotas(self, cloud):
     compute_resource = cloud.resources[utils.COMPUTE_RESOURCE]
     keystone_resource = cloud.resources[utils.IDENTITY_RESOURCE]
     tenant = cloud.cloud_config['cloud']['tenant']
     ten_id = keystone_resource.get_tenant_id_by_name(tenant)
     with proxy_client.expect_exception(nova_exceptions.ClientException):
         try:
             compute_resource.nova_client.quotas.update(ten_id)
         except nova_exceptions.ClientException:
             raise cf_exceptions.AbortMigrationError(
                 "'%s' cloud does not support quotas "
                 "(Nova quotas)." % cloud.position)
Ejemplo n.º 31
0
 def check_quotas(self, cloud):
     compute_resource = cloud.resources[utils.COMPUTE_RESOURCE]
     keystone_resource = cloud.resources[utils.IDENTITY_RESOURCE]
     tenant = cloud.cloud_config['cloud']['tenant']
     ten_id = keystone_resource.get_tenant_id_by_name(tenant)
     with proxy_client.expect_exception(nova_exceptions.ClientException):
         try:
             compute_resource.nova_client.quotas.update(ten_id)
         except nova_exceptions.ClientException:
             raise cf_exceptions.AbortMigrationError(
                 "'%s' cloud does not support quotas "
                 "(Nova quotas)." % cloud.position)
Ejemplo n.º 32
0
 def wait_for_instance_to_be_deleted(cls, nv_client, instance_id,
                                     timeout=60):
     try:
         delay = 1
         while timeout > delay:
             with proxy_client.expect_exception(nova_exc.NotFound):
                 nv_client.nova_client.servers.get(instance_id)
             LOG.info("Instance still exist, waiting %s sec", delay)
             time.sleep(delay)
             delay *= 2
     except nova_exc.NotFound:
         LOG.info("Instance successfully deleted.")
Ejemplo n.º 33
0
 def try_get_user_by_id(self, user_id, default=None):
     if default is None:
         admin_usr = self.try_get_user_by_name(self.config.cloud.user)
         default = admin_usr.id
     try:
         with proxy_client.expect_exception(ks_exceptions.NotFound):
             return self.keystone_client.users.find(id=user_id)
     except ks_exceptions.NotFound:
         LOG.warning(
             "User '%s' has not been found, returning default "
             "value = '%s'", user_id, default)
         return self.keystone_client.users.find(id=default)
Ejemplo n.º 34
0
    def get_server_groups(self):
        """
        Return list of dictionaries containing server group details

        Returns:
            list: Empty if no server groups exist or server groups are not
                supported

            [
                {
                    "user": "******",
                    "tenant": "<tenant name>",
                    "uuid": "<group uuid>",
                    "name": "<group name>",
                    "policies": [<policy_name>, ...]
                }
            ]
        """
        groups = []
        try:
            with proxy_client.expect_exception(nova_exc.NotFound):
                self._nova_client.server_groups.list()

            for row in self._execute(SQL_SELECT_ALL_GROUPS).fetchall():
                LOG.debug("Resulting row: %s", row)
                sql = SQL_SELECT_POLICY % row[4]
                policies = []
                for policy in self._execute(sql).fetchall():
                    policies.append(policy[0])

                tenant_name = self.identity.try_get_tenant_name_by_id(row[1])
                if tenant_name is None:
                    LOG.info(
                        "Tenant '%s' does not exist on the SRC. Skipping "
                        "server group '%s'...", row[1], row[3])
                    continue

                groups.append({
                    "user":
                    self.identity.try_get_username_by_id(row[0]),
                    "tenant":
                    tenant_name,
                    "uuid":
                    row[2],
                    "name":
                    row[3],
                    "policies":
                    policies
                })
        except nova_exc.NotFound:
            LOG.info("Cloud does not support server_groups")
        return groups
Ejemplo n.º 35
0
 def get_ref_image(self, image_id):
     try:
         # ssl.ZeroReturnError happens because a size of an image is zero
         with proxy_client.expect_exception(
             glance_exceptions.NotFound,
             glance_exceptions.HTTPInternalServerError,
             ssl.ZeroReturnError
         ):
             return self.get_resp(self.glance_client.images.data(image_id))
     except (glance_exceptions.HTTPInternalServerError,
             glance_exceptions.HTTPNotFound,
             ssl.ZeroReturnError):
         raise exception.ImageDownloadError
Ejemplo n.º 36
0
 def finish(self, vol):
     try:
         with proxy_client.expect_exception(cinder_exc.BadRequest):
             self.cinder_client.volumes.set_bootable(
                 vol[utils.VOLUME_BODY]['id'],
                 vol[utils.VOLUME_BODY]['bootable'])
     except cinder_exc.BadRequest:
         LOG.info("Can't update bootable flag of volume with id = %s "
                  "using API, trying to use DB...",
                  vol[utils.VOLUME_BODY]['id'])
         self.__patch_option_bootable_of_volume(
             vol[utils.VOLUME_BODY]['id'],
             vol[utils.VOLUME_BODY]['bootable'])
Ejemplo n.º 37
0
 def finish(self, vol):
     try:
         with proxy_client.expect_exception(cinder_exc.BadRequest):
             self.cinder_client.volumes.set_bootable(
                 vol[utl.VOLUME_BODY]['id'],
                 vol[utl.VOLUME_BODY]['bootable'])
     except cinder_exc.BadRequest:
         LOG.info("Can't update bootable flag of volume with id = %s "
                  "using API, trying to use DB...",
                  vol[utl.VOLUME_BODY]['id'])
         self.__patch_option_bootable_of_volume(
             vol[utl.VOLUME_BODY]['id'],
             vol[utl.VOLUME_BODY]['bootable'])
Ejemplo n.º 38
0
    def is_nova_instance(self, object_id):
        """
        Define OpenStack Nova Server instance by id.

        :param object_id: ID of supposed Nova Server instance
        :return: True - if it is Nova Server instance, False - if it is not
        """
        try:
            with proxy_client.expect_exception(nova_exc.NotFound):
                self.get_instance(object_id)
        except nova_exc.NotFound:
            LOG.error("%s is not a Nova Server instance", object_id)
            return False
        return True
Ejemplo n.º 39
0
    def try_get_tenant_by_id(self, tenant_id, default=None):
        """Returns `keystoneclient.tenants.Tenant` object based on tenant ID
        provided. If not found - returns :arg default: tenant. If
        :arg default: is not specified - returns `config.cloud.tenant`"""

        tenants = self.keystone_client.tenants
        try:
            with proxy_client.expect_exception(ks_exceptions.NotFound):
                return tenants.get(tenant_id)
        except ks_exceptions.NotFound:
            if default is None:
                return self.get_tenant_by_name(self.config.cloud.tenant)
            else:
                return tenants.get(default)
Ejemplo n.º 40
0
    def create_user(self, name, password=None, email=None, tenant_id=None,
                    enabled=True):
        """ Create new user in keystone. """

        try:
            with proxy_client.expect_exception(ks_exceptions.Conflict):
                return self.keystone_client.users.create(name=name,
                                                         password=password,
                                                         email=email,
                                                         tenant_id=tenant_id,
                                                         enabled=enabled)
        except ks_exceptions.Conflict:
            LOG.warning('Conflict creating user %s', name, exc_info=True)
            return self.try_get_user_by_name(name)
Ejemplo n.º 41
0
    def create_volume(self, size, **kwargs):
        """Creates volume of given size
        :raises: OverLimit in case quota exceeds for tenant
        """
        cinder = self.cinder_client
        tenant_id = kwargs.get('project_id')

        # if volume needs to be created in non-admin tenant, re-auth is
        # required in that tenant
        if tenant_id:
            identity = self.cloud.resources[utils.IDENTITY_RESOURCE]
            ks = identity.keystone_client
            user = self.config.cloud.user
            with keystone.AddAdminUserToNonAdminTenant(ks, user, tenant_id):
                tenant = ks.tenants.get(tenant_id)
                cinder = self.proxy(self.get_client(tenant=tenant.name),
                                    self.config)

                with proxy_client.expect_exception(cinder_exc.OverLimit):
                    return cinder.volumes.create(size, **kwargs)
        else:
            with proxy_client.expect_exception(cinder_exc.OverLimit):
                return cinder.volumes.create(size, **kwargs)
Ejemplo n.º 42
0
    def try_get_tenant_by_id(self, tenant_id, default=None):
        """Returns `keystoneclient.tenants.Tenant` object based on tenant ID
        provided. If not found - returns :arg default: tenant. If
        :arg default: is not specified - returns `config.cloud.tenant`"""

        tenants = self.keystone_client.tenants
        try:
            with proxy_client.expect_exception(ks_exceptions.NotFound):
                return tenants.get(tenant_id)
        except ks_exceptions.NotFound:
            if default is None:
                return self.get_tenant_by_name(self.config.cloud.tenant)
            else:
                return tenants.get(default)
Ejemplo n.º 43
0
    def create_volume(self, size, **kwargs):
        """Creates volume of given size
        :raises: OverLimit in case quota exceeds for tenant
        """
        cinder = self.cinder_client
        tenant_id = kwargs.get('project_id')

        # if volume needs to be created in non-admin tenant, re-auth is
        # required in that tenant
        if tenant_id:
            identity = self.cloud.resources[utils.IDENTITY_RESOURCE]
            ks = identity.keystone_client
            user = self.config.cloud.user
            with keystone.AddAdminUserToNonAdminTenant(ks, user, tenant_id):
                tenant = ks.tenants.get(tenant_id)
                cinder = self.proxy(self.get_client(tenant=tenant.name),
                                    self.config)

                with proxy_client.expect_exception(cinder_exc.OverLimit):
                    return cinder.volumes.create(size, **kwargs)
        else:
            with proxy_client.expect_exception(cinder_exc.OverLimit):
                return cinder.volumes.create(size, **kwargs)
Ejemplo n.º 44
0
    def is_nova_instance(self, object_id):
        """
        Define OpenStack Nova Server instance by id.

        :param object_id: ID of supposed Nova Server instance
        :return: True - if it is Nova Server instance, False - if it is not
        """
        try:
            with proxy_client.expect_exception(nova_exc.NotFound):
                self.get_instance(object_id)
        except nova_exc.NotFound:
            LOG.error("%s is not a Nova Server instance", object_id)
            return False
        return True
Ejemplo n.º 45
0
def is_vm_status_in(client, instance_id, statuses):
    """
    Returns True when nova instance with ID that is equal to instance_id
    argument have status that is equal to status argument.
    """
    statuses = [s.lower() for s in statuses]
    try:
        with proxy_client.expect_exception(nova_exc.NotFound):
            instance = client.servers.get(instance_id)
        status = instance.status.lower()
        if status == ERROR:
            raise RuntimeError("VM in error status")
        return status in statuses
    except nova_exc.NotFound:
        return False
Ejemplo n.º 46
0
    def get_instances_list(self,
                           detailed=True,
                           search_opts=None,
                           marker=None,
                           limit=None):
        """
        Get a list of servers.

        :param detailed: Whether to return detailed server info (optional).
        :param search_opts: Search options to filter out servers (optional).
        :param marker: Begin returning servers that appear later in the server
                       list than that represented by this server id (optional).
        :param limit: Maximum number of servers to return (optional).

        :rtype: list of :class:`Server`
        """
        ids = search_opts.get('id', None) if search_opts else None
        if not ids:
            servers = self.nova_client.servers.list(detailed=detailed,
                                                    search_opts=search_opts,
                                                    marker=marker,
                                                    limit=limit)
        else:
            ids = ids if isinstance(ids, list) else [ids]
            servers = []
            for i in ids:
                try:
                    with proxy_client.expect_exception(nova_exc.NotFound):
                        servers.append(self.nova_client.servers.get(i))
                except nova_exc.NotFound:
                    LOG.warning("No server with ID of '%s' exists.", i)

        active_computes = self.get_compute_hosts()
        active_servers = []

        for server in servers:
            server_host = getattr(server, INSTANCE_HOST_ATTRIBUTE)
            if server_host in active_computes:
                active_servers.append(server)
                continue

            LOG.debug(
                "Instance '%s' has been excluded from VMs list, because "
                "it is running on non-active compute host '%s'.", server.id,
                server_host)

        return active_servers
Ejemplo n.º 47
0
    def __init__(self, keystone, admin_user, tenant, member_role='admin'):
        """
        :tenant: can be either tenant name or tenant ID
        """

        self.keystone = keystone
        try:
            with proxy_client.expect_exception(ks_exceptions.NotFound):
                self.tenant = find_by_name(
                    'tenant', self.keystone.tenants.list(), tenant)
        except ks_exceptions.NotFound:
            self.tenant = self.keystone.tenants.get(tenant)
        self.user = find_by_name(
            'user', self.keystone.users.list(), admin_user)
        self.role = find_by_name(
            'role', self.keystone.roles.list(), member_role)
        self.already_member = False
Ejemplo n.º 48
0
    def get_server_groups(self):
        """
        Return list of dictionaries containing server group details

        Returns:
            list: Empty if no server groups exist or server groups are not
                supported

            [
                {
                    "user": "******",
                    "tenant": "<tenant name>",
                    "uuid": "<group uuid>",
                    "name": "<group name>",
                    "policies": [<policy_name>, ...]
                }
            ]
        """
        groups = []
        try:
            with proxy_client.expect_exception(nova_exc.NotFound):
                self._nova_client.server_groups.list()

            for row in self._execute(SQL_SELECT_ALL_GROUPS).fetchall():
                LOG.debug("Resulting row: %s", row)
                sql = SQL_SELECT_POLICY % row[4]
                policies = []
                for policy in self._execute(sql).fetchall():
                    policies.append(policy[0])

                tenant_name = self.identity.try_get_tenant_name_by_id(row[1])
                if tenant_name is None:
                    LOG.info("Tenant '%s' does not exist on the SRC. Skipping "
                             "server group '%s'...", row[1], row[3])
                    continue

                groups.append(
                    {"user": self.identity.try_get_username_by_id(row[0]),
                     "tenant": tenant_name,
                     "uuid": row[2],
                     "name": row[3],
                     "policies": policies})
        except nova_exc.NotFound:
            LOG.info("Cloud does not support server_groups")
        return groups
Ejemplo n.º 49
0
 def _check_opts_img(self, opts):
     image_resource = self.cloud.resources[utl.IMAGE_RESOURCE]
     if opts and opts.get('images_list'):
         images_list = opts['images_list']
         for img_id in images_list:
             LOG.debug('Filtered image id: %s', img_id)
             try:
                 with proxy_client.expect_exception(glance_exc.NotFound):
                     img = image_resource.glance_client.images.get(img_id)
                 if img:
                     LOG.debug('Filter config check: Image ID %s is OK',
                               img_id)
             except glance_exc.HTTPNotFound:
                 LOG.error('Filter config check: Image ID %s '
                           'is not present in source cloud, '
                           'please update your filter config. Aborting.',
                           img_id)
                 raise
Ejemplo n.º 50
0
    def create_user(self,
                    name,
                    password=None,
                    email=None,
                    tenant_id=None,
                    enabled=True):
        """ Create new user in keystone. """

        try:
            with proxy_client.expect_exception(ks_exceptions.Conflict):
                return self.keystone_client.users.create(name=name,
                                                         password=password,
                                                         email=email,
                                                         tenant_id=tenant_id,
                                                         enabled=enabled)
        except ks_exceptions.Conflict:
            LOG.warning('Conflict creating user %s', name, exc_info=True)
            return self.try_get_user_by_name(name)
Ejemplo n.º 51
0
    def __init__(self, keystone, admin_user, tenant, member_role='admin'):
        """
        :tenant: can be either tenant name or tenant ID
        """

        self.keystone = keystone
        try:
            with proxy_client.expect_exception(ks_exceptions.NotFound):
                self.tenant = find_by_name('tenant',
                                           self.keystone.tenants.list(),
                                           tenant)
        except ks_exceptions.NotFound:
            self.tenant = self.keystone.tenants.get(tenant)
        self.user = find_by_name('user', self.keystone.users.list(),
                                 admin_user)
        self.role = find_by_name('role', self.keystone.roles.list(),
                                 member_role)
        self.already_member = False
Ejemplo n.º 52
0
 def _upload_user_tenant_roles(self, user_tenants_roles, users, tenants):
     roles_id = {
         role.name.lower(): role.id
         for role in self.get_roles_list()
     }
     dst_users = {
         user.name.lower(): user.id
         for user in self.get_users_list()
     }
     dst_roles = {
         role.id: role.name.lower()
         for role in self.get_roles_list()
     }
     get_user_roles = self._get_user_roles_cached()
     for _user in users:
         user = _user['user']
         if user['name'] not in dst_users:
             continue
         for _tenant in tenants:
             tenant = _tenant['tenant']
             user_roles_objs = get_user_roles(_user['meta']['new_id'],
                                              _tenant['meta']['new_id'])
             exists_roles = [
                 dst_roles[role]
                 if not hasattr(role, 'name') else role.name.lower()
                 for role in user_roles_objs
             ]
             user_roles = user_tenants_roles[user['name'].lower()]
             for _role in user_roles[tenant['name'].lower()]:
                 role = _role['role']
                 if role['name'].lower() in exists_roles:
                     continue
                 try:
                     with proxy_client.expect_exception(
                             ks_exceptions.Conflict):
                         self.keystone_client.roles.add_user_role(
                             _user['meta']['new_id'],
                             roles_id[role['name'].lower()],
                             _tenant['meta']['new_id'])
                 except ks_exceptions.Conflict:
                     LOG.info(
                         "Role '%s' for user '%s' in tenant '%s' "
                         "already exists, skipping", role['name'],
                         user['name'], tenant['name'])
Ejemplo n.º 53
0
 def _check_opts_tenant(self, opts):
     ident_resource = self.cloud.resources[utl.IDENTITY_RESOURCE]
     if opts and opts.get('tenant_id'):
         tenants = opts['tenant_id']
         for tenant_id in tenants:
             LOG.debug('Filtered tenant id: %s', tenant_id)
             try:
                 with proxy_client.expect_exception(keystone_exc.NotFound):
                     tenant = ident_resource.keystone_client.tenants.find(
                         id=tenant_id)
                 if tenant:
                     LOG.debug('Filter config check: Tenant ID %s is OK',
                               tenant_id)
             except keystone_exc.NotFound:
                 LOG.error('Filter config check: Tenant ID %s '
                           'is not present in source cloud, '
                           'please update your filter config. Aborting.',
                           tenant_id)
                 raise
Ejemplo n.º 54
0
 def _check_opts_vol(self, opts):
     if not opts:
         return
     cinder_resource = self.cloud.resources[utl.STORAGE_RESOURCE]
     if opts.get('volumes_list'):
         volumes_list = opts['volumes_list']
         for vol_id in volumes_list:
             LOG.debug('Filtered volume id: %s', vol_id)
             try:
                 with proxy_client.expect_exception(cinder_exc.NotFound):
                     vol = cinder_resource.cinder_client.volumes.get(vol_id)
                 if vol:
                     LOG.debug('Filter config check: Volume ID %s is OK',
                               vol_id)
             except cinder_exc.NotFound:
                 LOG.error('Filter config check: Volume ID %s '
                           'is not present in source cloud, '
                           'please update your filter config. Aborting.',
                           vol_id)
                 raise
Ejemplo n.º 55
0
 def _check_opts_vol(self, opts):
     if not opts:
         return
     cinder_resource = self.cloud.resources[utl.STORAGE_RESOURCE]
     if opts.get('volumes_list'):
         volumes_list = opts['volumes_list']
         for vol_id in volumes_list:
             LOG.debug('Filtered volume id: %s', vol_id)
             try:
                 with proxy_client.expect_exception(cinder_exc.NotFound):
                     vol = cinder_resource.cinder_client.volumes.get(vol_id)
                 if vol:
                     LOG.debug('Filter config check: Volume ID %s is OK',
                               vol_id)
             except cinder_exc.NotFound:
                 LOG.error(
                     'Filter config check: Volume ID %s '
                     'is not present in source cloud, '
                     'please update your filter config. Aborting.', vol_id)
                 raise
Ejemplo n.º 56
0
    def _deploy_server_group(self, server_group):
        """
        Uses the sql connector to do the following:
            Inserts specified uuid, name, tenant uuid and user uuid into
            destination cloud.
            Retrieves internally incremented group id
            Inserts associated policies using group id

        """
        LOG.info("Deploying server_group for tenant %s to destination: %s",
                 server_group['tenant'], server_group['name'])

        try:
            with proxy_client.expect_exception(
                    keystone.ks_exceptions.NotFound):
                tenant_id = self.identity.get_tenant_id_by_name(
                    server_group["tenant"])
        except keystone.ks_exceptions.NotFound:
            LOG.info(
                "Tenant '%s' does not exist on DST. Skipping server group"
                " '%s' with id='%s'...", server_group['tenant'],
                server_group['name'], server_group['uuid'])
            return

        sql = SQL_INSERT_GROUP.format(
            server_group['uuid'],
            server_group['name'],
            tenant_id,
            self.identity.try_get_user_by_name(server_group["user"],
                                               self.config.cloud.user).id,
        )
        self._execute(sql)

        sql = SQL_SELECT_GROUP_ID.format(server_group['uuid'])
        gid = self._execute(sql).fetchone()[0]
        for policy in server_group['policies']:
            sql = SQL_INSERT_POLICY.format(gid, policy)
            self._execute(sql)

        return server_group
Ejemplo n.º 57
0
 def _check_opts_tenant(self, opts):
     ident_resource = self.cloud.resources[utl.IDENTITY_RESOURCE]
     if opts and opts.get('tenant_id'):
         tenants = opts['tenant_id']
         if len(tenants) > 1:
             raise exception.AbortMigrationError(
                 'More than one tenant in tenant filters is not supported.')
         for tenant_id in tenants:
             LOG.debug('Filtered tenant id: %s', tenant_id)
             try:
                 with proxy_client.expect_exception(keystone_exc.NotFound):
                     tenant = ident_resource.keystone_client.tenants.find(
                         id=tenant_id)
                 if tenant:
                     LOG.debug('Filter config check: Tenant ID %s is OK',
                               tenant_id)
             except keystone_exc.NotFound:
                 LOG.error(
                     'Filter config check: Tenant ID %s '
                     'is not present in source cloud, '
                     'please update your filter config. Aborting.',
                     tenant_id)
                 raise
Ejemplo n.º 58
0
 def try_get_username_by_id(self, user_id, default=None):
     try:
         with proxy_client.expect_exception(ks_exceptions.NotFound):
             return self.keystone_client.users.get(user_id).name
     except ks_exceptions.NotFound:
         return default