mirror of https://github.com/jumpserver/jumpserver
perf: 优化ops代码
parent
6233e2b3de
commit
d92b198a12
|
@ -1,9 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
from .base import SelfBulkModelViewSet
|
||||||
|
|
||||||
from rest_framework_bulk import BulkModelViewSet
|
|
||||||
|
|
||||||
from common.mixins import CommonApiMixin
|
|
||||||
from ..models import AdHoc
|
from ..models import AdHoc
|
||||||
from ..serializers import (
|
from ..serializers import (
|
||||||
AdHocSerializer
|
AdHocSerializer
|
||||||
|
@ -14,9 +10,7 @@ __all__ = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class AdHocViewSet(CommonApiMixin, BulkModelViewSet):
|
class AdHocViewSet(SelfBulkModelViewSet):
|
||||||
serializer_class = AdHocSerializer
|
serializer_class = AdHocSerializer
|
||||||
permission_classes = ()
|
permission_classes = ()
|
||||||
|
model = AdHoc
|
||||||
def get_queryset(self):
|
|
||||||
return AdHoc.objects.filter(creator=self.request.user)
|
|
||||||
|
|
|
@ -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 rest_framework_bulk import BulkModelViewSet
|
||||||
|
|
||||||
from common.mixins import CommonApiMixin
|
from common.mixins import CommonApiMixin
|
||||||
|
from ops.api.base import SelfBulkModelViewSet
|
||||||
from ops.models import Job, JobExecution
|
from ops.models import Job, JobExecution
|
||||||
from ops.serializers.job import JobSerializer, JobExecutionSerializer
|
from ops.serializers.job import JobSerializer, JobExecutionSerializer
|
||||||
|
|
||||||
|
@ -16,12 +17,13 @@ def set_task_to_serializer_data(serializer, task):
|
||||||
setattr(serializer, "_data", data)
|
setattr(serializer, "_data", data)
|
||||||
|
|
||||||
|
|
||||||
class JobViewSet(CommonApiMixin, BulkModelViewSet):
|
class JobViewSet(SelfBulkModelViewSet):
|
||||||
serializer_class = JobSerializer
|
serializer_class = JobSerializer
|
||||||
permission_classes = ()
|
permission_classes = ()
|
||||||
|
model = Job
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
query_set = Job.objects.filter(creator=self.request.user)
|
query_set = super().get_queryset()
|
||||||
if self.action != 'retrieve':
|
if self.action != 'retrieve':
|
||||||
return query_set.filter(instant=False)
|
return query_set.filter(instant=False)
|
||||||
return query_set
|
return query_set
|
||||||
|
@ -45,10 +47,11 @@ class JobViewSet(CommonApiMixin, BulkModelViewSet):
|
||||||
set_task_to_serializer_data(serializer, task)
|
set_task_to_serializer_data(serializer, task)
|
||||||
|
|
||||||
|
|
||||||
class JobExecutionViewSet(CommonApiMixin, BulkModelViewSet):
|
class JobExecutionViewSet(SelfBulkModelViewSet):
|
||||||
serializer_class = JobExecutionSerializer
|
serializer_class = JobExecutionSerializer
|
||||||
http_method_names = ('get', 'post', 'head', 'options',)
|
http_method_names = ('get', 'post', 'head', 'options',)
|
||||||
permission_classes = ()
|
permission_classes = ()
|
||||||
|
model = JobExecution
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
def perform_create(self, serializer):
|
||||||
instance = serializer.save()
|
instance = serializer.save()
|
||||||
|
@ -56,8 +59,7 @@ class JobExecutionViewSet(CommonApiMixin, BulkModelViewSet):
|
||||||
set_task_to_serializer_data(serializer, task)
|
set_task_to_serializer_data(serializer, task)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
query_set = JobExecution.objects.filter(creator=self.request.user)
|
query_set = super().get_queryset()
|
||||||
query_set = query_set.filter(creator=self.request.user)
|
|
||||||
job_id = self.request.query_params.get('job_id')
|
job_id = self.request.query_params.get('job_id')
|
||||||
if job_id:
|
if job_id:
|
||||||
query_set = query_set.filter(job_id=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 common.mixins import CommonApiMixin
|
||||||
from orgs.mixins.api import OrgBulkModelViewSet
|
from orgs.mixins.api import OrgBulkModelViewSet
|
||||||
|
from .base import SelfBulkModelViewSet
|
||||||
from ..exception import PlaybookNoValidEntry
|
from ..exception import PlaybookNoValidEntry
|
||||||
from ..models import Playbook
|
from ..models import Playbook
|
||||||
from ..serializers.playbook import PlaybookSerializer
|
from ..serializers.playbook import PlaybookSerializer
|
||||||
|
@ -19,7 +20,7 @@ def unzip_playbook(src, dist):
|
||||||
fz.extract(file, dist)
|
fz.extract(file, dist)
|
||||||
|
|
||||||
|
|
||||||
class PlaybookViewSet(CommonApiMixin, BulkModelViewSet):
|
class PlaybookViewSet(SelfBulkModelViewSet):
|
||||||
serializer_class = PlaybookSerializer
|
serializer_class = PlaybookSerializer
|
||||||
permission_classes = ()
|
permission_classes = ()
|
||||||
model = Playbook
|
model = Playbook
|
||||||
|
|
|
@ -28,7 +28,6 @@ logger = get_logger(__file__)
|
||||||
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task"))
|
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task"))
|
||||||
def run_ops_job(job_id):
|
def run_ops_job(job_id):
|
||||||
job = get_object_or_none(Job, id=job_id)
|
job = get_object_or_none(Job, id=job_id)
|
||||||
with tmp_to_org(job.org):
|
|
||||||
execution = job.create_execution()
|
execution = job.create_execution()
|
||||||
run_ops_job_execution(execution)
|
run_ops_job_execution(execution)
|
||||||
|
|
||||||
|
@ -36,7 +35,6 @@ def run_ops_job(job_id):
|
||||||
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task execution"))
|
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task execution"))
|
||||||
def run_ops_job_execution(execution_id, **kwargs):
|
def run_ops_job_execution(execution_id, **kwargs):
|
||||||
execution = get_object_or_none(JobExecution, id=execution_id)
|
execution = get_object_or_none(JobExecution, id=execution_id)
|
||||||
with tmp_to_org(execution.org):
|
|
||||||
try:
|
try:
|
||||||
execution.start()
|
execution.start()
|
||||||
except SoftTimeLimitExceeded:
|
except SoftTimeLimitExceeded:
|
||||||
|
|
|
@ -4,7 +4,7 @@ import uuid
|
||||||
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
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 orgs.utils import org_aware_func
|
||||||
from jumpserver.const import PROJECT_DIR
|
from jumpserver.const import PROJECT_DIR
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue