mirror of https://github.com/jumpserver/jumpserver
Merge branch 'v3' of github.com:jumpserver/jumpserver into v3
commit
47c405dee5
|
@ -24,7 +24,7 @@ class DomainSerializer(BulkOrgResourceModelSerializer):
|
|||
model = Domain
|
||||
fields_mini = ['id', 'name']
|
||||
fields_small = fields_mini + ['comment']
|
||||
fields_m2m = ['nodes', 'assets']
|
||||
fields_m2m = ['assets']
|
||||
read_only_fields = ['asset_count', 'gateway_count', 'date_created']
|
||||
fields = fields_small + fields_m2m + read_only_fields
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#
|
||||
|
||||
from rest_framework import viewsets
|
||||
|
||||
from orgs.mixins.api import OrgBulkModelViewSet
|
||||
from ..models import AdHoc
|
||||
from ..serializers import (
|
||||
AdHocSerializer
|
||||
|
@ -12,6 +14,7 @@ __all__ = [
|
|||
]
|
||||
|
||||
|
||||
class AdHocViewSet(viewsets.ModelViewSet):
|
||||
queryset = AdHoc.objects.all()
|
||||
class AdHocViewSet(OrgBulkModelViewSet):
|
||||
serializer_class = AdHocSerializer
|
||||
permission_classes = ()
|
||||
model = AdHoc
|
||||
|
|
|
@ -5,10 +5,16 @@ from ops.serializers.job import JobSerializer, JobExecutionSerializer
|
|||
|
||||
__all__ = ['JobViewSet', 'JobExecutionViewSet']
|
||||
|
||||
from ops.tasks import run_ops_job, run_ops_job_executions
|
||||
from ops.tasks import run_ops_job_execution
|
||||
from orgs.mixins.api import OrgBulkModelViewSet
|
||||
|
||||
|
||||
def set_task_to_serializer_data(serializer, task):
|
||||
data = getattr(serializer, "_data", {})
|
||||
data["task_id"] = task.id
|
||||
setattr(serializer, "_data", data)
|
||||
|
||||
|
||||
class JobViewSet(OrgBulkModelViewSet):
|
||||
serializer_class = JobSerializer
|
||||
model = Job
|
||||
|
@ -23,23 +29,25 @@ class JobViewSet(OrgBulkModelViewSet):
|
|||
def perform_create(self, serializer):
|
||||
instance = serializer.save()
|
||||
if instance.instant:
|
||||
run_ops_job.delay(instance.id)
|
||||
execution = instance.create_execution()
|
||||
task = run_ops_job_execution.delay(execution.id)
|
||||
set_task_to_serializer_data(serializer, task)
|
||||
|
||||
|
||||
class JobExecutionViewSet(OrgBulkModelViewSet):
|
||||
serializer_class = JobExecutionSerializer
|
||||
http_method_names = ('get', 'post', 'head', 'options',)
|
||||
# filter_fields = ('type',)
|
||||
permission_classes = ()
|
||||
model = JobExecution
|
||||
|
||||
def perform_create(self, serializer):
|
||||
instance = serializer.save()
|
||||
run_ops_job_executions.delay(instance.id)
|
||||
task = run_ops_job_execution.delay(instance.id)
|
||||
set_task_to_serializer_data(serializer, task)
|
||||
|
||||
def get_queryset(self):
|
||||
query_set = super().get_queryset()
|
||||
job_id = self.request.query_params.get('job_id')
|
||||
if job_id:
|
||||
self.queryset = query_set.filter(job_id=job_id)
|
||||
query_set = query_set.filter(job_id=job_id)
|
||||
return query_set
|
||||
|
|
|
@ -2,7 +2,8 @@ import os
|
|||
import zipfile
|
||||
|
||||
from django.conf import settings
|
||||
from rest_framework import viewsets
|
||||
|
||||
from orgs.mixins.api import OrgBulkModelViewSet
|
||||
from ..models import Playbook
|
||||
from ..serializers.playbook import PlaybookSerializer
|
||||
|
||||
|
@ -15,9 +16,10 @@ def unzip_playbook(src, dist):
|
|||
fz.extract(file, dist)
|
||||
|
||||
|
||||
class PlaybookViewSet(viewsets.ModelViewSet):
|
||||
queryset = Playbook.objects.all()
|
||||
class PlaybookViewSet(OrgBulkModelViewSet):
|
||||
serializer_class = PlaybookSerializer
|
||||
permission_classes = ()
|
||||
model = Playbook
|
||||
|
||||
def perform_create(self, serializer):
|
||||
instance = serializer.save()
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
# Generated by Django 3.2.14 on 2022-11-29 07:29
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ops', '0035_jobexecution_org_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='job',
|
||||
options={'ordering': ['date_created']},
|
||||
),
|
||||
migrations.AlterModelOptions(
|
||||
name='jobexecution',
|
||||
options={'ordering': ['-date_created']},
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='job',
|
||||
name='use_parameter_define',
|
||||
field=models.BooleanField(default=False, verbose_name='Use Parameter Define'),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,26 @@
|
|||
# Generated by Django 3.2.14 on 2022-11-29 11:26
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ops', '0036_auto_20221129_1529'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='adhoc',
|
||||
old_name='owner',
|
||||
new_name='creator',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='adhoc',
|
||||
name='org_id',
|
||||
field=models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization'),
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='AdHocExecution',
|
||||
),
|
||||
]
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 3.2.14 on 2022-11-29 11:31
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ops', '0037_auto_20221129_1926'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='playbook',
|
||||
name='org_id',
|
||||
field=models.CharField(blank=True, db_index=True, default='', max_length=36, verbose_name='Organization'),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,25 @@
|
|||
# Generated by Django 3.2.14 on 2022-11-29 11:32
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('ops', '0038_playbook_org_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='playbook',
|
||||
name='owner',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='playbook',
|
||||
name='creator',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL, verbose_name='Creator'),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,14 @@
|
|||
# Generated by Django 3.2.14 on 2022-11-29 11:47
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ops', '0036_auto_20221128_1839'),
|
||||
('ops', '0039_auto_20221129_1932'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
]
|
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 3.2.14 on 2022-11-29 11:52
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ops', '0040_merge_0036_auto_20221128_1839_0039_auto_20221129_1932'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='adhoc',
|
||||
name='comment',
|
||||
field=models.CharField(blank=True, default='', max_length=1024, null=True, verbose_name='Comment'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='playbook',
|
||||
name='comment',
|
||||
field=models.CharField(blank=True, default='', max_length=1024, null=True, verbose_name='Comment'),
|
||||
),
|
||||
]
|
|
@ -1,21 +1,18 @@
|
|||
# ~*~ coding: utf-8 ~*~
|
||||
import os.path
|
||||
import uuid
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.db.models import BaseCreateUpdateModel
|
||||
from common.utils import get_logger
|
||||
from .base import BaseAnsibleJob, BaseAnsibleExecution
|
||||
from ..ansible import AdHocRunner
|
||||
from orgs.mixins.models import JMSOrgBaseModel
|
||||
|
||||
__all__ = ["AdHoc", "AdHocExecution"]
|
||||
__all__ = ["AdHoc"]
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
|
||||
class AdHoc(BaseCreateUpdateModel):
|
||||
class AdHoc(JMSOrgBaseModel):
|
||||
class Modules(models.TextChoices):
|
||||
shell = 'shell', _('Shell')
|
||||
winshell = 'win_shell', _('Powershell')
|
||||
|
@ -26,7 +23,9 @@ class AdHoc(BaseCreateUpdateModel):
|
|||
module = models.CharField(max_length=128, choices=Modules.choices, default=Modules.shell,
|
||||
verbose_name=_('Module'))
|
||||
args = models.CharField(max_length=1024, default='', verbose_name=_('Args'))
|
||||
owner = models.ForeignKey('users.User', verbose_name=_("Creator"), on_delete=models.SET_NULL, null=True)
|
||||
creator = models.ForeignKey('users.User', verbose_name=_("Creator"), on_delete=models.SET_NULL, null=True)
|
||||
comment = models.CharField(max_length=1024, default='', verbose_name=_('Comment'), null=True, blank=True)
|
||||
|
||||
|
||||
@property
|
||||
def row_count(self):
|
||||
|
@ -41,28 +40,3 @@ class AdHoc(BaseCreateUpdateModel):
|
|||
|
||||
def __str__(self):
|
||||
return "{}: {}".format(self.module, self.args)
|
||||
|
||||
|
||||
class AdHocExecution(BaseAnsibleExecution):
|
||||
"""
|
||||
AdHoc running history.
|
||||
"""
|
||||
task = models.ForeignKey('AdHoc', verbose_name=_("Adhoc"), related_name='executions', on_delete=models.CASCADE)
|
||||
|
||||
def get_runner(self):
|
||||
inv = self.task.inventory
|
||||
inv.write_to_file(self.inventory_path)
|
||||
|
||||
runner = AdHocRunner(
|
||||
self.inventory_path, self.task.module, module_args=self.task.args,
|
||||
pattern=self.task.pattern, project_dir=self.private_dir
|
||||
)
|
||||
return runner
|
||||
|
||||
def task_display(self):
|
||||
return str(self.task)
|
||||
|
||||
class Meta:
|
||||
db_table = "ops_adhoc_execution"
|
||||
get_latest_by = 'date_start'
|
||||
verbose_name = _("AdHoc execution")
|
||||
|
|
|
@ -45,6 +45,7 @@ class Job(JMSOrgBaseModel, PeriodTaskModelMixin):
|
|||
runas = models.CharField(max_length=128, default='root', verbose_name=_('Runas'))
|
||||
runas_policy = models.CharField(max_length=128, choices=RunasPolicies.choices, default=RunasPolicies.skip,
|
||||
verbose_name=_('Runas policy'))
|
||||
use_parameter_define = models.BooleanField(default=False, verbose_name=(_('Use Parameter Define')))
|
||||
parameters_define = models.JSONField(default=dict, verbose_name=_('Parameters define'))
|
||||
comment = models.CharField(max_length=1024, default='', verbose_name=_('Comment'), null=True, blank=True)
|
||||
|
||||
|
@ -77,9 +78,9 @@ class Job(JMSOrgBaseModel, PeriodTaskModelMixin):
|
|||
return total_cost / finished_count if finished_count else 0
|
||||
|
||||
def get_register_task(self):
|
||||
from ..tasks import run_ops_job
|
||||
from ..tasks import run_ops_job_execution
|
||||
name = "run_ops_job_period_{}".format(str(self.id)[:8])
|
||||
task = run_ops_job.name
|
||||
task = run_ops_job_execution.name
|
||||
args = (str(self.id),)
|
||||
kwargs = {}
|
||||
return name, task, args, kwargs
|
||||
|
@ -108,6 +109,10 @@ class JobExecution(JMSOrgBaseModel):
|
|||
date_start = models.DateTimeField(null=True, verbose_name=_('Date start'), db_index=True)
|
||||
date_finished = models.DateTimeField(null=True, verbose_name=_("Date finished"))
|
||||
|
||||
@property
|
||||
def job_type(self):
|
||||
return self.job.type
|
||||
|
||||
def get_runner(self):
|
||||
inv = self.job.inventory
|
||||
inv.write_to_file(self.inventory_path)
|
||||
|
|
|
@ -5,14 +5,15 @@ from django.conf import settings
|
|||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from common.db.models import BaseCreateUpdateModel
|
||||
from orgs.mixins.models import JMSOrgBaseModel
|
||||
|
||||
|
||||
class Playbook(BaseCreateUpdateModel):
|
||||
class Playbook(JMSOrgBaseModel):
|
||||
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
||||
name = models.CharField(max_length=128, verbose_name=_('Name'), null=True)
|
||||
path = models.FileField(upload_to='playbooks/')
|
||||
owner = models.ForeignKey('users.User', verbose_name=_("Owner"), on_delete=models.SET_NULL, null=True)
|
||||
creator = models.ForeignKey('users.User', verbose_name=_("Creator"), on_delete=models.SET_NULL, null=True)
|
||||
comment = models.CharField(max_length=1024, default='', verbose_name=_('Comment'), null=True, blank=True)
|
||||
|
||||
@property
|
||||
def work_path(self):
|
||||
|
|
|
@ -6,70 +6,16 @@ import datetime
|
|||
from rest_framework import serializers
|
||||
|
||||
from common.drf.fields import ReadableHiddenField
|
||||
from ..models import AdHoc, AdHocExecution
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
from ..models import AdHoc
|
||||
|
||||
|
||||
class AdHocSerializer(serializers.ModelSerializer):
|
||||
owner = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
||||
class AdHocSerializer(BulkOrgResourceModelSerializer, serializers.ModelSerializer):
|
||||
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
||||
row_count = serializers.IntegerField(read_only=True)
|
||||
size = serializers.IntegerField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = AdHoc
|
||||
fields = ["id", "name", "module", "row_count", "size", "args", "owner", "date_created", "date_updated"]
|
||||
|
||||
|
||||
class AdHocExecutionSerializer(serializers.ModelSerializer):
|
||||
stat = serializers.SerializerMethodField()
|
||||
last_success = serializers.ListField(source='success_hosts')
|
||||
last_failure = serializers.DictField(source='failed_hosts')
|
||||
|
||||
class Meta:
|
||||
model = AdHocExecution
|
||||
fields_mini = ['id']
|
||||
fields_small = fields_mini + [
|
||||
'timedelta', 'result', 'summary', 'short_id',
|
||||
'is_finished', 'is_success',
|
||||
'date_start', 'date_finished',
|
||||
]
|
||||
fields_fk = ['task', 'task_display']
|
||||
fields_custom = ['stat', 'last_success', 'last_failure']
|
||||
fields = fields_small + fields_fk + fields_custom
|
||||
|
||||
@staticmethod
|
||||
def get_task(obj):
|
||||
return obj.task.id
|
||||
|
||||
@staticmethod
|
||||
def get_stat(obj):
|
||||
count_failed_hosts = len(obj.failed_hosts)
|
||||
count_success_hosts = len(obj.success_hosts)
|
||||
count_total = count_success_hosts + count_failed_hosts
|
||||
return {
|
||||
"total": count_total,
|
||||
"success": count_success_hosts,
|
||||
"failed": count_failed_hosts
|
||||
}
|
||||
|
||||
|
||||
class AdHocExecutionExcludeResultSerializer(AdHocExecutionSerializer):
|
||||
class Meta:
|
||||
model = AdHocExecution
|
||||
fields = [
|
||||
'id', 'task', 'task_display', 'hosts_amount', 'adhoc', 'date_start', 'stat',
|
||||
'date_finished', 'timedelta', 'is_finished', 'is_success',
|
||||
'short_id', 'adhoc_short_id', 'last_success', 'last_failure'
|
||||
]
|
||||
|
||||
|
||||
class AdHocExecutionNestSerializer(serializers.ModelSerializer):
|
||||
last_success = serializers.ListField(source='success_hosts')
|
||||
last_failure = serializers.DictField(source='failed_hosts')
|
||||
last_run = serializers.CharField(source='short_id')
|
||||
|
||||
class Meta:
|
||||
model = AdHocExecution
|
||||
fields = (
|
||||
'last_success', 'last_failure', 'last_run', 'timedelta',
|
||||
'is_finished', 'is_success'
|
||||
)
|
||||
fields = ["id", "name", "module", "row_count", "size", "args", "creator", "comment", "date_created",
|
||||
"date_updated"]
|
||||
|
|
|
@ -15,6 +15,7 @@ class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
|
|||
read_only_fields = ["id", "date_last_run", "date_created", "date_updated", "average_time_cost"]
|
||||
fields = read_only_fields + [
|
||||
"name", "instant", "type", "module", "args", "playbook", "assets", "runas_policy", "runas", "owner",
|
||||
"use_parameter_define",
|
||||
"parameters_define",
|
||||
"timeout",
|
||||
"chdir",
|
||||
|
@ -28,7 +29,7 @@ class JobExecutionSerializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = JobExecution
|
||||
read_only_fields = ["id", "task_id", "timedelta", "time_cost", 'is_finished', 'date_start', 'date_created',
|
||||
'is_success', 'task_id', 'short_id']
|
||||
'is_success', 'task_id', 'short_id', 'job_type']
|
||||
fields = read_only_fields + [
|
||||
"job", "parameters"
|
||||
]
|
||||
|
|
|
@ -4,6 +4,7 @@ from rest_framework import serializers
|
|||
|
||||
from common.drf.fields import ReadableHiddenField
|
||||
from ops.models import Playbook
|
||||
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
||||
|
||||
|
||||
def parse_playbook_name(path):
|
||||
|
@ -11,8 +12,8 @@ def parse_playbook_name(path):
|
|||
return file_name.split(".")[-2]
|
||||
|
||||
|
||||
class PlaybookSerializer(serializers.ModelSerializer):
|
||||
owner = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
||||
class PlaybookSerializer(BulkOrgResourceModelSerializer, serializers.ModelSerializer):
|
||||
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
||||
|
||||
def create(self, validated_data):
|
||||
name = validated_data.get('name')
|
||||
|
@ -24,5 +25,5 @@ class PlaybookSerializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = Playbook
|
||||
fields = [
|
||||
"id", "name", "path", "date_created", "owner", "date_updated"
|
||||
"id", "name", "path", "comment", "date_created", "creator", "date_updated"
|
||||
]
|
||||
|
|
|
@ -30,18 +30,11 @@ 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()
|
||||
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))
|
||||
run_ops_job_execution(execution)
|
||||
|
||||
|
||||
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task execution"))
|
||||
def run_ops_job_executions(execution_id, **kwargs):
|
||||
def run_ops_job_execution(execution_id, **kwargs):
|
||||
execution = get_object_or_none(JobExecution, id=execution_id)
|
||||
with tmp_to_org(execution.org):
|
||||
try:
|
||||
|
|
Loading…
Reference in New Issue