1919import time
2020
2121import pkg_resources
22- from stevedore import driver
22+ from stevedore import extension
2323from wsgiref import simple_server
2424
2525from ironic_python_agent .api import app
@@ -38,15 +38,13 @@ def _time():
3838
3939
4040class IronicPythonAgentStatus (encoding .Serializable ):
41- def __init__ (self , mode , started_at , version ):
42- self .mode = mode
41+ def __init__ (self , started_at , version ):
4342 self .started_at = started_at
4443 self .version = version
4544
4645 def serialize (self ):
4746 """Turn the status into a dict."""
4847 return utils .get_ordereddict ([
49- ('mode' , self .mode ),
5048 ('started_at' , self .started_at ),
5149 ('version' , self .version ),
5250 ])
@@ -113,7 +111,6 @@ def __init__(self, api_url, advertise_address, listen_address):
113111 self .api_client = ironic_api_client .APIClient (self .api_url )
114112 self .listen_address = listen_address
115113 self .advertise_address = advertise_address
116- self .mode_implementation = None
117114 self .version = pkg_resources .get_distribution ('ironic-python-agent' )\
118115 .version
119116 self .api = app .VersionSelectorApplication (self )
@@ -125,17 +122,15 @@ def __init__(self, api_url, advertise_address, listen_address):
125122 self .log = log .getLogger (__name__ )
126123 self .started_at = None
127124 self .node = None
128-
129- def get_mode_name (self ):
130- if self .mode_implementation :
131- return self .mode_implementation .name
132- else :
133- return 'NONE'
125+ self .ext_mgr = extension .ExtensionManager (
126+ namespace = 'ironic_python_agent.extensions' ,
127+ invoke_on_load = True ,
128+ propagate_map_exceptions = True ,
129+ )
134130
135131 def get_status (self ):
136132 """Retrieve a serializable status."""
137133 return IronicPythonAgentStatus (
138- mode = self .get_mode_name (),
139134 started_at = self .started_at ,
140135 version = self .version
141136 )
@@ -162,35 +157,27 @@ def _split_command(self, command_name):
162157 command_parts = command_name .split ('.' , 1 )
163158 if len (command_parts ) != 2 :
164159 raise errors .InvalidCommandError (
165- 'Command name must be of the form <mode >.<name>' )
160+ 'Command name must be of the form <extension >.<name>' )
166161
167162 return (command_parts [0 ], command_parts [1 ])
168163
169- def _verify_mode (self , mode_name , command_name ):
170- if not self .mode_implementation :
171- try :
172- self .mode_implementation = _load_mode_implementation (mode_name )
173- except Exception :
174- raise errors .InvalidCommandError (
175- 'Unknown mode: {0}' .format (mode_name ))
176- elif self .get_mode_name ().lower () != mode_name :
177- raise errors .InvalidCommandError (
178- 'Agent is already in {0} mode' .format (self .get_mode_name ()))
179-
180164 def execute_command (self , command_name , ** kwargs ):
181165 """Execute an agent command."""
182166 with self .command_lock :
183- mode_part , command_part = self ._split_command (command_name )
184- self ._verify_mode (mode_part , command_part )
167+ extension_part , command_part = self ._split_command (command_name )
185168
186169 if len (self .command_results ) > 0 :
187170 last_command = self .command_results .values ()[- 1 ]
188171 if not last_command .is_done ():
189172 raise errors .CommandExecutionError ('agent is busy' )
190173
191174 try :
192- result = self .mode_implementation .execute (command_part ,
193- ** kwargs )
175+ ext = self .ext_mgr [extension_part ].obj
176+ result = ext .execute (command_part , ** kwargs )
177+ except KeyError :
178+ # Extension Not found
179+ raise errors .RequestedObjectNotFoundError ('Extension' ,
180+ extension_part )
194181 except errors .InvalidContentError as e :
195182 # Any command may raise a InvalidContentError which will be
196183 # returned to the caller directly.
@@ -232,16 +219,6 @@ def run(self):
232219 self .heartbeater .stop ()
233220
234221
235- def _load_mode_implementation (mode_name ):
236- mgr = driver .DriverManager (
237- namespace = 'ironic_python_agent.modes' ,
238- name = mode_name .lower (),
239- invoke_on_load = True ,
240- invoke_args = [],
241- )
242- return mgr .driver
243-
244-
245222def build_agent (api_url ,
246223 advertise_host ,
247224 advertise_port ,
0 commit comments