2023-02-13 11:22:52 +00:00
|
|
|
import uuid
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-11 11:20:17 +00:00
|
|
|
from rest_framework import serializers
|
2022-12-26 11:02:05 +00:00
|
|
|
|
2023-02-22 07:35:59 +00:00
|
|
|
from assets.models import Node, Asset
|
2023-04-18 11:51:36 +00:00
|
|
|
from perms.models import PermNode
|
2023-01-16 11:02:09 +00:00
|
|
|
from perms.utils.user_perm import UserPermAssetUtil
|
|
|
|
from common.serializers.fields import ReadableHiddenField
|
2022-11-18 10:46:48 +00:00
|
|
|
from ops.mixin import PeriodTaskSerializerMixin
|
2022-11-11 11:20:17 +00:00
|
|
|
from ops.models import Job, JobExecution
|
2022-12-15 09:25:21 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2022-11-11 11:20:17 +00:00
|
|
|
|
|
|
|
|
2022-12-15 09:25:21 +00:00
|
|
|
class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
|
2022-12-06 03:03:14 +00:00
|
|
|
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
2022-12-21 09:14:07 +00:00
|
|
|
run_after_save = serializers.BooleanField(label=_("Run after save"), default=False, required=False)
|
2022-12-26 11:02:05 +00:00
|
|
|
nodes = serializers.ListField(required=False, child=serializers.CharField())
|
2023-01-16 11:02:09 +00:00
|
|
|
date_last_run = serializers.DateTimeField(label=_('Date last run'), read_only=True)
|
2023-02-13 11:22:52 +00:00
|
|
|
name = serializers.CharField(label=_('Name'), max_length=128, allow_blank=True, required=False)
|
2023-02-22 10:06:46 +00:00
|
|
|
assets = serializers.PrimaryKeyRelatedField(label=_('Assets'), queryset=Asset.objects, many=True,
|
2023-02-22 07:35:59 +00:00
|
|
|
required=False)
|
2023-02-13 11:22:52 +00:00
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
instant = data.get('instant', False)
|
|
|
|
if instant:
|
|
|
|
_uid = str(uuid.uuid4()).split('-')[-1]
|
|
|
|
data['name'] = f'job-{_uid}'
|
|
|
|
return super().to_internal_value(data)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
def get_request_user(self):
|
|
|
|
request = self.context.get('request')
|
|
|
|
user = request.user if request else None
|
|
|
|
return user
|
2022-12-26 11:02:05 +00:00
|
|
|
|
2022-11-11 11:20:17 +00:00
|
|
|
class Meta:
|
|
|
|
model = Job
|
2023-01-16 11:02:09 +00:00
|
|
|
read_only_fields = [
|
|
|
|
"id", "date_last_run", "date_created", "date_updated", "average_time_cost"
|
|
|
|
]
|
2022-11-18 10:46:48 +00:00
|
|
|
fields = read_only_fields + [
|
2023-01-16 11:02:09 +00:00
|
|
|
"name", "instant", "type", "module",
|
|
|
|
"args", "playbook", "assets",
|
|
|
|
"runas_policy", "runas", "creator",
|
|
|
|
"use_parameter_define", "parameters_define",
|
|
|
|
"timeout", "chdir", "comment", "summary",
|
|
|
|
"is_periodic", "interval", "crontab", "nodes",
|
|
|
|
"run_after_save",
|
2022-11-11 11:20:17 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-12-15 09:25:21 +00:00
|
|
|
class JobExecutionSerializer(BulkOrgResourceModelSerializer):
|
2022-12-06 03:03:14 +00:00
|
|
|
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
2022-12-06 11:43:32 +00:00
|
|
|
job_type = serializers.ReadOnlyField(label=_("Job type"))
|
2023-01-16 11:02:09 +00:00
|
|
|
material = serializers.ReadOnlyField(label=_("Command"))
|
|
|
|
is_success = serializers.ReadOnlyField(label=_("Is success"))
|
|
|
|
is_finished = serializers.ReadOnlyField(label=_("Is finished"))
|
|
|
|
time_cost = serializers.ReadOnlyField(label=_("Time cost"))
|
2022-12-06 03:03:14 +00:00
|
|
|
|
2022-11-11 11:20:17 +00:00
|
|
|
class Meta:
|
|
|
|
model = JobExecution
|
2023-01-16 11:02:09 +00:00
|
|
|
read_only_fields = ["id", "task_id", "timedelta", "time_cost",
|
|
|
|
'is_finished', 'date_start', 'date_finished',
|
2023-02-09 09:00:54 +00:00
|
|
|
'date_created', 'is_success', 'job_type',
|
2023-01-16 11:02:09 +00:00
|
|
|
'summary', 'material']
|
2022-11-11 11:20:17 +00:00
|
|
|
fields = read_only_fields + [
|
2022-12-16 10:39:59 +00:00
|
|
|
"job", "parameters", "creator"
|
2022-11-11 11:20:17 +00:00
|
|
|
]
|