2017-11-29 11:27:04 +00:00
|
|
|
from importlib import import_module
|
|
|
|
from django.conf import settings
|
|
|
|
from .command.serializers import SessionCommandSerializer
|
2019-12-05 07:09:25 +00:00
|
|
|
from ..const import COMMAND_STORAGE_TYPE_SERVER
|
2017-11-29 11:27:04 +00:00
|
|
|
|
2018-10-23 11:22:18 +00:00
|
|
|
|
2018-01-20 14:22:09 +00:00
|
|
|
TYPE_ENGINE_MAPPING = {
|
2018-01-21 09:27:27 +00:00
|
|
|
'elasticsearch': 'terminal.backends.command.es',
|
2019-10-14 07:15:06 +00:00
|
|
|
'es': 'terminal.backends.command.es',
|
2018-01-20 14:22:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 11:27:04 +00:00
|
|
|
|
2018-05-22 10:22:06 +00:00
|
|
|
def get_command_storage():
|
|
|
|
config = settings.COMMAND_STORAGE
|
|
|
|
engine_class = import_module(config['ENGINE'])
|
|
|
|
storage = engine_class.CommandStore(config)
|
2018-01-20 14:22:09 +00:00
|
|
|
return storage
|
|
|
|
|
|
|
|
|
2018-05-22 10:22:06 +00:00
|
|
|
def get_terminal_command_storages():
|
2019-12-05 07:09:25 +00:00
|
|
|
from ..models import CommandStorage
|
2018-01-20 14:22:09 +00:00
|
|
|
storage_list = {}
|
2019-12-05 07:09:25 +00:00
|
|
|
for s in CommandStorage.objects.all():
|
|
|
|
tp = s.type
|
|
|
|
if tp == COMMAND_STORAGE_TYPE_SERVER:
|
2018-05-22 10:22:06 +00:00
|
|
|
storage = get_command_storage()
|
2018-01-20 14:22:09 +00:00
|
|
|
else:
|
|
|
|
if not TYPE_ENGINE_MAPPING.get(tp):
|
2018-04-19 10:26:59 +00:00
|
|
|
continue
|
2018-01-20 14:22:09 +00:00
|
|
|
engine_class = import_module(TYPE_ENGINE_MAPPING[tp])
|
2019-12-05 07:09:25 +00:00
|
|
|
storage = engine_class.CommandStore(s.config)
|
|
|
|
storage_list[s.name] = storage
|
2018-01-20 14:22:09 +00:00
|
|
|
return storage_list
|
|
|
|
|
|
|
|
|
2018-05-22 10:22:06 +00:00
|
|
|
def get_multi_command_storage():
|
2018-01-21 09:27:27 +00:00
|
|
|
from .command.multi import CommandStore
|
2018-05-22 10:22:06 +00:00
|
|
|
storage_list = get_terminal_command_storages().values()
|
2018-01-21 09:27:27 +00:00
|
|
|
storage = CommandStore(storage_list)
|
|
|
|
return storage
|
|
|
|
|
2017-11-29 11:27:04 +00:00
|
|
|
|