jumpserver/apps/ops/api.py

92 lines
2.9 KiB
Python
Raw Normal View History

# ~*~ coding: utf-8 ~*~
2018-03-30 14:03:43 +00:00
import uuid
2018-04-01 15:45:37 +00:00
import os
2018-03-30 14:03:43 +00:00
from django.core.cache import cache
2017-12-10 16:29:25 +00:00
from django.shortcuts import get_object_or_404
2018-04-01 15:45:37 +00:00
from django.utils.translation import ugettext as _
from rest_framework import viewsets, generics
from rest_framework.views import Response
from .hands import IsSuperUser
2018-04-02 07:54:49 +00:00
from .models import Task, AdHoc, AdHocRunHistory, CeleryTask
from .serializers import TaskSerializer, AdHocSerializer, \
AdHocRunHistorySerializer
from .tasks import run_ansible_task
class TaskViewSet(viewsets.ModelViewSet):
2017-12-10 16:29:25 +00:00
queryset = Task.objects.all()
serializer_class = TaskSerializer
permission_classes = (IsSuperUser,)
2017-12-10 16:29:25 +00:00
class TaskRun(generics.RetrieveAPIView):
queryset = Task.objects.all()
serializer_class = TaskViewSet
permission_classes = (IsSuperUser,)
def retrieve(self, request, *args, **kwargs):
task = self.get_object()
2018-04-01 15:45:37 +00:00
t = run_ansible_task.delay(str(task.id))
return Response({"task": t.id})
2017-12-10 16:29:25 +00:00
class AdHocViewSet(viewsets.ModelViewSet):
queryset = AdHoc.objects.all()
serializer_class = AdHocSerializer
permission_classes = (IsSuperUser,)
def get_queryset(self):
task_id = self.request.query_params.get('task')
if task_id:
task = get_object_or_404(Task, id=task_id)
self.queryset = self.queryset.filter(task=task)
return self.queryset
class AdHocRunHistorySet(viewsets.ModelViewSet):
queryset = AdHocRunHistory.objects.all()
serializer_class = AdHocRunHistorySerializer
permission_classes = (IsSuperUser,)
def get_queryset(self):
task_id = self.request.query_params.get('task')
2017-12-20 03:30:15 +00:00
adhoc_id = self.request.query_params.get('adhoc')
2017-12-10 16:29:25 +00:00
if task_id:
task = get_object_or_404(Task, id=task_id)
adhocs = task.adhoc.all()
self.queryset = self.queryset.filter(adhoc__in=adhocs)
2017-12-20 03:30:15 +00:00
if adhoc_id:
adhoc = get_object_or_404(AdHoc, id=adhoc_id)
self.queryset = self.queryset.filter(adhoc=adhoc)
2017-12-10 16:29:25 +00:00
return self.queryset
2018-03-30 14:03:43 +00:00
2018-04-02 07:54:49 +00:00
class CeleryTaskLogApi(generics.RetrieveAPIView):
2018-03-30 14:03:43 +00:00
permission_classes = (IsSuperUser,)
buff_size = 1024 * 10
end = False
2018-04-02 07:54:49 +00:00
queryset = CeleryTask.objects.all()
2018-03-30 14:03:43 +00:00
2018-04-01 15:45:37 +00:00
def get(self, request, *args, **kwargs):
2018-03-30 14:03:43 +00:00
mark = request.query_params.get("mark") or str(uuid.uuid4())
2018-04-02 07:54:49 +00:00
task = super().get_object()
log_path = task.full_log_path
2018-03-30 14:03:43 +00:00
2018-04-02 07:54:49 +00:00
if not log_path or not os.path.isfile(log_path):
return Response({"data": _("Waiting ...")}, status=203)
2018-04-01 15:45:37 +00:00
2018-04-02 07:54:49 +00:00
with open(log_path, 'r') as f:
2018-03-30 14:03:43 +00:00
offset = cache.get(mark, 0)
f.seek(offset)
data = f.read(self.buff_size).replace('\n', '\r\n')
mark = str(uuid.uuid4())
cache.set(mark, f.tell(), 5)
2018-04-02 07:54:49 +00:00
if data == '' and task.is_finished():
2018-03-30 14:03:43 +00:00
self.end = True
return Response({"data": data, 'end': self.end, 'mark': mark})
2018-04-02 07:54:49 +00:00