Example #1
0
    def test_live_migration_dest_check_auto_set_host(self):
        instance = self._live_migration_instance()

        # Confirm dest is picked by scheduler if not set.
        self.mox.StubOutWithMock(self.driver, 'select_hosts')
        self.mox.StubOutWithMock(db, 'instance_type_get')

        instance_type = instance_types.extract_instance_type(instance)
        request_spec = {'instance_properties': instance,
                        'instance_type': instance_type,
                        'instance_uuids': [instance['uuid']],
                        'image': self.image_service.show(self.context,
                                                         instance['image_ref'])
                        }
        ignore_hosts = [instance['host']]
        filter_properties = {'ignore_hosts': ignore_hosts}

        db.instance_type_get(self.context, instance_type['id']).AndReturn(
            instance_type)
        self.driver.select_hosts(self.context, request_spec,
                                 filter_properties).AndReturn(['fake_host2'])

        self.mox.ReplayAll()
        result = self.driver._live_migration_dest_check(self.context, instance,
                                                        None, ignore_hosts)
        self.assertEqual('fake_host2', result)
Example #2
0
    def test_get_instance_nw_info(self):
        self.mox.StubOutWithMock(db, 'fixed_ip_get_by_instance')
        self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance')
        self.mox.StubOutWithMock(db, 'instance_type_get')

        db.fixed_ip_get_by_instance(mox.IgnoreArg(),
                                    mox.IgnoreArg()).AndReturn(fixed_ips)
        db.virtual_interface_get_by_instance(mox.IgnoreArg(),
                                             mox.IgnoreArg()).AndReturn(vifs)
        db.instance_type_get(mox.IgnoreArg(),
                                   mox.IgnoreArg()).AndReturn(flavor)
        self.mox.ReplayAll()

        nw_info = self.network.get_instance_nw_info(None, 0, 0, None)

        self.assertTrue(nw_info)

        for i, nw in enumerate(nw_info):
            i8 = i + 8
            check = {'bridge': 'fa%s' % i,
                     'cidr': '192.168.%s.0/24' % i,
                     'cidr_v6': '2001:db%s::/64' % i8,
                     'id': i,
                     'multi_host': False,
                     'injected': 'DONTCARE',
                     'bridge_interface': 'fake_fa%s' % i,
                     'vlan': None}

            self.assertDictMatch(nw[0], check)

            check = {'broadcast': '192.168.%s.255' % i,
                     'dhcp_server': '192.168.%s.1' % i,
                     'dns': 'DONTCARE',
                     'gateway': '192.168.%s.1' % i,
                     'gateway6': '2001:db%s::1' % i8,
                     'ip6s': 'DONTCARE',
                     'ips': 'DONTCARE',
                     'label': 'test%s' % i,
                     'mac': 'DE:AD:BE:EF:00:0%s' % i,
                     'vif_uuid': ('00000000-0000-0000-0000-000000000000000%s' %
                                  i),
                     'rxtx_cap': 'DONTCARE',
                     'should_create_vlan': False,
                     'should_create_bridge': False}
            self.assertDictMatch(nw[1], check)

            check = [{'enabled': 'DONTCARE',
                      'ip': '2001:db%s::dcad:beff:feef:%s' % (i8, i),
                      'netmask': '64'}]
            self.assertDictListMatch(nw[1]['ip6s'], check)

            check = [{'enabled': '1',
                      'ip': '192.168.%s.100' % i,
                      'netmask': '255.255.255.0'}]
            self.assertDictListMatch(nw[1]['ips'], check)
Example #3
0
    def test_finish_revert_resize(self):
        """Ensure that the flavor is reverted to the original on revert"""
        context = self.context.elevated()
        instance_id = self._create_instance()

        def fake(*args, **kwargs):
            pass

        self.stubs.Set(self.compute.driver, 'finish_migration', fake)
        self.stubs.Set(self.compute.driver, 'revert_migration', fake)
        self.stubs.Set(self.compute.network_api, 'get_instance_nw_info', fake)

        self.compute.run_instance(self.context, instance_id)

        # Confirm the instance size before the resize starts
        inst_ref = db.instance_get(context, instance_id)
        instance_type_ref = db.instance_type_get(context,
                inst_ref['instance_type_id'])
        self.assertEqual(instance_type_ref['flavorid'], 1)

        db.instance_update(self.context, instance_id, {'host': 'foo'})

        self.compute.prep_resize(context, inst_ref['uuid'], 3)

        migration_ref = db.migration_get_by_instance_and_status(context,
                inst_ref['uuid'], 'pre-migrating')

        self.compute.resize_instance(context, inst_ref['uuid'],
                migration_ref['id'])
        self.compute.finish_resize(context, inst_ref['uuid'],
                    int(migration_ref['id']), {})

        # Prove that the instance size is now the new size
        inst_ref = db.instance_get(context, instance_id)
        instance_type_ref = db.instance_type_get(context,
                inst_ref['instance_type_id'])
        self.assertEqual(instance_type_ref['flavorid'], 3)

        # Finally, revert and confirm the old flavor has been applied
        self.compute.revert_resize(context, inst_ref['uuid'],
                migration_ref['id'])
        self.compute.finish_revert_resize(context, inst_ref['uuid'],
                migration_ref['id'])

        inst_ref = db.instance_get(context, instance_id)
        instance_type_ref = db.instance_type_get(context,
                inst_ref['instance_type_id'])
        self.assertEqual(instance_type_ref['flavorid'], 1)

        self.compute.terminate_instance(context, instance_id)
 def test_instance_type_get_with_extra_specs(self):
     instance_type = db.instance_type_get(
                         self.context,
                         self.instance_type_id)
     self.assertEquals(instance_type['extra_specs'],
                       dict(cpu_arch="x86_64",
                            cpu_model="Nehalem",
                            xpu_arch="fermi",
                            xpus="2",
                            xpu_model="Tesla 2050"))
     instance_type = db.instance_type_get(
                         self.context,
                         5)
     self.assertEquals(instance_type['extra_specs'], {})
