jumpserver/apps/ops/api/job.py

127 lines
4.2 KiB
Python
Raw Normal View History

2022-12-26 11:02:05 +00:00
from django.db.models import Count
from rest_framework.views import APIView
from django.shortcuts import get_object_or_404
from rest_framework.response import Response
2022-11-11 11:20:17 +00:00
from ops.models import Job, JobExecution
from ops.serializers.job import JobSerializer, JobExecutionSerializer
2022-12-26 11:02:05 +00:00
__all__ = ['JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView',
2022-12-27 08:06:21 +00:00
'JobAssetDetail', 'JobExecutionTaskDetail', 'FrequentUsernames']
2022-11-11 11:20:17 +00:00
from ops.tasks import run_ops_job_execution
from ops.variables import JMS_JOB_VARIABLE_HELP
2022-12-15 09:25:21 +00:00
from orgs.mixins.api import OrgBulkModelViewSet
2022-12-23 10:23:04 +00:00
from orgs.utils import tmp_to_org, get_current_org_id, get_current_org
2022-12-26 11:02:05 +00:00
from assets.models import Account
2022-11-11 11:20:17 +00:00
def set_task_to_serializer_data(serializer, task):
data = getattr(serializer, "_data", {})
data["task_id"] = task.id
setattr(serializer, "_data", data)
2022-12-15 09:25:21 +00:00
class JobViewSet(OrgBulkModelViewSet):
2022-11-11 11:20:17 +00:00
serializer_class = JobSerializer
2022-11-23 16:50:37 +00:00
permission_classes = ()
2022-12-06 09:30:54 +00:00
model = Job
2022-11-11 11:20:17 +00:00
2022-12-21 09:14:07 +00:00
def allow_bulk_destroy(self, qs, filtered):
return True
2022-11-11 11:20:17 +00:00
def get_queryset(self):
2022-12-15 09:25:21 +00:00
queryset = super().get_queryset()
queryset = queryset.filter(creator=self.request.user)
2022-11-25 19:13:06 +00:00
if self.action != 'retrieve':
2022-12-15 09:25:21 +00:00
return queryset.filter(instant=False)
return queryset
2022-11-11 11:20:17 +00:00
def perform_create(self, serializer):
2022-12-21 09:14:07 +00:00
run_after_save = serializer.validated_data.pop('run_after_save', False)
2022-11-11 11:20:17 +00:00
instance = serializer.save()
2022-12-02 04:21:56 +00:00
if instance.instant or run_after_save:
self.run_job(instance, serializer)
def perform_update(self, serializer):
2022-12-21 09:14:07 +00:00
run_after_save = serializer.validated_data.pop('run_after_save', False)
2022-12-02 04:21:56 +00:00
instance = serializer.save()
if run_after_save:
self.run_job(instance, serializer)
2022-12-21 09:14:07 +00:00
def run_job(self, job, serializer):
2022-12-02 04:21:56 +00:00
execution = job.create_execution()
2022-12-21 09:14:07 +00:00
execution.creator = self.request.user
execution.save()
2022-12-02 04:21:56 +00:00
task = run_ops_job_execution.delay(execution.id)
set_task_to_serializer_data(serializer, task)
2022-11-11 11:20:17 +00:00
2022-12-15 09:25:21 +00:00
class JobExecutionViewSet(OrgBulkModelViewSet):
2022-11-11 11:20:17 +00:00
serializer_class = JobExecutionSerializer
http_method_names = ('get', 'post', 'head', 'options',)
2022-11-23 16:50:37 +00:00
permission_classes = ()
2022-12-06 09:30:54 +00:00
model = JobExecution
2022-11-11 11:20:17 +00:00
def perform_create(self, serializer):
instance = serializer.save()
2022-12-20 11:46:19 +00:00
instance.job_version = instance.job.version
2022-12-21 09:14:07 +00:00
instance.creator = self.request.user
2022-12-20 11:46:19 +00:00
instance.save()
task = run_ops_job_execution.delay(instance.id)
set_task_to_serializer_data(serializer, task)
2022-11-11 11:20:17 +00:00
def get_queryset(self):
2022-12-15 09:25:21 +00:00
queryset = super().get_queryset()
queryset = queryset.filter(creator=self.request.user)
2022-11-11 11:20:17 +00:00
job_id = self.request.query_params.get('job_id')
if job_id:
2022-12-15 09:25:21 +00:00
queryset = queryset.filter(job_id=job_id)
return queryset
class JobRunVariableHelpAPIView(APIView):
rbac_perms = ()
permission_classes = ()
def get(self, request, **kwargs):
return Response(data=JMS_JOB_VARIABLE_HELP)
class JobAssetDetail(APIView):
rbac_perms = ()
permission_classes = ()
def get(self, request, **kwargs):
execution_id = request.query_params.get('execution_id')
if execution_id:
execution = get_object_or_404(JobExecution, id=execution_id)
return Response(data=execution.assent_result_detail)
2022-12-23 10:23:04 +00:00
class JobExecutionTaskDetail(APIView):
rbac_perms = ()
permission_classes = ()
def get(self, request, **kwargs):
org = get_current_org()
task_id = request.query_params.get('task_id')
if task_id:
with tmp_to_org(org):
execution = get_object_or_404(JobExecution, task_id=task_id)
return Response(data={
2022-12-27 08:06:21 +00:00
'status': execution.status,
2022-12-23 10:23:04 +00:00
'is_finished': execution.is_finished,
'is_success': execution.is_success,
'time_cost': execution.time_cost,
})
2022-12-26 11:02:05 +00:00
class FrequentUsernames(APIView):
rbac_perms = ()
permission_classes = ()
def get(self, request, **kwargs):
top_accounts = Account.objects.all().values('username').annotate(total=Count('username')).order_by('total')
return Response(data=top_accounts)