Move compute_driver into nova.virt.driver

Commit 9102f80 moved compute_driver from nova.compute.manager into
nova.config. We're trying to get away from so many global options,
so where to put it?

The config option is used for two things - loading the driver and,
in a bunch of random places, checking if it matches a specific
value.

It seems to make perfect to sense to encapsulate both as functions
in nova.virt.driver and move the option there.

blueprint: scope-config-opts
Change-Id: I28a71df5b682c6bd381401f63da49d91fb187125
This commit is contained in:
Mark McLoughlin 2012-11-21 22:44:23 +00:00
parent 0e11414307
commit ac1c637522
14 changed files with 69 additions and 38 deletions

View File

@ -17,10 +17,7 @@
import re
from nova.openstack.common import cfg
CONF = cfg.CONF
CONF.import_opt('compute_driver', 'nova.config')
from nova.virt import driver
DEFAULT_ROOT_DEV_NAME = '/dev/sda1'
_DEFAULT_MAPPINGS = {'ami': 'sda1',
@ -95,7 +92,7 @@ def instance_block_mapping(instance, bdms):
root_device_name = instance['root_device_name']
# NOTE(clayg): remove this when xenapi is setting default_root_device
if root_device_name is None:
if CONF.compute_driver.endswith('xenapi.XenAPIDriver'):
if driver.compute_driver_matches('xenapi.XenAPIDriver'):
root_device_name = '/dev/xvda'
else:
return _DEFAULT_MAPPINGS

View File

@ -29,8 +29,6 @@ terminating it.
:instances_path: Where instances are kept on disk
:base_dir_name: Where cached images are stored under instances_path
:compute_driver: Name of class that is used to handle virtualization, loaded
by :func:`nova.openstack.common.importutils.import_object`
"""
@ -177,7 +175,6 @@ CONF.register_opts(interval_opts)
CONF.register_opts(timeout_opts)
CONF.register_opts(running_deleted_opts)
CONF.import_opt('allow_resize_to_same_host', 'nova.compute.api')
CONF.import_opt('compute_driver', 'nova.config')
CONF.import_opt('console_topic', 'nova.config')
CONF.import_opt('host', 'nova.config')
CONF.import_opt('my_ip', 'nova.config')
@ -313,27 +310,8 @@ class ComputeManager(manager.SchedulerDependentManager):
def __init__(self, compute_driver=None, *args, **kwargs):
"""Load configuration options and connect to the hypervisor."""
# TODO(vish): sync driver creation logic with the rest of the system
# and re-document the module docstring
if not compute_driver:
compute_driver = CONF.compute_driver
if not compute_driver:
LOG.error(_("Compute driver option required, but not specified"))
sys.exit(1)
self.virtapi = ComputeVirtAPI(self)
LOG.info(_("Loading compute driver '%s'") % compute_driver)
try:
self.driver = utils.check_isinstance(
importutils.import_object_ns('nova.virt', compute_driver,
self.virtapi),
driver.ComputeDriver)
except ImportError as e:
LOG.error(_("Unable to load the virtualization driver: %s") % (e))
sys.exit(1)
self.driver = driver.load_compute_driver(self.virtapi, compute_driver)
self.network_api = network.API()
self.volume_api = volume.API()
self.network_manager = importutils.import_object(

View File

@ -30,9 +30,9 @@ from nova.openstack.common import cfg
from nova.openstack.common import log
from nova.openstack.common.notifier import api as notifier_api
from nova import utils
from nova.virt import driver
CONF = cfg.CONF
CONF.import_opt('compute_driver', 'nova.config')
CONF.import_opt('host', 'nova.config')
LOG = log.getLogger(__name__)
@ -88,7 +88,7 @@ def get_device_name_for_instance(context, instance, device):
except (TypeError, AttributeError, ValueError):
raise exception.InvalidDevicePath(path=mappings['root'])
# NOTE(vish): remove this when xenapi is setting default_root_device
if CONF.compute_driver.endswith('xenapi.XenAPIDriver'):
if driver.compute_driver_matches('xenapi.XenAPIDriver'):
prefix = '/dev/xvd'
if req_prefix != prefix:
LOG.debug(_("Using %(prefix)s instead of %(req_prefix)s") % locals())
@ -103,7 +103,7 @@ def get_device_name_for_instance(context, instance, device):
# NOTE(vish): remove this when xenapi is properly setting
# default_ephemeral_device and default_swap_device
if CONF.compute_driver.endswith('xenapi.XenAPIDriver'):
if driver.compute_driver_matches('xenapi.XenAPIDriver'):
instance_type_id = instance['instance_type_id']
instance_type = instance_types.get_instance_type(instance_type_id)
if instance_type['ephemeral_gb']:

View File

@ -294,11 +294,6 @@ global_opts = [
cfg.StrOpt('auth_strategy',
default='noauth',
help='The strategy to use for auth: noauth or keystone.'),
cfg.StrOpt('compute_driver',
help='Driver to use for controlling virtualization. Options '
'include: libvirt.LibvirtDriver, xenapi.XenAPIDriver, '
'fake.FakeDriver, baremetal.BareMetalDriver, '
'vmwareapi.VMWareESXDriver'),
]
cfg.CONF.register_opts(global_opts)

View File

@ -37,6 +37,7 @@ from nova.tests import matchers
from nova import volume
CONF = cfg.CONF
CONF.import_opt('compute_driver', 'nova.virt.driver')
CONF.import_opt('default_instance_type', 'nova.config')
CONF.import_opt('use_ipv6', 'nova.config')
LOG = logging.getLogger(__name__)

View File

@ -49,6 +49,7 @@ from nova.virt import fake as fake_virt
from nova import volume
CONF = cfg.CONF
CONF.import_opt('compute_driver', 'nova.virt.driver')
CONF.import_opt('default_image', 'nova.config')
CONF.import_opt('default_instance_type', 'nova.config')
CONF.import_opt('use_ipv6', 'nova.config')

View File

@ -33,6 +33,7 @@ from nova.tests import fake_network
from nova.tests.image import fake
CONF = cfg.CONF
CONF.import_opt('compute_driver', 'nova.virt.driver')
LOG = logging.getLogger(__name__)

View File

@ -38,6 +38,7 @@ from nova import utils
LOG = logging.getLogger(__name__)
CONF = cfg.CONF
CONF.import_opt('compute_manager', 'nova.config')
CONF.import_opt('compute_driver', 'nova.virt.driver')
class ComputeValidateDeviceTestCase(test.TestCase):

View File

@ -28,6 +28,7 @@ from nova.virt import fake
CONF = cfg.CONF
CONF.import_opt('compute_manager', 'nova.config')
CONF.import_opt('compute_driver', 'nova.virt.driver')
class BaseTestCase(test.TestCase):

View File

@ -20,12 +20,12 @@ from nova.openstack.common import cfg
CONF = cfg.CONF
CONF.import_opt('state_path', 'nova.config')
CONF.import_opt('scheduler_driver', 'nova.scheduler.manager')
CONF.import_opt('fake_network', 'nova.network.manager')
CONF.import_opt('network_size', 'nova.network.manager')
CONF.import_opt('num_networks', 'nova.network.manager')
CONF.import_opt('policy_file', 'nova.policy')
CONF.import_opt('compute_driver', 'nova.virt.driver')
def set_defaults(conf):

View File

@ -26,12 +26,15 @@ from nova import context
from nova import db
from nova.network import api as network_api
from nova import notifications
from nova.openstack.common import cfg
from nova.openstack.common import log as logging
from nova.openstack.common.notifier import api as notifier_api
from nova.openstack.common.notifier import test_notifier
from nova import test
from nova.tests import fake_network
CONF = cfg.CONF
CONF.import_opt('compute_driver', 'nova.virt.driver')
LOG = logging.getLogger(__name__)

View File

@ -35,6 +35,7 @@ import nova.tests.image.fake
CONF = cfg.CONF
CONF.import_opt('scheduler_topic', 'nova.config')
CONF.import_opt('compute_driver', 'nova.virt.driver')
class QuotaIntegrationTestCase(test.TestCase):

View File

@ -58,6 +58,7 @@ LOG = logging.getLogger(__name__)
CONF = cfg.CONF
CONF.import_opt('compute_manager', 'nova.config')
CONF.import_opt('compute_driver', 'nova.virt.driver')
CONF.import_opt('host', 'nova.config')
CONF.import_opt('network_manager', 'nova.config')
CONF.import_opt('node_availability_zone', 'nova.config')

View File

@ -22,8 +22,23 @@ Driver base-classes:
types that support that contract
"""
from nova.openstack.common import log as logging
import sys
from nova.openstack.common import cfg
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova import utils
driver_opts = [
cfg.StrOpt('compute_driver',
help='Driver to use for controlling virtualization. Options '
'include: libvirt.LibvirtDriver, xenapi.XenAPIDriver, '
'fake.FakeDriver, baremetal.BareMetalDriver, '
'vmwareapi.VMWareESXDriver'),
]
CONF = cfg.CONF
CONF.register_opts(driver_opts)
LOG = logging.getLogger(__name__)
@ -751,3 +766,39 @@ class ComputeDriver(object):
if not isinstance(stats, list):
stats = [stats]
return [s['hypervisor_hostname'] for s in stats]
def load_compute_driver(virtapi, compute_driver=None):
"""Load a compute driver module.
Load the compute driver module specified by the compute_driver
configuration option or, if supplied, the driver name supplied as an
argument.
Compute drivers constructors take a VirtAPI object as their first object
and this must be supplied.
:param virtapi: a VirtAPI instance
:param compute_driver: a compute driver name to override the config opt
:returns: a ComputeDriver instance
"""
if not compute_driver:
compute_driver = CONF.compute_driver
if not compute_driver:
LOG.error(_("Compute driver option required, but not specified"))
sys.exit(1)
LOG.info(_("Loading compute driver '%s'") % compute_driver)
try:
driver = importutils.import_object_ns('nova.virt',
compute_driver,
virtapi)
return utils.check_isinstance(driver, ComputeDriver)
except ImportError as e:
LOG.error(_("Unable to load the virtualization driver: %s") % (e))
sys.exit(1)
def compute_driver_matches(match):
return CONF.compute_driver.endswith(match)