mirror of https://github.com/jumpserver/jumpserver
feat: ops 支持节点和资产
parent
dd630f0e14
commit
9ba792cf1c
|
@ -13,7 +13,8 @@ class AdHocRunner:
|
||||||
"reboot", 'shutdown', 'poweroff', 'halt', 'dd', 'half', 'top'
|
"reboot", 'shutdown', 'poweroff', 'halt', 'dd', 'half', 'top'
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, inventory, module, module_args='', pattern='*', project_dir='/tmp/', extra_vars={}):
|
def __init__(self, inventory, module, module_args='', pattern='*', project_dir='/tmp/', extra_vars={},
|
||||||
|
dry_run=False):
|
||||||
self.id = uuid.uuid4()
|
self.id = uuid.uuid4()
|
||||||
self.inventory = inventory
|
self.inventory = inventory
|
||||||
self.pattern = pattern
|
self.pattern = pattern
|
||||||
|
@ -23,6 +24,7 @@ class AdHocRunner:
|
||||||
self.cb = DefaultCallback()
|
self.cb = DefaultCallback()
|
||||||
self.runner = None
|
self.runner = None
|
||||||
self.extra_vars = extra_vars
|
self.extra_vars = extra_vars
|
||||||
|
self.dry_run = dry_run
|
||||||
|
|
||||||
def check_module(self):
|
def check_module(self):
|
||||||
if self.module not in self.cmd_modules_choices:
|
if self.module not in self.cmd_modules_choices:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from django.db.models import Count
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
@ -5,12 +6,14 @@ from rest_framework.response import Response
|
||||||
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', 'JobRunVariableHelpAPIView', 'JobAssetDetail', 'JobExecutionTaskDetail']
|
__all__ = ['JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView',
|
||||||
|
'JobAssetDetail', 'JobExecutionTaskDetail','FrequentUsernames']
|
||||||
|
|
||||||
from ops.tasks import run_ops_job_execution
|
from ops.tasks import run_ops_job_execution
|
||||||
from ops.variables import JMS_JOB_VARIABLE_HELP
|
from ops.variables import JMS_JOB_VARIABLE_HELP
|
||||||
from orgs.mixins.api import OrgBulkModelViewSet
|
from orgs.mixins.api import OrgBulkModelViewSet
|
||||||
from orgs.utils import tmp_to_org, get_current_org_id, get_current_org
|
from orgs.utils import tmp_to_org, get_current_org_id, get_current_org
|
||||||
|
from assets.models import Account
|
||||||
|
|
||||||
|
|
||||||
def set_task_to_serializer_data(serializer, task):
|
def set_task_to_serializer_data(serializer, task):
|
||||||
|
@ -111,3 +114,12 @@ class JobExecutionTaskDetail(APIView):
|
||||||
'is_success': execution.is_success,
|
'is_success': execution.is_success,
|
||||||
'time_cost': execution.time_cost,
|
'time_cost': execution.time_cost,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
|
@ -166,6 +166,10 @@ class JobExecution(JMSOrgBaseModel):
|
||||||
return
|
return
|
||||||
result = self.current_job.args
|
result = self.current_job.args
|
||||||
result += " chdir={}".format(self.current_job.chdir)
|
result += " chdir={}".format(self.current_job.chdir)
|
||||||
|
|
||||||
|
if self.current_job.module in ['python']:
|
||||||
|
result += " executable={}".format(self.current_job.module)
|
||||||
|
print(result)
|
||||||
return self.job.args
|
return self.job.args
|
||||||
|
|
||||||
def get_runner(self):
|
def get_runner(self):
|
||||||
|
@ -187,9 +191,17 @@ class JobExecution(JMSOrgBaseModel):
|
||||||
|
|
||||||
if self.current_job.type == 'adhoc':
|
if self.current_job.type == 'adhoc':
|
||||||
args = self.compile_shell()
|
args = self.compile_shell()
|
||||||
|
module = "shell"
|
||||||
|
if self.current_job.module not in ['python']:
|
||||||
|
module = self.current_job.module
|
||||||
|
|
||||||
runner = AdHocRunner(
|
runner = AdHocRunner(
|
||||||
self.inventory_path, self.current_job.module, module_args=args,
|
self.inventory_path,
|
||||||
pattern="all", project_dir=self.private_dir, extra_vars=extra_vars,
|
module,
|
||||||
|
module_args=args,
|
||||||
|
pattern="all",
|
||||||
|
project_dir=self.private_dir,
|
||||||
|
extra_vars=extra_vars,
|
||||||
)
|
)
|
||||||
elif self.current_job.type == 'playbook':
|
elif self.current_job.type == 'playbook':
|
||||||
runner = PlaybookRunner(
|
runner = PlaybookRunner(
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from assets.models import Node
|
||||||
from common.drf.fields import ReadableHiddenField
|
from common.drf.fields import ReadableHiddenField
|
||||||
from ops.mixin import PeriodTaskSerializerMixin
|
from ops.mixin import PeriodTaskSerializerMixin
|
||||||
from ops.models import Job, JobExecution
|
from ops.models import Job, JobExecution
|
||||||
|
@ -10,6 +12,16 @@ from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||||
class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
|
class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
|
||||||
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
||||||
run_after_save = serializers.BooleanField(label=_("Run after save"), default=False, required=False)
|
run_after_save = serializers.BooleanField(label=_("Run after save"), default=False, required=False)
|
||||||
|
nodes = serializers.ListField(required=False, child=serializers.CharField())
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
assets = validated_data.__getitem__('assets')
|
||||||
|
node_ids = validated_data.pop('nodes')
|
||||||
|
if node_ids:
|
||||||
|
nodes = Node.objects.filter(id__in=node_ids)
|
||||||
|
assets.extend(
|
||||||
|
Node.get_nodes_all_assets(*nodes).exclude(id__in=[asset.id for asset in assets]))
|
||||||
|
return super().create(validated_data)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Job
|
model = Job
|
||||||
|
@ -22,7 +34,7 @@ class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
|
||||||
"chdir",
|
"chdir",
|
||||||
"comment",
|
"comment",
|
||||||
"summary",
|
"summary",
|
||||||
"is_periodic", "interval", "crontab", "run_after_save"
|
"is_periodic", "interval", "crontab", "run_after_save", "nodes"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ urlpatterns = [
|
||||||
path('variables/help/', api.JobRunVariableHelpAPIView.as_view(), name='variable-help'),
|
path('variables/help/', api.JobRunVariableHelpAPIView.as_view(), name='variable-help'),
|
||||||
path('job-execution/asset-detail/', api.JobAssetDetail.as_view(), name='asset-detail'),
|
path('job-execution/asset-detail/', api.JobAssetDetail.as_view(), name='asset-detail'),
|
||||||
path('job-execution/task-detail/', api.JobExecutionTaskDetail.as_view(), name='task-detail'),
|
path('job-execution/task-detail/', api.JobExecutionTaskDetail.as_view(), name='task-detail'),
|
||||||
|
path('frequent-username/', api.FrequentUsernames.as_view(), name='frequent-usernames'),
|
||||||
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'),
|
||||||
|
|
||||||
path('celery/task/<uuid:name>/task-execution/<uuid:pk>/log/', api.CeleryTaskExecutionLogApi.as_view(),
|
path('celery/task/<uuid:name>/task-execution/<uuid:pk>/log/', api.CeleryTaskExecutionLogApi.as_view(),
|
||||||
|
|
Loading…
Reference in New Issue