Exemple #1
0
 def schedule_run_instance(self, context, request_spec,
                           admin_password, injected_files,
                           requested_networks, is_first_time,
                           filter_properties, legacy_bdm_in_spec):
     """Create and run an instance or instances."""
     instance_uuids = request_spec.get('instance_uuids')
     for num, instance_uuid in enumerate(instance_uuids):
         request_spec['instance_properties']['launch_index'] = num
         try:
             host = self._schedule(context, CONF.compute_topic,
                                   request_spec, filter_properties)
             updated_instance = driver.instance_update_db(context,
                     instance_uuid)
             self.compute_rpcapi.run_instance(context,
                     instance=updated_instance, host=host,
                     requested_networks=requested_networks,
                     injected_files=injected_files,
                     admin_password=admin_password,
                     is_first_time=is_first_time,
                     request_spec=request_spec,
                     filter_properties=filter_properties,
                     legacy_bdm_in_spec=legacy_bdm_in_spec)
         except Exception as ex:
             # NOTE(vish): we don't reraise the exception here to make sure
             #             that all instances in the request get set to
             #             error properly
             driver.handle_schedule_error(context, ex, instance_uuid,
                                          request_spec)
    def test_basic_schedule_run_instance(self):
        ctxt = context.RequestContext('fake', 'fake', False)
        ctxt_elevated = 'fake-context-elevated'
        instance_opts = {'fake_opt1': 'meow', 'launch_index': -1}
        instance1 = {'uuid': 'fake-uuid1'}
        instance2 = {'uuid': 'fake-uuid2'}
        request_spec = {'instance_uuids': ['fake-uuid1', 'fake-uuid2'],
                        'instance_properties': instance_opts}

        def inc_launch_index(*args):
            request_spec['instance_properties']['launch_index'] = (
                request_spec['instance_properties']['launch_index'] + 1)

        self.mox.StubOutWithMock(ctxt, 'elevated')
        self.mox.StubOutWithMock(self.driver, 'hosts_up')
        self.mox.StubOutWithMock(random, 'choice')
        self.mox.StubOutWithMock(driver, 'instance_update_db')
        self.mox.StubOutWithMock(compute_rpcapi.ComputeAPI, 'run_instance')

        ctxt.elevated().AndReturn(ctxt_elevated)
        # instance 1
        hosts_full = ['host1', 'host2', 'host3', 'host4']
        self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(hosts_full)
        random.choice(hosts_full).AndReturn('host3')
        driver.instance_update_db(ctxt, instance1['uuid']).WithSideEffects(
                inc_launch_index).AndReturn(instance1)
        compute_rpcapi.ComputeAPI.run_instance(ctxt, host='host3',
                instance=instance1, requested_networks=None,
                injected_files=None, admin_password=None, is_first_time=None,
                request_spec=request_spec, filter_properties={},
                legacy_bdm_in_spec=False)

        # instance 2
        ctxt.elevated().AndReturn(ctxt_elevated)
        self.driver.hosts_up(ctxt_elevated, 'compute').AndReturn(hosts_full)
        random.choice(hosts_full).AndReturn('host1')
        driver.instance_update_db(ctxt, instance2['uuid']).WithSideEffects(
                inc_launch_index).AndReturn(instance2)
        compute_rpcapi.ComputeAPI.run_instance(ctxt, host='host1',
                instance=instance2, requested_networks=None,
                injected_files=None, admin_password=None, is_first_time=None,
                request_spec=request_spec, filter_properties={},
                legacy_bdm_in_spec=False)

        self.mox.ReplayAll()
        self.driver.schedule_run_instance(ctxt, request_spec,
                None, None, None, None, {}, False)
Exemple #3
0
    def _provision_resource(
        self,
        context,
        weighed_host,
        request_spec,
        filter_properties,
        requested_networks,
        injected_files,
        admin_password,
        is_first_time,
        instance_uuid=None,
        legacy_bdm_in_spec=True,
    ):
        """Create the requested resource in this Zone."""
        # NOTE(vish): add our current instance back into the request spec
        request_spec["instance_uuids"] = [instance_uuid]
        payload = dict(request_spec=request_spec, weighted_host=weighed_host.to_dict(), instance_id=instance_uuid)
        self.notifier.info(context, "scheduler.run_instance.scheduled", payload)

        # Update the metadata if necessary
        scheduler_hints = filter_properties.get("scheduler_hints") or {}
        group = scheduler_hints.get("group", None)
        values = None
        if group:
            values = request_spec["instance_properties"]["system_metadata"]
            values.update({"group": group})
            values = {"system_metadata": values}

        try:
            updated_instance = driver.instance_update_db(context, instance_uuid, extra_values=values)

        except exception.InstanceNotFound:
            LOG.warning(_("Instance disappeared during scheduling"), context=context, instance_uuid=instance_uuid)

        else:
            scheduler_utils.populate_filter_properties(filter_properties, weighed_host.obj)

            self.compute_rpcapi.run_instance(
                context,
                instance=updated_instance,
                host=weighed_host.obj.host,
                request_spec=request_spec,
                filter_properties=filter_properties,
                requested_networks=requested_networks,
                injected_files=injected_files,
                admin_password=admin_password,
                is_first_time=is_first_time,
                node=weighed_host.obj.nodename,
                legacy_bdm_in_spec=legacy_bdm_in_spec,
            )
    def test_basic_schedule_run_instances_anti_affinity(self):
        filter_properties = {'scheduler_hints':
                             {'group': 'cats'}}
        # Request spec 1
        instance_opts1 = {'project_id': 1, 'os_type': 'Linux',
                          'memory_mb': 512, 'root_gb': 512,
                          'ephemeral_gb': 0, 'vcpus': 1,
                          'system_metadata': {'system': 'metadata'}}
        request_spec1 = {'instance_uuids': ['fake-uuid1-1', 'fake-uuid1-2'],
                         'instance_properties': instance_opts1,
                         'instance_type': {'memory_mb': 512, 'root_gb': 512,
                                           'ephemeral_gb': 0, 'vcpus': 1}}
        self.next_weight = 1.0

        def _fake_weigh_objects(_self, functions, hosts, options):
            self.next_weight += 2.0
            host_state = hosts[0]
            return [weights.WeighedHost(host_state, self.next_weight)]

        sched = fakes.FakeFilterScheduler()

        fake_context = context.RequestContext('user', 'project',
                is_admin=True)

        self.stubs.Set(sched.host_manager, 'get_filtered_hosts',
                fake_get_group_filtered_hosts)
        self.stubs.Set(weights.HostWeightHandler,
                'get_weighed_objects', _fake_weigh_objects)
        fakes.mox_host_manager_db_calls(self.mox, fake_context)

        self.mox.StubOutWithMock(driver, 'instance_update_db')
        self.mox.StubOutWithMock(compute_rpcapi.ComputeAPI, 'run_instance')
        self.mox.StubOutWithMock(sched, 'group_hosts')

        instance1_1 = {'uuid': 'fake-uuid1-1'}
        instance1_2 = {'uuid': 'fake-uuid1-2'}

        sched.group_hosts(mox.IgnoreArg(), 'cats').AndReturn([])

        def inc_launch_index1(*args, **kwargs):
            request_spec1['instance_properties']['launch_index'] = (
                request_spec1['instance_properties']['launch_index'] + 1)

        expected_metadata = {'system_metadata':
                             {'system': 'metadata', 'group': 'cats'}}
        driver.instance_update_db(fake_context, instance1_1['uuid'],
                extra_values=expected_metadata).WithSideEffects(
                inc_launch_index1).AndReturn(instance1_1)
        compute_rpcapi.ComputeAPI.run_instance(fake_context, host='host3',
                instance=instance1_1, requested_networks=None,
                injected_files=None, admin_password=None, is_first_time=None,
                request_spec=request_spec1, filter_properties=mox.IgnoreArg(),
                node='node3', legacy_bdm_in_spec=False)

        driver.instance_update_db(fake_context, instance1_2['uuid'],
                extra_values=expected_metadata).WithSideEffects(
                inc_launch_index1).AndReturn(instance1_2)
        compute_rpcapi.ComputeAPI.run_instance(fake_context, host='host4',
                instance=instance1_2, requested_networks=None,
                injected_files=None, admin_password=None, is_first_time=None,
                request_spec=request_spec1, filter_properties=mox.IgnoreArg(),
                node='node4', legacy_bdm_in_spec=False)
        self.mox.ReplayAll()
        sched.schedule_run_instance(fake_context, request_spec1,
                None, None, None, None, filter_properties, False)