Skip to content

Commit

Permalink
[feature] Allow specifying redis port and password
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier authored May 31, 2022
1 parent 34b660a commit aa6ce9b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
12 changes: 12 additions & 0 deletions docs/ENV.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 17 additions & 5 deletions images/common/openwisp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,25 @@
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}

# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases


DB_OPTIONS = {
'sslmode': os.environ['DB_SSLMODE'],
'sslkey': os.environ['DB_SSLKEY'],
Expand Down Expand Up @@ -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]},
},
}

Expand All @@ -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

Expand Down
9 changes: 8 additions & 1 deletion images/common/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit aa6ce9b

Please sign in to comment.