Example #5
0
    def prep_resize(self, context, image, update_db, request_spec,
                    filter_properties, instance=None, instance_uuid=None,
                    instance_type=None, instance_type_id=None, topic=None):
        """Tries to call schedule_prep_resize on the driver.
        Sets instance vm_state to ACTIVE on NoHostFound
        Sets vm_state to ERROR on other exceptions
        """
        if not instance:
            instance = db.instance_get_by_uuid(context, instance_uuid)

        if not instance_type:
            instance_type = db.instance_type_get(context, instance_type_id)

        try:
            kwargs = {
                'context': context,
                'image': image,
                'update_db': update_db,
                'request_spec': request_spec,
                'filter_properties': filter_properties,
                'instance': instance,
                'instance_type': instance_type,
            }
            return self.driver.schedule_prep_resize(**kwargs)
        except exception.NoValidHost as ex:
            self._set_vm_state_and_notify('prep_resize',
                                         {'vm_state': vm_states.ACTIVE,
                                          'task_state': None},
                                         context, ex, request_spec)
        except Exception as ex:
            with excutils.save_and_reraise_exception():
                self._set_vm_state_and_notify('prep_resize',
                                             {'vm_state': vm_states.ERROR},
                                             context, ex, request_spec)
Example #6
0
    def test_create_instances_here(self):
        # Just grab the first instance type
        inst_type = db.instance_type_get(self.ctxt, 1)
        image = {"properties": {}}
        instance_props = {
            "hostname": "meow",
            "display_name": "moo",
            "image_ref": "fake_image_ref",
            "user_id": self.ctxt.user_id,
            "project_id": self.ctxt.project_id,
        }
        request_spec = {
            "instance_type": inst_type,
            "image": image,
            "security_group": ["default"],
            "block_device_mapping": [],
            "instance_properties": instance_props,
            "instance_uuids": self.instance_uuids,
        }

        call_info = {"uuids": []}

        def _fake_instance_update_at_top(_ctxt, instance):
            call_info["uuids"].append(instance["uuid"])

        self.stubs.Set(self.msg_runner, "instance_update_at_top", _fake_instance_update_at_top)

        self.scheduler._create_instances_here(self.ctxt, request_spec)
        self.assertEqual(self.instance_uuids, call_info["uuids"])

        for instance_uuid in self.instance_uuids:
            instance = db.instance_get_by_uuid(self.ctxt, instance_uuid)
            self.assertEqual("meow", instance["hostname"])
            self.assertEqual("moo-%s" % instance["uuid"], instance["display_name"])
            self.assertEqual("fake_image_ref", instance["image_ref"])
Example #7
0
    def test_create_instances_here(self):
        # Just grab the first instance type
        inst_type = db.instance_type_get(self.ctxt, 1)
        image = {'properties': {}}
        instance_props = {'hostname': 'meow',
                          'display_name': 'moo',
                          'image_ref': 'fake_image_ref',
                          'user_id': self.ctxt.user_id,
                          'project_id': self.ctxt.project_id}
        request_spec = {'instance_type': inst_type,
                        'image': image,
                        'security_group': ['default'],
                        'block_device_mapping': [],
                        'instance_properties': instance_props,
                        'instance_uuids': self.instance_uuids}

        call_info = {'uuids': []}

        def _fake_instance_update_at_top(_ctxt, instance):
            call_info['uuids'].append(instance['uuid'])

        self.stubs.Set(self.msg_runner, 'instance_update_at_top',
                       _fake_instance_update_at_top)

        self.scheduler._create_instances_here(self.ctxt, request_spec)
        self.assertEqual(self.instance_uuids, call_info['uuids'])

        for instance_uuid in self.instance_uuids:
            instance = db.instance_get_by_uuid(self.ctxt, instance_uuid)
            self.assertEqual('meow', instance['hostname'])
            self.assertEqual('moo', instance['display_name'])
            self.assertEqual('fake_image_ref', instance['image_ref'])
Example #8
0
def get_instance_type(instance_type_id):
    """Retrieves single instance type by id."""
    if instance_type_id is None:
        return get_default_instance_type()

    ctxt = context.get_admin_context()
    return db.instance_type_get(ctxt, instance_type_id)
Example #9
0
    def schedule_prep_resize(self, context, request_spec, *args, **kwargs):
        """Select a target for resize.

        Selects a target host for the instance, post-resize, and casts
        the prep_resize operation to it.
        """

        # We need the new instance type ID...
        instance_type_id = kwargs['instance_type_id']

        elevated = context.elevated()
        LOG.debug(_("Attempting to determine target host for resize to "
                    "instance type %(instance_type_id)s") % locals())

        # Convert it to an actual instance type
        instance_type = db.instance_type_get(elevated, instance_type_id)

        # Now let's grab a possibility
        hosts = self._schedule(context, 'compute', request_spec,
                               *args, **kwargs)
        if not hosts:
            raise exception.NoValidHost(reason=_(""))
        host = hosts.pop(0)

        # NOTE(comstud): Make sure we do not pass this through.  It
        # contains an instance of RpcContext that cannot be serialized.
        kwargs.pop('filter_properties', None)

        # Forward off to the host
        driver.cast_to_compute_host(context, host.host_state.host,
                'prep_resize', **kwargs)
Example #10
0
    def test_create_instances_here(self):
        # Just grab the first instance type
        inst_type = db.instance_type_get(self.ctxt, 1)
        image = {'properties': {}}
        instance_uuids = self.instance_uuids
        instance_props = {
            'hostname': 'meow',
            'display_name': 'moo',
            'image_ref': 'fake_image_ref',
            'user_id': self.ctxt.user_id,
            'project_id': self.ctxt.project_id
        }

        call_info = {'uuids': []}

        def _fake_instance_update_at_top(_ctxt, instance):
            call_info['uuids'].append(instance['uuid'])

        self.stubs.Set(self.msg_runner, 'instance_update_at_top',
                       _fake_instance_update_at_top)

        self.scheduler._create_instances_here(self.ctxt, instance_uuids,
                                              instance_props, inst_type, image,
                                              ['default'], [])
        self.assertEqual(instance_uuids, call_info['uuids'])

        for instance_uuid in instance_uuids:
            instance = db.instance_get_by_uuid(self.ctxt, instance_uuid)
            self.assertEqual('meow', instance['hostname'])
            self.assertEqual('moo-%s' % instance['uuid'],
                             instance['display_name'])
            self.assertEqual('fake_image_ref', instance['image_ref'])
