Cleaned up ElasticSearch CLI interface
This commit is contained in:
@@ -69,16 +69,3 @@ class ResetNodeIndex(ResetIndexTask):
|
|||||||
class ResetUserIndex(ResetIndexTask):
|
class ResetUserIndex(ResetIndexTask):
|
||||||
index_key = 'USER'
|
index_key = 'USER'
|
||||||
doc_types = [documents.User]
|
doc_types = [documents.User]
|
||||||
def reset_node_index():
|
|
||||||
resettask = ResetNodeIndex()
|
|
||||||
resettask.execute()
|
|
||||||
|
|
||||||
|
|
||||||
def reset_index(indexnames):
|
|
||||||
if 'users' in indexnames:
|
|
||||||
resettask = ResetUserIndex()
|
|
||||||
resettask.execute()
|
|
||||||
if 'nodes' in indexnames:
|
|
||||||
resettask = ResetUserIndex()
|
|
||||||
resettask.execute()
|
|
||||||
|
|
||||||
|
@@ -5,17 +5,21 @@ import bson
|
|||||||
from flask_script import Manager
|
from flask_script import Manager
|
||||||
|
|
||||||
from pillar import current_app
|
from pillar import current_app
|
||||||
|
from pillar.api.search import index
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
manager_elk = Manager(
|
manager_elk = Manager(
|
||||||
current_app, usage="Elastic utilities, like reset_index()")
|
current_app, usage="Elastic utilities, like reset_index()")
|
||||||
|
|
||||||
indexes = ['users', 'nodes']
|
name_to_task = {
|
||||||
|
'nodes': index.ResetNodeIndex,
|
||||||
|
'users': index.ResetUserIndex,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@manager_elk.command
|
@manager_elk.option('indices', nargs='*')
|
||||||
def reset_index(elk_index=None):
|
def reset_index(indices):
|
||||||
"""
|
"""
|
||||||
Destroy and recreate elastic indices
|
Destroy and recreate elastic indices
|
||||||
|
|
||||||
@@ -23,16 +27,16 @@ def reset_index(elk_index=None):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
with current_app.app_context():
|
with current_app.app_context():
|
||||||
from pillar.api.search import index
|
if not indices:
|
||||||
if not elk_index:
|
indices = name_to_task.keys()
|
||||||
index.reset_index(indexes)
|
|
||||||
return
|
for elk_index in indices:
|
||||||
if elk_index == 'nodes':
|
try:
|
||||||
index.reset_index(['node'])
|
task = name_to_task[elk_index]()
|
||||||
return
|
except KeyError:
|
||||||
if elk_index == 'users':
|
raise SystemError('Unknown elk_index, choose from %s' %
|
||||||
index.reset_index(['user'])
|
(', '.join(name_to_task.keys())))
|
||||||
return
|
task.execute()
|
||||||
|
|
||||||
|
|
||||||
def _reindex_users():
|
def _reindex_users():
|
||||||
@@ -71,7 +75,6 @@ def _public_project_ids() -> typing.List[bson.ObjectId]:
|
|||||||
|
|
||||||
|
|
||||||
def _reindex_nodes():
|
def _reindex_nodes():
|
||||||
|
|
||||||
db = current_app.db()
|
db = current_app.db()
|
||||||
pipeline = [
|
pipeline = [
|
||||||
{'$match': {'project': {'$in': _public_project_ids()}}},
|
{'$match': {'project': {'$in': _public_project_ids()}}},
|
||||||
@@ -95,16 +98,15 @@ def _reindex_nodes():
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
@manager_elk.command
|
@manager_elk.option('indexname', nargs='?')
|
||||||
def reindex(indexname):
|
def reindex(indexname=''):
|
||||||
|
|
||||||
if not indexname:
|
if not indexname:
|
||||||
log.debug('reindex everything..')
|
log.info('reindex everything..')
|
||||||
_reindex_nodes()
|
_reindex_nodes()
|
||||||
_reindex_users()
|
_reindex_users()
|
||||||
elif indexname == 'users':
|
elif indexname == 'users':
|
||||||
log.debug('Indexing %s', indexname)
|
log.info('Indexing %s', indexname)
|
||||||
_reindex_users()
|
_reindex_users()
|
||||||
elif indexname == 'nodes':
|
elif indexname == 'nodes':
|
||||||
log.debug('Indexing %s', indexname)
|
log.info('Indexing %s', indexname)
|
||||||
_reindex_nodes()
|
_reindex_nodes()
|
||||||
|
Reference in New Issue
Block a user