From f886e7c2f5bca809f48b389d7dfbb6c96b7d9eca Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 27 Feb 2018 19:39:27 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9=E6=8E=88=E6=9D=83?= =?UTF-8?q?api,=20windows=E8=B5=84=E4=BA=A7=E5=8F=AA=E6=9C=89rdp=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=EF=BC=8Clinux=E5=8F=AA=E6=9C=89ssh=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/forms/asset.py | 4 ++-- apps/assets/models/asset.py | 18 +++++++++++++++++- apps/assets/templates/assets/asset_create.html | 1 + apps/assets/templates/assets/asset_update.html | 1 + apps/perms/api.py | 15 +++++++++++++-- .../templates/perms/asset_permission_list.html | 2 +- apps/terminal/models.py | 2 ++ apps/terminal/tasks.py | 16 +++++++++++++--- .../templates/terminal/session_list.html | 2 ++ 9 files changed, 52 insertions(+), 9 deletions(-) diff --git a/apps/assets/forms/asset.py b/apps/assets/forms/asset.py index 763608daf..34865c544 100644 --- a/apps/assets/forms/asset.py +++ b/apps/assets/forms/asset.py @@ -15,7 +15,7 @@ class AssetCreateForm(forms.ModelForm): model = Asset fields = [ 'hostname', 'ip', 'public_ip', 'port', 'comment', - 'nodes', 'is_active', 'admin_user', 'labels', + 'nodes', 'is_active', 'admin_user', 'labels', 'platform', ] widgets = { @@ -44,7 +44,7 @@ class AssetUpdateForm(forms.ModelForm): class Meta: model = Asset fields = [ - 'hostname', 'ip', 'port', 'nodes', 'is_active', + 'hostname', 'ip', 'port', 'nodes', 'is_active', 'platform', 'public_ip', 'number', 'comment', 'admin_user', 'labels', ] widgets = { diff --git a/apps/assets/models/asset.py b/apps/assets/models/asset.py index 01c9ad255..3f7dec4ec 100644 --- a/apps/assets/models/asset.py +++ b/apps/assets/models/asset.py @@ -38,6 +38,14 @@ def default_node(): class Asset(models.Model): # Important + PLATFORM_CHOICES = ( + ('Linux', 'Linux'), + ('Unix', 'Unix'), + ('MacOS', 'MacOS'), + ('BSD', 'BSD'), + ('Windows', 'Windows'), + ('Other', 'Other'), + ) id = models.UUIDField(default=uuid.uuid4, primary_key=True) ip = models.GenericIPAddressField(max_length=32, verbose_name=_('IP'), db_index=True) hostname = models.CharField(max_length=128, unique=True, verbose_name=_('Hostname')) @@ -64,7 +72,7 @@ class Asset(models.Model): disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total')) disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info')) - platform = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Platform')) + platform = models.CharField(max_length=128, choices=PLATFORM_CHOICES, default='Linux', verbose_name=_('Platform')) os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS')) os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version')) os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch')) @@ -87,6 +95,12 @@ class Asset(models.Model): return True, '' return False, warning + def is_unixlike(self): + if self.platform not in ("Windows", "Other"): + return True + else: + return False + @property def hardware_info(self): if self.cpu_count: @@ -99,6 +113,8 @@ class Asset(models.Model): @property def is_connective(self): + if not self.is_unixlike(): + return True val = cache.get(ASSET_ADMIN_CONN_CACHE_KEY.format(self.hostname)) if val == 1: return True diff --git a/apps/assets/templates/assets/asset_create.html b/apps/assets/templates/assets/asset_create.html index 7091aa74a..1eae7ebd4 100644 --- a/apps/assets/templates/assets/asset_create.html +++ b/apps/assets/templates/assets/asset_create.html @@ -17,6 +17,7 @@ {% bootstrap_field form.hostname layout="horizontal" %} {% bootstrap_field form.ip layout="horizontal" %} {% bootstrap_field form.port layout="horizontal" %} + {% bootstrap_field form.platform layout="horizontal" %} {% bootstrap_field form.public_ip layout="horizontal" %}
diff --git a/apps/assets/templates/assets/asset_update.html b/apps/assets/templates/assets/asset_update.html index 515780f55..5d8006451 100644 --- a/apps/assets/templates/assets/asset_update.html +++ b/apps/assets/templates/assets/asset_update.html @@ -22,6 +22,7 @@ {% bootstrap_field form.hostname layout="horizontal" %} {% bootstrap_field form.ip layout="horizontal" %} {% bootstrap_field form.port layout="horizontal" %} + {% bootstrap_field form.platform layout="horizontal" %} {% bootstrap_field form.public_ip layout="horizontal" %}
diff --git a/apps/perms/api.py b/apps/perms/api.py index 958ee6838..6b0b15f76 100644 --- a/apps/perms/api.py +++ b/apps/perms/api.py @@ -54,7 +54,11 @@ class UserGrantedAssetsApi(ListAPIView): user = self.request.user for k, v in NodePermissionUtil.get_user_assets(user).items(): - k.system_users_granted = v + if k.is_unixlike(): + system_users_granted = [s for s in v if s.protocol == 'ssh'] + else: + system_users_granted = [s for s in v if s.protocol == 'rdp'] + k.system_users_granted = system_users_granted queryset.append(k) return queryset @@ -118,9 +122,16 @@ class UserGrantedNodesWithAssetsApi(ListAPIView): user = get_object_or_404(User, id=user_id) nodes = NodePermissionUtil.get_user_nodes_with_assets(user) + assets = {} + for k, v in NodePermissionUtil.get_user_assets(user).items(): + if k.is_unixlike(): + system_users_granted = [s for s in v if s.protocol == 'ssh'] + else: + system_users_granted = [s for s in v if s.protocol == 'rdp'] + assets[k] = system_users_granted for node, v in nodes.items(): for asset in v['assets']: - asset.system_users_granted = v['system_users'] + asset.system_users_granted = assets[asset] node.assets_granted = v['assets'] queryset.append(node) return queryset diff --git a/apps/perms/templates/perms/asset_permission_list.html b/apps/perms/templates/perms/asset_permission_list.html index 9b1bd3875..f87241d54 100644 --- a/apps/perms/templates/perms/asset_permission_list.html +++ b/apps/perms/templates/perms/asset_permission_list.html @@ -223,7 +223,7 @@ $(document).ready(function(){ current_node = nodes[0]; url += "?node=" + current_node.id; } - window.open(url); + window.open(url, '_self'); }) .on('click', '.btn-del', function () { var $this = $(this); diff --git a/apps/terminal/models.py b/apps/terminal/models.py index 23cb70f8e..c4ac25192 100644 --- a/apps/terminal/models.py +++ b/apps/terminal/models.py @@ -4,6 +4,7 @@ import uuid from django.db import models from django.utils.translation import ugettext_lazy as _ +from django.utils import timezone from django.conf import settings from users.models import User @@ -127,6 +128,7 @@ class Session(models.Model): has_replay = models.BooleanField(default=False, verbose_name=_("Replay")) has_command = models.BooleanField(default=False, verbose_name=_("Command")) terminal = models.ForeignKey(Terminal, null=True, on_delete=models.CASCADE) + date_last_active = models.DateTimeField(verbose_name=_("Date last active"), default=timezone.now) date_start = models.DateTimeField(verbose_name=_("Date start")) date_end = models.DateTimeField(verbose_name=_("Date end"), null=True) diff --git a/apps/terminal/tasks.py b/apps/terminal/tasks.py index ed404a54b..f6b46f2a8 100644 --- a/apps/terminal/tasks.py +++ b/apps/terminal/tasks.py @@ -1,17 +1,27 @@ # -*- coding: utf-8 -*- # +import datetime + from celery import shared_task +from django.utils import timezone + +from common.celery import register_as_period_task, after_app_ready_start, \ + after_app_shutdown_clean +from .models import Status CACHE_REFRESH_INTERVAL = 10 RUNNING = False -# Todo: 定期清理上报history @shared_task -def clean_terminal_history(): - pass +@register_as_period_task(interval=3600) +@after_app_ready_start +@after_app_shutdown_clean +def delete_terminal_status_period(): + yesterday = timezone.now() - datetime.timedelta(days=1) + Status.objects.filter(date_created__lt=yesterday).delete() diff --git a/apps/terminal/templates/terminal/session_list.html b/apps/terminal/templates/terminal/session_list.html index 3bf2600c8..dd9908487 100644 --- a/apps/terminal/templates/terminal/session_list.html +++ b/apps/terminal/templates/terminal/session_list.html @@ -75,6 +75,7 @@ {% trans 'Terminal' %} {% trans 'Command' %} {% trans 'Date start' %} +{# {% trans 'Date last active' %}#} {% trans 'Duration' %} {% trans 'Action' %} {% endblock %} @@ -94,6 +95,7 @@ {{ session.id | get_session_command_amount }} {{ session.date_start }} +{# {{ session.date_last_active }}#} {{ session.date_start|time_util_with_seconds:session.date_end }} {% if session.is_finished %}