summaryrefslogtreecommitdiffstats
path: root/nova/virt
diff options
context:
space:
mode:
authorJay Pipes <jaypipes@gmail.com>2012-02-08 15:51:59 -0500
committerJay Pipes <jaypipes@gmail.com>2012-02-10 17:11:32 -0500
commit27c11c4bb4e5c54282caf49cba666f45cfc590c2 (patch)
tree2e6a76cfafcfb976f3a1a0daab808ccc73416332 /nova/virt
parentd808ce11668c08bd896771f578f70e15f1eeb88d (diff)
Remedies LP Bug #928910 - Use libvirt lookupByName() to check existence
Make determining if an instance exists on a host more efficient by adding an instance_exists() method to the base virt driver that can be overridden by drivers that have a more efficient mechanism of looking up an instance by its ID / name. Modifies the _check_instance_already_created method of the compute manager to use this new instance_exists() method. Someone from Citrix should look into how to make the instance_exists() method in the Xen and VMWare virt drivers more efficient than the base "loop over all domains and see if the instance ID exists" method now in the base driver class. Change-Id: Ibf219788f9c104698057367da89300a060945778
Diffstat (limited to 'nova/virt')
-rw-r--r--nova/virt/driver.py15
-rw-r--r--nova/virt/libvirt/connection.py8
2 files changed, 23 insertions, 0 deletions
diff --git a/nova/virt/driver.py b/nova/virt/driver.py
index 34ce9613b..ec143d8ca 100644
--- a/nova/virt/driver.py
+++ b/nova/virt/driver.py
@@ -114,6 +114,21 @@ class ComputeDriver(object):
# TODO(Vek): Need to pass context in for access to auth_token
raise NotImplementedError()
+ def instance_exists(self, instance_id):
+ """Checks existence of an instance on the host.
+
+ Returns True if an instance with the supplied ID exists on
+ the host, False otherwise.
+
+ :note This implementation works for all drivers, but it is
+ not particularly efficient. Maintainers of the virt drivers are
+ encouraged to override this method with something more
+ efficient.
+
+ :param instance_id: The ID / name of the instance to lookup
+ """
+ return instance_id in self.list_instances()
+
def list_instances(self):
"""
Return the names of all the instances known to the virtualization
diff --git a/nova/virt/libvirt/connection.py b/nova/virt/libvirt/connection.py
index 0ae8dd1fb..9e5f81690 100644
--- a/nova/virt/libvirt/connection.py
+++ b/nova/virt/libvirt/connection.py
@@ -281,6 +281,14 @@ class LibvirtConnection(driver.ComputeDriver):
else:
return libvirt.openAuth(uri, auth, 0)
+ def instance_exists(self, instance_id):
+ """Efficient override of base instance_exists method."""
+ try:
+ _ignored = self._conn.lookupByName(instance_id)
+ return True
+ except libvirt.libvirtError:
+ return False
+
def list_instances(self):
return [self._conn.lookupByID(x).name()
for x in self._conn.listDomainsID()