2020-12-10 12:50:22 +00:00
|
|
|
from __future__ import unicode_literals
|
2022-12-20 12:23:42 +00:00
|
|
|
|
2022-06-20 06:37:31 +00:00
|
|
|
import copy
|
2020-12-10 12:50:22 +00:00
|
|
|
import os
|
2021-02-22 10:35:53 +00:00
|
|
|
from importlib import import_module
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
import jms_storage
|
2022-12-20 12:23:42 +00:00
|
|
|
from django.conf import settings
|
2020-12-10 12:50:22 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-10-22 03:17:02 +00:00
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
from common.db.fields import EncryptJsonDictTextField
|
|
|
|
from common.db.models import JMSBaseModel
|
2022-11-04 06:22:38 +00:00
|
|
|
from common.plugins.es import QuerySet as ESQuerySet
|
2021-02-22 10:35:53 +00:00
|
|
|
from common.utils import get_logger
|
2022-05-09 08:37:31 +00:00
|
|
|
from common.utils.timezone import local_now_date_display
|
2022-12-20 12:23:42 +00:00
|
|
|
from terminal import const
|
2021-02-22 10:35:53 +00:00
|
|
|
from terminal.backends import TYPE_ENGINE_MAPPING
|
2020-12-10 12:50:22 +00:00
|
|
|
from .terminal import Terminal
|
2022-10-22 03:17:02 +00:00
|
|
|
from ..session.command import Command
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-02-22 10:35:53 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2021-06-28 02:32:59 +00:00
|
|
|
class CommonStorageModelMixin(models.Model):
|
2020-12-10 12:50:22 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_("Name"), unique=True)
|
|
|
|
meta = EncryptJsonDictTextField(default={})
|
2021-06-28 02:32:59 +00:00
|
|
|
is_default = models.BooleanField(default=False, verbose_name=_('Default storage'))
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-06-28 02:32:59 +00:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2021-06-28 02:32:59 +00:00
|
|
|
def set_to_default(self):
|
|
|
|
self.is_default = True
|
2022-11-18 08:55:53 +00:00
|
|
|
self.save(update_fields=['is_default'])
|
|
|
|
self.__class__.objects.select_for_update() \
|
|
|
|
.filter(is_default=True) \
|
|
|
|
.exclude(id=self.id) \
|
2021-06-28 02:32:59 +00:00
|
|
|
.update(is_default=False)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def default(cls):
|
|
|
|
objs = cls.objects.filter(is_default=True)
|
|
|
|
if not objs:
|
|
|
|
objs = cls.objects.filter(name='default', type='server')
|
|
|
|
if not objs:
|
|
|
|
objs = cls.objects.all()
|
|
|
|
return objs.first()
|
|
|
|
|
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
class CommandStorage(CommonStorageModelMixin, JMSBaseModel):
|
2021-06-28 02:32:59 +00:00
|
|
|
type = models.CharField(
|
2022-11-04 03:40:16 +00:00
|
|
|
max_length=16, choices=const.CommandStorageType.choices,
|
|
|
|
default=const.CommandStorageType.server.value, verbose_name=_('Type'),
|
2021-06-28 02:32:59 +00:00
|
|
|
)
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
@property
|
|
|
|
def type_null(self):
|
2022-11-04 03:40:16 +00:00
|
|
|
return self.type == const.CommandStorageType.null.value
|
2021-01-12 10:06:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type_server(self):
|
2022-11-04 03:40:16 +00:00
|
|
|
return self.type == const.CommandStorageType.server.value
|
2021-01-12 10:06:42 +00:00
|
|
|
|
2022-05-09 08:37:31 +00:00
|
|
|
@property
|
|
|
|
def type_es(self):
|
2022-11-04 03:40:16 +00:00
|
|
|
return self.type == const.CommandStorageType.es.value
|
2022-05-09 08:37:31 +00:00
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
@property
|
|
|
|
def type_null_or_server(self):
|
|
|
|
return self.type_null or self.type_server
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
@property
|
|
|
|
def config(self):
|
2023-02-16 07:55:33 +00:00
|
|
|
config = copy.deepcopy(self.meta)
|
2020-12-10 12:50:22 +00:00
|
|
|
config.update({'TYPE': self.type})
|
2023-02-16 07:55:33 +00:00
|
|
|
return config
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2022-05-09 08:37:31 +00:00
|
|
|
@property
|
|
|
|
def valid_config(self):
|
|
|
|
config = self.config
|
|
|
|
if self.type_es and config.get('INDEX_BY_DATE'):
|
|
|
|
engine_mod = import_module(TYPE_ENGINE_MAPPING[self.type])
|
2022-07-07 10:48:06 +00:00
|
|
|
# 这里使用一个全新的 config, 防止修改当前的 config
|
|
|
|
store = engine_mod.CommandStore(self.config)
|
2022-05-09 08:37:31 +00:00
|
|
|
store._ensure_index_exists()
|
|
|
|
index_prefix = config.get('INDEX') or 'jumpserver'
|
|
|
|
date = local_now_date_display()
|
|
|
|
config['INDEX'] = '%s-%s' % (index_prefix, date)
|
|
|
|
return config
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
def is_valid(self):
|
2021-01-12 10:06:42 +00:00
|
|
|
if self.type_null_or_server:
|
2020-12-10 12:50:22 +00:00
|
|
|
return True
|
2021-03-17 07:26:47 +00:00
|
|
|
|
|
|
|
if self.type not in TYPE_ENGINE_MAPPING:
|
|
|
|
logger.error(f'Command storage `{self.type}` not support')
|
|
|
|
return False
|
|
|
|
|
|
|
|
engine_mod = import_module(TYPE_ENGINE_MAPPING[self.type])
|
|
|
|
store = engine_mod.CommandStore(self.config)
|
|
|
|
return store.ping(timeout=3)
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
def is_use(self):
|
2021-03-11 12:17:44 +00:00
|
|
|
return Terminal.objects.filter(command_storage=self.name, is_deleted=False).exists()
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-02-22 10:35:53 +00:00
|
|
|
def get_command_queryset(self):
|
2022-05-07 09:50:49 +00:00
|
|
|
if self.type_null:
|
|
|
|
return Command.objects.none()
|
|
|
|
|
2021-02-22 10:35:53 +00:00
|
|
|
if self.type_server:
|
2022-05-07 09:50:49 +00:00
|
|
|
return Command.objects.all()
|
|
|
|
|
|
|
|
if self.type in TYPE_ENGINE_MAPPING:
|
2021-02-22 10:35:53 +00:00
|
|
|
engine_mod = import_module(TYPE_ENGINE_MAPPING[self.type])
|
2022-11-04 06:22:38 +00:00
|
|
|
store = engine_mod.CommandStore(self.config)
|
|
|
|
qs = ESQuerySet(store)
|
2021-02-22 10:35:53 +00:00
|
|
|
qs.model = Command
|
2022-05-07 09:50:49 +00:00
|
|
|
return qs
|
|
|
|
|
|
|
|
logger.error(f'Command storage `{self.type}` not support')
|
|
|
|
return Command.objects.none()
|
2021-02-22 10:35:53 +00:00
|
|
|
|
2021-04-13 01:18:29 +00:00
|
|
|
def save(self, force_insert=False, force_update=False, using=None,
|
|
|
|
update_fields=None):
|
2022-11-18 08:55:53 +00:00
|
|
|
super().save(
|
|
|
|
force_insert=force_insert, force_update=force_update,
|
|
|
|
using=using, update_fields=update_fields
|
|
|
|
)
|
2021-04-13 01:18:29 +00:00
|
|
|
|
|
|
|
if self.type in TYPE_ENGINE_MAPPING:
|
|
|
|
engine_mod = import_module(TYPE_ENGINE_MAPPING[self.type])
|
|
|
|
backend = engine_mod.CommandStore(self.config)
|
|
|
|
backend.pre_use_check()
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Command storage")
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
class ReplayStorage(CommonStorageModelMixin, JMSBaseModel):
|
2020-12-10 12:50:22 +00:00
|
|
|
type = models.CharField(
|
2022-11-04 03:40:16 +00:00
|
|
|
max_length=16, choices=const.ReplayStorageType.choices,
|
|
|
|
default=const.ReplayStorageType.server.value, verbose_name=_('Type')
|
2020-12-10 12:50:22 +00:00
|
|
|
)
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
@property
|
|
|
|
def type_null(self):
|
2022-11-04 03:40:16 +00:00
|
|
|
return self.type == const.ReplayStorageType.null.value
|
2021-01-12 10:06:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type_server(self):
|
2022-11-04 03:40:16 +00:00
|
|
|
return self.type == const.ReplayStorageType.server.value
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
@property
|
|
|
|
def type_null_or_server(self):
|
|
|
|
return self.type_null or self.type_server
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type_swift(self):
|
2022-11-04 03:40:16 +00:00
|
|
|
return self.type == const.ReplayStorageType.swift.value
|
2021-01-12 10:06:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type_ceph(self):
|
2022-11-04 03:40:16 +00:00
|
|
|
return self.type == const.ReplayStorageType.ceph.value
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def config(self):
|
2021-01-12 10:06:42 +00:00
|
|
|
_config = {}
|
|
|
|
|
|
|
|
# add type config
|
|
|
|
if self.type_ceph:
|
2022-11-04 03:40:16 +00:00
|
|
|
_type = const.ReplayStorageType.s3.value
|
2021-01-12 10:06:42 +00:00
|
|
|
else:
|
|
|
|
_type = self.type
|
|
|
|
_config.update({'TYPE': _type})
|
|
|
|
|
|
|
|
# add special config
|
|
|
|
if self.type_swift:
|
|
|
|
_config.update({'signer': 'S3SignerType'})
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
# add meta config
|
|
|
|
_config.update(self.meta)
|
|
|
|
return _config
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
def is_valid(self):
|
2021-01-12 10:06:42 +00:00
|
|
|
if self.type_null_or_server:
|
2020-12-10 12:50:22 +00:00
|
|
|
return True
|
|
|
|
storage = jms_storage.get_object_storage(self.config)
|
|
|
|
target = 'tests.py'
|
|
|
|
src = os.path.join(settings.BASE_DIR, 'common', target)
|
|
|
|
return storage.is_valid(src, target)
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
def is_use(self):
|
2021-11-15 08:29:22 +00:00
|
|
|
return Terminal.objects.filter(replay_storage=self.name, is_deleted=False).exists()
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Replay storage")
|