Example #11
0
    def schedule_prep_resize(self, context, request_spec, *args, **kwargs):
        """Select a target for resize.

        Selects a target host for the instance, post-resize, and casts
        the prep_resize operation to it.
        """

        # We need the new instance type ID...
        instance_type_id = kwargs['instance_type_id']

        elevated = context.elevated()
        LOG.debug(
            _("Attempting to determine target host for resize to "
              "instance type %(instance_type_id)s") % locals())

        # Convert it to an actual instance type
        instance_type = db.instance_type_get(elevated, instance_type_id)

        # Now let's grab a possibility
        hosts = self._schedule(elevated, 'compute', request_spec, *args,
                               **kwargs)
        if not hosts:
            raise exception.NoValidHost(reason=_(""))
        host = hosts.pop(0)

        # Forward off to the host
        driver.cast_to_host(context, 'compute', host.host, 'prep_resize',
                            **kwargs)
Example #12
0
    def test_create_instances_here(self):
        # Just grab the first instance type
        inst_type = db.instance_type_get(self.ctxt, 1)
        image = {'properties': {}}
        instance_uuids = self.instance_uuids
        instance_props = {'hostname': 'meow',
                          'display_name': 'moo',
                          'image_ref': 'fake_image_ref',
                          'user_id': self.ctxt.user_id,
                          'project_id': self.ctxt.project_id}

        call_info = {'uuids': []}

        def _fake_instance_update_at_top(_ctxt, instance):
            call_info['uuids'].append(instance['uuid'])

        self.stubs.Set(self.msg_runner, 'instance_update_at_top',
                       _fake_instance_update_at_top)

        self.scheduler._create_instances_here(self.ctxt, instance_uuids,
                instance_props, inst_type, image, ['default'], [])
        self.assertEqual(instance_uuids, call_info['uuids'])

        for instance_uuid in instance_uuids:
            instance = db.instance_get_by_uuid(self.ctxt, instance_uuid)
            self.assertEqual('meow', instance['hostname'])
            self.assertEqual('moo-%s' % instance['uuid'],
                             instance['display_name'])
            self.assertEqual('fake_image_ref', instance['image_ref'])
Example #13
0
def get_network_info(instance):
    # TODO(tr3buchet): this function needs to go away! network info
    #                  MUST be passed down from compute
    # TODO(adiantum) If we will keep this function
    # we should cache network_info
    admin_context = context.get_admin_context()

    try:
        fixed_ips = db.fixed_ip_get_by_instance(admin_context, instance['id'])
    except exception.FixedIpNotFoundForInstance:
        fixed_ips = []

    vifs = db.virtual_interface_get_by_instance(admin_context, instance['id'])
    flavor = db.instance_type_get(admin_context,
                                        instance['instance_type_id'])
    network_info = []

    for vif in vifs:
        network = vif['network']

        # determine which of the instance's IPs belong to this network
        network_ips = [fixed_ip['address'] for fixed_ip in fixed_ips if
                       fixed_ip['network_id'] == network['id']]

        def ip_dict(ip):
            return {
                'ip': ip,
                'netmask': network['netmask'],
                'enabled': '1'}

        def ip6_dict():
            prefix = network['cidr_v6']
            mac = vif['address']
            project_id = instance['project_id']
            return  {
                'ip': ipv6.to_global(prefix, mac, project_id),
                'netmask': network['netmask_v6'],
                'enabled': '1'}

        mapping = {
            'label': network['label'],
            'gateway': network['gateway'],
            'broadcast': network['broadcast'],
            'dhcp_server': network['gateway'],
            'mac': vif['address'],
            'rxtx_cap': flavor['rxtx_cap'],
            'dns': [],
            'ips': [ip_dict(ip) for ip in network_ips]}

        if network['dns1']:
            mapping['dns'].append(network['dns1'])
        if network['dns2']:
            mapping['dns'].append(network['dns2'])

        if FLAGS.use_ipv6:
            mapping['ip6s'] = [ip6_dict()]
            mapping['gateway6'] = network['gateway_v6']

        network_info.append((network, mapping))
    return network_info
Example #14
0
def get_instance_type(instance_type_id):
    """Retrieves single instance type by id."""
    if instance_type_id is None:
        return get_default_instance_type()

    ctxt = context.get_admin_context()
    return db.instance_type_get(ctxt, instance_type_id)
Example #15
0
    def schedule_prep_resize(self, context, request_spec, *args, **kwargs):
        """Select a target for resize.

        Selects a target host for the instance, post-resize, and casts
        the prep_resize operation to it.
        """

        # We need the new instance type ID...
        instance_type_id = kwargs["instance_type_id"]

        elevated = context.elevated()
        LOG.debug(
            _("Attempting to determine target host for resize to " "instance type %(instance_type_id)s") % locals()
        )

        # Convert it to an actual instance type
        instance_type = db.instance_type_get(elevated, instance_type_id)

        # Now let's grab a possibility
        hosts = self._schedule(elevated, "compute", request_spec, *args, **kwargs)
        if not hosts:
            raise exception.NoValidHost(reason=_(""))
        host = hosts.pop(0)

        # Forward off to the host
        driver.cast_to_host(context, "compute", host.host, "prep_resize", **kwargs)
Example #16
0
def get_instance_type(id):
    """Retrieves single instance type by id."""
    if id is None:
        return get_default_instance_type()
    try:
        ctxt = context.get_admin_context()
        return db.instance_type_get(ctxt, id)
    except exception.DBError:
        raise exception.ApiError(_("Unknown instance type: %s") % id)
Example #17
0
def get_instance_type(id):
    """Retrieves single instance type by id."""
    if id is None:
        return get_default_instance_type()
    try:
        ctxt = context.get_admin_context()
        return db.instance_type_get(ctxt, id)
    except exception.DBError:
        raise exception.ApiError(_("Unknown instance type: %s") % id)
