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
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'hosts_amount', 'timedelta', 'result', 'summary', 'short_id',
|
|
|
|
'is_finished', 'is_success',
|
|
|
|
'date_start', 'date_finished',
|
2020-06-15 11:42:36 +00:00
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_fk = ['task', 'task_display', 'adhoc', 'adhoc_short_id',]
|
|
|
|
fields_custom = ['stat', 'last_success', 'last_failure']
|
|
|
|
fields = fields_small + fields_fk + fields_custom
|
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
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'interval', 'crontab',
|
|
|
|
'is_periodic', 'is_deleted',
|
|
|
|
'date_created', 'date_updated',
|
|
|
|
'comment',
|
2019-12-18 07:37:53 +00:00
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_fk = ['latest_execution']
|
|
|
|
fields_custom = ['summary']
|
|
|
|
fields = fields_small + fields_fk + fields_custom
|
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
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'tasks', "pattern", "options", "run_as",
|
|
|
|
"become", "become_display", "short_id",
|
|
|
|
"run_as_admin",
|
|
|
|
"date_created",
|
2019-12-18 07:37:53 +00:00
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_fk = ["task"]
|
|
|
|
fields_m2m = ["hosts"]
|
|
|
|
fields = fields_small + fields_fk + fields_m2m
|
2019-12-18 07:37:53 +00:00
|
|
|
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 = (
|
2021-04-29 11:10:45 +00:00
|
|
|
'last_success', 'last_failure', 'last_run', 'timedelta',
|
|
|
|
'is_finished', 'is_success'
|
2020-06-15 11:42:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'command', 'result', 'log_url',
|
|
|
|
'is_finished',
|
|
|
|
'date_created', 'date_finished'
|
2018-12-10 02:11:54 +00:00
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_fk = ['run_as']
|
|
|
|
fields_m2m = ['hosts']
|
|
|
|
fields = fields_small + fields_fk + fields_m2m
|
2018-12-10 02:11:54 +00:00
|
|
|
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'
|
|
|
|
]
|
2020-12-11 09:21:52 +00:00
|
|
|
ref_name = 'OpsCommandExecution'
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|