mirror of https://github.com/jumpserver/jumpserver
perf: 优化ops代码
parent
6233e2b3de
commit
d92b198a12
|
@ -1,9 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
|
||||
from rest_framework_bulk import BulkModelViewSet
|
||||
|
||||
from common.mixins import CommonApiMixin
|
||||
from .base import SelfBulkModelViewSet
|
||||
from ..models import AdHoc
|
||||
from ..serializers import (
|
||||
AdHocSerializer
|
||||
|
@ -14,9 +10,7 @@ __all__ = [
|
|||
]
|
||||
|
||||
|
||||
class AdHocViewSet(CommonApiMixin, BulkModelViewSet):
|
||||
class AdHocViewSet(SelfBulkModelViewSet):
|
||||
serializer_class = AdHocSerializer
|
||||
permission_classes = ()
|
||||
|
||||
def get_queryset(self):
|
||||
return AdHoc.objects.filter(creator=self.request.user)
|
||||
model = AdHoc
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
from rest_framework_bulk import BulkModelViewSet
|
||||
|
||||
from common.mixins import CommonApiMixin
|
||||
|
||||
__all__ = ['SelfBulkModelViewSet']
|
||||
|
||||
|
||||
class SelfBulkModelViewSet(CommonApiMixin, BulkModelViewSet):
|
||||
|
||||
def get_queryset(self):
|
||||
if hasattr(self, 'model'):
|
||||
return self.model.objects.filter(creator=self.request.user)
|
||||
else:
|
||||
assert self.queryset is None, (
|
||||
"'%s' should not include a `queryset` attribute"
|
||||
% self.__class__.__name__
|
||||
)
|
|
@ -2,6 +2,7 @@ from rest_framework import viewsets
|
|||
from rest_framework_bulk import BulkModelViewSet
|
||||
|
||||
from common.mixins import CommonApiMixin
|
||||
from ops.api.base import SelfBulkModelViewSet
|
||||
from ops.models import Job, JobExecution
|
||||
from ops.serializers.job import JobSerializer, JobExecutionSerializer
|
||||
|
||||
|
@ -16,12 +17,13 @@ def set_task_to_serializer_data(serializer, task):
|
|||
setattr(serializer, "_data", data)
|
||||
|
||||
|
||||
class JobViewSet(CommonApiMixin, BulkModelViewSet):
|
||||
class JobViewSet(SelfBulkModelViewSet):
|
||||
serializer_class = JobSerializer
|
||||
permission_classes = ()
|
||||
model = Job
|
||||
|
||||
def get_queryset(self):
|
||||
query_set = Job.objects.filter(creator=self.request.user)
|
||||
query_set = super().get_queryset()
|
||||
if self.action != 'retrieve':
|
||||
return query_set.filter(instant=False)
|
||||
return query_set
|
||||
|
@ -45,10 +47,11 @@ class JobViewSet(CommonApiMixin, BulkModelViewSet):
|
|||
set_task_to_serializer_data(serializer, task)
|
||||
|
||||
|
||||
class JobExecutionViewSet(CommonApiMixin, BulkModelViewSet):
|
||||
class JobExecutionViewSet(SelfBulkModelViewSet):
|
||||
serializer_class = JobExecutionSerializer
|
||||
http_method_names = ('get', 'post', 'head', 'options',)
|
||||
permission_classes = ()
|
||||
model = JobExecution
|
||||
|
||||
def perform_create(self, serializer):
|
||||
instance = serializer.save()
|
||||
|
@ -56,8 +59,7 @@ class JobExecutionViewSet(CommonApiMixin, BulkModelViewSet):
|
|||
set_task_to_serializer_data(serializer, task)
|
||||
|
||||
def get_queryset(self):
|
||||
query_set = JobExecution.objects.filter(creator=self.request.user)
|
||||
query_set = query_set.filter(creator=self.request.user)
|
||||
query_set = super().get_queryset()
|
||||
job_id = self.request.query_params.get('job_id')
|
||||
if job_id:
|
||||
query_set = query_set.filter(job_id=job_id)
|
||||
|
|
|
@ -6,6 +6,7 @@ from rest_framework_bulk import BulkModelViewSet
|
|||
|
||||
from common.mixins import CommonApiMixin
|
||||
from orgs.mixins.api import OrgBulkModelViewSet
|
||||
from .base import SelfBulkModelViewSet
|
||||
from ..exception import PlaybookNoValidEntry
|
||||
from ..models import Playbook
|
||||
from ..serializers.playbook import PlaybookSerializer
|
||||
|
@ -19,7 +20,7 @@ def unzip_playbook(src, dist):
|
|||
fz.extract(file, dist)
|
||||
|
||||
|
||||
class PlaybookViewSet(CommonApiMixin, BulkModelViewSet):
|
||||
class PlaybookViewSet(SelfBulkModelViewSet):
|
||||
serializer_class = PlaybookSerializer
|
||||
permission_classes = ()
|
||||
model = Playbook
|
||||
|
|
|
@ -28,23 +28,21 @@ logger = get_logger(__file__)
|
|||
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task"))
|
||||
def run_ops_job(job_id):
|
||||
job = get_object_or_none(Job, id=job_id)
|
||||
with tmp_to_org(job.org):
|
||||
execution = job.create_execution()
|
||||
run_ops_job_execution(execution)
|
||||
execution = job.create_execution()
|
||||
run_ops_job_execution(execution)
|
||||
|
||||
|
||||
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task execution"))
|
||||
def run_ops_job_execution(execution_id, **kwargs):
|
||||
execution = get_object_or_none(JobExecution, id=execution_id)
|
||||
with tmp_to_org(execution.org):
|
||||
try:
|
||||
execution.start()
|
||||
except SoftTimeLimitExceeded:
|
||||
execution.set_error('Run timeout')
|
||||
logger.error("Run adhoc timeout")
|
||||
except Exception as e:
|
||||
execution.set_error(e)
|
||||
logger.error("Start adhoc execution error: {}".format(e))
|
||||
try:
|
||||
execution.start()
|
||||
except SoftTimeLimitExceeded:
|
||||
execution.set_error('Run timeout')
|
||||
logger.error("Run adhoc timeout")
|
||||
except Exception as e:
|
||||
execution.set_error(e)
|
||||
logger.error("Start adhoc execution error: {}".format(e))
|
||||
|
||||
|
||||
@shared_task(verbose_name=_('Periodic clear celery tasks'))
|
||||
|
|
|
@ -4,7 +4,7 @@ import uuid
|
|||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.utils import get_logger, get_object_or_none
|
||||
from common.utils import get_logger, get_object_or_none, make_dirs
|
||||
from orgs.utils import org_aware_func
|
||||
from jumpserver.const import PROJECT_DIR
|
||||
|
||||
|
|
Loading…
Reference in New Issue