mirror of
https://github.com/netbox-community/Device-Type-Library-Import.git
synced 2025-08-09 13:55:02 +02:00
Cleanup (#75)
* - Added exception handler function with easy map dictionary. - Added extra default values for ENV vars. - Moved parser args to Settings for global use. - Started implementing new exception handler - Moved git functions to gitcmd.py. - Implemented exception handler for git functions. - Removed extra imports where no longer needed. * Added how to fix ssl error in case it pops up * - Removed circular import of settings.py and gitcmd.py - Added exception_handler.py to handle exceptions (refer to above) - Made GitCMD a class with init and run methods - Removed git completely from nb-dt-import.py * Fixed missing arg * made exception handler a class to prevent circular import
This commit is contained in:
33
settings.py
33
settings.py
@ -1,12 +1,18 @@
|
||||
from argparse import ArgumentParser
|
||||
from sys import exit as system_exit
|
||||
import os
|
||||
from exception_handler import ExceptionHandler
|
||||
from gitcmd import GitCMD
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
REPO_URL = os.getenv("REPO_URL")
|
||||
REPO_BRANCH = os.getenv("REPO_BRANCH", "master")
|
||||
REPO_URL = os.getenv("REPO_URL",
|
||||
default="https://github.com/netbox-community/devicetype-library.git")
|
||||
REPO_BRANCH = os.getenv("REPO_BRANCH", default="master")
|
||||
NETBOX_URL = os.getenv("NETBOX_URL")
|
||||
NETBOX_TOKEN = os.getenv("NETBOX_TOKEN")
|
||||
IGNORE_SSL_ERRORS = (os.getenv("IGNORE_SSL_ERRORS", "False") == "True")
|
||||
IGNORE_SSL_ERRORS = (os.getenv("IGNORE_SSL_ERRORS", default="False") == "True")
|
||||
REPO_PATH = "./repo"
|
||||
|
||||
# optionally load vendors through a comma separated list as env var
|
||||
VENDORS = list(filter(None, os.getenv("VENDORS", "").split(",")))
|
||||
@ -18,8 +24,25 @@ NETBOX_FEATURES = {
|
||||
'modules': False,
|
||||
}
|
||||
|
||||
MANDATORY_ENV_VARS = ["REPO_URL", "NETBOX_URL", "NETBOX_TOKEN"]
|
||||
parser = ArgumentParser(description='Import Netbox Device Types')
|
||||
parser.add_argument('--vendors', nargs='+', default=VENDORS,
|
||||
help="List of vendors to import eg. apc cisco")
|
||||
parser.add_argument('--url', '--git', default=REPO_URL,
|
||||
help="Git URL with valid Device Type YAML files")
|
||||
parser.add_argument('--slugs', nargs='+', default=SLUGS,
|
||||
help="List of device-type slugs to import eg. ap4431 ws-c3850-24t-l")
|
||||
parser.add_argument('--branch', default=REPO_BRANCH,
|
||||
help="Git branch to use from repo")
|
||||
parser.add_argument('--verbose', action='store_true', default=False,
|
||||
help="Print verbose output")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
handle = ExceptionHandler(args)
|
||||
# Evaluate environment variables and exit if one of the mandatory ones are not set
|
||||
MANDATORY_ENV_VARS = ["REPO_URL", "NETBOX_URL", "NETBOX_TOKEN"]
|
||||
for var in MANDATORY_ENV_VARS:
|
||||
if var not in os.environ:
|
||||
raise EnvironmentError("Failed because {} is not set.".format(var))
|
||||
handle.exception("EnvironmentError", var, f'Environment variable "{var}" is not set.\n\nMANDATORY_ENV_VARS: {str(MANDATORY_ENV_VARS)}.\n\nCURRENT_ENV_VARS: {str(os.environ)}')
|
||||
|
||||
git_repo = GitCMD(args, REPO_PATH)
|
||||
|
Reference in New Issue
Block a user