mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Tweaked webhooks and Redis settings
This commit is contained in:
@@ -5,7 +5,7 @@ from django.core.exceptions import ImproperlyConfigured
|
||||
default_app_config = 'extras.apps.ExtrasConfig'
|
||||
|
||||
# check that django-rq is installed and we can connect to redis
|
||||
if settings.WEBHOOK_BACKEND_ENABLED:
|
||||
if settings.WEBHOOKS_ENABLED:
|
||||
try:
|
||||
import django_rq
|
||||
except ImportError:
|
||||
|
@@ -1,8 +1,6 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.apps import AppConfig
|
||||
from django.core.cache import caches
|
||||
from django.db.utils import ProgrammingError
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.conf import settings
|
||||
|
||||
@@ -13,17 +11,25 @@ class ExtrasConfig(AppConfig):
|
||||
def ready(self):
|
||||
import extras.signals
|
||||
|
||||
# check that we can connect to redis
|
||||
if settings.WEBHOOK_BACKEND_ENABLED:
|
||||
# Check that we can connect to the configured Redis database if webhooks are enabled.
|
||||
if settings.WEBHOOKS_ENABLED:
|
||||
try:
|
||||
import redis
|
||||
rs = redis.Redis(settings.REDIS_HOST,
|
||||
settings.REDIS_PORT,
|
||||
settings.REDIS_DB,
|
||||
settings.REDIS_PASSWORD or None)
|
||||
except ImportError:
|
||||
raise ImproperlyConfigured(
|
||||
"WEBHOOKS_ENABLED is True but the redis Python package is not installed. (Try 'pip install "
|
||||
"redis'.)"
|
||||
)
|
||||
try:
|
||||
rs = redis.Redis(
|
||||
host=settings.REDIS_HOST,
|
||||
port=settings.REDIS_PORT,
|
||||
db=settings.REDIS_DATABASE,
|
||||
password=settings.REDIS_PASSWORD or None,
|
||||
)
|
||||
rs.ping()
|
||||
except redis.exceptions.ConnectionError:
|
||||
raise ImproperlyConfigured(
|
||||
"Unable to connect to the redis database. You must provide "
|
||||
"connection settings to redis per the documentation."
|
||||
"Unable to connect to the Redis database. Check that the Redis configuration has been defined in "
|
||||
"configuration.py."
|
||||
)
|
||||
|
@@ -64,7 +64,7 @@ def post_save_receiver(sender, instance, created, **kwargs):
|
||||
Receives post_save signals from registered models. If the webhook
|
||||
backend is enabled, queue any webhooks that apply to the event.
|
||||
"""
|
||||
if settings.WEBHOOK_BACKEND_ENABLED:
|
||||
if settings.WEBHOOKS_ENABLED:
|
||||
signal_received_timestamp = time.time()
|
||||
webhook_cache = get_or_set_webhook_cache()
|
||||
# look for any webhooks that match this event
|
||||
@@ -88,7 +88,7 @@ def post_delete_receiver(sender, instance, **kwargs):
|
||||
Receives post_delete signals from registered models. If the webhook
|
||||
backend is enabled, queue any webhooks that apply to the event.
|
||||
"""
|
||||
if settings.WEBHOOK_BACKEND_ENABLED:
|
||||
if settings.WEBHOOKS_ENABLED:
|
||||
signal_received_timestamp = time.time()
|
||||
webhook_cache = get_or_set_webhook_cache()
|
||||
obj_type = ContentType.objects.get_for_model(sender)
|
||||
@@ -103,7 +103,7 @@ def bulk_operation_receiver(sender, **kwargs):
|
||||
Receives bulk_operation_signal signals from registered models. If the webhook
|
||||
backend is enabled, queue any webhooks that apply to the event.
|
||||
"""
|
||||
if settings.WEBHOOK_BACKEND_ENABLED:
|
||||
if settings.WEBHOOKS_ENABLED:
|
||||
signal_received_timestamp = time.time()
|
||||
event = kwargs['event']
|
||||
webhook_cache = get_or_set_webhook_cache()
|
||||
@@ -132,7 +132,7 @@ def register_signals(senders):
|
||||
Take a list of senders (Models) and register them to the post_save
|
||||
and post_delete signal receivers.
|
||||
"""
|
||||
if settings.WEBHOOK_BACKEND_ENABLED:
|
||||
if settings.WEBHOOKS_ENABLED:
|
||||
# only register signals if the backend is enabled
|
||||
# this reduces load by not firing signals if the
|
||||
# webhook backend feature is disabled
|
||||
|
@@ -118,18 +118,17 @@ PAGINATE_COUNT = 50
|
||||
# prefer IPv4 instead.
|
||||
PREFER_IPV4 = False
|
||||
|
||||
# The Webhook event backend is disabled by default. Set this to True to enable it. Besure to follow the documentation
|
||||
# on first enabling the required components for the webhook backend.
|
||||
WEBHOOK_BACKEND_ENABLED = False
|
||||
# The Webhook event backend is disabled by default. Set this to True to enable it. Note that this requires a Redis
|
||||
# database be configured and accessible by NetBox (see `REDIS` below).
|
||||
WEBHOOKS_ENABLED = False
|
||||
|
||||
# Redis settings. Redis is used in webhook backend so WEBHOOK_BACKEND_ENABLED must be enabled for these
|
||||
# to mean anything. Please refer to the netbox documentation on the webhook backend.
|
||||
# Redis database settings (optional). A Redis database is required only if the webhooks backend is enabled.
|
||||
REDIS = {
|
||||
'HOST': 'localhost',
|
||||
'PORT': 6379,
|
||||
'DEFAULT_TIMEOUT': 300,
|
||||
'PASSWORD': '',
|
||||
'DB': 0,
|
||||
'DATABASE': 0,
|
||||
'DEFAULT_TIMEOUT': 300,
|
||||
}
|
||||
|
||||
# The file path where custom reports will be stored. A trailing slash is not needed. Note that the default value of
|
||||
|
@@ -64,13 +64,13 @@ NAPALM_ARGS = getattr(configuration, 'NAPALM_ARGS', {})
|
||||
PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50)
|
||||
PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False)
|
||||
REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')
|
||||
WEBHOOK_BACKEND_ENABLED = getattr(configuration, 'WEBHOOK_BACKEND_ENABLED', False)
|
||||
REDIS = getattr(configuration, 'REDIS', {})
|
||||
SHORT_DATE_FORMAT = getattr(configuration, 'SHORT_DATE_FORMAT', 'Y-m-d')
|
||||
SHORT_DATETIME_FORMAT = getattr(configuration, 'SHORT_DATETIME_FORMAT', 'Y-m-d H:i')
|
||||
SHORT_TIME_FORMAT = getattr(configuration, 'SHORT_TIME_FORMAT', 'H:i:s')
|
||||
TIME_FORMAT = getattr(configuration, 'TIME_FORMAT', 'g:i a')
|
||||
TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
|
||||
WEBHOOKS_ENABLED = getattr(configuration, 'WEBHOOKS_ENABLED', False)
|
||||
|
||||
CSRF_TRUSTED_ORIGINS = ALLOWED_HOSTS
|
||||
|
||||
@@ -112,11 +112,17 @@ DATABASES = {
|
||||
}
|
||||
|
||||
# Redis
|
||||
REDIS_HOST = REDIS.get('REDIS_HOST', 'localhost')
|
||||
REDIS_PORT = REDIS.get('REDIS_PORT', 6379)
|
||||
REDIS_DEFAULT_TIMEOUT = REDIS.get('REDIS_DEFAULT_TIMEOUT', 300)
|
||||
REDIS_PASSWORD = REDIS.get('REDIS_PASSWORD', '')
|
||||
REDIS_DB = REDIS.get('REDIS_DB', 0)
|
||||
REDIS_HOST = REDIS.get('HOST', 'localhost')
|
||||
REDIS_PORT = REDIS.get('PORT', 6379)
|
||||
REDIS_PASSWORD = REDIS.get('PASSWORD', '')
|
||||
REDIS_DATABASE = REDIS.get('DATABASE', 0)
|
||||
REDIS_DEFAULT_TIMEOUT = REDIS.get('DEFAULT_TIMEOUT', 300)
|
||||
|
||||
print(REDIS_HOST)
|
||||
print(REDIS_PORT)
|
||||
print(REDIS_PASSWORD)
|
||||
print(REDIS_DATABASE)
|
||||
print(REDIS_DEFAULT_TIMEOUT)
|
||||
|
||||
# Email
|
||||
EMAIL_HOST = EMAIL.get('SERVER')
|
||||
@@ -156,8 +162,8 @@ INSTALLED_APPS = [
|
||||
'drf_yasg',
|
||||
]
|
||||
|
||||
# only load django-rq if the webhook backend is enabled
|
||||
if WEBHOOK_BACKEND_ENABLED:
|
||||
# Only load django-rq if the webhook backend is enabled
|
||||
if WEBHOOKS_ENABLED:
|
||||
INSTALLED_APPS.append('django_rq')
|
||||
|
||||
# Middleware
|
||||
@@ -259,12 +265,12 @@ REST_FRAMEWORK = {
|
||||
'VIEW_NAME_FUNCTION': 'netbox.api.get_view_name',
|
||||
}
|
||||
|
||||
# Django RQ (Webhook backend)
|
||||
# Django RQ (Webhooks backend)
|
||||
RQ_QUEUES = {
|
||||
'default': {
|
||||
'HOST': REDIS_HOST,
|
||||
'PORT': REDIS_PORT,
|
||||
'DB': REDIS_DB,
|
||||
'DB': REDIS_DATABASE,
|
||||
'PASSWORD': REDIS_PASSWORD,
|
||||
'DEFAULT_TIMEOUT': REDIS_DEFAULT_TIMEOUT,
|
||||
}
|
||||
|
@@ -64,7 +64,7 @@ _patterns = [
|
||||
|
||||
]
|
||||
|
||||
if settings.WEBHOOK_BACKEND_ENABLED:
|
||||
if settings.WEBHOOKS_ENABLED:
|
||||
_patterns += [
|
||||
url(r'^admin/webhook-backend-status/', include('django_rq.urls')),
|
||||
]
|
||||
|
Reference in New Issue
Block a user