Example #18
0
def get_instance_type(instance_type_id):
    """Retrieves single instance type by id."""
    if instance_type_id is None:
        return get_default_instance_type()

    ctxt = context.get_admin_context()
    try:
        return db.instance_type_get(ctxt, instance_type_id)
    except exception.InstanceTypeNotFound as e:
        raise exception.ApiError(e)
Example #19
0
def get_instance_type(instance_type_id, ctxt=None, inactive=False):
    """Retrieves single instance type by id."""
    if instance_type_id is None:
        return get_default_instance_type()

    if ctxt is None:
        ctxt = context.get_admin_context()

    if inactive:
        ctxt = ctxt.elevated(read_deleted="yes")

    return db.instance_type_get(ctxt, instance_type_id)
Example #20
0
def get_instance_type(instance_type_id, ctxt=None, inactive=False):
    """Retrieves single instance type by id."""
    if instance_type_id is None:
        return get_default_instance_type()

    if ctxt is None:
        ctxt = context.get_admin_context()

    if inactive:
        ctxt = ctxt.elevated(read_deleted="yes")

    return db.instance_type_get(ctxt, instance_type_id)
Example #21
0
def create_instance(testcase):
    fake.stub_out_image_service(testcase.stubs)
    ctxt = context.get_admin_context()
    instance_type = db.instance_type_get(ctxt, 1)
    sys_meta = flavors.save_instance_type_info({}, instance_type)
    return db.instance_create(ctxt,
                    {'user_id': 'fake',
                     'project_id': 'fake',
                     'instance_type_id': 1,
                     'memory_mb': 1024,
                     'vcpus': 2,
                     'image_ref': '155d900f-4e14-4e4c-a73d-069cbf4541e6',
                     'system_metadata': sys_meta})
Example #22
0
def create_instance(testcase):
    fake.stub_out_image_service(testcase.stubs)
    ctxt = context.get_admin_context()
    instance_type = db.instance_type_get(ctxt, 1)
    sys_meta = flavors.save_flavor_info({}, instance_type)
    return db.instance_create(ctxt,
                    {'user_id': 'fake',
                     'project_id': 'fake',
                     'instance_type_id': 1,
                     'memory_mb': 1024,
                     'vcpus': 2,
                     'image_ref': '155d900f-4e14-4e4c-a73d-069cbf4541e6',
                     'system_metadata': sys_meta})
Example #23
0
    def _live_migration_dest_check(self,
                                   context,
                                   instance_ref,
                                   dest,
                                   ignore_hosts=None):
        """Live migration check routine (for destination host).

        :param context: security context
        :param instance_ref: nova.db.sqlalchemy.models.Instance object
        :param dest: destination host
        :param ignore_hosts: hosts that should be avoided as dest host
        """

        # If dest is not specified, have scheduler pick one.
        if dest is None:
            instance_type = db.instance_type_get(
                context, instance_ref['instance_type_id'])
            image = self.image_service.show(context, instance_ref['image_ref'])
            request_spec = {
                'instance_properties': instance_ref,
                'instance_type': instance_type,
                'instance_uuids': [instance_ref['uuid']],
                'image': image
            }
            filter_properties = {'ignore_hosts': ignore_hosts}
            return self.select_hosts(context, request_spec,
                                     filter_properties)[0]

        # Checking whether The host where instance is running
        # and dest is not same.
        src = instance_ref['host']
        if dest == src:
            raise exception.UnableToMigrateToSelf(
                instance_id=instance_ref['uuid'], host=dest)

        # Checking dest exists and compute node.
        try:
            dservice_ref = db.service_get_by_compute_host(context, dest)
        except exception.NotFound:
            raise exception.ComputeServiceUnavailable(host=dest)

        # Checking dest host is alive.
        if not self.servicegroup_api.service_is_up(dservice_ref):
            raise exception.ComputeServiceUnavailable(host=dest)

        # Check memory requirements
        self._assert_compute_node_has_enough_memory(context, instance_ref,
                                                    dest)

        return dest
Example #24
0
    def prep_resize(self,
                    context,
                    image,
                    request_spec,
                    filter_properties,
                    update_db=None,
                    instance=None,
                    instance_uuid=None,
                    instance_type=None,
                    instance_type_id=None,
                    reservations=None,
                    topic=None):
        """Tries to call schedule_prep_resize on the driver.
        Sets instance vm_state to ACTIVE on NoHostFound
        Sets vm_state to ERROR on other exceptions
        """
        if not instance:
            instance = db.instance_get_by_uuid(context, instance_uuid)

        if not instance_type:
            instance_type = db.instance_type_get(context, instance_type_id)

        try:
            kwargs = {
                'context': context,
                'image': image,
                'request_spec': request_spec,
                'filter_properties': filter_properties,
                'instance': instance,
                'instance_type': instance_type,
                'reservations': reservations,
            }
            return self.driver.schedule_prep_resize(**kwargs)
        except exception.NoValidHost as ex:
            self._set_vm_state_and_notify('prep_resize', {
                'vm_state': vm_states.ACTIVE,
                'task_state': None
            }, context, ex, request_spec)
            if reservations:
                QUOTAS.rollback(context, reservations)
        except Exception as ex:
            with excutils.save_and_reraise_exception():
                self._set_vm_state_and_notify('prep_resize',
                                              {'vm_state': vm_states.ERROR},
                                              context, ex, request_spec)
                if reservations:
                    QUOTAS.rollback(context, reservations)
Example #25
0
def create_instance(testcase):
    fake.stub_out_image_service(testcase.stubs)
    ctxt = context.get_admin_context()
    instance_type = db.instance_type_get(ctxt, 1)
    sys_meta = flavors.save_instance_type_info({}, instance_type)
    return db.instance_create(
        ctxt,
        {
            "user_id": "fake",
            "project_id": "fake",
            "instance_type_id": 1,
            "memory_mb": 1024,
            "vcpus": 2,
            "image_ref": "155d900f-4e14-4e4c-a73d-069cbf4541e6",
            "system_metadata": sys_meta,
        },
    )
Example #26
0
    def test_create_instances_here(self):
        # Just grab the first instance type
        inst_type = db.instance_type_get(self.ctxt, 1)
        image = {'properties': {}}
        instance_uuids = self.instance_uuids
        instance_props = {
            'name': 'instance-00000001',
            'hostname': 'meow',
            'display_name': 'moo',
            'image_ref': 'fake_image_ref',
            'user_id': self.ctxt.user_id,
            # Test these as lists
            'metadata': [{
                'key': 'moo',
                'value': 'cow'
            }],
            'system_metadata': [{
                'key': 'meow',
                'value': 'cat'
            }],
            'project_id': self.ctxt.project_id
        }

        call_info = {'uuids': []}

        def _fake_instance_update_at_top(_ctxt, instance):
            call_info['uuids'].append(instance['uuid'])

        self.stubs.Set(self.msg_runner, 'instance_update_at_top',
                       _fake_instance_update_at_top)

        self.scheduler._create_instances_here(self.ctxt, instance_uuids,
                                              instance_props, inst_type, image,
                                              ['default'], [])
        self.assertEqual(instance_uuids, call_info['uuids'])

        for instance_uuid in instance_uuids:
            instance = db.instance_get_by_uuid(self.ctxt, instance_uuid)
            meta = utils.instance_meta(instance)
            self.assertEqual('cow', meta['moo'])
            sys_meta = utils.instance_sys_meta(instance)
            self.assertEqual('cat', sys_meta['meow'])
            self.assertEqual('meow', instance['hostname'])
            self.assertEqual('moo-%s' % instance['uuid'],
                             instance['display_name'])
            self.assertEqual('fake_image_ref', instance['image_ref'])
Example #27
0
    def _live_migration_dest_check(self, context, instance_ref, dest,
                                   ignore_hosts=None):
        """Live migration check routine (for destination host).

        :param context: security context
        :param instance_ref: nova.db.sqlalchemy.models.Instance object
        :param dest: destination host
        :param ignore_hosts: hosts that should be avoided as dest host
        """

        # If dest is not specified, have scheduler pick one.
        if dest is None:
            instance_type = db.instance_type_get(
                context, instance_ref['instance_type_id'])
            image = self.image_service.show(context, instance_ref['image_ref'])
            request_spec = {'instance_properties': instance_ref,
                            'instance_type': instance_type,
                            'instance_uuids': [instance_ref['uuid']],
                            'image': image}
            filter_properties = {'ignore_hosts': ignore_hosts}
            return self.select_hosts(context, request_spec,
                                     filter_properties)[0]

        # Checking whether The host where instance is running
        # and dest is not same.
        src = instance_ref['host']
        if dest == src:
            raise exception.UnableToMigrateToSelf(
                    instance_id=instance_ref['uuid'], host=dest)

        # Checking dest exists and compute node.
        try:
            dservice_ref = db.service_get_by_compute_host(context, dest)
        except exception.NotFound:
            raise exception.ComputeServiceUnavailable(host=dest)

        # Checking dest host is alive.
        if not self.servicegroup_api.service_is_up(dservice_ref):
            raise exception.ComputeServiceUnavailable(host=dest)

        # Check memory requirements
        self._assert_compute_node_has_enough_memory(context,
                                                   instance_ref, dest)

        return dest
Example #28
0
    def _check_vdi_size(cls, context, session, instance, vdi_uuid):
        size_bytes = cls._get_vdi_chain_size(context, session, vdi_uuid)

        # FIXME(jk0): this was copied directly from compute.manager.py, let's
        # refactor this to a common area
        instance_type_id = instance["instance_type_id"]
        instance_type = db.instance_type_get(context, instance_type_id)
        allowed_size_gb = instance_type["local_gb"]
        allowed_size_bytes = allowed_size_gb * 1024 * 1024 * 1024

        LOG.debug(_("image_size_bytes=%(size_bytes)d, allowed_size_bytes=" "%(allowed_size_bytes)d") % locals())

        if size_bytes > allowed_size_bytes:
            LOG.info(
                _("Image size %(size_bytes)d exceeded" " instance_type allowed size " "%(allowed_size_bytes)d")
                % locals()
            )
            raise exception.ImageTooLarge()
Example #29
0
    def _check_vdi_size(cls, context, session, instance, vdi_uuid):
        size_bytes = cls._get_vdi_chain_size(context, session, vdi_uuid)

        # FIXME(jk0): this was copied directly from compute.manager.py, let's
        # refactor this to a common area
        instance_type_id = instance['instance_type_id']
        instance_type = db.instance_type_get(context, instance_type_id)
        allowed_size_gb = instance_type['local_gb']
        allowed_size_bytes = allowed_size_gb * 1024 * 1024 * 1024

        LOG.debug(
            _("image_size_bytes=%(size_bytes)d, allowed_size_bytes="
              "%(allowed_size_bytes)d") % locals())

        if size_bytes > allowed_size_bytes:
            LOG.info(
                _("Image size %(size_bytes)d exceeded"
                  " instance_type allowed size "
                  "%(allowed_size_bytes)d") % locals())
            raise exception.ImageTooLarge()
    def setUp(self):
        super(LibvirtBlockInfoTest, self).setUp()

        self.user_id = 'fake'
        self.project_id = 'fake'
        self.context = context.get_admin_context()
        instance_type = db.instance_type_get(self.context, 2)
        sys_meta = flavors.save_instance_type_info({}, instance_type)
        nova.tests.image.fake.stub_out_image_service(self.stubs)
        self.test_instance = {
                'uuid': '32dfcb37-5af1-552b-357c-be8c3aa38310',
                'memory_kb': '1024000',
                'basepath': '/some/path',
                'bridge_name': 'br100',
                'vcpus': 2,
                'project_id': 'fake',
                'bridge': 'br101',
                'image_ref': '155d900f-4e14-4e4c-a73d-069cbf4541e6',
                'root_gb': 10,
                'ephemeral_gb': 20,
                'instance_type_id': 2,  # m1.tiny
                'system_metadata': sys_meta}
