From 01a101a710f0762381be7704fa996e2c1aeb6545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E5=B9=BF?= Date: Mon, 21 Jan 2019 17:05:31 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B?= =?UTF-8?q?terminal=20storage=20(#2357)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/models/asset.py | 2 +- apps/common/api.py | 6 ++-- apps/terminal/api/v1/terminal.py | 7 +++-- apps/terminal/models.py | 31 ++++++++++++++++--- apps/terminal/serializers/v1.py | 27 +++------------- apps/terminal/signals_handler.py | 25 --------------- .../templates/terminal/session_list.html | 2 +- 7 files changed, 40 insertions(+), 60 deletions(-) diff --git a/apps/assets/models/asset.py b/apps/assets/models/asset.py index 9ab5aae86..244c2b7e7 100644 --- a/apps/assets/models/asset.py +++ b/apps/assets/models/asset.py @@ -185,7 +185,7 @@ class Asset(OrgModelMixin): @property def connectivity(self): if not self.is_unixlike(): - return self.UNKNOWN + return self.REACHABLE key = self.CONNECTIVITY_CACHE_KEY.format(str(self.id)) cached = cache.get(key, None) return cached if cached is not None else self.UNKNOWN diff --git a/apps/common/api.py b/apps/common/api.py index ba7820505..98924930f 100644 --- a/apps/common/api.py +++ b/apps/common/api.py @@ -215,10 +215,10 @@ class LogTailApi(generics.RetrieveAPIView): return Response({ "data": 'Not found the log', 'end': True, - 'mark': mark} - ) + 'mark': mark + }) else: - return Response({"data": _("Waiting ...\n")}, status=200) + return Response({"data": "Waiting...\r\n"}, status=200) with open(log_path, 'r') as f: offset = cache.get(mark, 0) diff --git a/apps/terminal/api/v1/terminal.py b/apps/terminal/api/v1/terminal.py index b83c2d844..c44ac4320 100644 --- a/apps/terminal/api/v1/terminal.py +++ b/apps/terminal/api/v1/terminal.py @@ -100,15 +100,18 @@ class StatusViewSet(viewsets.ModelViewSet): task_serializer_class = serializers.TaskSerializer def create(self, request, *args, **kwargs): - super().create(request, *args, **kwargs) + self.handle_status(request) self.handle_sessions() tasks = self.request.user.terminal.task_set.filter(is_finished=False) serializer = self.task_serializer_class(tasks, many=True) return Response(serializer.data, status=201) + def handle_status(self, request): + request.user.terminal.is_alive = True + def handle_sessions(self): sessions_id = self.request.data.get('sessions', []) - Session.set_active_sessions(sessions_id) + Session.set_sessions_active(sessions_id) def get_queryset(self): terminal_id = self.kwargs.get("terminal", None) diff --git a/apps/terminal/models.py b/apps/terminal/models.py index 3107882d3..ac912e82a 100644 --- a/apps/terminal/models.py +++ b/apps/terminal/models.py @@ -13,6 +13,7 @@ from django.core.cache import cache from users.models import User from orgs.mixins import OrgModelMixin from common.utils import get_command_storage_setting, get_replay_storage_setting +from .backends import get_multi_command_storage from .backends.command.models import AbstractSessionCommand @@ -29,6 +30,17 @@ class Terminal(models.Model): is_deleted = models.BooleanField(default=False) date_created = models.DateTimeField(auto_now_add=True) comment = models.TextField(blank=True, verbose_name=_('Comment')) + 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) @property def is_active(self): @@ -42,7 +54,7 @@ class Terminal(models.Model): self.user.is_active = active self.user.save() - def get_common_storage(self): + def get_command_storage_setting(self): storage_all = get_command_storage_setting() if self.command_storage in storage_all: storage = storage_all.get(self.command_storage) @@ -50,7 +62,7 @@ class Terminal(models.Model): storage = storage_all.get('default') return {"TERMINAL_COMMAND_STORAGE": storage} - def get_replay_storage(self): + def get_replay_storage_setting(self): storage_all = get_replay_storage_setting() if self.replay_storage in storage_all: storage = storage_all.get(self.replay_storage) @@ -65,8 +77,8 @@ class Terminal(models.Model): if not k.startswith('TERMINAL'): continue configs[k] = getattr(settings, k) - configs.update(self.get_common_storage()) - configs.update(self.get_replay_storage()) + configs.update(self.get_command_storage_setting()) + configs.update(self.get_replay_storage_setting()) configs.update({ 'SECURITY_MAX_IDLE_TIME': settings.SECURITY_MAX_IDLE_TIME }) @@ -185,16 +197,25 @@ class Session(OrgModelMixin): return None, e @classmethod - def set_active_sessions(cls, sessions_id): + def set_sessions_active(cls, sessions_id): data = {cls.ACTIVE_CACHE_KEY_PREFIX.format(i): i for i in sessions_id} cache.set_many(data, timeout=5*60) + @classmethod + def get_active_sessions(cls): + return cls.objects.filter(is_finished=False) + def is_active(self): if self.protocol in ['ssh', 'telnet']: key = self.ACTIVE_CACHE_KEY_PREFIX.format(self.id) return bool(cache.get(key)) return True + @property + def command_amount(self): + command_store = get_multi_command_storage() + return command_store.count(session=str(self.id)) + class Meta: db_table = "terminal_session" ordering = ["-date_start"] diff --git a/apps/terminal/serializers/v1.py b/apps/terminal/serializers/v1.py index 688453ac1..d36a5cb8b 100644 --- a/apps/terminal/serializers/v1.py +++ b/apps/terminal/serializers/v1.py @@ -1,17 +1,15 @@ # -*- coding: utf-8 -*- # -from django.core.cache import cache from rest_framework import serializers from rest_framework_bulk.serializers import BulkListSerializer from common.mixins import BulkSerializerMixin from ..models import Terminal, Status, Session, Task -from ..backends import get_multi_command_storage class TerminalSerializer(serializers.ModelSerializer): session_online = serializers.SerializerMethodField() - is_alive = serializers.SerializerMethodField() + is_alive = serializers.BooleanField() class Meta: model = Terminal @@ -23,40 +21,23 @@ class TerminalSerializer(serializers.ModelSerializer): @staticmethod def get_session_online(obj): - return Session.objects.filter(terminal=obj.id, is_finished=False).count() - - @staticmethod - def get_is_alive(obj): - key = StatusSerializer.CACHE_KEY_PREFIX + str(obj.id) - return cache.get(key) + return Session.objects.filter(terminal=obj, is_finished=False).count() class SessionSerializer(BulkSerializerMixin, serializers.ModelSerializer): - command_amount = serializers.SerializerMethodField() - command_store = get_multi_command_storage() + command_amount = serializers.IntegerField() class Meta: model = Session list_serializer_class = BulkListSerializer fields = '__all__' - def get_command_amount(self, obj): - return self.command_store.count(session=str(obj.id)) - class StatusSerializer(serializers.ModelSerializer): - CACHE_KEY_PREFIX = 'terminal_status_' - class Meta: - fields = '__all__' + fields = ['id', 'terminal'] model = Status - def create(self, validated_data): - terminal_id = str(validated_data['terminal'].id) - key = self.CACHE_KEY_PREFIX + terminal_id - cache.set(key, 1, 60) - return validated_data - class TaskSerializer(BulkSerializerMixin, serializers.ModelSerializer): diff --git a/apps/terminal/signals_handler.py b/apps/terminal/signals_handler.py index 883dd51f2..3d98261b1 100644 --- a/apps/terminal/signals_handler.py +++ b/apps/terminal/signals_handler.py @@ -1,28 +1,3 @@ # -*- coding: utf-8 -*- # -from celery import shared_task -from django.core.cache import cache -from django.db.utils import ProgrammingError, OperationalError -from common.utils import get_logger -from .const import ASSETS_CACHE_KEY, USERS_CACHE_KEY, SYSTEM_USER_CACHE_KEY - -RUNNING = False -logger = get_logger(__file__) - - -def set_session_info_cache(): - logger.debug("") - from .utils import get_session_asset_list, get_session_user_list, \ - get_session_system_user_list - - try: - assets = get_session_asset_list() - users = get_session_user_list() - system_users = get_session_system_user_list() - - cache.set(ASSETS_CACHE_KEY, assets) - cache.set(USERS_CACHE_KEY, users) - cache.set(SYSTEM_USER_CACHE_KEY, system_users) - except (ProgrammingError, OperationalError): - pass diff --git a/apps/terminal/templates/terminal/session_list.html b/apps/terminal/templates/terminal/session_list.html index 92c114966..68b23611b 100644 --- a/apps/terminal/templates/terminal/session_list.html +++ b/apps/terminal/templates/terminal/session_list.html @@ -94,7 +94,7 @@ {{ session.remote_addr|default:"" }} {{ session.protocol }} {{ session.get_login_from_display }} - {{ session.id | get_session_command_amount }} + {{ session.command_amount }} {{ session.date_start }} {# {{ session.date_last_active }}#}