Beispiel #1
0
    def _setup_target_cell_db(self):
        """Creates the instance and its related records in the target cell

        Upon successful completion the self._target_cell_context and
        self._target_cell_instance variables are set.

        :returns: A 2-item tuple of:

            - The active Migration object from the target cell DB
            - The CellMapping for the target cell
        """
        LOG.debug(
            'Setting up the target cell database for the instance and '
            'its related records.',
            instance=self.instance)
        target_cell_mapping = self._get_target_cell_mapping()
        # Clone the context targeted at the source cell and then target the
        # clone at the target cell.
        self._target_cell_context = copy.copy(self.context)
        nova_context.set_target_cell(self._target_cell_context,
                                     target_cell_mapping)
        task = TargetDBSetupTask(self.context, self.instance,
                                 self.source_migration,
                                 self._target_cell_context)
        self._target_cell_instance, target_cell_migration = task.execute()
        self._completed_tasks['TargetDBSetupTask'] = task
        return target_cell_migration, target_cell_mapping
Beispiel #2
0
    def show(self, req, id):
        """Shows the physical/usage resource given by hosts.

        :param id: hostname
        :returns: expected to use HostShowTemplate.
            ex.::

                {'host': {'resource':D},..}
                D: {'host': 'hostname','project': 'admin',
                    'cpu': 1, 'memory_mb': 2048, 'disk_gb': 30}
        """
        context = req.environ['nova.context']
        context.can(hosts_policies.BASE_POLICY_NAME)
        host_name = id
        try:
            mapping = objects.HostMapping.get_by_host(context, host_name)
            nova_context.set_target_cell(context, mapping.cell_mapping)
            compute_node = (
                objects.ComputeNode.get_first_node_by_host_for_old_compat(
                    context, host_name))
            instances = self.api.instance_get_all_by_host(context, host_name)
        except (exception.ComputeHostNotFound,
                exception.HostMappingNotFound) as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())
        resources = [self._get_total_resources(host_name, compute_node)]
        resources.append(self._get_used_now_resources(host_name, compute_node))
        resources.append(
            self._get_resource_totals_from_instances(host_name, instances))
        by_proj_resources = self._get_resources_by_project(
            host_name, instances)
        for resource in six.itervalues(by_proj_resources):
            resources.append({'resource': resource})
        return {'host': resources}
Beispiel #3
0
    def show(self, req, id):
        """Shows the physical/usage resource given by hosts.

        :param id: hostname
        :returns: expected to use HostShowTemplate.
            ex.::

                {'host': {'resource':D},..}
                D: {'host': 'hostname','project': 'admin',
                    'cpu': 1, 'memory_mb': 2048, 'disk_gb': 30}
        """
        context = req.environ['nova.context']
        context.can(hosts_policies.BASE_POLICY_NAME)
        host_name = id
        try:
            mapping = objects.HostMapping.get_by_host(context, host_name)
            nova_context.set_target_cell(context, mapping.cell_mapping)
            compute_node = (
                objects.ComputeNode.get_first_node_by_host_for_old_compat(
                    context, host_name))
            instances = self.api.instance_get_all_by_host(context, host_name)
        except (exception.ComputeHostNotFound,
                exception.HostMappingNotFound) as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())
        resources = [self._get_total_resources(host_name, compute_node)]
        resources.append(self._get_used_now_resources(host_name,
                                                      compute_node))
        resources.append(self._get_resource_totals_from_instances(host_name,
                                                                  instances))
        by_proj_resources = self._get_resources_by_project(host_name,
                                                           instances)
        for resource in six.itervalues(by_proj_resources):
            resources.append({'resource': resource})
        return {'host': resources}
 def setUp(self):
     super(TargetDBSetupTaskTestCase, self).setUp()
     cells = list(self.cell_mappings.values())
     self.source_cell = cells[0]
     self.target_cell = cells[1]
     # Pass is_admin=True because of the funky DB API
     # _check_instance_exists_in_project check when creating instance tags.
     self.source_context = nova_context.RequestContext(
         user_id='fake-user', project_id='fake-project', is_admin=True)
     self.target_context = self.source_context.elevated()  # copy source
     nova_context.set_target_cell(self.source_context, self.source_cell)
     nova_context.set_target_cell(self.target_context, self.target_cell)
Beispiel #5
0
 def wrapper(self, context, *args, **kwargs):
     instance = kwargs.get('instance') or args[0]
     try:
         im = objects.InstanceMapping.get_by_instance_uuid(
             context, instance.uuid)
     except exception.InstanceMappingNotFound:
         LOG.error(_LE('InstanceMapping not found, unable to target cell'),
                   instance=instance)
         im = None
     else:
         LOG.debug('Targeting cell %(cell)s for conductor method %(meth)s',
                   {'cell': im.cell_mapping.identity,
                    'meth': fn.__name__})
         # NOTE(danms): Target our context to the cell for the rest of
         # this request, so that none of the subsequent code needs to
         # care about it.
         nova_context.set_target_cell(context, im.cell_mapping)
     return fn(self, context, *args, **kwargs)