[Stash] 删除结束会话之前

pull/530/head
ibuler 2017-02-11 19:49:15 +08:00
parent 42e4c64d06
commit 1f544b98ab
4 changed files with 32 additions and 4 deletions

View File

@ -9,11 +9,12 @@ from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIV
from rest_framework import viewsets
from rest_framework.views import APIView, Response
from rest_framework.permissions import AllowAny
from django.shortcuts import get_object_or_404
from rest_framework.decorators import api_view
from .models import Terminal, TerminalHeatbeat
from .serializers import TerminalSerializer, TerminalHeatbeatSerializer
from .hands import IsSuperUserOrAppUser, IsAppUser, User
from .hands import IsSuperUserOrAppUser, IsAppUser, User, ProxyLog
from common.utils import get_object_or_none
@ -64,6 +65,9 @@ class TerminalViewSet(viewsets.ModelViewSet):
# instance.user.delete()
# return super(TerminalViewSet, self).destroy(request, *args, **kwargs)
tasks = OrderedDict()
# tasks = {1: [{'name': 'kill_proxy', 'proxy_log_id': 23}]}
class TerminalHeatbeatViewSet(viewsets.ModelViewSet):
queryset = TerminalHeatbeat.objects.all()
@ -73,4 +77,24 @@ class TerminalHeatbeatViewSet(viewsets.ModelViewSet):
def create(self, request, *args, **kwargs):
terminal = request.user.terminal
TerminalHeatbeat.objects.create(terminal=terminal)
return Response({'msg': 'Success'}, status=201)
task = tasks.get(terminal.name)
tasks[terminal.name] = []
return Response({'msg': 'Success',
'tasks': task,},
status=201)
class TerminateConnectionView(APIView):
def post(self, request, *args, **kwargs):
proxy_log_id = request.data.get('proxy_log_id')
proxy_log = get_object_or_404(ProxyLog, id=proxy_log_id)
terminal_id = proxy_log.terminal
if terminal_id in tasks:
tasks[terminal_id].append({'name': 'kill_proxy',
'proxy_log_id': proxy_log_id})
else:
tasks[terminal_id] = [{'name': 'kill_proxy',
'proxy_log_id': proxy_log_id}]
print(tasks)
return Response({'msg': 'get it'})

View File

@ -14,7 +14,10 @@ router.register(r'v1/terminal/heatbeat', api.TerminalHeatbeatViewSet, 'terminal-
router.register(r'v1/terminal', api.TerminalViewSet, 'terminal')
urlpatterns = [
url(r'^v1/terminal/register/$', api.TerminalRegisterView.as_view(), name='terminal-register'),
url(r'^v1/terminal/register/$', api.TerminalRegisterView.as_view(),
name='terminal-register'),
url(r'^v1/terminate/connection/$', api.TerminateConnectionView.as_view(),
name='terminate-connection')
# url(r'^v1/terminal/heatbeat/$', api.TestHeatbeat.as_view())
]

View File

@ -14,7 +14,6 @@ from . import models, serializers
from .hands import IsSuperUserOrAppUser, IsAppUser
# Todo: 忘记当时为何不和ProxyLogViewSet复用了
class ProxyLogReceiveView(generics.CreateAPIView):
queryset = models.ProxyLog.objects.all()
serializer_class = serializers.ProxyLogSerializer

View File

@ -64,6 +64,7 @@
<th class="text-center">{% trans 'User' %}</th>
<th class="text-center">{% trans 'Asset' %}</th>
<th class="text-center">{% trans 'System user' %}</th>
<th class="text-center">{% trans 'Terminal' %}</th>
<th class="text-center">{% trans 'Command' %}</th>
<th class="text-center">{% trans 'Success' %}</th>
<th class="text-center">{% trans 'Finished' %}</th>
@ -81,6 +82,7 @@
<td class="text-center">{{ proxy_log.user }}</td>
<td class="text-center">{{ proxy_log.asset }}</td>
<td class="text-center">{{ proxy_log.system_user }}</td>
<td class="text-center">{{ proxy_log.terminal }}</td>
<td class="text-center">{{ proxy_log.commands.all|length}}</td>
<td class="text-center">
{% if proxy_log.is_failed %}