Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
course-discovery
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
course-discovery
Commits
5d160985
Commit
5d160985
authored
Dec 18, 2015
by
Calen Pennington
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Install elasticsearch indexes via a management command
parent
69d0ec29
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
40 additions
and
48 deletions
+40
-48
Makefile
+1
-0
course_discovery/apps/courses/apps.py
+0
-27
course_discovery/apps/courses/management/commands/install_es_indexes.py
+32
-0
course_discovery/apps/courses/tests/test_install_es_indexes.py
+7
-20
course_discovery/settings/test.py
+0
-1
No files found.
Makefile
View file @
5d160985
...
@@ -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
...
...
course_discovery/apps/courses/apps.py
View file @
5d160985
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
course_discovery/apps/courses/management/commands/install_es_indexes.py
0 → 100644
View file @
5d160985
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
course_discovery/apps/courses/tests/test_
app
s.py
→
course_discovery/apps/courses/tests/test_
install_es_indexe
s.py
View file @
5d160985
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.app
s'
LOGGER_NAME
=
'course
s.management.commands.install_es_indexe
s'
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
()
course_discovery/settings/test.py
View file @
5d160985
...
@@ -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
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment