Skip to content

Commit 0adca9b

Browse files
authored
Merge pull request #119 from networktocode/dga-issue-118-2.10
Update plugin for 2.10
2 parents 2589077 + 41e3b1a commit 0adca9b

15 files changed

+400
-200
lines changed

.travis.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ python:
55
- '3.7'
66
- '3.8'
77
env:
8-
# Each version of NetBox listed here must have a corresponding directory/configuration file
9-
# under development/netbox_<NETBOX_VER>/configuration.py
8+
# Each version of NetBox listed here uses configuration stored in development/configuration.py
109
matrix:
11-
- NETBOX_VER=v2.8.3
12-
- NETBOX_VER=master
10+
- NETBOX_VER=v2.8.9
11+
- NETBOX_VER=v2.9.11
12+
- NETBOX_VER=v2.10.2
1313
# Encrypted value for PYPI_TOKEN, this secret has been generated with the following command
1414
# travis encrypt PYPI_TOKEN=<value> --add env.global --com
1515
# Might need to update it once the repo is publish (travis-ci.org vs travis-ci.com)

development/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
ARG python_ver=3.7
33
FROM python:${python_ver}
44

5-
ARG netbox_ver=master
65
ENV PYTHONUNBUFFERED 1
76

87
RUN mkdir -p /opt
@@ -15,6 +14,7 @@ RUN pip install --upgrade pip\
1514
# -------------------------------------------------------------------------------------
1615
# Remove redis==3.4.1 from the requirements.txt file as a workaround to #4910
1716
# https://github.com/netbox-community/netbox/issues/4910, required for version 2.8.8 and earlier
17+
ARG netbox_ver=master
1818
RUN git clone --single-branch --branch ${netbox_ver} https://github.com/netbox-community/netbox.git /opt/netbox/ && \
1919
cd /opt/netbox/ && \
2020
sed -i '/^redis\=\=/d' /opt/netbox/requirements.txt && \

development/base_configuration.py development/configuration.py

+122-49
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
1-
"""NetBox configuration file."""
1+
"""NetBox configuration."""
22
import os
3+
from distutils.util import strtobool
4+
from packaging import version
5+
from django.core.exceptions import ImproperlyConfigured
6+
from .settings import VERSION # pylint: disable=relative-beyond-top-level
7+
8+
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+
14+
# Enforce required configuration parameters
15+
for key in [
16+
"ALLOWED_HOSTS",
17+
"POSTGRES_DB",
18+
"POSTGRES_USER",
19+
"POSTGRES_HOST",
20+
"POSTGRES_PASSWORD",
21+
"REDIS_HOST",
22+
"REDIS_PASSWORD",
23+
"SECRET_KEY",
24+
]:
25+
if not os.environ.get(key):
26+
raise ImproperlyConfigured(f"Required environment variable {key} is missing.")
27+
28+
29+
def is_truthy(arg):
30+
"""Convert "truthy" strings into Booleans.
31+
32+
Examples:
33+
>>> is_truthy('yes')
34+
True
35+
Args:
36+
arg (str): Truthy string (True values are y, yes, t, true, on and 1; false values are n, no,
37+
f, false, off and 0. Raises ValueError if val is anything else.
38+
"""
39+
if isinstance(arg, bool):
40+
return 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)
48+
349

450
# For reference see http://netbox.readthedocs.io/en/latest/configuration/mandatory-settings/
551
# Based on https://github.com/digitalocean/netbox/blob/develop/netbox/netbox/configuration.example.py
@@ -16,46 +62,52 @@
1662
# access to the server via any other hostnames. The first FQDN in the list will be treated as the preferred name.
1763
#
1864
# Example: ALLOWED_HOSTS = ['netbox.example.com', 'netbox.internal.local']
19-
ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "").split(" ")
65+
ALLOWED_HOSTS = os.environ["ALLOWED_HOSTS"].split(" ")
2066

