Commit 47d87e38 authored by yumaojun03's avatar yumaojun03

补充task需要的一些接口

parent 150e1030
......@@ -53,7 +53,13 @@ class CronTableSerializer(serializers.ModelSerializer):
model = CronTable
class TaskSerializer(serializers.ModelSerializer):
sub_tasks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Task
read_only_fields = ('record',)
class SubTaskSerializer(serializers.ModelSerializer):
class Meta:
model = SubTask
......@@ -14,6 +14,7 @@ __all__ = ["HostAliaViewSet",
"SudoViewSet",
"CronTableViewSet",
"TaskViewSet",
"SubTaskViewSet",
]
......@@ -69,5 +70,10 @@ class TaskViewSet(viewsets.ModelViewSet):
serializer_class = TaskSerializer
permission_classes = (AdminUserRequired,)
class SubTaskViewSet(viewsets.ModelViewSet):
queryset = SubTask.objects.all()
serializer_class = SubTaskSerializer
permission_classes = (AdminUserRequired,)
......@@ -2,4 +2,5 @@ from ansible import *
from cron import *
from sudo import *
from utils import *
from task import *
......@@ -4,12 +4,10 @@ from __future__ import unicode_literals, absolute_import
import logging
import json
from assets.models import Asset
from django.db import models
from django.utils.translation import ugettext_lazy as _
__all__ = ["Task", "TaskRecord", "AnsiblePlay", "AnsibleTask", "AnsibleHostResult"]
__all__ = ["TaskRecord", "AnsiblePlay", "AnsibleTask", "AnsibleHostResult"]
logger = logging.getLogger(__name__)
......@@ -291,18 +289,3 @@ class AnsibleHostResult(models.Model):
except Exception as e:
print('Error: %s, continue...' % e.message)
continue
class Task(models.Model):
record = models.OneToOneField(TaskRecord)
name = models.CharField(max_length=128, blank=True, verbose_name=_('Name'))
module_name = models.CharField(max_length=128, verbose_name=_('Ansible Module Name'))
module_args = models.CharField(max_length=512, blank=True, verbose_name=_("Ansible Module Args"))
register = models.CharField(max_length=128, blank=True, verbose_name=_('Ansible Task Register'))
is_gather_facts = models.BooleanField(default=False,verbose_name=_('Is Gather Ansible Facts'))
asset = models.ManyToManyField(Asset, related_name='tasks')
def __unicode__(self):
return "%s %s" % (self.module_name, self.module_args)
def run(self):
pass
......@@ -11,8 +11,7 @@ from django.utils.translation import ugettext_lazy as _
logger = logging.getLogger(__name__)
__all__ = ["HostAlia", "UserAlia", "CmdAlia", "RunasAlia", "Privilege",
"Extra_conf", "Sudo"]
__all__ = ["HostAlia", "UserAlia", "CmdAlia", "RunasAlia", "Privilege", "Extra_conf", "Sudo"]
class HostAlia(models.Model):
......
# ~*~ coding: utf-8 ~*~
from __future__ import unicode_literals, absolute_import
import logging
from uuid import uuid4
from assets.models import Asset
from ops.models import TaskRecord
from ops.utils.ansible_api import ADHocRunner, Config
from django.db import models
from django.utils.translation import ugettext_lazy as _
__all__ = ["Task", "SubTask"]
logger = logging.getLogger(__name__)
class Task(models.Model):
record = models.OneToOneField(TaskRecord)
name = models.CharField(max_length=128, blank=True, verbose_name=_('Name'))
is_gather_facts = models.BooleanField(default=False,verbose_name=_('Is Gather Ansible Facts'))
assets = models.ManyToManyField(Asset, related_name='tasks')
def __unicode__(self):
return "%s" % self.name
@property
def ansible_assets(self):
return []
def run(self):
conf = Config()
gather_facts = "yes" if self.is_gather_facts else "no"
play_source = {
"name": "Ansible Play",
"hosts": "default",
"gather_facts": gather_facts,
"tasks": [
dict(action=dict(module='ping')),
]
}
hoc = ADHocRunner(conf, play_source, *self.ansible_assets)
uuid = "tasker-" + uuid4().hex
ext_code, result = hoc.run("test_task", uuid)
print(ext_code)
print(result)
class SubTask(models.Model):
task = models.ForeignKey(Task, related_name='sub_tasks', verbose_name=_('Ansible Task'))
module_name = models.CharField(max_length=128, verbose_name=_('Ansible Module Name'))
module_args = models.CharField(max_length=512, blank=True, verbose_name=_("Ansible Module Args"))
register = models.CharField(max_length=128, blank=True, verbose_name=_('Ansible Task Register'))
def __unicode__(self):
return "%s %s" % (self.module_name, self.module_args)
\ No newline at end of file
......@@ -16,5 +16,6 @@ api_router.register(r'v1/privilege', v1_api.PrivilegeViewSet)
api_router.register(r'v1/sudo', v1_api.SudoViewSet)
api_router.register(r'v1/cron', v1_api.CronTableViewSet)
api_router.register(r'v1/task', v1_api.TaskViewSet)
api_router.register(r'v1/subtask', v1_api.SubTaskViewSet)
urlpatterns = api_router.urls
\ No newline at end of file
......@@ -20,6 +20,8 @@ from ansible.plugins.callback import CallbackBase
from ops.models import TaskRecord, AnsiblePlay, AnsibleTask, AnsibleHostResult
__all__ = ["ADHocRunner", "Config"]
logger = logging.getLogger(__name__)
......@@ -392,7 +394,7 @@ class PlayBookRunner(InventoryMixin):
class ADHocRunner(InventoryMixin):
"""ADHoc接口
"""
def __init__(self, config, play_data, *hosts, **group_vars):
def __init__(self, play_data, config=None, *hosts, **group_vars):
"""
:param hosts: 见PlaybookRunner参数
:param group_vars: 见PlaybookRunner参数
......@@ -406,8 +408,7 @@ class ADHocRunner(InventoryMixin):
tasks=[dict(action=dict(module='service', args={'name': 'vsftpd', 'state': 'restarted'}), async=async, poll=poll)]
)
"""
self.options = config
self.options = config if config != None else Config()
# 设置verbosity级别, 及命令行的--verbose选项
self.display = Display()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment