11"""NetBox configuration."""
22import os
33from distutils .util import strtobool
4+ from packaging import version
45from django .core .exceptions import ImproperlyConfigured
56from .settings import VERSION # pylint: disable=relative-beyond-top-level
67
78
9+ NETBOX_RELEASE_CURRENT = version .parse (VERSION )
10+ NETBOX_RELEASE_28 = version .parse ("2.8" )
11+ NETBOX_RELEASE_29 = version .parse ("2.9" )
12+ NETBOX_RELEASE_211 = version .parse ("2.11" )
13+
814# Enforce required configuration parameters
915for key in [
1016 "ALLOWED_HOSTS" ,
@@ -32,7 +38,13 @@ def is_truthy(arg):
3238 """
3339 if isinstance (arg , bool ):
3440 return arg
35- return bool (strtobool (arg ))
41+
42+ try :
43+ bool_val = strtobool (arg )
44+ except ValueError :
45+ raise ImproperlyConfigured (f"Unexpected variable value: { arg } " ) # pylint: disable=raise-missing-from
46+
47+ return bool (bool_val )
3648
3749
3850# For reference see http://netbox.readthedocs.io/en/latest/configuration/mandatory-settings/
@@ -59,7 +71,7 @@ def is_truthy(arg):
5971 "PASSWORD" : os .environ ["POSTGRES_PASSWORD" ],
6072 # PostgreSQL password
6173 "HOST" : os .environ ["POSTGRES_HOST" ], # Database server
62- "PORT" : 5432 if not os .environ . get ( "POSTGRES_PORT" , False ) else int (os .environ ["POSTGRES_PORT" ]), # Database port
74+ "PORT" : 5432 if "POSTGRES_PORT" not in os .environ else int (os .environ ["POSTGRES_PORT" ]), # Database port
6375}
6476
6577# This key is used for secure generation of random numbers and strings. It must never be exposed outside of this file.
@@ -88,14 +100,14 @@ def is_truthy(arg):
88100 },
89101}
90102
91- if VERSION . startswith ( "2.8." ) :
103+ if NETBOX_RELEASE_28 < NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_29 :
92104 # NetBox 2.8.x Specific Settings
93105 REDIS ["caching" ]["DEFAULT_TIMEOUT" ] = 300
94106 REDIS ["tasks" ]["DEFAULT_TIMEOUT" ] = 300
95- elif VERSION . startswith ( "2.9." ) or VERSION . startswith ( "2.10." ) :
107+ elif NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_211 :
96108 RQ_DEFAULT_TIMEOUT = 300
97109else :
98- raise ImproperlyConfigured (f"Version { VERSION } of NetBox is unsupported at this time." )
110+ raise ImproperlyConfigured (f"Version { NETBOX_RELEASE_CURRENT } of NetBox is unsupported at this time." )
99111
100112#########################
101113# #
@@ -232,15 +244,15 @@ def is_truthy(arg):
232244REMOTE_AUTH_AUTO_CREATE_USER = True
233245REMOTE_AUTH_DEFAULT_GROUPS = []
234246
235- if VERSION . startswith ( "2.8." ) :
247+ if NETBOX_RELEASE_28 < NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_29 :
236248 # NetBox 2.8.x Specific Settings
237249 REMOTE_AUTH_BACKEND = "utilities.auth_backends.RemoteUserBackend"
238250 REMOTE_AUTH_DEFAULT_PERMISSIONS = []
239- elif VERSION . startswith ( "2.9." ) or VERSION . startswith ( "2.10." ) :
251+ elif NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_211 :
240252 REMOTE_AUTH_BACKEND = "netbox.authentication.RemoteUserBackend"
241253 REMOTE_AUTH_DEFAULT_PERMISSIONS = {}
242254else :
243- raise ImproperlyConfigured (f"Version { VERSION } of NetBox is unsupported at this time." )
255+ raise ImproperlyConfigured (f"Version { NETBOX_RELEASE_CURRENT } of NetBox is unsupported at this time." )
244256
245257# This determines how often the GitHub API is called to check the latest release of NetBox. Must be at least 1 hour.
246258RELEASE_CHECK_TIMEOUT = 24 * 3600
0 commit comments