2167
# PostgreSQL database configuration.
2268
DATABASE = {
23-
"NAME": os.environ.get("DB_NAME", "netbox"), # Database name
24-
"USER": os.environ.get("DB_USER", ""), # PostgreSQL username
25-
"PASSWORD": os.environ.get("DB_PASSWORD", ""),
69+
"NAME": os.environ["POSTGRES_DB"], # Database name
70+
"USER": os.environ["POSTGRES_USER"], # PostgreSQL username
71+
"PASSWORD": os.environ["POSTGRES_PASSWORD"],
2672
# PostgreSQL password
27-
"HOST": os.environ.get("DB_HOST", "localhost"), # Database server
28-
"PORT": os.environ.get("DB_PORT", ""), # Database port (leave blank for default)
73+
"HOST": os.environ["POSTGRES_HOST"], # Database server
74+
"PORT": 5432 if "POSTGRES_PORT" not in os.environ else int(os.environ["POSTGRES_PORT"]), # Database port
2975
}
3076

3177
# This key is used for secure generation of random numbers and strings. It must never be exposed outside of this file.
3278
# For optimal security, SECRET_KEY should be at least 50 characters in length and contain a mix of letters, numbers, and
3379
# symbols. NetBox will not run without this defined. For more information, see
3480
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-SECRET_KEY
35-
SECRET_KEY = os.environ.get("SECRET_KEY", "")
81+
SECRET_KEY = os.environ["SECRET_KEY"]
3682

3783
# Redis database settings. The Redis database is used for caching and background processing such as webhooks
3884
# Seperate sections for webhooks and caching allow for connecting to seperate Redis instances/datbases if desired.
3985
# Full connection details are required in both sections, even if they are the same.
4086
REDIS = {
4187
"caching": {
42-
"HOST": os.environ.get("REDIS_HOST", "redis"),
88+
"HOST": os.environ["REDIS_HOST"],
4389
"PORT": int(os.environ.get("REDIS_PORT", 6379)),
44-
"PASSWORD": os.environ.get("REDIS_PASSWORD", ""),
90+
"PASSWORD": os.environ["REDIS_PASSWORD"],
4591
"DATABASE": 1,
46-
"SSL": bool(os.environ.get("REDIS_SSL", False)),
92+
"SSL": is_truthy(os.environ.get("REDIS_SSL", False)),
4793
},
4894
"tasks": {
49-
"HOST": os.environ.get("REDIS_HOST", "redis"),
95+
"HOST": os.environ["REDIS_HOST"],
5096
"PORT": int(os.environ.get("REDIS_PORT", 6379)),
51-
"PASSWORD": os.environ.get("REDIS_PASSWORD", ""),
97+
"PASSWORD": os.environ["REDIS_PASSWORD"],
5298
"DATABASE": 0,
53-
"SSL": bool(os.environ.get("REDIS_SSL", False)),
99+
"SSL": is_truthy(os.environ.get("REDIS_SSL", False)),
54100
},
55101
}
56102

57-
RQ_DEFAULT_TIMEOUT = 300
58-
103+
if NETBOX_RELEASE_28 < NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_29:
104+
# NetBox 2.8.x Specific Settings
105+
REDIS["caching"]["DEFAULT_TIMEOUT"] = 300
106+
REDIS["tasks"]["DEFAULT_TIMEOUT"] = 300
107+
elif NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_211:
108+
RQ_DEFAULT_TIMEOUT = 300
109+
else:
110+
raise ImproperlyConfigured(f"Version {NETBOX_RELEASE_CURRENT} of NetBox is unsupported at this time.")
59111

60112
#########################
61113
# #
@@ -71,8 +123,8 @@
71123

72124
# Optionally display a persistent banner at the top and/or bottom of every page. HTML is allowed. To display the same
73125
# content in both banners, define BANNER_TOP and set BANNER_BOTTOM = BANNER_TOP.
74-
BANNER_TOP = os.environ.get("BANNER_TOP", None)
75-
BANNER_BOTTOM = os.environ.get("BANNER_BOTTOM", None)
126+
BANNER_TOP = os.environ.get("BANNER_TOP", "")
127+
BANNER_BOTTOM = os.environ.get("BANNER_BOTTOM", "")
76128

