From a4cf3ef163f79e6ae8d77f5743ccc19edb60a1f8 Mon Sep 17 00:00:00 2001 From: Yaguang Tang Date: Tue, 23 Jul 2013 18:10:01 +0800 Subject: [PATCH] Update BareMetal driver to current nova.network.model Update baremetal driver and vif to use classes from nova.network.model instead of legacy "network,mapping" tuple. Partly implement blueprint nova-network-legacy Co-author: Amir Sadoughi Change-Id: Ia66360f51e1d7a12273df2a080931958cc4f86b5 --- nova/tests/virt/baremetal/test_driver.py | 2 +- nova/tests/virt/baremetal/test_pxe.py | 49 +++++++++++++++++++----- nova/tests/virt/baremetal/test_tilera.py | 17 ++++---- nova/virt/baremetal/driver.py | 10 ++--- nova/virt/baremetal/utils.py | 34 ++++++++++------ nova/virt/baremetal/vif_driver.py | 14 +++---- 6 files changed, 83 insertions(+), 43 deletions(-) diff --git a/nova/tests/virt/baremetal/test_driver.py b/nova/tests/virt/baremetal/test_driver.py index 77f47816ecf2..3fe26903383b 100644 --- a/nova/tests/virt/baremetal/test_driver.py +++ b/nova/tests/virt/baremetal/test_driver.py @@ -121,7 +121,7 @@ class BareMetalDriverWithDBTestCase(bm_db_base.BMDBTestCase): None, result['instance']), injected_files=[('/fake/path', 'hello world')], instance=result['instance'], - network_info=utils.get_test_network_info(), + network_info=utils.get_test_network_info(legacy_model=False), ) result['destroy_params'] = dict( instance=result['instance'], diff --git a/nova/tests/virt/baremetal/test_pxe.py b/nova/tests/virt/baremetal/test_pxe.py index cd4e5c1435ec..90b2d82ac2e4 100644 --- a/nova/tests/virt/baremetal/test_pxe.py +++ b/nova/tests/virt/baremetal/test_pxe.py @@ -69,7 +69,8 @@ class BareMetalPXETestCase(bm_db_base.BMDBTestCase): self.context = utils.get_test_admin_context() self.test_block_device_info = None, self.instance = utils.get_test_instance() - self.test_network_info = utils.get_test_network_info(), + self.test_network_info = utils.get_test_network_info( + legacy_model=False), self.node_info = bm_db_utils.new_bm_node( service_host='test_host', cpus=4, @@ -146,23 +147,23 @@ class PXEClassMethodsTestCase(BareMetalPXETestCase): pxe_network_config=True, group='baremetal', ) - net = utils.get_test_network_info(1) + net = utils.get_test_network_info(1, legacy_model=False) config = pxe.build_pxe_network_config(net) self.assertIn('eth0:off', config) self.assertNotIn('eth1', config) - net = utils.get_test_network_info(2) + net = utils.get_test_network_info(2, legacy_model=False) config = pxe.build_pxe_network_config(net) self.assertIn('eth0:off', config) self.assertIn('eth1:off', config) def test_build_network_config(self): - net = utils.get_test_network_info(1) + net = utils.get_test_network_info(1, legacy_model=False) config = pxe.build_network_config(net) self.assertIn('eth0', config) self.assertNotIn('eth1', config) - net = utils.get_test_network_info(2) + net = utils.get_test_network_info(2, legacy_model=False) config = pxe.build_network_config(net) self.assertIn('eth0', config) self.assertIn('eth1', config) @@ -173,8 +174,8 @@ class PXEClassMethodsTestCase(BareMetalPXETestCase): 'net-dhcp.ubuntu.template', group='baremetal', ) - net = utils.get_test_network_info() - net[0][1]['ips'][0]['ip'] = '1.2.3.4' + net = utils.get_test_network_info(legacy_model=False) + net[0]['network']['subnets'][0]['ips'][0]['address'] = '1.2.3.4' config = pxe.build_network_config(net) self.assertIn('iface eth0 inet dhcp', config) self.assertNotIn('address 1.2.3.4', config) @@ -185,12 +186,40 @@ class PXEClassMethodsTestCase(BareMetalPXETestCase): 'net-static.ubuntu.template', group='baremetal', ) - net = utils.get_test_network_info() - net[0][1]['ips'][0]['ip'] = '1.2.3.4' + net = utils.get_test_network_info(legacy_model=False) + net[0]['network']['subnets'][0]['ips'][0]['address'] = '1.2.3.4' config = pxe.build_network_config(net) self.assertIn('iface eth0 inet static', config) self.assertIn('address 1.2.3.4', config) + def test_build_network_config_static_parameters(self): + self.flags(use_ipv6=True) + self.flags( + net_config_template='$pybasedir/nova/virt/baremetal/' + 'net-static.ubuntu.template', + group='baremetal' + ) + + net = utils.get_test_network_info(legacy_model=False) + net[0]['network']['subnets'][0]['cidr'] = '10.1.1.0/24' + net[0]['network']['subnets'][0]['gateway']['address'] = '10.1.1.1' + net[0]['network']['subnets'][0]['dns'][0]['address'] = '10.1.1.2' + net[0]['network']['subnets'][0]['dns'][1]['address'] = '10.1.1.3' + + net[0]['network']['subnets'][1]['cidr'] = 'fc00::/7' + net[0]['network']['subnets'][1]['ips'][0]['address'] = 'fc00::1' + net[0]['network']['subnets'][1]['gateway']['address'] = 'fc00::2' + config = pxe.build_network_config(net) + + self.assertIn('iface eth0 inet static', config) + self.assertIn('gateway 10.1.1.1', config) + self.assertIn('dns-nameservers 10.1.1.2 10.1.1.3', config) + + self.assertIn('iface eth0 inet6 static', config) + self.assertIn('address fc00::1', config) + self.assertIn('netmask 7', config) + self.assertIn('gateway fc00::2', config) + def test_image_dir_path(self): self.assertEqual( pxe.get_image_dir_path(self.instance), @@ -379,7 +408,7 @@ class PXEPrivateMethodsTestCase(BareMetalPXETestCase): self.instance['hostname'] = 'fake hostname' files.append(('/etc/hostname', 'fake hostname')) self.instance['key_data'] = 'fake ssh key' - net_info = utils.get_test_network_info(1) + net_info = utils.get_test_network_info(1, legacy_model=False) net = pxe.build_network_config(net_info) admin_password = 'fake password' diff --git a/nova/tests/virt/baremetal/test_tilera.py b/nova/tests/virt/baremetal/test_tilera.py index 7ad5c4b6a5ee..329808fa6971 100755 --- a/nova/tests/virt/baremetal/test_tilera.py +++ b/nova/tests/virt/baremetal/test_tilera.py @@ -65,7 +65,8 @@ class BareMetalTileraTestCase(bm_db_base.BMDBTestCase): self.context = utils.get_test_admin_context() self.test_block_device_info = None, self.instance = utils.get_test_instance() - self.test_network_info = utils.get_test_network_info(), + self.test_network_info = utils.get_test_network_info( + legacy_model=False), self.node_info = bm_db_utils.new_bm_node( service_host='test_host', cpus=4, @@ -104,12 +105,12 @@ class BareMetalTileraTestCase(bm_db_base.BMDBTestCase): class TileraClassMethodsTestCase(BareMetalTileraTestCase): def test_build_network_config(self): - net = utils.get_test_network_info(1) + net = utils.get_test_network_info(1, legacy_model=False) config = tilera.build_network_config(net) self.assertIn('eth0', config) self.assertNotIn('eth1', config) - net = utils.get_test_network_info(2) + net = utils.get_test_network_info(2, legacy_model=False) config = tilera.build_network_config(net) self.assertIn('eth0', config) self.assertIn('eth1', config) @@ -120,8 +121,8 @@ class TileraClassMethodsTestCase(BareMetalTileraTestCase): 'net-dhcp.ubuntu.template', group='baremetal', ) - net = utils.get_test_network_info() - net[0][1]['ips'][0]['ip'] = '1.2.3.4' + net = utils.get_test_network_info(legacy_model=False) + net[0]['network']['subnets'][0]['ips'][0]['address'] = '1.2.3.4' config = tilera.build_network_config(net) self.assertIn('iface eth0 inet dhcp', config) self.assertNotIn('address 1.2.3.4', config) @@ -132,8 +133,8 @@ class TileraClassMethodsTestCase(BareMetalTileraTestCase): 'net-static.ubuntu.template', group='baremetal', ) - net = utils.get_test_network_info() - net[0][1]['ips'][0]['ip'] = '1.2.3.4' + net = utils.get_test_network_info(legacy_model=False) + net[0]['network']['subnets'][0]['ips'][0]['address'] = '1.2.3.4' config = tilera.build_network_config(net) self.assertIn('iface eth0 inet static', config) self.assertIn('address 1.2.3.4', config) @@ -235,7 +236,7 @@ class TileraPrivateMethodsTestCase(BareMetalTileraTestCase): self.instance['hostname'] = 'fake hostname' files.append(('/etc/hostname', 'fake hostname')) self.instance['key_data'] = 'fake ssh key' - net_info = utils.get_test_network_info(1) + net_info = utils.get_test_network_info(1, legacy_model=False) net = tilera.build_network_config(net_info) admin_password = 'fake password' diff --git a/nova/virt/baremetal/driver.py b/nova/virt/baremetal/driver.py index 47aac760ae9e..458fd80462b7 100755 --- a/nova/virt/baremetal/driver.py +++ b/nova/virt/baremetal/driver.py @@ -166,7 +166,7 @@ class BareMetalDriver(driver.ComputeDriver): return 1 def legacy_nwinfo(self): - return True + return False def list_instances(self): l = [] @@ -481,12 +481,12 @@ class BareMetalDriver(driver.ComputeDriver): for pif in pifs: if pif['vif_uuid']: db.bm_interface_set_vif_uuid(context, pif['id'], None) - for (network, mapping) in network_info: - self.vif_driver.plug(instance, (network, mapping)) + for vif in network_info: + self.vif_driver.plug(instance, vif) def _unplug_vifs(self, instance, network_info): - for (network, mapping) in network_info: - self.vif_driver.unplug(instance, (network, mapping)) + for vif in network_info: + self.vif_driver.unplug(instance, vif) def manage_image_cache(self, context, all_instances): """Manage the local cache of images.""" diff --git a/nova/virt/baremetal/utils.py b/nova/virt/baremetal/utils.py index 8b77b5e9e148..7e1376dc01ed 100644 --- a/nova/virt/baremetal/utils.py +++ b/nova/virt/baremetal/utils.py @@ -94,20 +94,32 @@ def map_network_interfaces(network_info, use_ipv6=False): network_info = [network_info] interfaces = [] - for id, (network, mapping) in enumerate(network_info): - address_v6 = None - gateway_v6 = None - netmask_v6 = None + for id, vif in enumerate(network_info): + address_v6 = gateway_v6 = netmask_v6 = None + address_v4 = gateway_v4 = netmask_v4 = dns_v4 = None + if use_ipv6: - address_v6 = mapping['ip6s'][0]['ip'] - netmask_v6 = mapping['ip6s'][0]['netmask'] - gateway_v6 = mapping['gateway_v6'] + subnets_v6 = [s for s in vif['network']['subnets'] + if s['version'] == 6] + if len(subnets_v6): + address_v6 = subnets_v6[0]['ips'][0]['address'] + netmask_v6 = subnets_v6[0].as_netaddr()._prefixlen + gateway_v6 = subnets_v6[0]['gateway']['address'] + + subnets_v4 = [s for s in vif['network']['subnets'] + if s['version'] == 4] + if len(subnets_v4): + address_v4 = subnets_v4[0]['ips'][0]['address'] + netmask_v4 = subnets_v4[0].as_netaddr().netmask + gateway_v4 = subnets_v4[0]['gateway']['address'] + dns_v4 = ' '.join([x['address'] for x in subnets_v4[0]['dns']]) + interface = { 'name': 'eth%d' % id, - 'address': mapping['ips'][0]['ip'], - 'gateway': mapping['gateway'], - 'netmask': mapping['ips'][0]['netmask'], - 'dns': ' '.join(mapping['dns']), + 'address': address_v4, + 'gateway': gateway_v4, + 'netmask': netmask_v4, + 'dns': dns_v4, 'address_v6': address_v6, 'gateway_v6': gateway_v6, 'netmask_v6': netmask_v6, diff --git a/nova/virt/baremetal/vif_driver.py b/nova/virt/baremetal/vif_driver.py index ad1521cac29a..ff9f19752138 100644 --- a/nova/virt/baremetal/vif_driver.py +++ b/nova/virt/baremetal/vif_driver.py @@ -28,17 +28,16 @@ LOG = logging.getLogger(__name__) class BareMetalVIFDriver(object): - def _after_plug(self, instance, network, mapping, pif): + def _after_plug(self, instance, vif, pif): pass - def _after_unplug(self, instance, network, mapping, pif): + def _after_unplug(self, instance, vif, pif): pass def plug(self, instance, vif): LOG.debug(_("plug: instance_uuid=%(uuid)s vif=%(vif)s") % {'uuid': instance['uuid'], 'vif': vif}) - network, mapping = vif - vif_uuid = mapping['vif_uuid'] + vif_uuid = vif['id'] ctx = context.get_admin_context() node = bmdb.bm_node_get_by_instance_uuid(ctx, instance['uuid']) @@ -50,7 +49,7 @@ class BareMetalVIFDriver(object): bmdb.bm_interface_set_vif_uuid(ctx, pif['id'], vif_uuid) LOG.debug(_("pif:%(id)s is plugged (vif_uuid=%(vif_uuid)s)") % {'id': pif['id'], 'vif_uuid': vif_uuid}) - self._after_plug(instance, network, mapping, pif) + self._after_plug(instance, vif, pif) return # NOTE(deva): should this really be raising an exception @@ -63,14 +62,13 @@ class BareMetalVIFDriver(object): def unplug(self, instance, vif): LOG.debug(_("unplug: instance_uuid=%(uuid)s vif=%(vif)s"), {'uuid': instance['uuid'], 'vif': vif}) - network, mapping = vif - vif_uuid = mapping['vif_uuid'] + vif_uuid = vif['id'] ctx = context.get_admin_context() try: pif = bmdb.bm_interface_get_by_vif_uuid(ctx, vif_uuid) bmdb.bm_interface_set_vif_uuid(ctx, pif['id'], None) LOG.debug(_("pif:%(id)s is unplugged (vif_uuid=%(vif_uuid)s)") % {'id': pif['id'], 'vif_uuid': vif_uuid}) - self._after_unplug(instance, network, mapping, pif) + self._after_unplug(instance, vif, pif) except exception.NovaException: LOG.warn(_("no pif for vif_uuid=%s") % vif_uuid)