mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 00:36:11 -06:00
Catch import errors during backend init
This commit is contained in:
parent
cb7cf1f66a
commit
a9450a1929
1
.github/workflows/ci.yml
vendored
1
.github/workflows/ci.yml
vendored
@ -60,7 +60,6 @@ jobs:
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
pip install boto3 dulwich
|
||||
pip install pycodestyle coverage tblib
|
||||
|
||||
- name: Build documentation
|
||||
|
@ -54,13 +54,6 @@ class DataBackend:
|
||||
"""
|
||||
return
|
||||
|
||||
def handle_missing_dependency(self, e):
|
||||
"""
|
||||
Hook for handling exceptions related to the attempted import of missing modules. Returns an exception
|
||||
to be raised.
|
||||
"""
|
||||
return e
|
||||
|
||||
@property
|
||||
def url_scheme(self):
|
||||
return urlparse(self.url).scheme.lower()
|
||||
@ -105,10 +98,7 @@ class GitBackend(DataBackend):
|
||||
sensitive_parameters = ['password']
|
||||
|
||||
def init_config(self):
|
||||
try:
|
||||
from dulwich.config import ConfigDict
|
||||
except ModuleNotFoundError as e:
|
||||
raise self.handle_missing_dependency(e)
|
||||
|
||||
# Initialize backend config
|
||||
config = ConfigDict()
|
||||
@ -120,18 +110,9 @@ class GitBackend(DataBackend):
|
||||
|
||||
return config
|
||||
|
||||
def handle_missing_dependency(self, e):
|
||||
return ImportError(_(
|
||||
"Unable to initialize the git data backend: dulwich library is not installed. Run 'pip install dulwich' "
|
||||
"within the NetBox Python environment to install it."
|
||||
))
|
||||
|
||||
@contextmanager
|
||||
def fetch(self):
|
||||
try:
|
||||
from dulwich import porcelain
|
||||
except ModuleNotFoundError as e:
|
||||
raise self.handle_missing_dependency(e)
|
||||
|
||||
local_path = tempfile.TemporaryDirectory()
|
||||
|
||||
@ -179,28 +160,16 @@ class S3Backend(DataBackend):
|
||||
REGION_REGEX = r's3\.([a-z0-9-]+)\.amazonaws\.com'
|
||||
|
||||
def init_config(self):
|
||||
try:
|
||||
from botocore.config import Config as Boto3Config
|
||||
except ModuleNotFoundError as e:
|
||||
raise self.handle_missing_dependency(e)
|
||||
|
||||
# Initialize backend config
|
||||
return Boto3Config(
|
||||
proxies=settings.HTTP_PROXIES,
|
||||
)
|
||||
|
||||
def handle_missing_dependency(self, e):
|
||||
return ImportError(_(
|
||||
"Unable to initialize the Amazon S3 backend: boto3 library is not installed. Run 'pip install boto3' "
|
||||
"within the NetBox Python environment to install it."
|
||||
))
|
||||
|
||||
@contextmanager
|
||||
def fetch(self):
|
||||
try:
|
||||
import boto3
|
||||
except ModuleNotFoundError as e:
|
||||
raise self.handle_missing_dependency(e)
|
||||
|
||||
local_path = tempfile.TemporaryDirectory()
|
||||
|
||||
|
@ -151,7 +151,7 @@ class DataSource(JobsMixin, PrimaryModel):
|
||||
Create/update/delete child DataFiles as necessary to synchronize with the remote source.
|
||||
"""
|
||||
if self.status == DataSourceStatusChoices.SYNCING:
|
||||
raise SyncError(f"Cannot initiate sync; syncing already in progress.")
|
||||
raise SyncError("Cannot initiate sync; syncing already in progress.")
|
||||
|
||||
# Emit the pre_sync signal
|
||||
pre_sync.send(sender=self.__class__, instance=self)
|
||||
@ -160,7 +160,10 @@ class DataSource(JobsMixin, PrimaryModel):
|
||||
DataSource.objects.filter(pk=self.pk).update(status=self.status)
|
||||
|
||||
# Replicate source data locally
|
||||
try:
|
||||
backend = self.get_backend()
|
||||
except ModuleNotFoundError as e:
|
||||
raise SyncError(f"There was an error initializing the backend: {e}")
|
||||
with backend.fetch() as local_path:
|
||||
|
||||
logger.debug(f'Syncing files from source root {local_path}')
|
||||
|
Loading…
Reference in New Issue
Block a user