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
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
from .models import Task, AdHoc, AdHocRunHistory, CommandExecution
|
|
|
|
|
|
|
|
|
|
|
|
class CeleryResultSerializer(serializers.Serializer):
|
|
|
|
id = serializers.UUIDField()
|
|
|
|
result = serializers.JSONField()
|
|
|
|
state = serializers.CharField(max_length=16)
|
|
|
|
|
|
|
|
|
|
|
|
class CeleryTaskSerializer(serializers.Serializer):
|
|
|
|
pass
|
2017-03-22 16:15:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TaskSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
2017-12-10 16:29:25 +00:00
|
|
|
model = Task
|
2019-09-19 13:21:05 +00:00
|
|
|
fields = [
|
|
|
|
'id', 'name', 'interval', 'crontab', 'is_periodic',
|
|
|
|
'is_deleted', 'comment', 'created_by', 'date_created',
|
|
|
|
'versions', 'is_success', 'timedelta', 'assets_amount',
|
|
|
|
'date_updated', 'history_summary',
|
|
|
|
]
|
2017-03-22 16:15:25 +00:00
|
|
|
|
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
class AdHocSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = AdHoc
|
|
|
|
exclude = ('_tasks', '_options', '_hosts', '_become')
|
|
|
|
|
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
fields = super().get_field_names(declared_fields, info)
|
|
|
|
fields.extend(['tasks', 'options', 'hosts', 'become', 'short_id'])
|
|
|
|
return fields
|
|
|
|
|
|
|
|
|
|
|
|
class AdHocRunHistorySerializer(serializers.ModelSerializer):
|
|
|
|
task = serializers.SerializerMethodField()
|
|
|
|
adhoc_short_id = serializers.SerializerMethodField()
|
2017-12-11 09:08:43 +00:00
|
|
|
stat = serializers.SerializerMethodField()
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdHocRunHistory
|
|
|
|
exclude = ('_result', '_summary')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_adhoc_short_id(obj):
|
|
|
|
return obj.adhoc.short_id
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_task(obj):
|
|
|
|
return obj.adhoc.task.id
|
|
|
|
|
2017-12-11 09:08:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_stat(obj):
|
|
|
|
return {
|
2018-12-17 03:44:43 +00:00
|
|
|
"total": obj.adhoc.hosts.count(),
|
2018-01-10 10:08:27 +00:00
|
|
|
"success": len(obj.summary.get("contacted", [])),
|
|
|
|
"failed": len(obj.summary.get("dark", [])),
|
2017-12-11 09:08:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
fields = super().get_field_names(declared_fields, info)
|
|
|
|
fields.extend(['summary', 'short_id'])
|
|
|
|
return fields
|
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})
|