From 75d38d59f1743ad594831ce5bc1a123be263495a Mon Sep 17 00:00:00 2001 From: Michael Still Date: Tue, 14 Feb 2012 19:17:12 +1100 Subject: Resolve bug/927714 -- get instance names from db. I thought when I wrote the imagecache code for libvirt that the instance names were of a fixed format. That is not correct. First off the format is set in the instance_name_template flag. Worse than that, it can change over time and old instances are not renamed. So, now I check the database to see if the directory name is a valid instance name (for this machine). Change-Id: I318215aef5a10cee32959fd947ad628edff293a2 --- nova/tests/test_imagecache.py | 38 +++++++++++++++++++++++++++++++++++--- nova/virt/libvirt/imagecache.py | 12 +++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/nova/tests/test_imagecache.py b/nova/tests/test_imagecache.py index f9d0e2103..70b9042b0 100644 --- a/nova/tests/test_imagecache.py +++ b/nova/tests/test_imagecache.py @@ -40,6 +40,13 @@ LOG = logging.getLogger(__name__) class ImageCacheManagerTestCase(test.TestCase): + def setUp(self): + super(ImageCacheManagerTestCase, self).setUp() + self.stock_instance_names = {'instance-00000001': '123', + 'instance-00000002': '456', + 'instance-00000003': '789', + 'banana-42-hamster': '444'} + def test_read_stored_checksum_missing(self): self.stubs.Set(os.path, 'exists', lambda x: False) @@ -113,13 +120,16 @@ class ImageCacheManagerTestCase(test.TestCase): self.stubs.Set(db, 'instance_get_all', lambda x: [{'image_ref': 'image-1', 'host': FLAGS.host, - 'name': 'inst-1'}, + 'name': 'inst-1', + 'uuid': '123'}, {'image_ref': 'image-2', 'host': FLAGS.host, - 'name': 'inst-2'}, + 'name': 'inst-2', + 'uuid': '456'}, {'image_ref': 'image-2', 'host': 'remotehost', - 'name': 'inst-3'}]) + 'name': 'inst-3', + 'uuid': '789'}]) image_cache_manager = imagecache.ImageCacheManager() @@ -150,6 +160,7 @@ class ImageCacheManagerTestCase(test.TestCase): image_cache_manager = imagecache.ImageCacheManager() image_cache_manager.unexplained_images = [found] + image_cache_manager.instance_names = self.stock_instance_names inuse_images = image_cache_manager._list_backing_images() @@ -172,6 +183,27 @@ class ImageCacheManagerTestCase(test.TestCase): image_cache_manager = imagecache.ImageCacheManager() image_cache_manager.unexplained_images = [found] + image_cache_manager.instance_names = self.stock_instance_names + + inuse_images = image_cache_manager._list_backing_images() + + self.assertEquals(inuse_images, [found]) + self.assertEquals(len(image_cache_manager.unexplained_images), 0) + + def test_list_backing_images_instancename(self): + self.stubs.Set(os, 'listdir', + lambda x: ['_base', 'banana-42-hamster']) + self.stubs.Set(os.path, 'exists', + lambda x: x.find('banana-42-hamster') != -1) + self.stubs.Set(virtutils, 'get_disk_backing_file', + lambda x: 'e97222e91fc4241f49a7f520d1dcf446751129b3_sm') + + found = os.path.join(FLAGS.instances_path, '_base', + 'e97222e91fc4241f49a7f520d1dcf446751129b3_sm') + + image_cache_manager = imagecache.ImageCacheManager() + image_cache_manager.unexplained_images = [found] + image_cache_manager.instance_names = self.stock_instance_names inuse_images = image_cache_manager._list_backing_images() diff --git a/nova/virt/libvirt/imagecache.py b/nova/virt/libvirt/imagecache.py index d98644487..e8d448337 100644 --- a/nova/virt/libvirt/imagecache.py +++ b/nova/virt/libvirt/imagecache.py @@ -82,6 +82,11 @@ class ImageCacheManager(object): self.unexplained_images = [] self.originals = [] + # These are populated by a call to _list_running_instances() + self.used_images = {} + self.image_popularity = {} + self.instance_names = {} + def _store_image(self, base_dir, ent, original=False): """Store a base image for later examination.""" entpath = os.path.join(base_dir, ent) @@ -112,9 +117,12 @@ class ImageCacheManager(object): """List running instances (on all compute nodes).""" self.used_images = {} self.image_popularity = {} + self.instance_names = {} instances = db.instance_get_all(context) for instance in instances: + self.instance_names[instance['name']] = instance['uuid'] + image_ref_str = str(instance['image_ref']) local, remote, insts = self.used_images.get(image_ref_str, (0, 0, [])) @@ -132,9 +140,11 @@ class ImageCacheManager(object): """List the backing images currently in use.""" inuse_images = [] for ent in os.listdir(FLAGS.instances_path): - if ent.startswith('instance-'): + if ent in self.instance_names: + LOG.debug(_('%s is a valid instance name'), ent) disk_path = os.path.join(FLAGS.instances_path, ent, 'disk') if os.path.exists(disk_path): + LOG.debug(_('%s has a disk file'), ent) backing_file = virtutils.get_disk_backing_file(disk_path) LOG.debug(_('Instance %(instance)s is backed by ' '%(backing)s'), -- cgit