Example #31
0
    def setUp(self):
        super(LibvirtBlockInfoTest, self).setUp()

        self.user_id = 'fake'
        self.project_id = 'fake'
        self.context = context.get_admin_context()
        instance_type = db.instance_type_get(self.context, 2)
        sys_meta = instance_types.save_instance_type_info({}, instance_type)
        nova.tests.image.fake.stub_out_image_service(self.stubs)
        self.test_instance = {
            'uuid': '32dfcb37-5af1-552b-357c-be8c3aa38310',
            'memory_kb': '1024000',
            'basepath': '/some/path',
            'bridge_name': 'br100',
            'vcpus': 2,
            'project_id': 'fake',
            'bridge': 'br101',
            'image_ref': '155d900f-4e14-4e4c-a73d-069cbf4541e6',
            'root_gb': 10,
            'ephemeral_gb': 20,
            'instance_type_id': 2,  # m1.tiny
            'system_metadata': sys_meta
        }
    def test_create_instances_here(self):
        # Just grab the first instance type
        inst_type = db.instance_type_get(self.ctxt, 1)
        image = {'properties': {}}
        instance_uuids = self.instance_uuids
        instance_props = {'name': 'instance-00000001',
                          'hostname': 'meow',
                          'display_name': 'moo',
                          'image_ref': 'fake_image_ref',
                          'user_id': self.ctxt.user_id,
                          # Test these as lists
                          'metadata': [{'key': 'moo', 'value': 'cow'}],
                          'system_metadata': [{'key': 'meow', 'value': 'cat'}],
                          'project_id': self.ctxt.project_id}

        call_info = {'uuids': []}

        def _fake_instance_update_at_top(_ctxt, instance):
            call_info['uuids'].append(instance['uuid'])

        self.stubs.Set(self.msg_runner, 'instance_update_at_top',
                       _fake_instance_update_at_top)

        self.scheduler._create_instances_here(self.ctxt, instance_uuids,
                instance_props, inst_type, image, ['default'], [])
        self.assertEqual(instance_uuids, call_info['uuids'])

        for instance_uuid in instance_uuids:
            instance = db.instance_get_by_uuid(self.ctxt, instance_uuid)
            meta = utils.instance_meta(instance)
            self.assertEqual('cow', meta['moo'])
            sys_meta = utils.instance_sys_meta(instance)
            self.assertEqual('cat', sys_meta['meow'])
            self.assertEqual('meow', instance['hostname'])
            self.assertEqual('moo-%s' % instance['uuid'],
                             instance['display_name'])
            self.assertEqual('fake_image_ref', instance['image_ref'])
Example #33
0
    def test_create_instances_here(self):
        # Just grab the first instance type
        inst_type = db.instance_type_get(self.ctxt, 1)
        image = {'properties': {}}
        instance_props = {
            'hostname': 'meow',
            'display_name': 'moo',
            'image_ref': 'fake_image_ref',
            'user_id': self.ctxt.user_id,
            'project_id': self.ctxt.project_id
        }
        request_spec = {
            'instance_type': inst_type,
            'image': image,
            'security_group': ['default'],
            'block_device_mapping': [],
            'instance_properties': instance_props,
            'instance_uuids': self.instance_uuids
        }

        call_info = {'uuids': []}

        def _fake_instance_update_at_top(_ctxt, instance):
            call_info['uuids'].append(instance['uuid'])

        self.stubs.Set(self.msg_runner, 'instance_update_at_top',
                       _fake_instance_update_at_top)

        self.scheduler._create_instances_here(self.ctxt, request_spec)
        self.assertEqual(self.instance_uuids, call_info['uuids'])

        for instance_uuid in self.instance_uuids:
            instance = db.instance_get_by_uuid(self.ctxt, instance_uuid)
            self.assertEqual('meow', instance['hostname'])
            self.assertEqual('moo', instance['display_name'])
            self.assertEqual('fake_image_ref', instance['image_ref'])
Example #34
0
 def instance_type_get(self, context, instance_type_id):
     return db.instance_type_get(context, instance_type_id)
Example #35
0
 def test_instance_type_get(self):
     self.mox.StubOutWithMock(db, 'instance_type_get')
     db.instance_type_get(self.context, 'fake-id').AndReturn('fake-type')
     self.mox.ReplayAll()
     result = self.conductor.instance_type_get(self.context, 'fake-id')
     self.assertEqual(result, 'fake-type')
Example #36
0
 def instance_type_get(self, context, instance_type_id):
     return db.instance_type_get(context, instance_type_id)
Example #37
0
 def test_instance_type_get(self):
     self.mox.StubOutWithMock(db, 'instance_type_get')
     db.instance_type_get(self.context, 'fake-id').AndReturn('fake-type')
     self.mox.ReplayAll()
     result = self.conductor.instance_type_get(self.context, 'fake-id')
     self.assertEqual(result, 'fake-type')
