Skip to content

Commit 26399e2

Browse files
committed
Make dynamic configuration parameters actually work
1 parent 951c121 commit 26399e2

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
lines changed

configuration/configuration.py

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import re
88
from os import environ
99
from os.path import abspath, dirname, join
10-
from typing import Any, Callable
10+
from termios import VREPRINT
11+
from typing import Any, Callable, Tuple
1112

1213
# For reference see https://docs.netbox.dev/en/stable/configuration/
1314
# Based on https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/configuration_example.py
@@ -117,29 +118,33 @@ def _environ_get_and_map(variable_name: str, default: str | None = None, map_fn:
117118
# # ['John Doe', 'jdoe@example.com'],
118119
# ]
119120

120-
# URL schemes that are allowed within links in NetBox
121-
_DEFAULT_ALLOWED_URL_SCHEMES = (
122-
'file', 'ftp', 'ftps', 'http', 'https', 'irc', 'mailto', 'sftp', 'ssh', 'tel', 'telnet', 'tftp', 'vnc', 'xmpp',
123-
)
124-
ALLOWED_URL_SCHEMES = _environ_get_and_map('ALLOWED_URL_SCHEMES', ' '.join(_DEFAULT_ALLOWED_URL_SCHEMES), _SPLIT_ON_SPACE)
121+
_ALLOWED_URL_SCHEMES = _environ_get_and_map('ALLOWED_URL_SCHEMES', None, _SPLIT_ON_SPACE)
122+
if _ALLOWED_URL_SCHEMES:
123+
ALLOWED_URL_SCHEMES = _ALLOWED_URL_SCHEMES
125124

126125
# Optionally display a persistent banner at the top and/or bottom of every page. HTML is allowed. To display the same
127126
# content in both banners, define BANNER_TOP and set BANNER_BOTTOM = BANNER_TOP.
128-
BANNER_TOP = environ.get('BANNER_TOP', None)
129-
BANNER_BOTTOM = environ.get('BANNER_BOTTOM', None)
127+
if 'BANNER_TOP' in environ:
128+
BANNER_TOP = environ.get('BANNER_TOP', None)
129+
if 'BANNER_BOTTOM' in environ:
130+
BANNER_BOTTOM = environ.get('BANNER_BOTTOM', None)
130131

131132
# Text to include on the login page above the login form. HTML is allowed.
132-
BANNER_LOGIN = environ.get('BANNER_LOGIN', None)
133+
if 'BANNER_LOGIN' in environ:
134+
BANNER_LOGIN = environ.get('BANNER_LOGIN', None)
133135

134136
# Base URL path if accessing NetBox within a directory. For example, if installed at http://example.com/netbox/, set:
135137
# BASE_PATH = 'netbox/'
136138
BASE_PATH = environ.get('BASE_PATH', '')
137139

138140
# Maximum number of days to retain logged changes. Set to 0 to retain changes indefinitely. (Default: 90)
139-
CHANGELOG_RETENTION = _environ_get_and_map('CHANGELOG_RETENTION', None, _AS_INT)
141+
_CHANGELOG_RETENTION = _environ_get_and_map('CHANGELOG_RETENTION', None, _AS_INT)
142+
if _CHANGELOG_RETENTION:
143+
CHANGELOG_RETENTION = _CHANGELOG_RETENTION
140144

141145
# Maximum number of days to retain job results (scripts and reports). Set to 0 to retain job results in the database indefinitely. (Default: 90)
142-
JOBRESULT_RETENTION = _environ_get_and_map('CHANGELOG_RETENTION', None, _AS_INT)
146+
if 'JOBRESULT_RETENTION' in environ:
147+
JOBRESULT_RETENTION = _environ_get_and_map('JOBRESULT_RETENTION', None, _AS_INT)
143148

144149
# API Cross-Origin Resource Sharing (CORS) settings. If CORS_ORIGIN_ALLOW_ALL is set to True, all origins will be
145150
# allowed. Otherwise, define a list of allowed origins using either CORS_ORIGIN_WHITELIST or
@@ -156,7 +161,7 @@ def _environ_get_and_map(variable_name: str, default: str | None = None, map_fn:
156161
# This parameter serves as a safeguard to prevent some potentially dangerous behavior,
157162
# such as generating new database schema migrations.
158163
# Set this to True only if you are actively developing the NetBox code base.
159-
DEVELOPER = _environ_get_and_map('DEBUG', 'False', _EQUALS_TRUE)
164+
DEVELOPER = _environ_get_and_map('DEVELOPER', 'False', _EQUALS_TRUE)
160165

161166
# Email settings
162167
EMAIL = {
@@ -174,7 +179,9 @@ def _environ_get_and_map(variable_name: str, default: str | None = None, map_fn:
174179

175180
# Enforcement of unique IP space can be toggled on a per-VRF basis. To enforce unique IP space within the global table
176181
# (all prefixes and IP addresses not assigned to a VRF), set ENFORCE_GLOBAL_UNIQUE to True.
177-
ENFORCE_GLOBAL_UNIQUE = _environ_get_and_map('ENFORCE_GLOBAL_UNIQUE', None, _EQUALS_TRUE)
182+
_ENFORCE_GLOBAL_UNIQUE = _environ_get_and_map('ENFORCE_GLOBAL_UNIQUE', None, _EQUALS_TRUE)
183+
if _ENFORCE_GLOBAL_UNIQUE:
184+
ENFORCE_GLOBAL_UNIQUE = _ENFORCE_GLOBAL_UNIQUE
178185

179186
# Exempt certain models from the enforcement of view permissions. Models listed here will be viewable by all users and
180187
# by anonymous users. List models in the form `<app>.<model>`. Add '*' to this list to exempt all models.
@@ -191,7 +198,9 @@ def _environ_get_and_map(variable_name: str, default: str | None = None, map_fn:
191198
INTERNAL_IPS = _environ_get_and_map('INTERNAL_IPS', '127.0.0.1 ::1', _SPLIT_ON_SPACE)
192199

193200
# Enable GraphQL API.
194-
GRAPHQL_ENABLED = _environ_get_and_map('GRAPHQL_ENABLED', None, _EQUALS_TRUE)
201+
_GRAPHQL_ENABLED = _environ_get_and_map('GRAPHQL_ENABLED', None, _EQUALS_TRUE)
202+
if _GRAPHQL_ENABLED:
203+
GRAPHQL_ENABLED = _GRAPHQL_ENABLED
195204

196205
# # Enable custom logging. Please see the Django documentation for detailed guidance on configuring custom logs:
197206
# # https://docs.djangoproject.com/en/stable/topics/logging/
@@ -210,15 +219,20 @@ def _environ_get_and_map(variable_name: str, default: str | None = None, map_fn:
210219
LOGIN_TIMEOUT = _environ_get_and_map('LOGIN_TIMEOUT', 1209600, _AS_INT)
211220

212221
# Setting this to True will display a "maintenance mode" banner at the top of every page.
213-
MAINTENANCE_MODE = _environ_get_and_map('MAINTENANCE_MODE', None, _EQUALS_TRUE)
222+
_MAINTENANCE_MODE = _environ_get_and_map('MAINTENANCE_MODE', None, _EQUALS_TRUE)
223+
if _MAINTENANCE_MODE:
224+
MAINTENANCE_MODE = _MAINTENANCE_MODE
214225

215226
# Maps provider
216-
MAPS_URL = environ.get('MAPS_URL', None)
227+
if 'MAPS_URL' in environ:
228+
MAPS_URL = environ.get('MAPS_URL', None)
217229

218230
# An API consumer can request an arbitrary number of objects =by appending the "limit" parameter to the URL (e.g.
219231
# "?limit=1000"). This setting defines the maximum limit. Setting it to 0 or None will allow an API consumer to request
220232
# all objects by specifying "?limit=0".
221-
MAX_PAGE_SIZE = _environ_get_and_map('MAX_PAGE_SIZE', None, _AS_INT)
233+
_MAX_PAGE_SIZE = _environ_get_and_map('MAX_PAGE_SIZE', None, _AS_INT)
234+
if _MAX_PAGE_SIZE:
235+
MAX_PAGE_SIZE = _MAX_PAGE_SIZE
222236

223237
# The file path where uploaded media such as image attachments are stored. A trailing slash is not needed. Note that
224238
# the default value of this setting is derived from the installed location.
@@ -228,18 +242,24 @@ def _environ_get_and_map(variable_name: str, default: str | None = None, map_fn:
228242
METRICS_ENABLED = _environ_get_and_map('METRICS_ENABLED', 'False', _EQUALS_TRUE)
229243

230244
# Credentials that NetBox will uses to authenticate to devices when connecting via NAPALM.
231-
NAPALM_USERNAME = environ.get('NAPALM_USERNAME', None)
232-
NAPALM_PASSWORD = _read_secret('napalm_password', environ.get('NAPALM_PASSWORD', None))
245+
if 'NAPALM_USERNAME' in environ:
246+
NAPALM_USERNAME = environ.get('NAPALM_USERNAME', None)
247+
if 'NAPALM_PASSWORD' in environ:
248+
NAPALM_PASSWORD = _read_secret('napalm_password', environ.get('NAPALM_PASSWORD', None))
233249

234250
# NAPALM timeout (in seconds). (Default: 30)
235-
NAPALM_TIMEOUT = _environ_get_and_map('NAPALM_TIMEOUT', None, _AS_INT)
251+
_NAPALM_TIMEOUT = _environ_get_and_map('NAPALM_TIMEOUT', None, _AS_INT)
252+
if _NAPALM_TIMEOUT:
253+
NAPALM_TIMEOUT = _NAPALM_TIMEOUT
236254

237255
# # NAPALM optional arguments (see http://napalm.readthedocs.io/en/latest/support/#optional-arguments). Arguments must
238256
# # be provided as a dictionary.
239257
# NAPALM_ARGS = None
240258

241259
# Determine how many objects to display per page within a list. (Default: 50)
242-
PAGINATE_COUNT = _environ_get_and_map('PAGINATE_COUNT', None, _AS_INT)
260+
_PAGINATE_COUNT = _environ_get_and_map('PAGINATE_COUNT', None, _AS_INT)
261+
if _PAGINATE_COUNT:
262+
PAGINATE_COUNT = _PAGINATE_COUNT
243263

244264
# # Enable installed plugins. Add the name of each plugin to the list.
245265
# PLUGINS = []
@@ -251,20 +271,32 @@ def _environ_get_and_map(variable_name: str, default: str | None = None, map_fn:
251271

252272
# When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to
253273
# prefer IPv4 instead.
254-
PREFER_IPV4 = _environ_get_and_map('PREFER_IPV4', None, _EQUALS_TRUE)
274+
_PREFER_IPV4 = _environ_get_and_map('PREFER_IPV4', None, _EQUALS_TRUE)
275+
if _PREFER_IPV4:
276+
PREFER_IPV = _PREFER_IPV4
255277

256278
# The default value for the amperage field when creating new power feeds.
257-
POWERFEED_DEFAULT_AMPERAGE = _environ_get_and_map('POWERFEED_DEFAULT_AMPERAGE', None, _AS_INT)
279+
_POWERFEED_DEFAULT_AMPERAGE = _environ_get_and_map('POWERFEED_DEFAULT_AMPERAGE', None, _AS_INT)
280+
if _POWERFEED_DEFAULT_AMPERAGE:
281+
POWERFEED_DEFAULT_AMPERAGE = _POWERFEED_DEFAULT_AMPERAGE
258282

259283
# The default value (percentage) for the max_utilization field when creating new power feeds.
260-
POWERFEED_DEFAULT_MAX_UTILIZATION = _environ_get_and_map('POWERFEED_DEFAULT_MAX_UTILIZATION', None, _AS_INT)
284+
_POWERFEED_DEFAULT_MAX_UTILIZATION = _environ_get_and_map('POWERFEED_DEFAULT_MAX_UTILIZATION', None, _AS_INT)
285+
if _POWERFEED_DEFAULT_MAX_UTILIZATION:
286+
POWERFEED_DEFAULT_MAX_UTILIZATION = _POWERFEED_DEFAULT_MAX_UTILIZATION
261287

262288
# The default value for the voltage field when creating new power feeds.
263-
POWERFEED_DEFAULT_VOLTAGE = _environ_get_and_map('POWERFEED_DEFAULT_VOLTAGE', None, _AS_INT)
289+
_POWERFEED_DEFAULT_VOLTAGE = _environ_get_and_map('POWERFEED_DEFAULT_VOLTAGE', None, _AS_INT)
290+
if _POWERFEED_DEFAULT_VOLTAGE:
291+
POWERFEED_DEFAULT_VOLTAGE = _POWERFEED_DEFAULT_VOLTAGE
264292

265293
# Rack elevation size defaults, in pixels. For best results, the ratio of width to height should be roughly 10:1.
266-
RACK_ELEVATION_DEFAULT_UNIT_HEIGHT = _environ_get_and_map('RACK_ELEVATION_DEFAULT_UNIT_HEIGHT', None, _AS_INT)
267-
RACK_ELEVATION_DEFAULT_UNIT_WIDTH = _environ_get_and_map('RACK_ELEVATION_DEFAULT_UNIT_WIDTH', None, _AS_INT)
294+
_RACK_ELEVATION_DEFAULT_UNIT_HEIGHT = _environ_get_and_map('RACK_ELEVATION_DEFAULT_UNIT_HEIGHT', None, _AS_INT)
295+
if _RACK_ELEVATION_DEFAULT_UNIT_HEIGHT:
296+
RACK_ELEVATION_DEFAULT_UNIT_HEIGHT = _RACK_ELEVATION_DEFAULT_UNIT_HEIGHT
297+
_RACK_ELEVATION_DEFAULT_UNIT_WIDTH = _environ_get_and_map('RACK_ELEVATION_DEFAULT_UNIT_WIDTH', None, _AS_INT)
298+
if _RACK_ELEVATION_DEFAULT_UNIT_WIDTH:
299+
RACK_ELEVATION_DEFAULT_UNIT_WIDTH = _RACK_ELEVATION_DEFAULT_UNIT_WIDTH
268300

269301
# Remote authentication support
270302
REMOTE_AUTH_ENABLED = _environ_get_and_map('REMOTE_AUTH_ENABLED', 'False', _EQUALS_TRUE)
@@ -304,7 +336,7 @@ def _environ_get_and_map(variable_name: str, default: str | None = None, map_fn:
304336
# By default, NetBox will store session data in the database. Alternatively, a file path can be specified here to use
305337
# local file storage instead. (This can be useful for enabling authentication on a standby instance with read-only
306338
# database access.) Note that the user as which NetBox runs must have read and write permissions to this path.
307-
SESSION_FILE_PATH = environ.get('SESSIONS_ROOT', None)
339+
SESSION_FILE_PATH = environ.get('SESSION_FILE_PATH', environ.get('SESSIONS_ROOT', None))
308340

309341
# Time zone (default: UTC)
310342
TIME_ZONE = environ.get('TIME_ZONE', 'UTC')

env/netbox.env

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ EMAIL_USE_SSL=false
1616
EMAIL_USE_TLS=false
1717
GRAPHQL_ENABLED=true
1818
HOUSEKEEPING_INTERVAL=86400
19-
MAX_PAGE_SIZE=1000
2019
MEDIA_ROOT=/opt/netbox/netbox/media
2120
METRICS_ENABLED=false
22-
NAPALM_PASSWORD=
23-
NAPALM_TIMEOUT=10
24-
NAPALM_USERNAME=
2521
REDIS_CACHE_DATABASE=1
2622
REDIS_CACHE_HOST=redis-cache
2723
REDIS_CACHE_INSECURE_SKIP_TLS_VERIFY=false

0 commit comments

Comments
 (0)