2017-11-29 11:27:04 +00:00
|
|
|
from importlib import import_module
|
|
|
|
from django.conf import settings
|
2020-09-27 06:34:47 +00:00
|
|
|
from django.utils.functional import LazyObject
|
|
|
|
|
2021-03-18 06:23:24 +00:00
|
|
|
from common.utils import get_logger
|
2018-10-23 11:22:18 +00:00
|
|
|
|
2021-03-18 06:23:24 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-09-27 06:34:47 +00:00
|
|
|
def get_server_replay_storage():
|
|
|
|
from jms_storage import get_object_storage
|
|
|
|
config = settings.SERVER_REPLAY_STORAGE
|
|
|
|
storage = get_object_storage(config)
|
|
|
|
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():
|
2021-03-18 06:23:24 +00:00
|
|
|
if not s.is_valid():
|
|
|
|
logger.warn(f'Command storage invalid: storage={s}')
|
|
|
|
continue
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
if s.type_server:
|
2018-05-22 10:22:06 +00:00
|
|
|
storage = get_command_storage()
|
2018-01-20 14:22:09 +00:00
|
|
|
else:
|
2021-01-12 10:06:42 +00:00
|
|
|
if not TYPE_ENGINE_MAPPING.get(s.type):
|
2018-04-19 10:26:59 +00:00
|
|
|
continue
|
2021-01-12 10:06:42 +00:00
|
|
|
engine_class = import_module(TYPE_ENGINE_MAPPING[s.type])
|
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
|
|
|
|
2020-09-27 06:34:47 +00:00
|
|
|
class ServerReplayStorage(LazyObject):
|
|
|
|
def _setup(self):
|
|
|
|
self._wrapped = get_server_replay_storage()
|
|
|
|
|
|
|
|
|
|
|
|
class ServerCommandStorage(LazyObject):
|
|
|
|
def _setup(self):
|
|
|
|
self._wrapped = get_command_storage()
|
|
|
|
|
|
|
|
|
|
|
|
server_command_storage = ServerCommandStorage()
|
|
|
|
server_replay_storage = ServerReplayStorage()
|