2017-03-22 16:15:25 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from rest_framework import serializers
|
2018-12-10 02:11:54 +00:00
|
|
|
from django.shortcuts import reverse
|
2017-03-22 16:15:25 +00:00
|
|
|
|
2020-11-04 06:53:49 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2020-03-12 08:24:38 +00:00
|
|
|
from ..models import Task, AdHoc, AdHocExecution, CommandExecution
|
2017-03-22 16:15:25 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AdHocExecutionSerializer(serializers.ModelSerializer):
|
2017-12-11 09:08:43 +00:00
|
|
|
stat = serializers.SerializerMethodField()
|
2020-06-15 11:42:36 +00:00
|
|
|
last_success = serializers.ListField(source='success_hosts')
|
|
|
|
last_failure = serializers.DictField(source='failed_hosts')
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-03-12 08:24:38 +00:00
|
|
|
model = AdHocExecution
|
2020-06-15 11:42:36 +00:00
|
|
|
fields = [
|
|
|
|
'id', 'task', 'task_display', 'hosts_amount', 'adhoc', 'date_start', 'stat',
|
|
|
|
'date_finished', 'timedelta', 'is_finished', 'is_success', 'result', 'summary',
|
|
|
|
'short_id', 'adhoc_short_id', 'last_success', 'last_failure'
|
|
|
|
]
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_task(obj):
|
2019-12-18 07:37:53 +00:00
|
|
|
return obj.task.id
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2017-12-11 09:08:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_stat(obj):
|
2020-05-19 07:15:17 +00:00
|
|
|
count_failed_hosts = len(obj.failed_hosts)
|
|
|
|
count_success_hosts = len(obj.success_hosts)
|
|
|
|
count_total = count_success_hosts + count_failed_hosts
|
2017-12-11 09:08:43 +00:00
|
|
|
return {
|
2020-05-19 07:15:17 +00:00
|
|
|
"total": count_total,
|
|
|
|
"success": count_success_hosts,
|
|
|
|
"failed": count_failed_hosts
|
2017-12-11 09:08:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AdHocExecutionExcludeResultSerializer(AdHocExecutionSerializer):
|
2020-06-15 11:42:36 +00:00
|
|
|
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'
|
|
|
|
]
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
|
2020-11-04 06:53:49 +00:00
|
|
|
class TaskSerializer(BulkOrgResourceModelSerializer):
|
2020-03-18 11:58:08 +00:00
|
|
|
summary = serializers.ReadOnlyField(source='history_summary')
|
2020-03-12 08:24:38 +00:00
|
|
|
latest_execution = AdHocExecutionExcludeResultSerializer(read_only=True)
|
2019-12-18 07:37:53 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Task
|
|
|
|
fields = [
|
|
|
|
'id', 'name', 'interval', 'crontab', 'is_periodic',
|
2020-03-12 08:24:38 +00:00
|
|
|
'is_deleted', 'comment', 'date_created',
|
2020-03-18 11:58:08 +00:00
|
|
|
'date_updated', 'latest_execution', 'summary',
|
2019-12-18 07:37:53 +00:00
|
|
|
]
|
|
|
|
read_only_fields = [
|
2020-03-12 08:24:38 +00:00
|
|
|
'is_deleted', 'date_created', 'date_updated',
|
|
|
|
'latest_adhoc', 'latest_execution', 'total_run_amount',
|
2020-03-18 11:58:08 +00:00
|
|
|
'success_run_amount', 'summary',
|
2019-12-18 07:37:53 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-06-09 12:26:23 +00:00
|
|
|
class TaskDetailSerializer(TaskSerializer):
|
2020-06-15 11:42:36 +00:00
|
|
|
contents = serializers.ListField(source='latest_adhoc.tasks')
|
2020-06-09 12:26:23 +00:00
|
|
|
|
|
|
|
class Meta(TaskSerializer.Meta):
|
2020-06-15 11:42:36 +00:00
|
|
|
fields = TaskSerializer.Meta.fields + ['contents']
|
2020-06-09 12:26:23 +00:00
|
|
|
|
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
class AdHocSerializer(serializers.ModelSerializer):
|
|
|
|
become_display = serializers.ReadOnlyField()
|
2020-06-15 11:42:36 +00:00
|
|
|
tasks = serializers.ListField()
|
2019-12-18 07:37:53 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdHoc
|
|
|
|
fields = [
|
|
|
|
"id", "task", 'tasks', "pattern", "options",
|
|
|
|
"hosts", "run_as_admin", "run_as", "become",
|
2020-03-12 08:24:38 +00:00
|
|
|
"date_created", "short_id",
|
2019-12-18 07:37:53 +00:00
|
|
|
"become_display",
|
|
|
|
]
|
|
|
|
read_only_fields = [
|
2020-03-12 08:24:38 +00:00
|
|
|
'date_created'
|
2019-12-18 07:37:53 +00:00
|
|
|
]
|
|
|
|
extra_kwargs = {
|
|
|
|
"become": {'write_only': True}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-15 11:42:36 +00:00
|
|
|
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'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class AdHocDetailSerializer(AdHocSerializer):
|
|
|
|
latest_execution = AdHocExecutionNestSerializer(allow_null=True)
|
|
|
|
task_name = serializers.CharField(source='task.name')
|
|
|
|
|
|
|
|
class Meta(AdHocSerializer.Meta):
|
|
|
|
fields = AdHocSerializer.Meta.fields + [
|
|
|
|
'latest_execution', 'created_by', 'run_times', 'task_name'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
class CommandExecutionSerializer(serializers.ModelSerializer):
|
|
|
|
result = serializers.JSONField(read_only=True)
|
|
|
|
log_url = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = CommandExecution
|
|
|
|
fields = [
|
|
|
|
'id', 'hosts', 'run_as', 'command', 'result', 'log_url',
|
|
|
|
'is_finished', 'date_created', 'date_finished'
|
|
|
|
]
|
|
|
|
read_only_fields = [
|
2019-03-19 11:09:09 +00:00
|
|
|
'result', 'is_finished', 'log_url', 'date_created',
|
2018-12-10 02:11:54 +00:00
|
|
|
'date_finished'
|
|
|
|
]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_log_url(obj):
|
|
|
|
return reverse('api-ops:celery-task-log', kwargs={'pk': obj.id})
|
2019-12-05 07:09:25 +00:00
|
|
|
|