From e6cae13dd21f03948a28088d45be678f6a0e5c4e Mon Sep 17 00:00:00 2001 From: Arata Notsu Date: Thu, 20 Dec 2012 21:29:13 +0900 Subject: attach/detach_volume() take instance as a parameter Previously the methods take instance['name'] as a parameter. With this change, ComputeDriver can lookup informations about the instance from hypervisor (in bare-metal driver, it is a local DB) by any other attributes of the instance. blueprint general-bare-metal-provisioning-framework Change-Id: Ibd0567f34ed5053909ce1a408f9cbf87516ba597 --- nova/compute/manager.py | 4 ++-- nova/tests/test_hypervapi.py | 4 ++-- nova/tests/test_libvirt.py | 4 ++-- nova/tests/test_virt_drivers.py | 8 ++++---- nova/tests/test_xenapi.py | 4 ++-- nova/virt/baremetal/driver.py | 8 ++++---- nova/virt/baremetal/volume_driver.py | 34 ++++++++++------------------------ nova/virt/driver.py | 4 ++-- nova/virt/fake.py | 7 ++++--- nova/virt/hyperv/driver.py | 8 ++++---- nova/virt/libvirt/driver.py | 6 ++++-- nova/virt/vmwareapi/driver.py | 4 ++-- nova/virt/xenapi/driver.py | 8 ++++---- 13 files changed, 46 insertions(+), 57 deletions(-) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index b161f504c..8ac92f053 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -2388,7 +2388,7 @@ class ComputeManager(manager.SchedulerDependentManager): try: self.driver.attach_volume(connection_info, - instance['name'], + instance, mountpoint) except Exception: # pylint: disable=W0702 with excutils.save_and_reraise_exception(): @@ -2435,7 +2435,7 @@ class ComputeManager(manager.SchedulerDependentManager): connection_info['serial'] = volume_id try: self.driver.detach_volume(connection_info, - instance['name'], + instance, mp) except Exception: # pylint: disable=W0702 with excutils.save_and_reraise_exception(): diff --git a/nova/tests/test_hypervapi.py b/nova/tests/test_hypervapi.py index cab877da9..f5713c457 100644 --- a/nova/tests/test_hypervapi.py +++ b/nova/tests/test_hypervapi.py @@ -508,7 +508,7 @@ class HyperVAPITestCase(basetestcase.BaseTestCase): self._volume_target_portal, self._volume_id) self._conn.attach_volume(connection_info, - self._instance_data["name"], '/dev/sdc') + self._instance_data, '/dev/sdc') def test_attach_volume(self): self._attach_volume() @@ -527,7 +527,7 @@ class HyperVAPITestCase(basetestcase.BaseTestCase): self._volume_target_portal, self._volume_id) self._conn.detach_volume(connection_info, - self._instance_data["name"], '/dev/sdc') + self._instance_data, '/dev/sdc') (_, volumes_paths, _) = self._hypervutils.get_vm_disks( self._instance_data["name"]) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 6bc18251f..7464b9728 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -1707,8 +1707,8 @@ class LibvirtConnTestCase(test.TestCase): self.assertRaises(exception.VolumeDriverNotFound, conn.attach_volume, {"driver_volume_type": "badtype"}, - "fake", - "/dev/fake") + {"name": "fake-instance"}, + "/dev/fake") def test_multi_nic(self): instance_data = dict(self.test_instance) diff --git a/nova/tests/test_virt_drivers.py b/nova/tests/test_virt_drivers.py index 9d9ebcad9..9e9309dfe 100644 --- a/nova/tests/test_virt_drivers.py +++ b/nova/tests/test_virt_drivers.py @@ -379,10 +379,10 @@ class _VirtDriverTestCase(_FakeDriverBackendTestCase): def test_attach_detach_volume(self): instance_ref, network_info = self._get_running_instance() self.connection.attach_volume({'driver_volume_type': 'fake'}, - instance_ref['name'], + instance_ref, '/mnt/nova/something') self.connection.detach_volume({'driver_volume_type': 'fake'}, - instance_ref['name'], + instance_ref, '/mnt/nova/something') @catch_notimplementederror @@ -390,11 +390,11 @@ class _VirtDriverTestCase(_FakeDriverBackendTestCase): instance_ref, network_info = self._get_running_instance() self.connection.power_off(instance_ref) self.connection.attach_volume({'driver_volume_type': 'fake'}, - instance_ref['name'], + instance_ref, '/mnt/nova/something') self.connection.power_on(instance_ref) self.connection.detach_volume({'driver_volume_type': 'fake'}, - instance_ref['name'], + instance_ref, '/mnt/nova/something') @catch_notimplementederror diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index 8b57dfef4..02d688a3b 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -271,7 +271,7 @@ class XenAPIVolumeTestCase(stubs.XenAPITestBase): instance = db.instance_create(self.context, self.instance_values) vm = xenapi_fake.create_vm(instance['name'], 'Running') result = conn.attach_volume(self._make_connection_info(), - instance['name'], '/dev/sdc') + instance, '/dev/sdc') # check that the VM has a VBD attached to it # Get XenAPI record for VBD @@ -290,7 +290,7 @@ class XenAPIVolumeTestCase(stubs.XenAPITestBase): self.assertRaises(exception.VolumeDriverNotFound, conn.attach_volume, {'driver_volume_type': 'nonexist'}, - instance['name'], + instance, '/dev/sdc') diff --git a/nova/virt/baremetal/driver.py b/nova/virt/baremetal/driver.py index e517b399d..1d46e85a3 100644 --- a/nova/virt/baremetal/driver.py +++ b/nova/virt/baremetal/driver.py @@ -308,14 +308,14 @@ class BareMetalDriver(driver.ComputeDriver): def get_volume_connector(self, instance): return self.volume_driver.get_volume_connector(instance) - def attach_volume(self, connection_info, instance_name, mountpoint): + def attach_volume(self, connection_info, instance, mountpoint): return self.volume_driver.attach_volume(connection_info, - instance_name, mountpoint) + instance, mountpoint) @exception.wrap_exception() - def detach_volume(self, connection_info, instance_name, mountpoint): + def detach_volume(self, connection_info, instance, mountpoint): return self.volume_driver.detach_volume(connection_info, - instance_name, mountpoint) + instance, mountpoint) def get_info(self, instance): # NOTE(deva): compute/manager.py expects to get NotFound exception diff --git a/nova/virt/baremetal/volume_driver.py b/nova/virt/baremetal/volume_driver.py index 7f59ec517..09088dd53 100644 --- a/nova/virt/baremetal/volume_driver.py +++ b/nova/virt/baremetal/volume_driver.py @@ -50,22 +50,9 @@ CONF.import_opt('libvirt_volume_drivers', 'nova.virt.libvirt.driver') LOG = logging.getLogger(__name__) -def _get_baremetal_node_by_instance_name(virtapi, instance_name): +def _get_baremetal_node_by_instance_uuid(instance_uuid): context = nova_context.get_admin_context() - # TODO(deva): optimize this DB query. - # I don't think it should be _get_all - for node in bmdb.bm_node_get_all(context, service_host=CONF.host): - if not node['instance_uuid']: - continue - try: - inst = virtapi.instance_get_by_uuid(context, node['instance_uuid']) - if inst['name'] == instance_name: - return node - except exception.InstanceNotFound: - continue - - # raise exception if we found no matching instance - raise exception.InstanceNotFound(instance_name) + return bmdb.bm_node_get_by_instance_uuid(context, instance_uuid) def _create_iscsi_export_tgtadm(path, tid, iqn): @@ -200,10 +187,10 @@ class VolumeDriver(object): 'host': CONF.host, } - def attach_volume(self, connection_info, instance_name, mountpoint): + def attach_volume(self, connection_info, instance, mountpoint): raise NotImplementedError() - def detach_volume(self, connection_info, instance_name, mountpoint): + def detach_volume(self, connection_info, instance, mountpoint): raise NotImplementedError() @@ -227,22 +214,21 @@ class LibvirtVolumeDriver(VolumeDriver): method = getattr(driver, method_name) return method(connection_info, *args, **kwargs) - def attach_volume(self, connection_info, instance_name, mountpoint): - node = _get_baremetal_node_by_instance_name(self.virtapi, - instance_name) + def attach_volume(self, connection_info, instance, mountpoint): + node = _get_baremetal_node_by_instance_uuid(instance['uuid']) ctx = nova_context.get_admin_context() pxe_ip = bmdb.bm_pxe_ip_get_by_bm_node_id(ctx, node['id']) if not pxe_ip: if not CONF.baremetal.use_unsafe_iscsi: raise exception.NovaException(_( - 'No fixed PXE IP is associated to %s') % instance_name) + 'No fixed PXE IP is associated to %s') % instance['uuid']) mount_device = mountpoint.rpartition("/")[2] self._volume_driver_method('connect_volume', connection_info, mount_device) device_path = connection_info['data']['device_path'] - iqn = _get_iqn(instance_name, mountpoint) + iqn = _get_iqn(instance['name'], mountpoint) tid = _get_next_tid() _create_iscsi_export_tgtadm(device_path, tid, iqn) @@ -259,10 +245,10 @@ class LibvirtVolumeDriver(VolumeDriver): _allow_iscsi_tgtadm(tid, 'ALL') @exception.wrap_exception() - def detach_volume(self, connection_info, instance_name, mountpoint): + def detach_volume(self, connection_info, instance, mountpoint): mount_device = mountpoint.rpartition("/")[2] try: - iqn = _get_iqn(instance_name, mountpoint) + iqn = _get_iqn(instance['name'], mountpoint) tid = _find_tid(iqn) if tid is not None: _delete_iscsi_export_tgtadm(tid) diff --git a/nova/virt/driver.py b/nova/virt/driver.py index 7d627e80c..f7d9c21f5 100644 --- a/nova/virt/driver.py +++ b/nova/virt/driver.py @@ -263,11 +263,11 @@ class ComputeDriver(object): # TODO(Vek): Need to pass context in for access to auth_token raise NotImplementedError() - def attach_volume(self, connection_info, instance_name, mountpoint): + def attach_volume(self, connection_info, instance, mountpoint): """Attach the disk to the instance at mountpoint using info""" raise NotImplementedError() - def detach_volume(self, connection_info, instance_name, mountpoint): + def detach_volume(self, connection_info, instance, mountpoint): """Detach the disk attached to the instance""" raise NotImplementedError() diff --git a/nova/virt/fake.py b/nova/virt/fake.py index 5d3b3c926..cf43f4c68 100644 --- a/nova/virt/fake.py +++ b/nova/virt/fake.py @@ -198,17 +198,18 @@ class FakeDriver(driver.ComputeDriver): {'key': key, 'inst': self.instances}, instance=instance) - def attach_volume(self, connection_info, instance_name, mountpoint): + def attach_volume(self, connection_info, instance, mountpoint): """Attach the disk to the instance at mountpoint using info""" + instance_name = instance['name'] if not instance_name in self._mounts: self._mounts[instance_name] = {} self._mounts[instance_name][mountpoint] = connection_info return True - def detach_volume(self, connection_info, instance_name, mountpoint): + def detach_volume(self, connection_info, instance, mountpoint): """Detach the disk attached to the instance""" try: - del self._mounts[instance_name][mountpoint] + del self._mounts[instance['name']][mountpoint] except KeyError: pass return True diff --git a/nova/virt/hyperv/driver.py b/nova/virt/hyperv/driver.py index 2b57ba0b1..1fba67dc6 100644 --- a/nova/virt/hyperv/driver.py +++ b/nova/virt/hyperv/driver.py @@ -104,16 +104,16 @@ class HyperVDriver(driver.ComputeDriver): def get_info(self, instance): return self._vmops.get_info(instance) - def attach_volume(self, connection_info, instance_name, mountpoint): + def attach_volume(self, connection_info, instance, mountpoint): """Attach volume storage to VM instance""" return self._volumeops.attach_volume(connection_info, - instance_name, + instance['name'], mountpoint) - def detach_volume(self, connection_info, instance_name, mountpoint): + def detach_volume(self, connection_info, instance, mountpoint): """Detach volume storage to VM instance""" return self._volumeops.detach_volume(connection_info, - instance_name, + instance['name'], mountpoint) def get_volume_connector(self, instance): diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index e3d95c62e..939260208 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -651,7 +651,8 @@ class LibvirtDriver(driver.ComputeDriver): return method(connection_info, *args, **kwargs) @exception.wrap_exception() - def attach_volume(self, connection_info, instance_name, mountpoint): + def attach_volume(self, connection_info, instance, mountpoint): + instance_name = instance['name'] virt_dom = self._lookup_by_name(instance_name) mount_device = mountpoint.rpartition("/")[2] conf = self.volume_driver_method('connect_volume', @@ -705,7 +706,8 @@ class LibvirtDriver(driver.ComputeDriver): return xml @exception.wrap_exception() - def detach_volume(self, connection_info, instance_name, mountpoint): + def detach_volume(self, connection_info, instance, mountpoint): + instance_name = instance['name'] mount_device = mountpoint.rpartition("/")[2] try: virt_dom = self._lookup_by_name(instance_name) diff --git a/nova/virt/vmwareapi/driver.py b/nova/virt/vmwareapi/driver.py index 50fc3e922..7c9afc403 100644 --- a/nova/virt/vmwareapi/driver.py +++ b/nova/virt/vmwareapi/driver.py @@ -178,11 +178,11 @@ class VMWareESXDriver(driver.ComputeDriver): 'host': None } - def attach_volume(self, connection_info, instance_name, mountpoint): + def attach_volume(self, connection_info, instance, mountpoint): """Attach volume storage to VM instance.""" pass - def detach_volume(self, connection_info, instance_name, mountpoint): + def detach_volume(self, connection_info, instance, mountpoint): """Detach volume storage to VM instance.""" pass diff --git a/nova/virt/xenapi/driver.py b/nova/virt/xenapi/driver.py index d3047d364..680f563ab 100644 --- a/nova/virt/xenapi/driver.py +++ b/nova/virt/xenapi/driver.py @@ -355,16 +355,16 @@ class XenAPIDriver(driver.ComputeDriver): xs_url = urlparse.urlparse(CONF.xenapi_connection_url) return xs_url.netloc - def attach_volume(self, connection_info, instance_name, mountpoint): + def attach_volume(self, connection_info, instance, mountpoint): """Attach volume storage to VM instance""" return self._volumeops.attach_volume(connection_info, - instance_name, + instance['name'], mountpoint) - def detach_volume(self, connection_info, instance_name, mountpoint): + def detach_volume(self, connection_info, instance, mountpoint): """Detach volume storage to VM instance""" return self._volumeops.detach_volume(connection_info, - instance_name, + instance['name'], mountpoint) def get_console_pool_info(self, console_type): -- cgit