Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.

Commit be6262b

Browse files
committed
re-enable deprecated ES_{SEARCH,LOG}_{HOST,PORT} settings
ES settings disabled in settings.py. The README already instructs users to create a local_settings.py. If they forget to configure Elasticsearch, they get an exception rather than AVRXL trying to connect to localhost.
1 parent 23f3515 commit be6262b

3 files changed

Lines changed: 48 additions & 6 deletions

File tree

avresearcher/app.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def create_app(package_name='avresearcher', settings_override=None):
2626

2727
app.config.from_object('avresearcher.settings')
2828
app.config.from_object(settings_override)
29+
_check_es_config(app.config)
2930
_validate(app.config)
3031

3132
if app.config['DEBUG'] and app.config['SENTRY_DSN']:
@@ -57,6 +58,25 @@ def load_user(user_id):
5758
return app
5859

5960

61+
def _check_es_config(config):
62+
# Check whether we have ES_SEARCH_CONFIG and ES_LOG_CONFIG.
63+
# If not, set it from the deprecated ES_{SEARCH,LOG}_{HOST,PORT} settings.
64+
65+
for estype in ["SEARCH", "LOG"]:
66+
es_config = "ES_%s_CONFIG" % estype
67+
if es_config not in config:
68+
host = "ES_%s_HOST" % estype
69+
port = "ES_%s_PORT" % estype
70+
if host not in config or port not in config:
71+
raise ValueError("need either %s setting or %s and %s"
72+
% (es_config, host, port))
73+
74+
config[es_config] = {
75+
"hosts": [config[host]],
76+
"port": config[port],
77+
}
78+
79+
6080
def _validate(config):
6181
# Settings validation: should catch common settings.py/local_settings.py
6282
# mistakes. Add rules as needed.

avresearcher/settings.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
SECRET_KEY = ''
44

55
# ElasticSearch instance that contains the document collection(s).
6-
ES_SEARCH_CONFIG = {'hosts': ['localhost'], 'port': 9200}
6+
#ES_SEARCH_CONFIG = {'hosts': ['localhost'], 'port': 9200}
77

88
# To connect to an Elasticsearch instance via a secure (HTTPS) connection,
99
# use a setting like the following. This works for ES_LOG_CONFIG, too.
@@ -15,10 +15,12 @@
1515
# 'verify_certs': True, 'ca_certs': certifi.where()}
1616

1717
# ElasticSearch instance used to store usage logs (clicks, queries, etc.).
18+
# This has the same format as ES_SEARCH_CONFIG; use the following to use the
19+
# same host for logging:
20+
#ES_LOG_CONFIG = ES_SEARCH_CONFIG
21+
#ES_LOG_INDEX = 'avresearcherxl_logs'
1822
# To disable logs, use:
19-
# ES_LOG_CONFIG = None
20-
ES_LOG_CONFIG = ES_SEARCH_CONFIG # Same instance that holds the collections.
21-
ES_LOG_INDEX = 'avresearcherxl_logs'
23+
#ES_LOG_CONFIG = None
2224

2325
# User database URI
2426
SQLALCHEMY_DATABASE_URI = 'mysql://user:pass@host/db'

avresearcher/tests/test_app.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from avresearcher.app import _validate
1+
from avresearcher.app import _check_es_config, _validate
22

33
from copy import deepcopy
44

5-
from nose.tools import assert_raises
5+
from nose.tools import assert_equal, assert_in, assert_raises
66

77

88
config = {
@@ -24,6 +24,26 @@
2424
}
2525

2626

27+
def test_check_es_config():
28+
assert_raises(ValueError, _check_es_config, config)
29+
30+
c = deepcopy(config)
31+
c["ES_SEARCH_HOST"] = "localhost"
32+
c["ES_SEARCH_PORT"] = 9200
33+
c["ES_LOG_HOST"] = "loghost"
34+
c["ES_LOG_PORT"] = 9200
35+
_check_es_config(c)
36+
37+
assert_in("ES_SEARCH_CONFIG", c)
38+
assert_equal(c["ES_SEARCH_CONFIG"], {"hosts": ["localhost"], "port": 9200})
39+
assert_in("ES_LOG_CONFIG", c)
40+
assert_equal(c["ES_LOG_CONFIG"], {"hosts": ["loghost"], "port": 9200})
41+
42+
# Allow ES_LOG_CONFIG = None
43+
c["ES_LOG_CONFIG"] = None
44+
_check_es_config(c)
45+
46+
2747
def test_validate():
2848
_validate(config)
2949

0 commit comments

Comments
 (0)