diff options
| author | Jenkins <jenkins@review.openstack.org> | 2012-02-11 01:03:58 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2012-02-11 01:03:58 +0000 |
| commit | 3858bcfbbc0a1cc7b0a455dbef16675c25b322a4 (patch) | |
| tree | cf29f3fcf293c166f81463dcedb16708f95e43e0 /nova | |
| parent | 7c7632bd194fea29c58a0ea597ca4d1c5cf32421 (diff) | |
| parent | 27c11c4bb4e5c54282caf49cba666f45cfc590c2 (diff) | |
| download | nova-3858bcfbbc0a1cc7b0a455dbef16675c25b322a4.tar.gz nova-3858bcfbbc0a1cc7b0a455dbef16675c25b322a4.tar.xz nova-3858bcfbbc0a1cc7b0a455dbef16675c25b322a4.zip | |
Merge "Remedies LP Bug #928910 - Use libvirt lookupByName() to check existence"
Diffstat (limited to 'nova')
| -rw-r--r-- | nova/compute/manager.py | 2 | ||||
| -rw-r--r-- | nova/virt/driver.py | 15 | ||||
| -rw-r--r-- | nova/virt/libvirt/connection.py | 8 |
3 files changed, 24 insertions, 1 deletions
diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 90a6a84ec..078f53928 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -440,7 +440,7 @@ class ComputeManager(manager.SchedulerDependentManager): def _check_instance_not_already_created(self, context, instance): """Ensure an instance with the same name is not already present.""" - if instance['name'] in self.driver.list_instances(): + if self.driver.instance_exists(instance['name']): raise exception.Error(_("Instance has already been created")) def _check_image_size(self, context, instance): diff --git a/nova/virt/driver.py b/nova/virt/driver.py index 200bada29..f1d382ac4 100644 --- a/nova/virt/driver.py +++ b/nova/virt/driver.py @@ -122,6 +122,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 3e8dbb8c1..087dba446 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() |