77129
# Text to include on the login page above the login form. HTML is allowed.
78130
BANNER_LOGIN = os.environ.get("BANNER_LOGIN", "")
@@ -87,58 +139,65 @@
87139
# API Cross-Origin Resource Sharing (CORS) settings. If CORS_ORIGIN_ALLOW_ALL is set to True, all origins will be
88140
# allowed. Otherwise, define a list of allowed origins using either CORS_ORIGIN_WHITELIST or
89141
# CORS_ORIGIN_REGEX_WHITELIST. For more information, see https://github.com/ottoyiu/django-cors-headers
90-
CORS_ORIGIN_ALLOW_ALL = True
142+
CORS_ORIGIN_ALLOW_ALL = is_truthy(os.environ.get("CORS_ORIGIN_ALLOW_ALL", False))
91143
CORS_ORIGIN_WHITELIST = []
92144
CORS_ORIGIN_REGEX_WHITELIST = []
93145

94146
# Set to True to enable server debugging. WARNING: Debugging introduces a substantial performance penalty and may reveal
95147
# sensitive information about your installation. Only enable debugging while performing testing. Never enable debugging
96148
# on a production system.
97-
DEBUG = True
98-
DEVELOPER = True
149+
DEBUG = is_truthy(os.environ.get("DEBUG", False))
150+
DEVELOPER = is_truthy(os.environ.get("DEVELOPER", False))
99151

100152
# Email settings
101153
EMAIL = {
102-
"SERVER": "localhost",
103-
"PORT": 25,
104-
"USERNAME": "",
105-
"PASSWORD": "",
106-
"TIMEOUT": 10,
107-
"FROM_EMAIL": "",
154+
"SERVER": os.environ.get("EMAIL_SERVER", "localhost"),
155+
"PORT": int(os.environ.get("EMAIL_PORT", 25)),
156+
"USERNAME": os.environ.get("EMAIL_USERNAME", ""),
157+
"PASSWORD": os.environ.get("EMAIL_PASSWORD", ""),
158+
"TIMEOUT": int(os.environ.get("EMAIL_TIMEOUT", 10)), # seconds
159+
"FROM_EMAIL": os.environ.get("EMAIL_FROM", ""),
108160
}
109161

110162
# Enforcement of unique IP space can be toggled on a per-VRF basis.
111163
# To enforce unique IP space within the global table (all prefixes and IP addresses not assigned to a VRF),
112164
# set ENFORCE_GLOBAL_UNIQUE to True.
113-
ENFORCE_GLOBAL_UNIQUE = False
165+
ENFORCE_GLOBAL_UNIQUE = is_truthy(os.environ.get("ENFORCE_GLOBAL_UNIQUE", False))
166+
167+
# HTTP proxies NetBox should use when sending outbound HTTP requests (e.g. for webhooks).
168+
# HTTP_PROXIES = {
169+
# "http": "http://192.0.2.1:3128",
170+
# "https": "http://192.0.2.1:1080",
171+
# }
172+
173+
# IP addresses recognized as internal to the system. The debugging toolbar will be available only to clients accessing
174+
# NetBox from an internal IP.
175+
INTERNAL_IPS = ("127.0.0.1", "::1")
176+
177+
LOG_LEVEL = os.environ.get("LOG_LEVEL", "DEBUG" if DEBUG else "INFO")
114178

