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<amir.sadoughi@gmail.com>
Change-Id: Ia66360f51e1d7a12273df2a080931958cc4f86b5
This commit is contained in:
Yaguang Tang 2013-07-23 18:10:01 +08:00
parent c4e7df7a98
commit a4cf3ef163
6 changed files with 83 additions and 43 deletions

View File

@ -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'],

View File

@ -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'

View File

@ -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'

View File

@ -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."""

View File

@ -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,

View File

@ -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)