2023-03-03 22:03:17 +01:00
from argparse import ArgumentParser
from sys import exit as system_exit
2020-02-26 15:49:12 +01:00
import os
2023-03-03 22:03:17 +01:00
from exception_handler import ExceptionHandler
from gitcmd import GitCMD
2021-02-26 23:26:32 +01:00
from dotenv import load_dotenv
load_dotenv ( )
2020-02-26 15:49:12 +01:00
2023-03-03 22:03:17 +01:00
REPO_URL = os . getenv ( " REPO_URL " ,
default = " https://github.com/netbox-community/devicetype-library.git " )
REPO_BRANCH = os . getenv ( " REPO_BRANCH " , default = " master " )
2021-08-25 18:57:38 +02:00
NETBOX_URL = os . getenv ( " NETBOX_URL " )
NETBOX_TOKEN = os . getenv ( " NETBOX_TOKEN " )
2023-03-03 22:03:17 +01:00
IGNORE_SSL_ERRORS = ( os . getenv ( " IGNORE_SSL_ERRORS " , default = " False " ) == " True " )
REPO_PATH = " ./repo "
2020-02-26 15:49:12 +01:00
2021-12-16 23:26:22 +01:00
# optionally load vendors through a comma separated list as env var
VENDORS = list ( filter ( None , os . getenv ( " VENDORS " , " " ) . split ( " , " ) ) )
2021-05-16 14:46:09 +02:00
2021-10-05 17:02:21 +02:00
# optionally load device types through a space separated list as env var
SLUGS = os . getenv ( " SLUGS " , " " ) . split ( )
2022-09-08 18:48:24 +02:00
NETBOX_FEATURES = {
' modules ' : False ,
}
2021-10-05 17:02:21 +02:00
2023-03-03 22:03:17 +01:00
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 ( )
2020-02-26 15:49:12 +01:00
2023-03-03 22:03:17 +01:00
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 " ]
2020-02-26 15:49:12 +01:00
for var in MANDATORY_ENV_VARS :
if var not in os . environ :
2023-03-03 22:03:17 +01:00
handle . exception ( " EnvironmentError " , var , f ' Environment variable " { var } " is not set. \n \n MANDATORY_ENV_VARS: { str ( MANDATORY_ENV_VARS ) } . \n \n CURRENT_ENV_VARS: { str ( os . environ ) } ' )
git_repo = GitCMD ( args , REPO_PATH )