115179
# Enable custom logging. Please see the Django documentation for detailed guidance on configuring custom logs:
116180
# https://docs.djangoproject.com/en/1.11/topics/logging/
117181
LOGGING = {
118182
"version": 1,
119183
"disable_existing_loggers": False,
120-
"formatters": {"rq_console": {"format": "%(asctime)s %(message)s", "datefmt": "%H:%M:%S",},},
121-
"handlers": {
122-
"rq_console": {
123-
"level": "DEBUG",
124-
"class": "rq.utils.ColorizingStreamHandler",
125-
"formatter": "rq_console",
126-
"exclude": ["%(asctime)s"],
184+
"formatters": {
185+
"verbose": {
186+
"format": "{asctime} {levelname} {message} - {name} - {module} - {pathname}:{lineno}",
187+
"datefmt": "%H:%M:%S",
188+
"style": "{",
127189
},
128190
},
129-
"loggers": {"rq.worker": {"handlers": ["rq_console"], "level": "DEBUG"},},
191+
"handlers": {"console": {"level": "DEBUG", "class": "rq.utils.ColorizingStreamHandler", "formatter": "verbose"}},
192+
"root": {"handlers": ["console"], "level": LOG_LEVEL},
130193
}
131194

132195
# Setting this to True will permit only authenticated users to access any part of NetBox. By default, anonymous users
133196
# are permitted to access most data in NetBox (excluding secrets) but not make any changes.
134-
LOGIN_REQUIRED = False
135-
136-
# Base URL path if accessing NetBox within a directory. For example, if installed at http://example.com/netbox/, set:
137-
# BASE_PATH = 'netbox/'
138-
BASE_PATH = os.environ.get("BASE_PATH", "")
197+
LOGIN_REQUIRED = is_truthy(os.environ.get("LOGIN_REQUIRED", False))
139198

140199
# Setting this to True will display a "maintenance mode" banner at the top of every page.
141-
MAINTENANCE_MODE = os.environ.get("MAINTENANCE_MODE", False)
200+
MAINTENANCE_MODE = is_truthy(os.environ.get("MAINTENANCE_MODE", False))
142201

143202
# An API consumer can request an arbitrary number of objects =by appending the "limit" parameter to the URL (e.g.
144203
# "?limit=1000"). This setting defines the maximum limit. Setting it to 0 or None will allow an API consumer to request
@@ -149,11 +208,14 @@
149208
# the default value of this setting is derived from the installed location.
150209
MEDIA_ROOT = os.environ.get("MEDIA_ROOT", os.path.join(BASE_DIR, "media"))
151210

211+
# Expose Prometheus monitoring metrics at the HTTP endpoint '/metrics'
212+
METRICS_ENABLED = True
213+
152214
NAPALM_USERNAME = os.environ.get("NAPALM_USERNAME", "")
153215
NAPALM_PASSWORD = os.environ.get("NAPALM_PASSWORD", "")
154216

155217
# NAPALM timeout (in seconds). (Default: 30)
156-
NAPALM_TIMEOUT = os.environ.get("NAPALM_TIMEOUT", 30)
218+
NAPALM_TIMEOUT = int(os.environ.get("NAPALM_TIMEOUT", 30))
157219

158220
# NAPALM optional arguments (see http://napalm.readthedocs.io/en/latest/support/#optional-arguments). Arguments must
159221
# be provided as a dictionary.
@@ -163,27 +225,34 @@
163225
}
164226

165227
# Determine how many objects to display per page within a list. (Default: 50)
166-
PAGINATE_COUNT = os.environ.get("PAGINATE_COUNT", 50)
228+
PAGINATE_COUNT = int(os.environ.get("PAGINATE_COUNT", 50))
167229

168230
# Enable installed plugins. Add the name of each plugin to the list.
169231
PLUGINS = ["netbox_onboarding"]
170232

171-
PLUGINS_CONFIG = {"netbox_onboarding": {}}
172233
# Plugins configuration settings. These settings are used by various plugins that the user may have installed.
173234
# Each key in the dictionary is the name of an installed plugin and its value is a dictionary of settings.
174-
# PLUGINS_CONFIG = {}
235+
PLUGINS_CONFIG = {}
175236

176237
# When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to
177238
# prefer IPv4 instead.
178-
PREFER_IPV4 = os.environ.get("PREFER_IPV4", False)
239+
PREFER_IPV4 = is_truthy(os.environ.get("PREFER_IPV4", False))
179240

180241
# Remote authentication support
181242
REMOTE_AUTH_ENABLED = False
182-
REMOTE_AUTH_BACKEND = "netbox.authentication.RemoteUserBackend"
183243
REMOTE_AUTH_HEADER = "HTTP_REMOTE_USER"
184244
REMOTE_AUTH_AUTO_CREATE_USER = True
185245
REMOTE_AUTH_DEFAULT_GROUPS = []
186-
REMOTE_AUTH_DEFAULT_PERMISSIONS = {}
246+
247+
if NETBOX_RELEASE_28 < NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_29:
248+
# NetBox 2.8.x Specific Settings
249+
REMOTE_AUTH_BACKEND = "utilities.auth_backends.RemoteUserBackend"
250+
REMOTE_AUTH_DEFAULT_PERMISSIONS = []
251+
elif NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_211:
252+
REMOTE_AUTH_BACKEND = "netbox.authentication.RemoteUserBackend"
253+
REMOTE_AUTH_DEFAULT_PERMISSIONS = {}
254+
else:
255+
raise ImproperlyConfigured(f"Version {NETBOX_RELEASE_CURRENT} of NetBox is unsupported at this time.")
187256

188257
# This determines how often the GitHub API is called to check the latest release of NetBox. Must be at least 1 hour.
189258
RELEASE_CHECK_TIMEOUT = 24 * 3600
@@ -199,6 +268,10 @@
199268
# this setting is derived from the installed location.
200269
REPORTS_ROOT = os.environ.get("REPORTS_ROOT", os.path.join(BASE_DIR, "reports"))
201270

271+
# The file path where custom scripts will be stored. A trailing slash is not needed. Note that the default value of
272+
# this setting is derived from the installed location.
273+
SCRIPTS_ROOT = os.environ.get("SCRIPTS_ROOT", os.path.join(BASE_DIR, "scripts"))
274+
202275
# Time zone (default: UTC)
203276
TIME_ZONE = os.environ.get("TIME_ZONE", "UTC")
204277

development/dev.env

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
ALLOWED_HOSTS=*
2-
DB_NAME=netbox
3-
DB_USER=netbox
4-
DB_PASSWORD=decinablesprewad
5-
PGPASSWORD=decinablesprewad
6-
DB_HOST=postgres
7-
NAPALM_TIMEOUT=5
2+
BANNER_TOP="Onboarding plugin dev"
3+
CHANGELOG_RETENTION=0
4+
DEBUG=True
5+
DEVELOPER=True
6+
7+
EMAIL_PASSWORD=
8+
EMAIL_PORT=25
9+
EMAIL_SERVER=localhost
10+
EMAIL_TIMEOUT=5
11+
EMAIL_USERNAME=netbox
812
MAX_PAGE_SIZE=0
9-
SECRET_KEY=bqn8nn4qmjvx4hv2u5qr4pp46s3w9skbb63y
10-
POSTGRES_USER=netbox
11-
POSTGRES_PASSWORD=decinablesprewad
13+
METRICS_ENABLED=True
14+
NAPALM_TIMEOUT=5
1215
POSTGRES_DB=netbox
13-
CHANGELOG_RETENTION=0
16+
POSTGRES_HOST=postgres
17+
POSTGRES_PASSWORD=notverysecurepwd
18+
POSTGRES_USER=netbox
1419
REDIS_HOST=redis
20+
REDIS_PASSWORD=notverysecurepwd
1521
REDIS_PORT=6379
16-
# Uncomment REDIS_SSL if using SSL
1722
# REDIS_SSL=True
18-
REDIS_PASSWORD=decinablesprewad
19-
23+
# Uncomment REDIS_SSL if using SSL
24+
SECRET_KEY=r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj
25+
SUPERUSER_API_TOKEN=0123456789abcdef0123456789abcdef01234567
26+
SUPERUSER_EMAIL=[email protected]
27+
SUPERUSER_NAME=admin
28+
SUPERUSER_PASSWORD=admin

development/docker-compose.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ services:
1717
env_file:
1818
- ./dev.env
1919
volumes:
20-
- ./base_configuration.py:/opt/netbox/netbox/netbox/base_configuration.py
21-
- ./netbox_${NETBOX_VER}/configuration.py:/opt/netbox/netbox/netbox/configuration.py
20+
- ./configuration.py:/opt/netbox/netbox/netbox/configuration.py
2221
- ../:/source
2322
tty: true
2423
worker:
@@ -32,8 +31,7 @@ services:
3231
env_file:
3332
- ./dev.env
3433
volumes:
35-
- ./base_configuration.py:/opt/netbox/netbox/netbox/base_configuration.py
36-
- ./netbox_${NETBOX_VER}/configuration.py:/opt/netbox/netbox/netbox/configuration.py
34+
- ./configuration.py:/opt/netbox/netbox/netbox/configuration.py
3735
- ../netbox_onboarding:/source/netbox_onboarding
3836
tty: true
3937
postgres:

development/netbox_master/configuration.py

-4
This file was deleted.

development/netbox_v2.8.3/configuration.py

-8
This file was deleted.

0 commit comments

Comments
 (0)