Skip to content

Commit aa6ce9b

Browse files
authored
[feature] Allow specifying redis port and password
1 parent 34b660a commit aa6ce9b

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

docs/ENV.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,18 @@ Any OpenWISP Configuration of type `string`. `int`, `bool` or `json` is supporte
732732
- **Valid Values:** Domain | IP address
733733
- **Default:** redis
734734

735+
### `REDIS_PORT`
736+
737+
- **Explanation:** Port to establish redis connection.
738+
- **Valid Values:** INT
739+
- **Default:** `6379`
740+
741+
### `REDIS_PASS`
742+
743+
- **Explanation:** Redis password, optional.
744+
- **Valid Values:** STRING
745+
- **Default:** `None`
746+
735747
### `DASHBOARD_APP_SERVICE`
736748

737749
- **Explanation:** Host to establish OpenWISP dashboard connection.

images/common/openwisp/settings.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,25 @@
130130
ASGI_APPLICATION = 'openwisp.asgi.application'
131131

132132
REDIS_HOST = os.environ['REDIS_HOST']
133-
CELERY_BROKER_URL = f'redis://{REDIS_HOST}:6379/1'
133+
REDIS_PORT = os.environ.get('REDIS_PORT', 6379)
134+
REDIS_PASS = os.environ.get('REDIS_PASS')
135+
136+
if not REDIS_PASS:
137+
CHANNEL_REDIS_HOST = f'redis://{REDIS_HOST}:{REDIS_PORT}/1'
138+
else:
139+
CHANNEL_REDIS_HOST = f'redis://:{REDIS_PASS}@{REDIS_HOST}:{REDIS_PORT}/1'
140+
141+
if not REDIS_PASS:
142+
CELERY_BROKER_URL = f'redis://{REDIS_HOST}:{REDIS_PORT}/2'
143+
else:
144+
CELERY_BROKER_URL = f'redis://:{REDIS_PASS}@{REDIS_HOST}:{REDIS_PORT}/2'
134145
CELERY_TASK_ACKS_LATE = True
135146
CELERY_WORKER_PREFETCH_MULTIPLIER = 1
136147
CELERY_BROKER_TRANSPORT_OPTIONS = {'max_retries': 10}
137148

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

141-
142152
DB_OPTIONS = {
143153
'sslmode': os.environ['DB_SSLMODE'],
144154
'sslkey': os.environ['DB_SSLKEY'],
@@ -170,11 +180,10 @@
170180

171181
# Channels(Websocket)
172182
# https://channels.readthedocs.io/en/latest/topics/channel_layers.html#configuration
173-
174183
CHANNEL_LAYERS = {
175184
'default': {
176185
'BACKEND': 'channels_redis.core.RedisChannelLayer',
177-
'CONFIG': {'hosts': [(REDIS_HOST, 6379)]},
186+
'CONFIG': {'hosts': [CHANNEL_REDIS_HOST]},
178187
},
179188
}
180189

@@ -184,13 +193,16 @@
184193
CACHES = {
185194
'default': {
186195
'BACKEND': 'django_redis.cache.RedisCache',
187-
'LOCATION': f'redis://{REDIS_HOST}:6379/1',
196+
'LOCATION': f'redis://{REDIS_HOST}:{REDIS_PORT}/0',
188197
'OPTIONS': {
189198
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
190199
},
191200
}
192201
}
193202

203+
if REDIS_PASS:
204+
CACHES['default']['OPTIONS']['PASSWORD'] = os.environ['REDIS_PASS']
205+
194206
# Leaflet Configurations
195207
# https://django-leaflet.readthedocs.io/en/latest/templates.html#configuration
196208

images/common/services.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ def dashboard_status():
4040

4141

4242
def redis_status():
43-
rs = redis.Redis(os.environ['REDIS_HOST'])
43+
kwargs = {}
44+
redis_pass = os.environ.get('REDIS_PASS')
45+
redis_port = os.environ.get('REDIS_PORT', 6379)
46+
if redis_pass:
47+
kwargs['password'] = redis_pass
48+
if redis_port:
49+
kwargs['port'] = redis_port
50+
rs = redis.Redis(os.environ['REDIS_HOST'], **kwargs)
4451
try:
4552
rs.ping()
4653
except redis.ConnectionError:

0 commit comments

Comments
 (0)