feat: 增加运行的内置变量,优化self资源的代码

This commit is contained in:
Aaron3S 2022-12-06 19:43:32 +08:00
parent d92b198a12
commit 07b3774d3d
9 changed files with 14055 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -1,14 +1,15 @@
from rest_framework import viewsets from rest_framework.views import APIView
from rest_framework_bulk import BulkModelViewSet
from rest_framework.response import Response
from common.mixins import CommonApiMixin
from ops.api.base import SelfBulkModelViewSet 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):
@ -64,3 +65,11 @@ class JobExecutionViewSet(SelfBulkModelViewSet):
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)

View File

@ -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]

View File

@ -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

View File

@ -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'),

33
apps/ops/variables.py Normal file
View File

@ -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'),
}