Example #38
0
    def _usage_for_period(self, context, period_start, period_stop, tenant_id=None):
        fields = ['id',
                  'image_ref',
                  'project_id',
                  'user_id',
                  'vcpus',
                  'hostname',
                  'display_name',
                  'host',
                  'vm_state',
                  'instance_type_id',
                  'launched_at',
                  'terminated_at']

        tenant_clause = ''
        if tenant_id:
            tenant_clause = " and project_id='%s'" % tenant_id

        connection = get_session().connection()
        rows = connection.execute("select %s from instances where \
                                   (terminated_at is NULL or terminated_at > '%s') \
                                   and (launched_at < '%s') %s" %\
                                   (','.join(fields), period_start.isoformat(' '),\
                                   period_stop.isoformat(' '), tenant_clause
                                   )).fetchall()

        rval = {}
        flavors = {}

        for row in rows:
            o = {}
            for i in range(len(fields)):
                o[fields[i]] = row[i]
            o['hours'] = self._hours_for(o, period_start, period_stop)
            flavor_type = o['instance_type_id']

            try:
                flavors[flavor_type] = \
                    db.instance_type_get_by_id(context, flavor_type)

            except AttributeError:
                # The most recent version of nova renamed this function
                flavors[flavor_type] = \
                    db.instance_type_get(context, flavor_type)
            except exception.InstanceTypeNotFound:
                # can't bill if there is no instance type
                continue

            flavor = flavors[flavor_type]

            o['name'] = o['display_name']
            del(o['display_name'])

            o['ram_size'] = flavor['memory_mb']
            o['disk_size'] = flavor['local_gb']

            o['tenant_id'] = o['project_id']
            del(o['project_id'])

            o['flavor'] = flavor['name']
            del(o['instance_type_id'])

            o['started_at'] = o['launched_at']
            del(o['launched_at'])

            o['ended_at'] = o['terminated_at']
            del(o['terminated_at'])

            if o['ended_at']:
                o['state'] = 'terminated'
            else:
                o['state'] = o['vm_state']

            del(o['vm_state'])

            now = datetime.utcnow()

            if o['state'] == 'terminated':
                delta = self._parse_datetime(o['ended_at'])\
                             - self._parse_datetime(o['started_at'])
            else:
                delta = now - self._parse_datetime(o['started_at'])

            o['uptime'] = delta.days * 24 * 60 + delta.seconds

            if not o['tenant_id'] in rval:
                summary = {}
                summary['tenant_id'] = o['tenant_id']
                summary['instances'] = []
                summary['total_disk_usage'] = 0
                summary['total_cpu_usage'] = 0
                summary['total_ram_usage'] = 0

                summary['total_active_ram_size'] = 0
                summary['total_active_disk_size'] = 0
                summary['total_active_vcpus'] = 0
                summary['total_active_instances'] = 0

                summary['total_hours'] = 0
                summary['begin'] = period_start
                summary['stop'] = period_stop
                rval[o['tenant_id']] = summary

            rval[o['tenant_id']]['total_disk_usage'] += o['disk_size'] * o['hours']
            rval[o['tenant_id']]['total_cpu_usage'] += o['vcpus'] * o['hours']
            rval[o['tenant_id']]['total_ram_usage'] += o['ram_size'] * o['hours']

            if o['state'] is not 'terminated':
                rval[o['tenant_id']]['total_active_ram_size'] += o['ram_size']
                rval[o['tenant_id']]['total_active_vcpus'] += o['vcpus']
                rval[o['tenant_id']]['total_active_disk_size'] += o['disk_size']
                rval[o['tenant_id']]['total_active_instances'] += 1

            rval[o['tenant_id']]['total_hours'] += o['hours']
            rval[o['tenant_id']]['instances'].append(o)

        return rval.values()
Example #39
0
    def _usage_for_period(self, context, period_start, period_stop, tenant_id=None):
        fields = [
            "id",
            "image_ref",
            "project_id",
            "user_id",
            "vcpus",
            "hostname",
            "display_name",
            "host",
            "task_state",
            "instance_type_id",
            "launched_at",
            "terminated_at",
        ]

        tenant_clause = ""
        if tenant_id:
            tenant_clause = " and project_id='%s'" % tenant_id

        connection = get_session().connection()
        rows = connection.execute(
            "select %s from instances where \
                                   (terminated_at is NULL or terminated_at > '%s') \
                                   and (launched_at < '%s') %s"
            % (",".join(fields), period_start.isoformat(" "), period_stop.isoformat(" "), tenant_clause)
        ).fetchall()

        rval = {}
        flavors = {}

        for row in rows:
            o = {}
            for i in range(len(fields)):
                o[fields[i]] = row[i]
            o["hours"] = self._hours_for(o, period_start, period_stop)
            flavor_type = o["instance_type_id"]

            try:
                flavors[flavor_type] = db.instance_type_get_by_id(context, flavor_type)

            except AttributeError:
                # The most recent version of nova renamed this function
                flavors[flavor_type] = db.instance_type_get(context, flavor_type)
            except exception.InstanceTypeNotFound:
                # can't bill if there is no instance type
                continue

            flavor = flavors[flavor_type]

            o["name"] = o["display_name"]
            del (o["display_name"])

            o["ram_size"] = flavor["memory_mb"]
            o["disk_size"] = flavor["local_gb"]

            o["tenant_id"] = o["project_id"]
            del (o["project_id"])

            o["flavor"] = flavor["name"]
            del (o["instance_type_id"])

            o["started_at"] = o["launched_at"]
            del (o["launched_at"])

            o["ended_at"] = o["terminated_at"]
            del (o["terminated_at"])

            if o["ended_at"]:
                o["state"] = "terminated"
            else:
                o["state"] = o["task_state"]

            del (o["task_state"])

            now = datetime.utcnow()

            if o["state"] == "terminated":
                delta = self._parse_datetime(o["ended_at"]) - self._parse_datetime(o["started_at"])
            else:
                delta = now - self._parse_datetime(o["started_at"])

            o["uptime"] = delta.days * 24 * 60 + delta.seconds

            if not o["tenant_id"] in rval:
                summary = {}
                summary["tenant_id"] = o["tenant_id"]
                summary["instances"] = []
                summary["total_disk_usage"] = 0
                summary["total_cpu_usage"] = 0
                summary["total_ram_usage"] = 0

                summary["total_active_ram_size"] = 0
                summary["total_active_disk_size"] = 0
                summary["total_active_vcpus"] = 0
                summary["total_active_instances"] = 0

                summary["total_hours"] = 0
                summary["begin"] = period_start
                summary["stop"] = period_stop
                rval[o["tenant_id"]] = summary

            rval[o["tenant_id"]]["total_disk_usage"] += o["disk_size"] * o["hours"]
            rval[o["tenant_id"]]["total_cpu_usage"] += o["vcpus"] * o["hours"]
            rval[o["tenant_id"]]["total_ram_usage"] += o["ram_size"] * o["hours"]

            if o["state"] is not "terminated":
                rval[o["tenant_id"]]["total_active_ram_size"] += o["ram_size"]
                rval[o["tenant_id"]]["total_active_vcpus"] += o["vcpus"]
                rval[o["tenant_id"]]["total_active_disk_size"] += o["disk_size"]
                rval[o["tenant_id"]]["total_active_instances"] += 1

            rval[o["tenant_id"]]["total_hours"] += o["hours"]
            rval[o["tenant_id"]]["instances"].append(o)

        return rval.values()
