Commit 5d160985 by Calen Pennington

Install elasticsearch indexes via a management command

parent 69d0ec29
...@@ -54,6 +54,7 @@ validate: test quality ...@@ -54,6 +54,7 @@ validate: test quality
migrate: migrate:
python manage.py migrate --noinput python manage.py migrate --noinput
python manage.py install_es_indexes --noinput
html_coverage: html_coverage:
coverage html && open htmlcov/index.html coverage html && open htmlcov/index.html
......
import logging
from django.apps import AppConfig from django.apps import AppConfig
from django.conf import settings
from elasticsearch import Elasticsearch, TransportError
from course_discovery.apps.courses.config import COURSES_INDEX_CONFIG
logger = logging.getLogger(__name__)
class CoursesConfig(AppConfig): class CoursesConfig(AppConfig):
name = 'courses' name = 'courses'
verbose_name = 'Courses' verbose_name = 'Courses'
def ready(self):
if settings.ELASTICSEARCH.get('connect_on_startup', False):
host = settings.ELASTICSEARCH['host']
index = settings.ELASTICSEARCH['index']
logger.info('Attempting to establish initial connection to Elasticsearch host [%s]...', host)
es = Elasticsearch(host, sniff_on_start=True)
logger.info('...success!')
logger.info('Making sure index [%s] exists...', index)
try:
es.indices.create(index=index, body=COURSES_INDEX_CONFIG)
logger.info('...index created.')
except TransportError as e:
if e.status_code == 400:
logger.info('...index already exists.')
else:
raise
import logging
from django.conf import settings
from django.core.management import BaseCommand
from elasticsearch import Elasticsearch, TransportError
from course_discovery.apps.courses.config import COURSES_INDEX_CONFIG
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Install any required elasticsearch indexes'
def handle(self, *args, **options):
host = settings.ELASTICSEARCH['host']
index = settings.ELASTICSEARCH['index']
logger.info('Attempting to establish initial connection to Elasticsearch host [%s]...', host)
es = Elasticsearch(host, sniff_on_start=True)
logger.info('...success!')
logger.info('Making sure index [%s] exists...', index)
try:
es.indices.create(index=index, body=COURSES_INDEX_CONFIG)
logger.info('...index created.')
except TransportError as e:
if e.status_code == 400:
logger.info('...index already exists.')
else:
raise
import mock import mock
from django.apps import AppConfig
from django.conf import settings from django.conf import settings
from django.test import TestCase, override_settings from django.test import TestCase
from django.core.management import call_command
from elasticsearch import TransportError from elasticsearch import TransportError
from elasticsearch.client import IndicesClient from elasticsearch.client import IndicesClient
from testfixtures import LogCapture from testfixtures import LogCapture
from course_discovery.apps.core.tests.mixins import ElasticsearchTestMixin from course_discovery.apps.core.tests.mixins import ElasticsearchTestMixin
LOGGER_NAME = 'course_discovery.apps.courses.apps' LOGGER_NAME = 'courses.management.commands.install_es_indexes'
class CoursesConfigTests(ElasticsearchTestMixin, TestCase): class CourseInstallEsIndexes(ElasticsearchTestMixin, TestCase):
def setUp(self):
super(CoursesConfigTests, self).setUp()
self.app_config = AppConfig.create('course_discovery.apps.courses')
def test_ready_create_index(self): def test_ready_create_index(self):
""" Verify the app does not setup a new Elasticsearch index if one exists already. """ """ Verify the app does not setup a new Elasticsearch index if one exists already. """
host = settings.ELASTICSEARCH['host'] host = settings.ELASTICSEARCH['host']
...@@ -26,7 +22,7 @@ class CoursesConfigTests(ElasticsearchTestMixin, TestCase): ...@@ -26,7 +22,7 @@ class CoursesConfigTests(ElasticsearchTestMixin, TestCase):
self.assertFalse(self.es.indices.exists(index=index)) self.assertFalse(self.es.indices.exists(index=index))
with LogCapture(LOGGER_NAME) as l: with LogCapture(LOGGER_NAME) as l:
self.app_config.ready() call_command('install_es_indexes')
# Verify the index was created # Verify the index was created
self.assertTrue(self.es.indices.exists(index=index)) self.assertTrue(self.es.indices.exists(index=index))
...@@ -52,7 +48,7 @@ class CoursesConfigTests(ElasticsearchTestMixin, TestCase): ...@@ -52,7 +48,7 @@ class CoursesConfigTests(ElasticsearchTestMixin, TestCase):
with LogCapture(LOGGER_NAME) as l: with LogCapture(LOGGER_NAME) as l:
# This call should NOT raise an exception. # This call should NOT raise an exception.
self.app_config.ready() call_command('install_es_indexes')
# Verify the index still exists # Verify the index still exists
self.assertTrue(self.es.indices.exists(index=index)) self.assertTrue(self.es.indices.exists(index=index))
...@@ -71,13 +67,4 @@ class CoursesConfigTests(ElasticsearchTestMixin, TestCase): ...@@ -71,13 +67,4 @@ class CoursesConfigTests(ElasticsearchTestMixin, TestCase):
mock_create.side_effect = TransportError(500) mock_create.side_effect = TransportError(500)
with self.assertRaises(TransportError): with self.assertRaises(TransportError):
self.app_config.ready() call_command('install_es_indexes')
@override_settings(ELASTICSEARCH={'connect_on_startup': False})
def test_ready_without_connect_on_startup(self):
"""
Verify the app does not attempt to connect to Elasticsearch if the connect_on_startup setting is not set.
"""
with mock.patch.object(IndicesClient, 'create') as mock_create:
self.app_config.ready()
mock_create.assert_not_called()
...@@ -33,5 +33,4 @@ DATABASES = { ...@@ -33,5 +33,4 @@ DATABASES = {
ELASTICSEARCH = { ELASTICSEARCH = {
'host': os.environ.get('TEST_ELASTICSEARCH_HOST', 'localhost'), 'host': os.environ.get('TEST_ELASTICSEARCH_HOST', 'localhost'),
'index': 'course_discovery_test', 'index': 'course_discovery_test',
'connect_on_startup': True
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment