2016-10-15 09:14:56 +00:00
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
2018-12-18 03:29:21 +00:00
|
|
|
|
import os
|
2017-11-23 06:08:01 +00:00
|
|
|
|
import uuid
|
2019-12-05 07:09:25 +00:00
|
|
|
|
import jms_storage
|
2017-11-23 06:08:01 +00:00
|
|
|
|
|
2016-10-15 09:14:56 +00:00
|
|
|
|
from django.db import models
|
2019-09-25 08:31:58 +00:00
|
|
|
|
from django.db.models.signals import post_save
|
2016-10-15 10:28:49 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-02-27 11:39:27 +00:00
|
|
|
|
from django.utils import timezone
|
2018-01-20 14:22:09 +00:00
|
|
|
|
from django.conf import settings
|
2018-12-18 03:29:21 +00:00
|
|
|
|
from django.core.files.storage import default_storage
|
2019-01-15 02:23:30 +00:00
|
|
|
|
from django.core.cache import cache
|
2016-10-15 09:14:56 +00:00
|
|
|
|
|
2016-10-15 10:28:49 +00:00
|
|
|
|
from users.models import User
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2019-12-05 07:09:25 +00:00
|
|
|
|
from common.mixins import CommonModelMixin
|
|
|
|
|
from common.fields.model import EncryptJsonDictTextField
|
2019-01-21 09:05:31 +00:00
|
|
|
|
from .backends import get_multi_command_storage
|
2017-11-29 11:27:04 +00:00
|
|
|
|
from .backends.command.models import AbstractSessionCommand
|
2019-12-05 07:09:25 +00:00
|
|
|
|
from . import const
|
2016-10-15 10:28:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Terminal(models.Model):
|
2017-11-23 06:08:01 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2017-12-25 04:22:49 +00:00
|
|
|
|
name = models.CharField(max_length=32, verbose_name=_('Name'))
|
2018-11-23 02:25:35 +00:00
|
|
|
|
remote_addr = models.CharField(max_length=128, blank=True, verbose_name=_('Remote Address'))
|
2017-10-31 03:34:20 +00:00
|
|
|
|
ssh_port = models.IntegerField(verbose_name=_('SSH Port'), default=2222)
|
|
|
|
|
http_port = models.IntegerField(verbose_name=_('HTTP Port'), default=5000)
|
2018-01-23 02:36:20 +00:00
|
|
|
|
command_storage = models.CharField(max_length=128, verbose_name=_("Command storage"), default='default')
|
2018-01-20 14:22:09 +00:00
|
|
|
|
replay_storage = models.CharField(max_length=128, verbose_name=_("Replay storage"), default='default')
|
2017-10-31 03:34:20 +00:00
|
|
|
|
user = models.OneToOneField(User, related_name='terminal', verbose_name='Application User', null=True, on_delete=models.CASCADE)
|
2016-12-25 15:10:53 +00:00
|
|
|
|
is_accepted = models.BooleanField(default=False, verbose_name='Is Accepted')
|
2017-11-14 01:44:16 +00:00
|
|
|
|
is_deleted = models.BooleanField(default=False)
|
2016-10-15 15:34:02 +00:00
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True)
|
2016-10-19 10:33:14 +00:00
|
|
|
|
comment = models.TextField(blank=True, verbose_name=_('Comment'))
|
2019-01-21 09:05:31 +00:00
|
|
|
|
STATUS_KEY_PREFIX = 'terminal_status_'
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_alive(self):
|
|
|
|
|
key = self.STATUS_KEY_PREFIX + str(self.id)
|
|
|
|
|
return bool(cache.get(key))
|
|
|
|
|
|
|
|
|
|
@is_alive.setter
|
|
|
|
|
def is_alive(self, value):
|
|
|
|
|
key = self.STATUS_KEY_PREFIX + str(self.id)
|
|
|
|
|
cache.set(key, value, 60)
|
2016-10-15 10:28:49 +00:00
|
|
|
|
|
2016-12-21 16:36:31 +00:00
|
|
|
|
@property
|
|
|
|
|
def is_active(self):
|
|
|
|
|
if self.user and self.user.is_active:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
@is_active.setter
|
|
|
|
|
def is_active(self, active):
|
|
|
|
|
if self.user:
|
|
|
|
|
self.user.is_active = active
|
|
|
|
|
self.user.save()
|
|
|
|
|
|
2019-12-05 08:21:11 +00:00
|
|
|
|
def get_command_storage(self):
|
|
|
|
|
storage = CommandStorage.objects.filter(name=self.command_storage).first()
|
|
|
|
|
return storage
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
def get_command_storage_config(self):
|
2019-12-05 08:21:11 +00:00
|
|
|
|
s = self.get_command_storage()
|
2019-12-05 07:09:25 +00:00
|
|
|
|
if s:
|
|
|
|
|
config = s.config
|
2018-01-21 07:12:59 +00:00
|
|
|
|
else:
|
2019-12-05 07:09:25 +00:00
|
|
|
|
config = settings.DEFAULT_TERMINAL_COMMAND_STORAGE
|
2019-12-05 08:21:11 +00:00
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
def get_command_storage_setting(self):
|
|
|
|
|
config = self.get_command_storage_config()
|
2019-12-05 07:09:25 +00:00
|
|
|
|
return {"TERMINAL_COMMAND_STORAGE": config}
|
2018-01-20 14:22:09 +00:00
|
|
|
|
|
2019-12-05 08:21:11 +00:00
|
|
|
|
def get_replay_storage(self):
|
|
|
|
|
storage = ReplayStorage.objects.filter(name=self.replay_storage).first()
|
|
|
|
|
return storage
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
def get_replay_storage_config(self):
|
2019-12-05 08:21:11 +00:00
|
|
|
|
s = self.get_replay_storage()
|
2019-12-05 07:09:25 +00:00
|
|
|
|
if s:
|
|
|
|
|
config = s.config
|
2018-02-26 10:11:46 +00:00
|
|
|
|
else:
|
2019-12-05 07:09:25 +00:00
|
|
|
|
config = settings.DEFAULT_TERMINAL_REPLAY_STORAGE
|
2019-12-05 08:21:11 +00:00
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
def get_replay_storage_setting(self):
|
|
|
|
|
config = self.get_replay_storage_config()
|
2019-12-05 07:09:25 +00:00
|
|
|
|
return {"TERMINAL_REPLAY_STORAGE": config}
|
2018-01-20 14:22:09 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def config(self):
|
2018-01-21 07:12:59 +00:00
|
|
|
|
configs = {}
|
|
|
|
|
for k in dir(settings):
|
2019-01-10 03:50:08 +00:00
|
|
|
|
if not k.startswith('TERMINAL'):
|
|
|
|
|
continue
|
|
|
|
|
configs[k] = getattr(settings, k)
|
2019-12-05 08:21:11 +00:00
|
|
|
|
configs.update(self.get_command_storage_setting())
|
|
|
|
|
configs.update(self.get_replay_storage_setting())
|
2018-09-03 03:24:25 +00:00
|
|
|
|
configs.update({
|
2018-11-22 04:27:27 +00:00
|
|
|
|
'SECURITY_MAX_IDLE_TIME': settings.SECURITY_MAX_IDLE_TIME
|
2018-09-03 03:24:25 +00:00
|
|
|
|
})
|
2018-01-21 07:12:59 +00:00
|
|
|
|
return configs
|
2018-01-20 14:22:09 +00:00
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
|
@property
|
|
|
|
|
def service_account(self):
|
|
|
|
|
return self.user
|
|
|
|
|
|
2017-11-14 01:44:16 +00:00
|
|
|
|
def create_app_user(self):
|
2017-12-25 04:22:49 +00:00
|
|
|
|
random = uuid.uuid4().hex[:6]
|
2018-11-19 04:45:33 +00:00
|
|
|
|
user, access_key = User.create_app_user(
|
|
|
|
|
name="{}-{}".format(self.name, random), comment=self.comment
|
|
|
|
|
)
|
2016-12-25 09:44:39 +00:00
|
|
|
|
self.user = user
|
|
|
|
|
self.save()
|
|
|
|
|
return user, access_key
|
2016-10-17 07:24:41 +00:00
|
|
|
|
|
2017-01-02 16:11:44 +00:00
|
|
|
|
def delete(self, using=None, keep_parents=False):
|
|
|
|
|
if self.user:
|
|
|
|
|
self.user.delete()
|
2017-12-25 04:22:49 +00:00
|
|
|
|
self.user = None
|
2017-11-14 01:44:16 +00:00
|
|
|
|
self.is_deleted = True
|
|
|
|
|
self.save()
|
|
|
|
|
return
|
2017-01-02 16:11:44 +00:00
|
|
|
|
|
2017-10-31 03:34:20 +00:00
|
|
|
|
def __str__(self):
|
2017-11-14 01:44:16 +00:00
|
|
|
|
status = "Active"
|
|
|
|
|
if not self.is_accepted:
|
|
|
|
|
status = "NotAccept"
|
|
|
|
|
elif self.is_deleted:
|
|
|
|
|
status = "Deleted"
|
|
|
|
|
elif not self.is_active:
|
|
|
|
|
status = "Disable"
|
|
|
|
|
return '%s: %s' % (self.name, status)
|
2016-12-21 16:36:31 +00:00
|
|
|
|
|
2016-10-15 10:28:49 +00:00
|
|
|
|
class Meta:
|
2016-12-26 16:59:52 +00:00
|
|
|
|
ordering = ('is_accepted',)
|
2017-12-01 09:28:47 +00:00
|
|
|
|
db_table = "terminal"
|
2016-10-15 10:28:49 +00:00
|
|
|
|
|
|
|
|
|
|
2017-12-01 09:28:47 +00:00
|
|
|
|
class Status(models.Model):
|
2017-11-23 06:08:01 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
session_online = models.IntegerField(verbose_name=_("Session Online"), default=0)
|
|
|
|
|
cpu_used = models.FloatField(verbose_name=_("CPU Usage"))
|
|
|
|
|
memory_used = models.FloatField(verbose_name=_("Memory Used"))
|
|
|
|
|
connections = models.IntegerField(verbose_name=_("Connections"))
|
|
|
|
|
threads = models.IntegerField(verbose_name=_("Threads"))
|
|
|
|
|
boot_time = models.FloatField(verbose_name=_("Boot Time"))
|
|
|
|
|
terminal = models.ForeignKey(Terminal, null=True, on_delete=models.CASCADE)
|
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
db_table = 'terminal_status'
|
2017-12-01 13:22:32 +00:00
|
|
|
|
get_latest_by = 'date_created'
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.date_created.strftime("%Y-%m-%d %H:%M:%S")
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
2018-07-25 07:13:53 +00:00
|
|
|
|
class Session(OrgModelMixin):
|
2017-11-14 01:44:16 +00:00
|
|
|
|
LOGIN_FROM_CHOICES = (
|
|
|
|
|
('ST', 'SSH Terminal'),
|
|
|
|
|
('WT', 'Web Terminal'),
|
|
|
|
|
)
|
2018-04-23 03:32:46 +00:00
|
|
|
|
PROTOCOL_CHOICES = (
|
|
|
|
|
('ssh', 'ssh'),
|
2018-12-26 06:43:43 +00:00
|
|
|
|
('rdp', 'rdp'),
|
2019-09-23 07:31:09 +00:00
|
|
|
|
('vnc', 'vnc'),
|
|
|
|
|
('telnet', 'telnet'),
|
2019-12-19 07:28:17 +00:00
|
|
|
|
('mysql', 'mysql'),
|
2018-04-23 03:32:46 +00:00
|
|
|
|
)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
2017-12-03 16:21:26 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
user = models.CharField(max_length=128, verbose_name=_("User"), db_index=True)
|
|
|
|
|
user_id = models.CharField(blank=True, default='', max_length=36, db_index=True)
|
2020-01-06 07:36:03 +00:00
|
|
|
|
asset = models.CharField(max_length=128, verbose_name=_("Asset"), db_index=True)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
asset_id = models.CharField(blank=True, default='', max_length=36, db_index=True)
|
|
|
|
|
system_user = models.CharField(max_length=128, verbose_name=_("System user"), db_index=True)
|
|
|
|
|
system_user_id = models.CharField(blank=True, default='', max_length=36, db_index=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
login_from = models.CharField(max_length=2, choices=LOGIN_FROM_CHOICES, default="ST")
|
2019-12-05 07:09:25 +00:00
|
|
|
|
remote_addr = models.CharField(max_length=128, verbose_name=_("Remote addr"), blank=True, null=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
is_finished = models.BooleanField(default=False)
|
2017-11-22 02:54:59 +00:00
|
|
|
|
has_replay = models.BooleanField(default=False, verbose_name=_("Replay"))
|
|
|
|
|
has_command = models.BooleanField(default=False, verbose_name=_("Command"))
|
2018-03-07 11:05:43 +00:00
|
|
|
|
terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
protocol = models.CharField(choices=PROTOCOL_CHOICES, default='ssh', max_length=8, db_index=True)
|
2018-04-23 03:40:30 +00:00
|
|
|
|
date_start = models.DateTimeField(verbose_name=_("Date start"), db_index=True, default=timezone.now)
|
2017-12-15 09:38:46 +00:00
|
|
|
|
date_end = models.DateTimeField(verbose_name=_("Date end"), null=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
2018-12-18 03:29:21 +00:00
|
|
|
|
upload_to = 'replay'
|
2019-01-15 02:23:30 +00:00
|
|
|
|
ACTIVE_CACHE_KEY_PREFIX = 'SESSION_ACTIVE_{}'
|
2019-06-03 06:47:33 +00:00
|
|
|
|
_DATE_START_FIRST_HAS_REPLAY_RDP_SESSION = None
|
2018-12-18 03:29:21 +00:00
|
|
|
|
|
2019-02-15 03:14:37 +00:00
|
|
|
|
def get_rel_replay_path(self, version=2):
|
2018-12-18 03:29:21 +00:00
|
|
|
|
"""
|
|
|
|
|
获取session日志的文件路径
|
|
|
|
|
:param version: 原来后缀是 .gz,为了统一新版本改为 .replay.gz
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
suffix = '.replay.gz'
|
|
|
|
|
if version == 1:
|
|
|
|
|
suffix = '.gz'
|
2019-02-15 03:14:37 +00:00
|
|
|
|
date = self.date_start.strftime('%Y-%m-%d')
|
2018-12-18 03:29:21 +00:00
|
|
|
|
return os.path.join(date, str(self.id) + suffix)
|
|
|
|
|
|
2019-02-15 03:14:37 +00:00
|
|
|
|
def get_local_path(self, version=2):
|
2018-12-18 03:29:21 +00:00
|
|
|
|
rel_path = self.get_rel_replay_path(version=version)
|
|
|
|
|
if version == 2:
|
|
|
|
|
local_path = os.path.join(self.upload_to, rel_path)
|
|
|
|
|
else:
|
|
|
|
|
local_path = rel_path
|
|
|
|
|
return local_path
|
|
|
|
|
|
2019-06-03 06:47:33 +00:00
|
|
|
|
@property
|
|
|
|
|
def _date_start_first_has_replay_rdp_session(self):
|
2019-07-04 07:36:57 +00:00
|
|
|
|
if self.__class__._DATE_START_FIRST_HAS_REPLAY_RDP_SESSION is None:
|
2019-06-03 06:47:33 +00:00
|
|
|
|
instance = self.__class__.objects.filter(
|
2019-07-04 07:36:57 +00:00
|
|
|
|
protocol='rdp', has_replay=True
|
|
|
|
|
).order_by('date_start').first()
|
2019-06-03 06:47:33 +00:00
|
|
|
|
if not instance:
|
2019-07-04 07:36:57 +00:00
|
|
|
|
date_start = timezone.now() - timezone.timedelta(days=365)
|
|
|
|
|
else:
|
|
|
|
|
date_start = instance.date_start
|
|
|
|
|
self.__class__._DATE_START_FIRST_HAS_REPLAY_RDP_SESSION = date_start
|
|
|
|
|
return self.__class__._DATE_START_FIRST_HAS_REPLAY_RDP_SESSION
|
2019-06-03 06:47:33 +00:00
|
|
|
|
|
2019-03-19 11:09:09 +00:00
|
|
|
|
def can_replay(self):
|
|
|
|
|
if self.has_replay:
|
|
|
|
|
return True
|
2019-06-03 06:47:33 +00:00
|
|
|
|
if self.date_start < self._date_start_first_has_replay_rdp_session:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
2019-03-19 11:09:09 +00:00
|
|
|
|
|
2018-12-18 03:29:21 +00:00
|
|
|
|
def save_to_storage(self, f):
|
|
|
|
|
local_path = self.get_local_path()
|
|
|
|
|
try:
|
|
|
|
|
name = default_storage.save(local_path, f)
|
|
|
|
|
return name, None
|
|
|
|
|
except OSError as e:
|
|
|
|
|
return None, e
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
|
@classmethod
|
2019-01-21 09:05:31 +00:00
|
|
|
|
def set_sessions_active(cls, sessions_id):
|
2019-01-15 02:23:30 +00:00
|
|
|
|
data = {cls.ACTIVE_CACHE_KEY_PREFIX.format(i): i for i in sessions_id}
|
|
|
|
|
cache.set_many(data, timeout=5*60)
|
|
|
|
|
|
2019-01-21 09:05:31 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def get_active_sessions(cls):
|
|
|
|
|
return cls.objects.filter(is_finished=False)
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
|
def is_active(self):
|
2019-12-31 08:47:25 +00:00
|
|
|
|
if self.protocol in ['ssh', 'telnet', 'rdp', 'mysql']:
|
2019-01-15 02:23:30 +00:00
|
|
|
|
key = self.ACTIVE_CACHE_KEY_PREFIX.format(self.id)
|
|
|
|
|
return bool(cache.get(key))
|
|
|
|
|
return True
|
|
|
|
|
|
2019-01-21 09:05:31 +00:00
|
|
|
|
@property
|
|
|
|
|
def command_amount(self):
|
|
|
|
|
command_store = get_multi_command_storage()
|
|
|
|
|
return command_store.count(session=str(self.id))
|
|
|
|
|
|
2019-07-03 14:28:20 +00:00
|
|
|
|
@property
|
|
|
|
|
def login_from_display(self):
|
|
|
|
|
return self.get_login_from_display()
|
|
|
|
|
|
2017-11-14 01:44:16 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
db_table = "terminal_session"
|
2017-12-04 08:41:00 +00:00
|
|
|
|
ordering = ["-date_start"]
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return "{0.id} of {0.user} to {0.asset}".format(self)
|
|
|
|
|
|
|
|
|
|
|
2017-12-01 09:28:47 +00:00
|
|
|
|
class Task(models.Model):
|
2017-12-03 12:42:36 +00:00
|
|
|
|
NAME_CHOICES = (
|
|
|
|
|
("kill_session", "Kill Session"),
|
|
|
|
|
)
|
|
|
|
|
|
2017-11-23 06:08:01 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2017-12-03 12:42:36 +00:00
|
|
|
|
name = models.CharField(max_length=128, choices=NAME_CHOICES, verbose_name=_("Name"))
|
2017-12-15 09:38:46 +00:00
|
|
|
|
args = models.CharField(max_length=1024, verbose_name=_("Args"))
|
2018-03-07 11:05:43 +00:00
|
|
|
|
terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
is_finished = models.BooleanField(default=False)
|
2016-10-18 15:49:04 +00:00
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True)
|
2017-11-14 01:44:16 +00:00
|
|
|
|
date_finished = models.DateTimeField(null=True)
|
2016-10-15 10:28:49 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
2017-11-14 01:44:16 +00:00
|
|
|
|
db_table = "terminal_task"
|
2017-11-29 11:27:04 +00:00
|
|
|
|
|
|
|
|
|
|
2019-09-25 08:31:58 +00:00
|
|
|
|
class CommandManager(models.Manager):
|
|
|
|
|
def bulk_create(self, objs, **kwargs):
|
|
|
|
|
resp = super().bulk_create(objs, **kwargs)
|
|
|
|
|
for i in objs:
|
|
|
|
|
post_save.send(i.__class__, instance=i, created=True)
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
2017-12-01 09:28:47 +00:00
|
|
|
|
class Command(AbstractSessionCommand):
|
2019-09-25 08:31:58 +00:00
|
|
|
|
objects = CommandManager()
|
2017-11-29 11:27:04 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
2017-12-01 09:28:47 +00:00
|
|
|
|
db_table = "terminal_command"
|
2017-12-04 08:41:00 +00:00
|
|
|
|
ordering = ('-timestamp',)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandStorage(CommonModelMixin):
|
|
|
|
|
TYPE_CHOICES = const.COMMAND_STORAGE_TYPE_CHOICES
|
|
|
|
|
TYPE_DEFAULTS = dict(const.REPLAY_STORAGE_TYPE_CHOICES_DEFAULT).keys()
|
|
|
|
|
TYPE_SERVER = const.COMMAND_STORAGE_TYPE_SERVER
|
|
|
|
|
|
|
|
|
|
name = models.CharField(max_length=32, verbose_name=_("Name"), unique=True)
|
|
|
|
|
type = models.CharField(
|
|
|
|
|
max_length=16, choices=TYPE_CHOICES, verbose_name=_('Type'),
|
|
|
|
|
default=TYPE_SERVER
|
|
|
|
|
)
|
|
|
|
|
meta = EncryptJsonDictTextField(default={})
|
|
|
|
|
comment = models.TextField(
|
|
|
|
|
max_length=128, default='', blank=True, verbose_name=_('Comment')
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def config(self):
|
|
|
|
|
config = self.meta
|
|
|
|
|
config.update({'TYPE': self.type})
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
def in_defaults(self):
|
|
|
|
|
return self.type in self.TYPE_DEFAULTS
|
|
|
|
|
|
|
|
|
|
def is_valid(self):
|
|
|
|
|
if self.in_defaults():
|
|
|
|
|
return True
|
|
|
|
|
storage = jms_storage.get_log_storage(self.config)
|
|
|
|
|
return storage.ping()
|
|
|
|
|
|
|
|
|
|
def can_delete(self):
|
|
|
|
|
return not self.in_defaults()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReplayStorage(CommonModelMixin):
|
|
|
|
|
TYPE_CHOICES = const.REPLAY_STORAGE_TYPE_CHOICES
|
|
|
|
|
TYPE_SERVER = const.REPLAY_STORAGE_TYPE_SERVER
|
|
|
|
|
TYPE_DEFAULTS = dict(const.REPLAY_STORAGE_TYPE_CHOICES_DEFAULT).keys()
|
|
|
|
|
|
|
|
|
|
name = models.CharField(max_length=32, verbose_name=_("Name"), unique=True)
|
|
|
|
|
type = models.CharField(
|
|
|
|
|
max_length=16, choices=TYPE_CHOICES, verbose_name=_('Type'),
|
|
|
|
|
default=TYPE_SERVER
|
|
|
|
|
)
|
|
|
|
|
meta = EncryptJsonDictTextField(default={})
|
|
|
|
|
comment = models.TextField(
|
|
|
|
|
max_length=128, default='', blank=True, verbose_name=_('Comment')
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.name
|
|
|
|
|
|
2019-12-05 10:19:27 +00:00
|
|
|
|
def convert_type(self):
|
|
|
|
|
s3_type_list = [
|
|
|
|
|
const.REPLAY_STORAGE_TYPE_CEPH, const.REPLAY_STORAGE_TYPE_SWIFT
|
|
|
|
|
]
|
|
|
|
|
tp = self.type
|
|
|
|
|
if tp in s3_type_list:
|
|
|
|
|
tp = const.REPLAY_STORAGE_TYPE_S3
|
|
|
|
|
return tp
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
@property
|
|
|
|
|
def config(self):
|
|
|
|
|
config = self.meta
|
2019-12-05 10:19:27 +00:00
|
|
|
|
config.update({'TYPE': self.convert_type()})
|
2019-12-05 07:09:25 +00:00
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
def in_defaults(self):
|
|
|
|
|
return self.type in self.TYPE_DEFAULTS
|
|
|
|
|
|
|
|
|
|
def is_valid(self):
|
|
|
|
|
if self.in_defaults():
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
def can_delete(self):
|
|
|
|
|
return not self.in_defaults()
|
|
|
|
|
|