32
32
from .constants import NETMIKO_TO_NAPALM_STATIC
33
33
from .exceptions import OnboardException
34
34
35
+ logger = logging .getLogger ("rq.worker" )
36
+
35
37
PLUGIN_SETTINGS = settings .PLUGINS_CONFIG ["netbox_onboarding" ]
36
38
37
39
@@ -144,7 +146,7 @@ def check_reachability(self):
144
146
OnboardException('fail-connect'):
145
147
When device unreachable
146
148
"""
147
- logging .info ("CHECK: IP %s:%s" , self .hostname , self .port )
149
+ logger .info ("CHECK: IP %s:%s" , self .hostname , self .port )
148
150
149
151
try :
150
152
sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
@@ -169,32 +171,32 @@ def guess_netmiko_device_type(self):
169
171
}
170
172
171
173
try :
172
- logging .info ("INFO guessing device type: %s" , self .hostname )
174
+ logger .info ("INFO guessing device type: %s" , self .hostname )
173
175
guesser = SSHDetect (** remote_device )
174
176
guessed_device_type = guesser .autodetect ()
175
- logging .info ("INFO guessed device type: %s" , guessed_device_type )
177
+ logger .info ("INFO guessed device type: %s" , guessed_device_type )
176
178
177
179
except NetMikoAuthenticationException as err :
178
- logging .error ("ERROR %s" , err )
180
+ logger .error ("ERROR %s" , err )
179
181
raise OnboardException (reason = "fail-login" , message = f"ERROR: { str (err )} " )
180
182
181
183
except (NetMikoTimeoutException , SSHException ) as err :
182
- logging .error ("ERROR: %s" , str (err ))
184
+ logger .error ("ERROR: %s" , str (err ))
183
185
raise OnboardException (reason = "fail-connect" , message = f"ERROR: { str (err )} " )
184
186
185
187
except Exception as err :
186
- logging .error ("ERROR: %s" , str (err ))
188
+ logger .error ("ERROR: %s" , str (err ))
187
189
raise OnboardException (reason = "fail-general" , message = f"ERROR: { str (err )} " )
188
190
189
- logging .info ("INFO device type is: %s" , guessed_device_type )
191
+ logger .info ("INFO device type is: %s" , guessed_device_type )
190
192
191
193
return guessed_device_type
192
194
193
195
def set_napalm_driver_name (self ):
194
196
"""Sets napalm driver name."""
195
197
if not self .napalm_driver :
196
198
netmiko_device_type = self .guess_netmiko_device_type ()
197
- logging .info ("Guessed Netmiko Device Type: %s" , netmiko_device_type )
199
+ logger .info ("Guessed Netmiko Device Type: %s" , netmiko_device_type )
198
200
199
201
self .netmiko_device_type = netmiko_device_type
200
202
@@ -234,7 +236,7 @@ def get_onboarding_facts(self):
234
236
235
237
self .check_reachability ()
236
238
237
- logging .info ("COLLECT: device information %s" , self .hostname )
239
+ logger .info ("COLLECT: device information %s" , self .hostname )
238
240
239
241
try :
240
242
# Get Napalm Driver with Netmiko if needed
@@ -257,20 +259,32 @@ def get_onboarding_facts(self):
257
259
258
260
napalm_device .open ()
259
261
260
- logging .info ("COLLECT: device facts" )
262
+ logger .info ("COLLECT: device facts" )
261
263
self .facts = napalm_device .get_facts ()
262
264
263
- logging .info ("COLLECT: device interface IPs" )
265
+ logger .info ("COLLECT: device interface IPs" )
264
266
self .ip_ifs = napalm_device .get_interfaces_ip ()
265
267
266
- try :
267
- module_name = PLUGIN_SETTINGS ["onboarding_extensions_map" ].get (self .napalm_driver )
268
- module = importlib .import_module (module_name )
269
- driver_addon_class = module .OnboardingDriverExtensions (napalm_device = napalm_device )
270
- self .onboarding_class = driver_addon_class .onboarding_class
271
- self .driver_addon_result = driver_addon_class .ext_result
272
- except ImportError as exc :
273
- logging .info ("No onboarding extension found for driver %s" , self .napalm_driver )
268
+ module_name = PLUGIN_SETTINGS ["onboarding_extensions_map" ].get (self .napalm_driver )
269
+
270
+ if module_name :
271
+ try :
272
+ module = importlib .import_module (module_name )
273
+ driver_addon_class = module .OnboardingDriverExtensions (napalm_device = napalm_device )
274
+ self .onboarding_class = driver_addon_class .onboarding_class
275
+ self .driver_addon_result = driver_addon_class .ext_result
276
+ except ModuleNotFoundError as exc :
277
+ raise OnboardException (
278
+ reason = "fail-general" ,
279
+ message = f"ERROR: ModuleNotFoundError: Onboarding extension for napalm driver { self .napalm_driver } configured but can not be imported per configuration" ,
280
+ )
281
+ except ImportError as exc :
282
+ raise OnboardException (reason = "fail-general" , message = "ERROR: ImportError: %s" % exc .args [0 ])
283
+ else :
284
+ logger .info (
285
+ "INFO: No onboarding extension defined for napalm driver %s, using default napalm driver" ,
286
+ self .napalm_driver ,
287
+ )
274
288
275
289
except ConnectionException as exc :
276
290
raise OnboardException (reason = "fail-login" , message = exc .args [0 ])
0 commit comments