From aa6ce9b5af3af672b59e62b3302ff6cd027c3754 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Tue, 31 May 2022 15:18:04 -0400 Subject: [PATCH] [feature] Allow specifying redis port and password --- docs/ENV.md | 12 ++++++++++++ images/common/openwisp/settings.py | 22 +++++++++++++++++----- images/common/services.py | 9 ++++++++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/docs/ENV.md b/docs/ENV.md index 8ee8e02a..6440f4eb 100644 --- a/docs/ENV.md +++ b/docs/ENV.md @@ -732,6 +732,18 @@ Any OpenWISP Configuration of type `string`. `int`, `bool` or `json` is supporte - **Valid Values:** Domain | IP address - **Default:** redis +### `REDIS_PORT` + +- **Explanation:** Port to establish redis connection. +- **Valid Values:** INT +- **Default:** `6379` + +### `REDIS_PASS` + +- **Explanation:** Redis password, optional. +- **Valid Values:** STRING +- **Default:** `None` + ### `DASHBOARD_APP_SERVICE` - **Explanation:** Host to establish OpenWISP dashboard connection. diff --git a/images/common/openwisp/settings.py b/images/common/openwisp/settings.py index dfcfb6d9..1953cd20 100644 --- a/images/common/openwisp/settings.py +++ b/images/common/openwisp/settings.py @@ -130,7 +130,18 @@ ASGI_APPLICATION = 'openwisp.asgi.application' REDIS_HOST = os.environ['REDIS_HOST'] -CELERY_BROKER_URL = f'redis://{REDIS_HOST}:6379/1' +REDIS_PORT = os.environ.get('REDIS_PORT', 6379) +REDIS_PASS = os.environ.get('REDIS_PASS') + +if not REDIS_PASS: + CHANNEL_REDIS_HOST = f'redis://{REDIS_HOST}:{REDIS_PORT}/1' +else: + CHANNEL_REDIS_HOST = f'redis://:{REDIS_PASS}@{REDIS_HOST}:{REDIS_PORT}/1' + +if not REDIS_PASS: + CELERY_BROKER_URL = f'redis://{REDIS_HOST}:{REDIS_PORT}/2' +else: + CELERY_BROKER_URL = f'redis://:{REDIS_PASS}@{REDIS_HOST}:{REDIS_PORT}/2' CELERY_TASK_ACKS_LATE = True CELERY_WORKER_PREFETCH_MULTIPLIER = 1 CELERY_BROKER_TRANSPORT_OPTIONS = {'max_retries': 10} @@ -138,7 +149,6 @@ # Database # https://docs.djangoproject.com/en/1.9/ref/settings/#databases - DB_OPTIONS = { 'sslmode': os.environ['DB_SSLMODE'], 'sslkey': os.environ['DB_SSLKEY'], @@ -170,11 +180,10 @@ # Channels(Websocket) # https://channels.readthedocs.io/en/latest/topics/channel_layers.html#configuration - CHANNEL_LAYERS = { 'default': { 'BACKEND': 'channels_redis.core.RedisChannelLayer', - 'CONFIG': {'hosts': [(REDIS_HOST, 6379)]}, + 'CONFIG': {'hosts': [CHANNEL_REDIS_HOST]}, }, } @@ -184,13 +193,16 @@ CACHES = { 'default': { 'BACKEND': 'django_redis.cache.RedisCache', - 'LOCATION': f'redis://{REDIS_HOST}:6379/1', + 'LOCATION': f'redis://{REDIS_HOST}:{REDIS_PORT}/0', 'OPTIONS': { 'CLIENT_CLASS': 'django_redis.client.DefaultClient', }, } } +if REDIS_PASS: + CACHES['default']['OPTIONS']['PASSWORD'] = os.environ['REDIS_PASS'] + # Leaflet Configurations # https://django-leaflet.readthedocs.io/en/latest/templates.html#configuration diff --git a/images/common/services.py b/images/common/services.py index f19862a4..5b972b89 100644 --- a/images/common/services.py +++ b/images/common/services.py @@ -40,7 +40,14 @@ def dashboard_status(): def redis_status(): - rs = redis.Redis(os.environ['REDIS_HOST']) + kwargs = {} + redis_pass = os.environ.get('REDIS_PASS') + redis_port = os.environ.get('REDIS_PORT', 6379) + if redis_pass: + kwargs['password'] = redis_pass + if redis_port: + kwargs['port'] = redis_port + rs = redis.Redis(os.environ['REDIS_HOST'], **kwargs) try: rs.ping() except redis.ConnectionError: