cache a global hardware manager

This commit is contained in:
Russell Haering 2014-01-22 17:28:24 -08:00
parent e25f5dd4ce
commit 5ea4484a2c
2 changed files with 22 additions and 16 deletions

View File

@ -118,7 +118,7 @@ class TeethAgent(object):
self.api = api.TeethAgentAPIServer(self)
self.command_results = collections.OrderedDict()
self.heartbeater = TeethAgentHeartbeater(self)
self.hardware = hardware.load_hardware_manager()
self.hardware = hardware.get_manager()
self.command_lock = threading.Lock()
self.log = structlog.get_logger()
self.started_at = None
@ -235,7 +235,7 @@ def build_agent(api_url,
if not listen_host:
listen_host = advertise_host
mac_addr = hardware.load_hardware_manager().get_primary_mac_address()
mac_addr = hardware.get_manager().get_primary_mac_address()
api_client = overlord_agent_api.APIClient(api_url)
log.info('fetching agent configuration from API',

View File

@ -59,20 +59,26 @@ def _compare_extensions(ext1, ext2):
return mgr1.evaluate_hardware_support() - mgr2.evaluate_hardware_support()
def load_hardware_manager():
log = structlog.get_logger()
extension_manager = stevedore.ExtensionManager(
namespace='teeth_agent.hardware_managers',
invoke_on_load=True)
def get_manager():
global _global_manager
# There will always be at least one extension available (the
# GenericHardwareManager).
preferred_extension = sorted(extension_manager, _compare_extensions)[0]
preferred_manager = preferred_extension.obj
if not _global_manager:
log = structlog.get_logger()
extension_manager = stevedore.ExtensionManager(
namespace='teeth_agent.hardware_managers',
invoke_on_load=True)
if preferred_manager.evaluate_hardware_support() <= 0:
raise RuntimeError('No suitable HardwareManager could be found')
# There will always be at least one extension available (the
# GenericHardwareManager).
preferred_extension = sorted(extension_manager, _compare_extensions)[0]
preferred_manager = preferred_extension.obj
log.info('selected hardware manager',
manager_name=preferred_extension.entry_point_target)
return preferred_manager
if preferred_manager.evaluate_hardware_support() <= 0:
raise RuntimeError('No suitable HardwareManager could be found')
log.info('selected hardware manager',
manager_name=preferred_extension.entry_point_target)
_global_manager = preferred_manager
return _global_manager