Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/user/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,33 @@ framework.
- **Valid Values:** ``True``, ``False``.
- **Default:** ``False``.

``REDIS_CACHE_URL``
~~~~~~~~~~~~~~~~~~~

- **Explanation:** Allows freely redefining the Redis database URL for the
Django cache.
- **Valid Values:** STRING.
- **Default:**
``redis://<REDIS_USER>:<REDIS_PASS>@<REDIS_HOST>:<REDIS_PORT>/0``.

``CHANNEL_REDIS_URL``
~~~~~~~~~~~~~~~~~~~~~

- **Explanation:** Allows freely redefining the Redis database URL for
Django Channels' layer.
- **Valid Values:** STRING.
- **Default:**
``redis://<REDIS_USER>:<REDIS_PASS>@<REDIS_HOST>:<REDIS_PORT>/1``.

``CELERY_BROKER_URL``
~~~~~~~~~~~~~~~~~~~~~

- **Explanation:** Allows freely redefining the Redis database URL for the
Celery broker.
- **Valid Values:** STRING.
- **Default:**
``redis://<REDIS_USER>:<REDIS_PASS>@<REDIS_HOST>:<REDIS_PORT>/2``.

DJANGO_LOG_LEVEL
~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -1044,13 +1071,27 @@ Misc Services
- **Valid Values:** INTEGER.
- **Default:** ``6379``.

``REDIS_USER``
~~~~~~~~~~~~~~

- **Explanation:** Redis username, optional.
- **Valid Values:** STRING.
- **Default:** ``""`` (empty string).

``REDIS_PASS``
~~~~~~~~~~~~~~

- **Explanation:** Redis password, optional.
- **Valid Values:** STRING.
- **Default:** ``None``.

``REDIS_USE_TLS``
~~~~~~~~~~~~~~~~~

- **Explanation:** Whether to use TLS for redis connection.
- **Valid Values:** ``True``, ``False``.
- **Default:** ``False``.

``DASHBOARD_APP_SERVICE``
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
27 changes: 19 additions & 8 deletions images/common/openwisp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import sys
from urllib.parse import quote

import tldextract
from openwisp.utils import (
Expand Down Expand Up @@ -138,17 +139,27 @@

REDIS_HOST = os.environ["REDIS_HOST"]
REDIS_PORT = os.environ.get("REDIS_PORT", 6379)
REDIS_USER = os.environ.get("REDIS_USER")
REDIS_PASS = os.environ.get("REDIS_PASS")
REDIS_SCHEME = (
"rediss" if env_bool(os.environ.get("REDIS_USE_TLS", "False")) else "redis"
)

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"
# Build base Redis URL

if not REDIS_PASS:
CELERY_BROKER_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/2"
if REDIS_USER and REDIS_PASS:
credentials = f"{quote(REDIS_USER)}:{quote(REDIS_PASS)}@"
elif REDIS_PASS:
# Password only
credentials = f":{quote(REDIS_PASS)}@"
else:
CELERY_BROKER_URL = f"redis://:{REDIS_PASS}@{REDIS_HOST}:{REDIS_PORT}/2"
credentials = ""
REDIS_BASE_URL = f"{REDIS_SCHEME}://{credentials}{REDIS_HOST}:{REDIS_PORT}"

REDIS_CACHE_URL = os.environ.get("REDIS_CACHE_URL", f"{REDIS_BASE_URL}/0")
CHANNEL_REDIS_HOST = os.environ.get("CHANNEL_REDIS_URL", f"{REDIS_BASE_URL}/1")
CELERY_BROKER_URL = os.environ.get("CELERY_BROKER_URL", f"{REDIS_BASE_URL}/2")

CELERY_TASK_ACKS_LATE = True
CELERY_WORKER_PREFETCH_MULTIPLIER = 1
CELERY_BROKER_TRANSPORT_OPTIONS = {"max_retries": 10}
Expand Down Expand Up @@ -203,7 +214,7 @@
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": f"redis://{REDIS_HOST}:{REDIS_PORT}/0",
"LOCATION": REDIS_CACHE_URL,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
},
Expand Down
7 changes: 7 additions & 0 deletions images/common/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ def dashboard_status():

def redis_status():
kwargs = {}
redis_user = os.environ.get("REDIS_USER")
redis_pass = os.environ.get("REDIS_PASS")
redis_port = os.environ.get("REDIS_PORT", 6379)
redis_use_tls = os.environ.get("REDIS_USE_TLS", "False").lower() == "true"

if redis_user:
kwargs["username"] = redis_user
if redis_pass:
kwargs["password"] = redis_pass
if redis_port:
kwargs["port"] = redis_port
if redis_use_tls:
kwargs["ssl"] = redis_use_tls
rs = redis.Redis(os.environ["REDIS_HOST"], **kwargs)
try:
rs.ping()
Expand Down