mirror of https://github.com/jumpserver/jumpserver
Merge branch 'v3' of github.com:jumpserver/jumpserver into v3
commit
1cab84bb62
|
@ -1,3 +1,3 @@
|
||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:0818af791dad7cd50e19c41de0bc8967f9d08f949f48d5c2020786153a743349
|
oid sha256:5cc8f923c01a87b106a54f8a7c53abdb98683b1c4b4f975f9a3ae8af5fae73c8
|
||||||
size 116392
|
size 373
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,3 +1,3 @@
|
||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:1c09abdddb5699aeaf832e1162b58ea9b520c10df3f80390c0ec680da3e18f4d
|
oid sha256:6c0ba1103efe746ecf579fe27832b5d2969858508f4aabdcc42723b13c1b01f8
|
||||||
size 103641
|
size 383
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -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__
|
||||||
|
)
|
|
@ -1,13 +1,15 @@
|
||||||
from rest_framework import viewsets
|
from rest_framework.views import APIView
|
||||||
from rest_framework_bulk import BulkModelViewSet
|
|
||||||
|
|
||||||
from common.mixins import CommonApiMixin
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
__all__ = ['JobViewSet', 'JobExecutionViewSet']
|
__all__ = ['JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView']
|
||||||
|
|
||||||
from ops.tasks import run_ops_job_execution
|
from ops.tasks import run_ops_job_execution
|
||||||
|
from ops.variables import JMS_JOB_VARIABLE_HELP
|
||||||
|
|
||||||
|
|
||||||
def set_task_to_serializer_data(serializer, task):
|
def set_task_to_serializer_data(serializer, task):
|
||||||
|
@ -16,12 +18,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 +48,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,9 +60,16 @@ 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)
|
||||||
return query_set
|
return query_set
|
||||||
|
|
||||||
|
|
||||||
|
class JobRunVariableHelpAPIView(APIView):
|
||||||
|
rbac_perms = ()
|
||||||
|
permission_classes = ()
|
||||||
|
|
||||||
|
def get(self, request, **kwargs):
|
||||||
|
return Response(data=JMS_JOB_VARIABLE_HELP)
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -14,6 +14,7 @@ __all__ = ["Job", "JobExecution"]
|
||||||
from common.db.models import JMSBaseModel
|
from common.db.models import JMSBaseModel
|
||||||
from ops.ansible import JMSInventory, AdHocRunner, PlaybookRunner
|
from ops.ansible import JMSInventory, AdHocRunner, PlaybookRunner
|
||||||
from ops.mixin import PeriodTaskModelMixin
|
from ops.mixin import PeriodTaskModelMixin
|
||||||
|
from ops.variables import *
|
||||||
|
|
||||||
|
|
||||||
class Job(JMSBaseModel, PeriodTaskModelMixin):
|
class Job(JMSBaseModel, PeriodTaskModelMixin):
|
||||||
|
@ -128,6 +129,9 @@ class JobExecution(JMSBaseModel):
|
||||||
else:
|
else:
|
||||||
extra_vars = {}
|
extra_vars = {}
|
||||||
|
|
||||||
|
static_variables = self.gather_static_variables()
|
||||||
|
extra_vars.update(static_variables)
|
||||||
|
|
||||||
if self.job.type == 'adhoc':
|
if self.job.type == 'adhoc':
|
||||||
args = self.compile_shell()
|
args = self.compile_shell()
|
||||||
runner = AdHocRunner(
|
runner = AdHocRunner(
|
||||||
|
@ -142,6 +146,14 @@ class JobExecution(JMSBaseModel):
|
||||||
raise Exception("unsupported job type")
|
raise Exception("unsupported job type")
|
||||||
return runner
|
return runner
|
||||||
|
|
||||||
|
def gather_static_variables(self):
|
||||||
|
default = {
|
||||||
|
JMS_USERNAME: self.creator.username,
|
||||||
|
JMS_JOB_ID: self.job.id,
|
||||||
|
JMS_JOB_NAME: self.job.name,
|
||||||
|
}
|
||||||
|
return default
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def short_id(self):
|
def short_id(self):
|
||||||
return str(self.id).split('-')[-1]
|
return str(self.id).split('-')[-1]
|
||||||
|
|
|
@ -27,6 +27,7 @@ class JobSerializer(serializers.ModelSerializer, PeriodTaskSerializerMixin):
|
||||||
|
|
||||||
class JobExecutionSerializer(serializers.ModelSerializer):
|
class JobExecutionSerializer(serializers.ModelSerializer):
|
||||||
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
||||||
|
job_type = serializers.ReadOnlyField(label=_("Job type"))
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = JobExecution
|
model = JobExecution
|
||||||
|
|
|
@ -28,23 +28,21 @@ 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)
|
|
||||||
|
|
||||||
|
|
||||||
@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:
|
execution.set_error('Run timeout')
|
||||||
execution.set_error('Run timeout')
|
logger.error("Run adhoc timeout")
|
||||||
logger.error("Run adhoc timeout")
|
except Exception as e:
|
||||||
except Exception as e:
|
execution.set_error(e)
|
||||||
execution.set_error(e)
|
logger.error("Start adhoc execution error: {}".format(e))
|
||||||
logger.error("Start adhoc execution error: {}".format(e))
|
|
||||||
|
|
||||||
|
|
||||||
@shared_task(verbose_name=_('Periodic clear celery tasks'))
|
@shared_task(verbose_name=_('Periodic clear celery tasks'))
|
||||||
|
|
|
@ -23,6 +23,7 @@ router.register(r'tasks', api.CeleryTaskViewSet, 'task')
|
||||||
router.register(r'task-executions', api.CeleryTaskExecutionViewSet, 'task-executions')
|
router.register(r'task-executions', api.CeleryTaskExecutionViewSet, 'task-executions')
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
path('variables/help/', api.JobRunVariableHelpAPIView.as_view(), name='variable-help'),
|
||||||
|
|
||||||
path('ansible/job-execution/<uuid:pk>/log/', api.AnsibleTaskLogApi.as_view(), name='job-execution-log'),
|
path('ansible/job-execution/<uuid:pk>/log/', api.AnsibleTaskLogApi.as_view(), name='job-execution-log'),
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
# JumpServer
|
||||||
|
JMS_USERNAME = "jms_username"
|
||||||
|
|
||||||
|
# ASSENT
|
||||||
|
JMS_ASSET_ID = "jms_asset.id"
|
||||||
|
JMS_ASSET_TYPE = "jms_asset.type"
|
||||||
|
JMS_ASSET_CATEGORY = "jms_asset.category"
|
||||||
|
JMS_ASSET_PROTOCOL = "jms_asset.protocol"
|
||||||
|
JMS_ASSET_PORT = "jms_asset.port"
|
||||||
|
JMS_ASSET_NAME = "jms_asset.name"
|
||||||
|
JMS_ASSET_ADDRESS = "jms_asset.address"
|
||||||
|
|
||||||
|
# Account
|
||||||
|
JMS_ACCOUNT_ID = "jms_account.id"
|
||||||
|
JMS_ACCOUNT_USERNAME = "jms_account.name"
|
||||||
|
|
||||||
|
# JOB
|
||||||
|
JMS_JOB_ID = "jms_job_id"
|
||||||
|
JMS_JOB_NAME = "jms_job_name"
|
||||||
|
|
||||||
|
JMS_JOB_VARIABLE_HELP = {
|
||||||
|
JMS_USERNAME: _('The current user`s username of JumpServer'),
|
||||||
|
JMS_ASSET_ID: _('The id of the asset in the JumpServer'),
|
||||||
|
JMS_ASSET_TYPE: _('The type of the asset in the JumpServer'),
|
||||||
|
JMS_ASSET_CATEGORY: _('The category of the asset in the JumpServer'),
|
||||||
|
JMS_ASSET_NAME: _('The name of the asset in the JumpServer'),
|
||||||
|
JMS_ASSET_ADDRESS: _('Address used to connect this asset in JumpServer'),
|
||||||
|
JMS_ASSET_PORT: _('Port used to connect this asset in JumpServer'),
|
||||||
|
JMS_JOB_ID: _('ID of the job'),
|
||||||
|
JMS_JOB_NAME: _('Name of the job'),
|
||||||
|
}
|
Loading…
Reference in New Issue