2022-11-04 12:14:19 +00:00
|
|
|
import time
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2022-11-17 12:48:50 +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-11-04 06:22:38 +00:00
|
|
|
from common.const.signals import SKIP_SIGNAL
|
2022-12-20 12:23:42 +00:00
|
|
|
from common.db.models import JMSBaseModel
|
2022-12-05 08:07:14 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
2021-04-13 09:37:29 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2022-11-17 12:48:50 +00:00
|
|
|
from terminal.const import TerminalType as TypeChoices
|
|
|
|
from users.models import User
|
2022-10-22 03:17:02 +00:00
|
|
|
from ..session import Session
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-02-05 09:05:13 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
class TerminalStatusMixin:
|
|
|
|
id: str
|
2022-11-04 03:40:16 +00:00
|
|
|
ALIVE_KEY = 'TERMINAL_ALIVE_{}'
|
|
|
|
status_set: models.Manager
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2022-11-04 03:40:16 +00:00
|
|
|
@lazyproperty
|
|
|
|
def last_stat(self):
|
|
|
|
return self.status_set.order_by('date_created').last()
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2022-11-04 03:40:16 +00:00
|
|
|
@lazyproperty
|
|
|
|
def load(self):
|
|
|
|
from ...utils import ComputeLoadUtil
|
|
|
|
return ComputeLoadUtil.compute_load(self.last_stat)
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
@property
|
|
|
|
def is_alive(self):
|
2022-11-04 03:40:16 +00:00
|
|
|
if not self.last_stat:
|
|
|
|
return False
|
2022-11-04 12:14:19 +00:00
|
|
|
return time.time() - self.last_stat.date_created.timestamp() < 150
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
class StorageMixin:
|
|
|
|
command_storage: str
|
|
|
|
replay_storage: str
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
def get_command_storage(self):
|
|
|
|
from .storage import CommandStorage
|
|
|
|
storage = CommandStorage.objects.filter(name=self.command_storage).first()
|
|
|
|
return storage
|
|
|
|
|
|
|
|
def get_command_storage_config(self):
|
|
|
|
s = self.get_command_storage()
|
|
|
|
if s:
|
2022-05-09 08:37:31 +00:00
|
|
|
config = s.valid_config
|
2020-12-10 12:50:22 +00:00
|
|
|
else:
|
|
|
|
config = settings.DEFAULT_TERMINAL_COMMAND_STORAGE
|
|
|
|
return config
|
|
|
|
|
|
|
|
def get_command_storage_setting(self):
|
|
|
|
config = self.get_command_storage_config()
|
|
|
|
return {"TERMINAL_COMMAND_STORAGE": config}
|
|
|
|
|
|
|
|
def get_replay_storage(self):
|
|
|
|
from .storage import ReplayStorage
|
|
|
|
storage = ReplayStorage.objects.filter(name=self.replay_storage).first()
|
|
|
|
return storage
|
|
|
|
|
|
|
|
def get_replay_storage_config(self):
|
|
|
|
s = self.get_replay_storage()
|
|
|
|
if s:
|
|
|
|
config = s.config
|
|
|
|
else:
|
|
|
|
config = settings.DEFAULT_TERMINAL_REPLAY_STORAGE
|
|
|
|
return config
|
|
|
|
|
|
|
|
def get_replay_storage_setting(self):
|
|
|
|
config = self.get_replay_storage_config()
|
|
|
|
return {"TERMINAL_REPLAY_STORAGE": config}
|
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
class Terminal(StorageMixin, TerminalStatusMixin, JMSBaseModel):
|
2021-03-26 11:09:34 +00:00
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
|
|
|
type = models.CharField(
|
2022-11-03 08:55:38 +00:00
|
|
|
choices=TypeChoices.choices, default=TypeChoices.koko,
|
2021-03-26 11:09:34 +00:00
|
|
|
max_length=64, verbose_name=_('type')
|
|
|
|
)
|
|
|
|
remote_addr = models.CharField(max_length=128, blank=True, verbose_name=_('Remote Address'))
|
|
|
|
command_storage = models.CharField(max_length=128, verbose_name=_("Command storage"), default='default')
|
|
|
|
replay_storage = models.CharField(max_length=128, verbose_name=_("Replay storage"), default='default')
|
2022-12-05 08:07:14 +00:00
|
|
|
user = models.OneToOneField(User, related_name='terminal', verbose_name=_('Application User'), null=True,
|
|
|
|
on_delete=models.CASCADE)
|
2022-12-05 10:54:12 +00:00
|
|
|
is_deleted = models.BooleanField(default=False)
|
2021-03-26 11:09:34 +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()
|
|
|
|
|
|
|
|
def get_online_sessions(self):
|
2021-04-13 09:37:29 +00:00
|
|
|
with tmp_to_root_org():
|
|
|
|
return Session.objects.filter(terminal=self, is_finished=False)
|
2021-03-26 11:09:34 +00:00
|
|
|
|
|
|
|
def get_online_session_count(self):
|
|
|
|
return self.get_online_sessions().count()
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_login_title_setting():
|
2021-10-22 12:06:16 +00:00
|
|
|
from settings.utils import get_login_title
|
|
|
|
return {'TERMINAL_HEADER_TITLE': get_login_title()}
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
configs = {}
|
|
|
|
for k in dir(settings):
|
|
|
|
if not k.startswith('TERMINAL'):
|
|
|
|
continue
|
|
|
|
configs[k] = getattr(settings, k)
|
|
|
|
configs.update(self.get_command_storage_setting())
|
|
|
|
configs.update(self.get_replay_storage_setting())
|
|
|
|
configs.update(self.get_login_title_setting())
|
|
|
|
configs.update({
|
2021-09-07 11:06:56 +00:00
|
|
|
'SECURITY_MAX_IDLE_TIME': settings.SECURITY_MAX_IDLE_TIME,
|
|
|
|
'SECURITY_SESSION_SHARE': settings.SECURITY_SESSION_SHARE
|
2020-12-10 12:50:22 +00:00
|
|
|
})
|
|
|
|
return configs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def service_account(self):
|
|
|
|
return self.user
|
|
|
|
|
|
|
|
def delete(self, using=None, keep_parents=False):
|
|
|
|
if self.user:
|
2022-11-04 06:22:38 +00:00
|
|
|
setattr(self.user, SKIP_SIGNAL, True)
|
2020-12-10 12:50:22 +00:00
|
|
|
self.user.delete()
|
|
|
|
self.user = None
|
|
|
|
self.is_deleted = True
|
|
|
|
self.save()
|
|
|
|
return
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
status = "Active"
|
2022-11-03 08:55:38 +00:00
|
|
|
if self.is_deleted:
|
2020-12-10 12:50:22 +00:00
|
|
|
status = "Deleted"
|
|
|
|
elif not self.is_active:
|
|
|
|
status = "Disable"
|
2021-07-30 07:42:06 +00:00
|
|
|
elif not self.is_alive:
|
|
|
|
status = 'Offline'
|
2020-12-10 12:50:22 +00:00
|
|
|
return '%s: %s' % (self.name, status)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = "terminal"
|
2022-02-17 12:13:31 +00:00
|
|
|
verbose_name = _("Terminal")
|
|
|
|
permissions = (
|
2022-03-15 11:15:25 +00:00
|
|
|
('view_terminalconfig', _('Can view terminal config')),
|
2022-02-17 12:13:31 +00:00
|
|
|
)
|