Add instance_type_get() to virt api

...and remove the use of instance['extra_specs'] from the libvirt and
baremetal virt drivers. Also remove the hack in instance_update()
which places them there in the first place.

Fixes bug 1133572

Change-Id: I39e9fabb28b48dc52ec47f58d76b0bf2c6ee0204
This commit is contained in:
Dan Smith 2013-02-27 14:50:50 -05:00
parent 3246f3051d
commit a42d83d2be
4 changed files with 58 additions and 32 deletions

View File

@ -37,6 +37,7 @@ from nova.virt.baremetal import db
from nova.virt.baremetal import pxe
from nova.virt.baremetal import utils as bm_utils
from nova.virt.disk import api as disk_api
from nova.virt import fake as fake_virt
CONF = cfg.CONF
@ -61,7 +62,7 @@ class BareMetalPXETestCase(bm_db_base.BMDBTestCase):
super(BareMetalPXETestCase, self).setUp()
self.flags(**COMMON_FLAGS)
self.flags(**BAREMETAL_FLAGS)
self.driver = pxe.PXE()
self.driver = pxe.PXE(fake_virt.FakeVirtAPI())
fake_image.stub_out_image_service(self.stubs)
self.addCleanup(fake_image.FakeImageService_reset)
@ -239,20 +240,21 @@ class PXEClassMethodsTestCase(BareMetalPXETestCase):
self.assertEqual(sizes[1], 1)
def test_get_tftp_image_info(self):
instance_type = utils.get_test_instance_type()
# Raises an exception when options are neither specified
# on the instance nor in configuration file
CONF.baremetal.deploy_kernel = None
CONF.baremetal.deploy_ramdisk = None
self.assertRaises(exception.NovaException,
pxe.get_tftp_image_info,
self.instance)
self.instance, instance_type)
# Test that other non-true values also raise an exception
CONF.baremetal.deploy_kernel = ""
CONF.baremetal.deploy_ramdisk = ""
self.assertRaises(exception.NovaException,
pxe.get_tftp_image_info,
self.instance)
self.instance, instance_type)
# Even if the instance includes kernel_id and ramdisk_id,
# we still need deploy_kernel_id and deploy_ramdisk_id.
@ -262,7 +264,7 @@ class PXEClassMethodsTestCase(BareMetalPXETestCase):
self.instance['ramdisk_id'] = 'bbbb'
self.assertRaises(exception.NovaException,
pxe.get_tftp_image_info,
self.instance)
self.instance, instance_type)
# If an instance doesn't specify deploy_kernel_id or deploy_ramdisk_id,
# but defaults are set in the config file, we should use those.
@ -272,7 +274,7 @@ class PXEClassMethodsTestCase(BareMetalPXETestCase):
CONF.baremetal.deploy_kernel = 'cccc'
CONF.baremetal.deploy_ramdisk = 'dddd'
base = os.path.join(CONF.baremetal.tftp_root, self.instance['uuid'])
res = pxe.get_tftp_image_info(self.instance)
res = pxe.get_tftp_image_info(self.instance, instance_type)
expected = {
'kernel': ['aaaa', os.path.join(base, 'kernel')],
'ramdisk': ['bbbb', os.path.join(base, 'ramdisk')],
@ -290,8 +292,8 @@ class PXEClassMethodsTestCase(BareMetalPXETestCase):
'deploy_kernel_id': 'eeee',
'deploy_ramdisk_id': 'ffff',
}
self.instance['extra_specs'] = extra_specs
res = pxe.get_tftp_image_info(self.instance)
instance_type['extra_specs'] = extra_specs
res = pxe.get_tftp_image_info(self.instance, instance_type)
self.assertEqual(res['deploy_kernel'][0], 'eeee')
self.assertEqual(res['deploy_ramdisk'][0], 'ffff')
@ -301,10 +303,10 @@ class PXEClassMethodsTestCase(BareMetalPXETestCase):
'deploy_kernel_id': '',
'deploy_ramdisk_id': '',
}
self.instance['extra_specs'] = extra_specs
instance_type['extra_specs'] = extra_specs
self.assertRaises(exception.NovaException,
pxe.get_tftp_image_info,
self.instance)
self.instance, instance_type)
class PXEPrivateMethodsTestCase(BareMetalPXETestCase):
@ -320,12 +322,13 @@ class PXEPrivateMethodsTestCase(BareMetalPXETestCase):
def test_cache_tftp_images(self):
self.instance['kernel_id'] = 'aaaa'
self.instance['ramdisk_id'] = 'bbbb'
instance_type = utils.get_test_instance_type()
extra_specs = {
'deploy_kernel_id': 'cccc',
'deploy_ramdisk_id': 'dddd',
}
self.instance['extra_specs'] = extra_specs
image_info = pxe.get_tftp_image_info(self.instance)
instance_type['extra_specs'] = extra_specs
image_info = pxe.get_tftp_image_info(self.instance, instance_type)
self.mox.StubOutWithMock(os, 'makedirs')
self.mox.StubOutWithMock(os.path, 'exists')
@ -390,12 +393,15 @@ class PXEPublicMethodsTestCase(BareMetalPXETestCase):
def test_cache_images(self):
self._create_node()
self.mox.StubOutWithMock(self.driver.virtapi, 'instance_type_get')
self.mox.StubOutWithMock(pxe, "get_tftp_image_info")
self.mox.StubOutWithMock(self.driver, "_cache_tftp_images")
self.mox.StubOutWithMock(self.driver, "_cache_image")
self.mox.StubOutWithMock(self.driver, "_inject_into_image")
pxe.get_tftp_image_info(self.instance).AndReturn([])
self.driver.virtapi.instance_type_get(
self.context, self.instance['instance_type_id']).AndReturn({})
pxe.get_tftp_image_info(self.instance, {}).AndReturn([])
self.driver._cache_tftp_images(self.context, self.instance, [])
self.driver._cache_image(self.context, self.instance, [])
self.driver._inject_into_image(self.context, self.node, self.instance,
@ -440,6 +446,7 @@ class PXEPublicMethodsTestCase(BareMetalPXETestCase):
pxe_path = pxe.get_pxe_config_file_path(self.instance)
image_path = pxe.get_image_file_path(self.instance)
self.mox.StubOutWithMock(self.driver.virtapi, 'instance_type_get')
self.mox.StubOutWithMock(pxe, 'get_tftp_image_info')
self.mox.StubOutWithMock(pxe, 'get_partition_sizes')
self.mox.StubOutWithMock(bm_utils, 'random_alnum')
@ -447,7 +454,9 @@ class PXEPublicMethodsTestCase(BareMetalPXETestCase):
self.mox.StubOutWithMock(bm_utils, 'write_to_file')
self.mox.StubOutWithMock(bm_utils, 'create_link_without_raise')
pxe.get_tftp_image_info(self.instance).AndReturn(image_info)
self.driver.virtapi.instance_type_get(
self.context, self.instance['instance_type_id']).AndReturn({})
pxe.get_tftp_image_info(self.instance, {}).AndReturn(image_info)
pxe.get_partition_sizes(self.instance).AndReturn((0, 0))
bm_utils.random_alnum(32).AndReturn('alnum')
pxe.build_pxe_config(
@ -466,18 +475,24 @@ class PXEPublicMethodsTestCase(BareMetalPXETestCase):
def test_activate_and_deactivate_bootloader(self):
self._create_node()
extra_specs = {
instance_type = {
'extra_specs': {
'deploy_kernel_id': 'eeee',
'deploy_ramdisk_id': 'ffff',
}
}
self.instance['extra_specs'] = extra_specs
self.instance['uuid'] = 'fake-uuid'
self.mox.StubOutWithMock(self.driver.virtapi, 'instance_type_get')
self.mox.StubOutWithMock(bm_utils, 'write_to_file')
self.mox.StubOutWithMock(bm_utils, 'create_link_without_raise')
self.mox.StubOutWithMock(bm_utils, 'unlink_without_raise')
self.mox.StubOutWithMock(bm_utils, 'rmtree_without_raise')
self.driver.virtapi.instance_type_get(
self.context, self.instance['instance_type_id']).AndReturn(
instance_type)
# create the config file
bm_utils.write_to_file(mox.StrContains('fake-uuid'),
mox.StrContains(CONF.baremetal.tftp_root))
@ -525,7 +540,9 @@ class PXEPublicMethodsTestCase(BareMetalPXETestCase):
self.mox.StubOutWithMock(pxe, 'get_tftp_image_info')
self.mox.StubOutWithMock(self.driver, '_collect_mac_addresses')
pxe.get_tftp_image_info(self.instance).\
extra_specs = dict(extra_specs=dict(deploy_ramdisk_id='ignore',
deploy_kernel_id='ignore'))
pxe.get_tftp_image_info(self.instance, extra_specs).\
AndRaise(exception.NovaException)
bm_utils.unlink_without_raise(pxe_path)
self.driver._collect_mac_addresses(self.context, self.node).\

View File

@ -21,8 +21,8 @@ from nova.virt.baremetal import baremetal_states
class NodeDriver(object):
def __init__(self):
pass
def __init__(self, virtapi):
self.virtapi = virtapi
def cache_images(self, context, node, instance, **kwargs):
raise NotImplementedError()

View File

@ -122,7 +122,7 @@ class BareMetalDriver(driver.ComputeDriver):
super(BareMetalDriver, self).__init__(virtapi)
self.driver = importutils.import_object(
CONF.baremetal.driver)
CONF.baremetal.driver, virtapi)
self.vif_driver = importutils.import_object(
CONF.baremetal.vif_driver)
self.firewall_driver = firewall.load_driver(

View File

@ -148,13 +148,13 @@ def build_network_config(network_info):
return network_config
def get_deploy_aki_id(instance):
return instance.get('extra_specs', {}).\
def get_deploy_aki_id(instance_type):
return instance_type.get('extra_specs', {}).\
get('deploy_kernel_id', CONF.baremetal.deploy_kernel)
def get_deploy_ari_id(instance):
return instance.get('extra_specs', {}).\
def get_deploy_ari_id(instance_type):
return instance_type.get('extra_specs', {}).\
get('deploy_ramdisk_id', CONF.baremetal.deploy_ramdisk)
@ -196,13 +196,13 @@ def get_pxe_mac_path(mac):
)
def get_tftp_image_info(instance):
def get_tftp_image_info(instance, instance_type):
"""Generate the paths for tftp files for this instance
Raises NovaException if
- instance does not contain kernel_id or ramdisk_id
- deploy_kernel_id or deploy_ramdisk_id can not be read from
instance['extra_specs'] and defaults are not set
instance_type['extra_specs'] and defaults are not set
"""
image_info = {
@ -214,8 +214,8 @@ def get_tftp_image_info(instance):
try:
image_info['kernel'][0] = str(instance['kernel_id'])
image_info['ramdisk'][0] = str(instance['ramdisk_id'])
image_info['deploy_kernel'][0] = get_deploy_aki_id(instance)
image_info['deploy_ramdisk'][0] = get_deploy_ari_id(instance)
image_info['deploy_kernel'][0] = get_deploy_aki_id(instance_type)
image_info['deploy_ramdisk'][0] = get_deploy_ari_id(instance_type)
except KeyError as e:
pass
@ -237,8 +237,8 @@ def get_tftp_image_info(instance):
class PXE(base.NodeDriver):
"""PXE bare metal driver."""
def __init__(self):
super(PXE, self).__init__()
def __init__(self, virtapi):
super(PXE, self).__init__(virtapi)
def _collect_mac_addresses(self, context, node):
macs = set()
@ -341,7 +341,9 @@ class PXE(base.NodeDriver):
def cache_images(self, context, node, instance,
admin_password, image_meta, injected_files, network_info):
"""Prepare all the images for this instance."""
tftp_image_info = get_tftp_image_info(instance)
instance_type = self.virtapi.instance_type_get(
context, instance['instance_type_id'])
tftp_image_info = get_tftp_image_info(instance, instance_type)
self._cache_tftp_images(context, instance, tftp_image_info)
self._cache_image(context, instance, image_meta)
@ -374,7 +376,9 @@ class PXE(base.NodeDriver):
./pxelinux.cfg/
{mac} -> ../{uuid}/config
"""
image_info = get_tftp_image_info(instance)
instance_type = self.virtapi.instance_type_get(
context, instance['instance_type_id'])
image_info = get_tftp_image_info(instance, instance_type)
(root_mb, swap_mb) = get_partition_sizes(instance)
pxe_config_file_path = get_pxe_config_file_path(instance)
image_file_path = get_image_file_path(instance)
@ -416,8 +420,13 @@ class PXE(base.NodeDriver):
except exception.NodeNotFound:
pass
# NOTE(danms): the instance_type extra_specs do not need to be
# present/correct at deactivate time, so pass something empty
# to avoid an extra lookup
instance_type = dict(extra_specs=dict(deploy_ramdisk_id='ignore',
deploy_kernel_id='ignore'))
try:
image_info = get_tftp_image_info(instance)
image_info = get_tftp_image_info(instance, instance_type)
except exception.NovaException:
pass
else: