Skip to content

Commit

Permalink
down console model and mysql5.7 plugin join in.
Browse files Browse the repository at this point in the history
  • Loading branch information
yo committed Oct 13, 2018
1 parent 6495ad6 commit 1880c95
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 97 deletions.
1 change: 0 additions & 1 deletion apps/console/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.utils.translation import ugettext_lazy as _
from django.db import models
from manager.models import Group, Host
from ops.models import META_CONTENT
from authority.models import ExtendUser
import uuid
__all__ = [
Expand Down
77 changes: 53 additions & 24 deletions apps/ops/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,51 @@
from authority.models import ExtendUser
from utils.models import FILE
from django.conf import settings
from deveops.fields import JSONField
import os
import uuid
from django_mysql.models import JSONField
import uuid, yaml

__all__ = [
'META',
'Mission', 'Push_Mission'
]


def null_tasks():
return {
'tasks': [],
}


class CONTENTS(models.Model):
id = models.AutoField(primary_key=True)
uuid = models.UUIDField(auto_created=True, default=uuid.uuid4, editable=False)
_tasks = JSONField(default=null_tasks)

@property
def tasks(self):
return yaml.dump(self._tasks, default_flow_style=False)

@tasks.getter
def tasks(self, tasks):
self._tasks = yaml.load(tasks)

def to_yaml(self, proxy):
tasks = self._tasks
tasks.insert(proxy, 0)
return tasks

@property
def file_list(self):
FIND_LABEL = 'file:'
files = []
for task in self._tasks.get('tasks'):
if task.get('copy') is not None:
if "file:" in task.get('copy'):
copy_list = task.get('copy').split(FIND_LABEL)
file_name = copy_list[1].split(' ')
files.append(file_name[0][2:-2])
return files

class META(models.Model):
# 指定某幾台主機進行操作的元操作
id = models.AutoField(primary_key=True)
Expand All @@ -24,8 +60,7 @@ class META(models.Model):
# 當hosts為空 則說明該meta任務為本地執行
hosts = models.ManyToManyField(Host, blank=True, related_name='user_metas', verbose_name=_("metas"))
info = models.CharField(default='', max_length=5000)
# contents = models.ManyToManyField(META_CONTENT, blank=True, related_name='contents', verbose_name=_("contents"))
contents = JSONField()
contents = models.OneToOneField(CONTENTS, on_delete=models.CASCADE)

class Meta:
permissions = (('yo_list_meta', u'罗列元操作'),
Expand All @@ -35,28 +70,23 @@ class Meta:

@property
def file_list(self):

files = []
for content in self.contents.all():
if content.file_name != "":
files.append(content.file_name)
return files
return self.contents.file_list

@property
def to_yaml(self):
tasks = []
proxy = {}
hosts_list = []
for host in self.hosts.all():
if host.status == settings.STATUS_HOST_CAN_BE_USE:
hosts_list.append(host.connect_ip)

if self.group.jumper is not None:
tasks.append(self.group.jumper.to_yaml)
proxy = self.group.jumper.to_yaml

return {
'tasks': self.contents,
return {
'gather_facts': 'no',
'hosts': hosts_list or 'localhost',
}
}.update(self.contents.to_yaml(proxy))


class Mission(models.Model):
Expand All @@ -74,11 +104,11 @@ class Meta:

@property
def file_list(self):
list = []
files = []
for meta in self.metas.all():
if len(meta.file_list) !=0 :
list = list + meta.file_list
return list
files = files + meta.file_list
return files

@property
def vars_dict(self):
Expand All @@ -90,19 +120,18 @@ def vars_dict(self):

@property
def to_yaml(self):
list = []
tasks_list = []
for meta in self.metas.all():
list.append(meta.to_yaml)
return list
tasks_list.append(meta.to_yaml)
return tasks_list

@property
def count(self):
return self.push_missions.count()

@property
def playbook(self):
import yaml
return yaml.safe_dump_all(self.to_yaml)
return yaml.dump(self.to_yaml, default_flow_style=False)

def model_to_dict(self):
from django.forms.models import model_to_dict
Expand Down
41 changes: 6 additions & 35 deletions apps/ops/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@
from rest_framework import serializers

__all__ = [
"MetaSerializer", "MetaContentSerializer", "MissionNeedFileSerializer",
"MetaSerializer", "MissionNeedFileSerializer",
"MissionSerializer",
]

class MetaContentSerializer(serializers.ModelSerializer):
class Meta:
model = models.META_CONTENT
fields = (
'id', 'name', 'module', 'args', 'sort',
)
read_only_fields = (
'id',
)


class MetaSerializer(serializers.ModelSerializer):
hosts = serializers.PrimaryKeyRelatedField(required=False,many=True, queryset=models.Host.objects.all(),allow_null=True)
need_files = serializers.ListField(source="file_list", read_only=True)
contents = MetaContentSerializer(required=True, many=True, allow_null=True)
contents = serializers.JSONField()
group = serializers.PrimaryKeyRelatedField(queryset=models.Group.objects.all())
group_name = serializers.CharField(source="group.name",read_only=True)
qrcode = serializers.CharField(required=True, write_only=True,)
Expand All @@ -32,52 +22,33 @@ class Meta:
'id', 'uuid', 'hosts', 'contents', 'group', 'group_name', 'info', 'qrcode', 'need_files'
)
read_only_fields = (
'id', 'uuid','group_name'
'id', 'uuid', 'group_name'
)

def create(self, validated_data):
contents = validated_data.pop('contents')
validated_data.pop('qrcode')
id_list = []
hosts = None
for content in contents:
content_instance = models.META_CONTENT.objects.create(**content)
id_list.append(content_instance.id)

if 'hosts' in validated_data.keys():
hosts = validated_data.pop('hosts')

contests_list = models.META_CONTENT.objects.filter(id__in=id_list)

obj = models.META.objects.create(**validated_data)
obj.contents.add(*contests_list)

if hosts is not None:
obj.hosts.set(hosts)
obj.save()

return obj

def update(self, instance, validated_data):
contents = validated_data.pop('contents')
validated_data.pop('qrcode')
obj = super(MetaSerializer,self).update(instance,validated_data)
for content in obj.contents.all():
content.delete()
obj.contents.clear()

id_list = []
for content in contents:
content_instance = models.META_CONTENT.objects.create(**content)
id_list.append(content_instance.id)

contests_list = models.META_CONTENT.objects.filter(id__in=id_list)
obj.contents.add(*contests_list)
obj = super(MetaSerializer, self).update(instance, validated_data)
obj.save()
return obj


class MissionNeedFileSerializer(serializers.ModelSerializer):
filelist = serializers.ListField(source='file_list',read_only=True)
filelist = serializers.ListField(source='file_list', read_only=True)

class Meta:
model = models.Mission
Expand Down
2 changes: 1 addition & 1 deletion apps/ops/urls/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
path(r'v1/mission/<uuid:pk>/delete/', MissionAPI.OpsMissionDeleteAPI.as_view()),

# Quick
path(r'v1/quick/create/', QuickAPI.OpsQuickCreateAPI.as_view()),
# path(r'v1/quick/create/', QuickAPI.OpsQuickCreateAPI.as_view()),
]
34 changes: 0 additions & 34 deletions deveops/fields.py

This file was deleted.

6 changes: 5 additions & 1 deletion deveops/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
'yodns.apps.YoDnsConfig',
'zdb.apps.ZDBConfig',
'monitor.apps.MonitorConfig',
'console.apps.ConsoleConfig',
# 'console.apps.ConsoleConfig',
'ezsetup.apps.EZSetupConfig',
'yocdn.apps.YoCDNConfig',
'pool.apps.PoolConfig',
Expand All @@ -62,6 +62,7 @@
'django.contrib.staticfiles',
'django_celery_beat',
'channels',
'django_mysql',
# 'django.contrib.messages',
# 'bootstrap3',
# 'djcelery', #celery
Expand Down Expand Up @@ -140,6 +141,9 @@
'PASSWORD':DEVEOPS_CONF.DB_PASSWD,
'HOST':DEVEOPS_CONF.DB_HOST,
'PORT':DEVEOPS_CONF.DB_PORT,
'OPTIONS':{
'charset': 'utf8mb4'
},
},
}

Expand Down
2 changes: 1 addition & 1 deletion deveops/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
path(r'api-zdb/', include('zdb.urls.api_urls')),
path(r'api-dashboard/', include('dashboard.urls.api_urls')),
path(r'api-monitor/', include('monitor.urls.api_urls')),
path(r'api-console/', include('console.urls.api_urls')),
# path(r'api-console/', include('console.urls.api_urls')),
path(r'api-ezsetup/', include('ezsetup.urls.api_urls')),
path(r'api-ipool/', include('pool.urls.api_urls')),
path(r'api-timeline/', include('timeline.urls.api_urls')),
Expand Down

0 comments on commit 1880c95

Please sign in to comment.