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.api = api.TeethAgentAPIServer(self)
self.command_results = collections.OrderedDict() self.command_results = collections.OrderedDict()
self.heartbeater = TeethAgentHeartbeater(self) self.heartbeater = TeethAgentHeartbeater(self)
self.hardware = hardware.load_hardware_manager() self.hardware = hardware.get_manager()
self.command_lock = threading.Lock() self.command_lock = threading.Lock()
self.log = structlog.get_logger() self.log = structlog.get_logger()
self.started_at = None self.started_at = None
@ -235,7 +235,7 @@ def build_agent(api_url,
if not listen_host: if not listen_host:
listen_host = advertise_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) api_client = overlord_agent_api.APIClient(api_url)
log.info('fetching agent configuration from API', 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() return mgr1.evaluate_hardware_support() - mgr2.evaluate_hardware_support()
def load_hardware_manager(): def get_manager():
log = structlog.get_logger() global _global_manager
extension_manager = stevedore.ExtensionManager(
namespace='teeth_agent.hardware_managers',
invoke_on_load=True)
# There will always be at least one extension available (the if not _global_manager:
# GenericHardwareManager). log = structlog.get_logger()
preferred_extension = sorted(extension_manager, _compare_extensions)[0] extension_manager = stevedore.ExtensionManager(
preferred_manager = preferred_extension.obj namespace='teeth_agent.hardware_managers',
invoke_on_load=True)
if preferred_manager.evaluate_hardware_support() <= 0: # There will always be at least one extension available (the
raise RuntimeError('No suitable HardwareManager could be found') # GenericHardwareManager).
preferred_extension = sorted(extension_manager, _compare_extensions)[0]
preferred_manager = preferred_extension.obj
log.info('selected hardware manager', if preferred_manager.evaluate_hardware_support() <= 0:
manager_name=preferred_extension.entry_point_target) raise RuntimeError('No suitable HardwareManager could be found')
return preferred_manager
log.info('selected hardware manager',
manager_name=preferred_extension.entry_point_target)
_global_manager = preferred_manager
return _global_manager