def get_image_metadata(context, image_api, image_id_or_uri, instance): image_system_meta = {} # In case of boot from volume, image_id_or_uri may be None or '' if image_id_or_uri is not None and image_id_or_uri != '': # If the base image is still available, get its metadata try: image = image_api.get(context, image_id_or_uri) except (exception.ImageNotAuthorized, exception.ImageNotFound, exception.Invalid) as e: LOG.warning(_LW("Can't access image %(image_id)s: %(error)s"), { "image_id": image_id_or_uri, "error": e }, instance=instance) else: flavor = instance.get_flavor() image_system_meta = utils.get_system_metadata_from_image( image, flavor) # Get the system metadata from the instance system_meta = utils.instance_sys_meta(instance) # Merge the metadata from the instance with the image's, if any system_meta.update(image_system_meta) # Convert the system metadata to image metadata return utils.get_image_from_system_metadata(system_meta)
def drop_resize_claim(self, context, instance, instance_type=None, image_meta=None, prefix='new_'): """Remove usage for an incoming/outgoing migration.""" if instance['uuid'] in self.tracked_migrations: migration, itype = self.tracked_migrations.pop(instance['uuid']) if not instance_type: ctxt = context.elevated() instance_type = self._get_instance_type(ctxt, instance, prefix) if image_meta is None: image_meta = utils.get_image_from_system_metadata( instance['system_metadata']) if (instance_type is not None and instance_type['id'] == itype['id']): numa_topology = hardware.numa_get_constraints( itype, image_meta) usage = self._get_usage_dict( itype, numa_topology=numa_topology) if self.pci_tracker: self.pci_tracker.update_pci_for_migration(context, instance, sign=-1) self._update_usage(context, self.compute_node, usage, sign=-1) ctxt = context.elevated() self._update(ctxt, self.compute_node)
def get_image_metadata(context, image_api, image_id_or_uri, instance): image_system_meta = {} # In case of boot from volume, image_id_or_uri may be None or '' if image_id_or_uri is not None and image_id_or_uri != '': # If the base image is still available, get its metadata try: image = image_api.get(context, image_id_or_uri) except (exception.ImageNotAuthorized, exception.ImageNotFound, exception.Invalid) as e: LOG.warning(_LW("Can't access image %(image_id)s: %(error)s"), {"image_id": image_id_or_uri, "error": e}, instance=instance) else: flavor = instance.get_flavor() image_system_meta = utils.get_system_metadata_from_image(image, flavor) # Get the system metadata from the instance system_meta = utils.instance_sys_meta(instance) # Merge the metadata from the instance with the image's, if any system_meta.update(image_system_meta) # Convert the system metadata to image metadata return utils.get_image_from_system_metadata(system_meta)
def test_non_inheritable_image_properties(self): sys_meta = self.get_system_metadata() sys_meta["%soo1" % utils.SM_IMAGE_PROP_PREFIX] = "bar" self.flags(non_inheritable_image_properties=["foo1"]) image = utils.get_image_from_system_metadata(sys_meta) # Verify that the foo1 key has not been inherited self.assertNotIn("foo1", image)
def test_dont_inherit_empty_values(self): sys_meta = self.get_system_metadata() for key in utils.SM_INHERITABLE_KEYS: sys_key = "%s%s" % (utils.SM_IMAGE_PROP_PREFIX, key) sys_meta[sys_key] = None image = utils.get_image_from_system_metadata(sys_meta) # Verify that the empty properties have not been inherited for key in utils.SM_INHERITABLE_KEYS: self.assertNotIn(key, image)
def test_image_from_system_metadata(self): sys_meta = self.get_system_metadata() sys_meta["%soo1" % utils.SM_IMAGE_PROP_PREFIX] = "bar" sys_meta["%soo2" % utils.SM_IMAGE_PROP_PREFIX] = "baz" image = utils.get_image_from_system_metadata(sys_meta) # Verify that we inherit all the needed keys for key in utils.SM_INHERITABLE_KEYS: sys_key = "%s%s" % (utils.SM_IMAGE_PROP_PREFIX, key) self.assertEqual(image[key], sys_meta.get(sys_key)) # Verify that we inherit the rest of metadata as properties self.assertIn("properties", image) for key, value in image["properties"].iteritems(): sys_key = "%s%s" % (utils.SM_IMAGE_PROP_PREFIX, key) self.assertEqual(image["properties"][key], sys_meta[sys_key])
def _update_usage_from_migration(self, context, instance, image_meta, resources, migration): """Update usage for a single migration. The record may represent an incoming or outbound migration. """ uuid = migration.instance_uuid LOG.info(_LI("Updating from migration %s") % uuid) incoming = (migration.dest_compute == self.host and migration.dest_node == self.nodename) outbound = (migration.source_compute == self.host and migration.source_node == self.nodename) same_node = (incoming and outbound) record = self.tracked_instances.get(uuid, None) itype = None if same_node: # same node resize. record usage for whichever instance type the # instance is *not* in: if (instance['instance_type_id'] == migration.old_instance_type_id): itype = self._get_instance_type(context, instance, 'new_', migration.new_instance_type_id) else: # instance record already has new flavor, hold space for a # possible revert to the old instance type: itype = self._get_instance_type(context, instance, 'old_', migration.old_instance_type_id) elif incoming and not record: # instance has not yet migrated here: itype = self._get_instance_type(context, instance, 'new_', migration.new_instance_type_id) elif outbound and not record: # instance migrated, but record usage for a possible revert: itype = self._get_instance_type(context, instance, 'old_', migration.old_instance_type_id) if image_meta is None: image_meta = utils.get_image_from_system_metadata( instance['system_metadata']) if itype: host_topology = resources.get('numa_topology') if host_topology: host_topology = objects.NUMATopology.obj_from_db_obj( host_topology) numa_topology = hardware.numa_get_constraints(itype, image_meta) numa_topology = ( hardware.numa_fit_instance_to_host( host_topology, numa_topology)) usage = self._get_usage_dict( itype, numa_topology=numa_topology) if self.pci_tracker: self.pci_tracker.update_pci_for_migration(context, instance) self._update_usage(context, resources, usage) if self.pci_tracker: resources['pci_device_pools'] = self.pci_tracker.stats else: resources['pci_device_pools'] = [] self.tracked_migrations[uuid] = (migration, itype)