Example #40
0
    def test_get_instance_nw_info(self):
        self.mox.StubOutWithMock(db, 'fixed_ip_get_by_instance')
        self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance')
        self.mox.StubOutWithMock(db, 'instance_type_get')

        db.fixed_ip_get_by_instance(mox.IgnoreArg(),
                                    mox.IgnoreArg()).AndReturn(fixed_ips)
        db.virtual_interface_get_by_instance(mox.IgnoreArg(),
                                             mox.IgnoreArg()).AndReturn(vifs)
        db.instance_type_get(mox.IgnoreArg(),
                             mox.IgnoreArg()).AndReturn(flavor)
        self.mox.ReplayAll()

        nw_info = self.network.get_instance_nw_info(None, 0, 0, None)

        self.assertTrue(nw_info)

        for i, nw in enumerate(nw_info):
            i8 = i + 8
            check = {
                'bridge': 'fa%s' % i,
                'cidr': '192.168.%s.0/24' % i,
                'cidr_v6': '2001:db%s::/64' % i8,
                'id': i,
                'multi_host': False,
                'injected': 'DONTCARE',
                'bridge_interface': 'fake_fa%s' % i,
                'vlan': None
            }

            self.assertDictMatch(nw[0], check)

            check = {
                'broadcast': '192.168.%s.255' % i,
                'dhcp_server': '192.168.%s.1' % i,
                'dns': 'DONTCARE',
                'gateway': '192.168.%s.1' % i,
                'gateway6': '2001:db%s::1' % i8,
                'ip6s': 'DONTCARE',
                'ips': 'DONTCARE',
                'label': 'test%s' % i,
                'mac': 'DE:AD:BE:EF:00:0%s' % i,
                'rxtx_cap': 'DONTCARE',
                'should_create_vlan': False,
                'should_create_bridge': False
            }
            self.assertDictMatch(nw[1], check)

            check = [{
                'enabled': 'DONTCARE',
                'ip': '2001:db%s::dcad:beff:feef:%s' % (i8, i),
                'netmask': '64'
            }]
            self.assertDictListMatch(nw[1]['ip6s'], check)

            check = [{
                'enabled': '1',
                'ip': '192.168.%s.100' % i,
                'netmask': '255.255.255.0'
            }]
            self.assertDictListMatch(nw[1]['ips'], check)
Example #41
0
 def test_instance_type_get(self):
     self.mox.StubOutWithMock(db, "instance_type_get")
     db.instance_type_get(self.context, "fake-id").AndReturn("fake-type")
     self.mox.ReplayAll()
     result = self.conductor.instance_type_get(self.context, "fake-id")
     self.assertEqual(result, "fake-type")
Example #42
0
    def test_live_migration_auto_set_dest(self):
        instance = self._live_migration_instance()

        # Confirm scheduler picks target host if none given.
        self.mox.StubOutWithMock(db, 'instance_type_get')
        self.mox.StubOutWithMock(self.driver, '_live_migration_src_check')
        self.mox.StubOutWithMock(self.driver, 'select_hosts')
        self.mox.StubOutWithMock(self.driver, '_live_migration_common_check')
        self.mox.StubOutWithMock(rpc, 'call')
        self.mox.StubOutWithMock(self.driver.compute_rpcapi, 'live_migration')

        dest = None
        block_migration = False
        disk_over_commit = False
        instance_type = instance_types.extract_instance_type(instance)
        request_spec = {'instance_properties': instance,
                        'instance_type': instance_type,
                        'instance_uuids': [instance['uuid']],
                        'image': self.image_service.show(self.context,
                                                         instance['image_ref'])
                        }

        self.driver._live_migration_src_check(self.context, instance)

        db.instance_type_get(self.context,
                             instance_type['id']).MultipleTimes().AndReturn(
                                instance_type)

        # First selected host raises exception.InvalidHypervisorType
        self.driver.select_hosts(self.context, request_spec,
                {'ignore_hosts': [instance['host']]}).AndReturn(['fake_host2'])
        self.driver._live_migration_common_check(self.context, instance,
                'fake_host2').AndRaise(exception.InvalidHypervisorType())

        # Second selected host raises exception.InvalidCPUInfo
        self.driver.select_hosts(self.context, request_spec,
                {'ignore_hosts': [instance['host'],
                                  'fake_host2']}).AndReturn(['fake_host3'])
        self.driver._live_migration_common_check(self.context, instance,
                                                 'fake_host3')
        rpc.call(self.context, "compute.fake_host3",
                   {"method": 'check_can_live_migrate_destination',
                    "args": {'instance': instance,
                             'block_migration': block_migration,
                             'disk_over_commit': disk_over_commit},
                    "version": compute_rpcapi.ComputeAPI.BASE_RPC_API_VERSION},
                 None).AndRaise(exception.InvalidCPUInfo(reason=""))

        # Third selected host pass all checks
        self.driver.select_hosts(self.context, request_spec,
                {'ignore_hosts': [instance['host'],
                                  'fake_host2',
                                  'fake_host3']}).AndReturn(['fake_host4'])
        self.driver._live_migration_common_check(self.context, instance,
                                                 'fake_host4')
        rpc.call(self.context, "compute.fake_host4",
                   {"method": 'check_can_live_migrate_destination',
                    "args": {'instance': instance,
                             'block_migration': block_migration,
                             'disk_over_commit': disk_over_commit},
                    "version": compute_rpcapi.ComputeAPI.BASE_RPC_API_VERSION},
                 None).AndReturn({})
        self.driver.compute_rpcapi.live_migration(self.context,
                host=instance['host'], instance=instance, dest='fake_host4',
                block_migration=block_migration, migrate_data={})

        self.mox.ReplayAll()
        result = self.driver.schedule_live_migration(self.context,
                instance=instance, dest=dest,
                block_migration=block_migration,
                disk_over_commit=disk_over_commit)
        self.assertEqual(result, None)