Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #23 from YoLoveLife/devel
Browse files Browse the repository at this point in the history
flush
  • Loading branch information
YoLoveLife authored Sep 27, 2017
2 parents 104437b + c1e13f0 commit f79a801
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ travis & django TestCase</br>

如果你关注:自动化运维、运维资源管理等内容 :star: 我,[分享](http://www.yolovelife.com)给其他的运维人员</br>
如果你关注:django开发、rest-framework等内容 :star: 我,[分享](http://www.yolovelife.com)给其他的开发者</br>
求求你们 :star: 我吧!!!求求你们 :star: 我吧!!!求求你们 :star: 我吧!!!</br>
求求你们 :star: 我吧!!! 求求你们 :star: 我吧!!! 求求你们 :star: 我吧!!!</br>

*关于我开发中遇到的问题 我会写在issues当中供有需要的朋友 :mag: 查询*</br>
*这些问题都是在查询了诸多资料并亲身尝试大量解决方案 :grimacing: 最后得出的结论*</br>
Expand Down
6 changes: 6 additions & 0 deletions apps/application/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class DB(models.Model):
datadir=models.CharField(max_length=100,default='/storage/mysql')
softlib=models.ForeignKey(Softlib,default=1,)
# online=models.BooleanField(default=False)

class DBControl(models.Model):
id = models.AutoField(primary_key=True)
db = models.ForeignKey(DB,default=1,related_name='dbcontrols')


#
#
# class Java(models.Model):
Expand Down
9 changes: 7 additions & 2 deletions apps/execute/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
from manager.models import Host
from service.catch.basic import BasicAnsibleService
class UpdateHostAPI(generics.ListAPIView):
serializer_class = serializers.ExecuteSerializer
serializer_class = serializers.UpdateHostSerializer
permission_classes = [IsAuthenticated]

def get_queryset(self):
return Host.objects.filter(id=self.kwargs['pk'])

def get(self, request, *args, **kwargs):
host = Host.objects.get(id=self.kwargs['pk'])
playbook = PlayBook.objects.get(id=1)
bas = BasicAnsibleService(hostlist=[host])
bas.run(tasklist=playbook.tasks.all().order_by('-sort'),hostlist=[host])
bas.run(tasklist=playbook.tasks.all().order_by('-sort'),hostlist=[host])
return super(UpdateHostAPI,self).get(request,*args,**kwargs)
9 changes: 5 additions & 4 deletions apps/execute/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import models
from manager import models
from rest_framework import serializers
class ExecuteSerializer(serializers.HyperlinkedModelSerializer):

class UpdateHostSerializer(serializers.ModelSerializer):
class Meta:
model = models.Callback
fields = ('id')
model=models.Host
fields = ('id','service_ip')
5 changes: 2 additions & 3 deletions apps/execute/service/catch/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
__metaclass__ = type
class BasicAnsibleService(AnsibleService):
def __init__(self,hostlist):
maker = Maker()
self.maker = maker
self.maker = Maker()
self.maker.inventory_maker(hostlist)
super(BasicAnsibleService,self).__init__(maker.filename)
super(BasicAnsibleService,self).__init__(self.maker.filename)

def run(self,hostlist,tasklist):
callback = basic.BasicResultCallback()
Expand Down
25 changes: 17 additions & 8 deletions apps/manager/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
import models,serializers
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
class GroupListAPI(generics.ListAPIView):
from rest_framework.response import Response
from manager.query import hostQuery
class ManagerGroupListAPI(generics.ListAPIView):
module = models.Group
serializer_class = serializers.GroupSerializer
permission_classes = [IsAuthenticated]
def get_queryset(self):
queryset=models.Group.objects.all()
return queryset

class HostListByGroupAPI(generics.ListAPIView):
class ManagerHostListByGroupAPI(generics.ListAPIView):
module = models.Host
serializer_class = serializers.HostSerializer
permission_classes = [IsAuthenticated]
Expand All @@ -22,7 +24,7 @@ def get_queryset(self):
queryset=models.Group.objects.get(id=self.kwargs['pk']).hosts
return queryset

class StorageListAPI(generics.ListAPIView):
class ManagerStorageListAPI(generics.ListAPIView):
module = models.Storage
serializer_class = serializers.StorageSerializer
permission_classes = [IsAuthenticated]
Expand All @@ -32,7 +34,7 @@ def get_queryset(self):
return queryset


class StorageListByGroup(generics.ListAPIView):
class ManagerStorageListByGroup(generics.ListAPIView):
module = models.Storage
serializer_class = serializers.StorageSerializer
permission_classes = [IsAuthenticated]
Expand All @@ -44,9 +46,16 @@ def get_queryset(self):
queryset ={}
return queryset

class SystemTypeAPI(generics.ListAPIView):
serializer_class = serializers.SystemTypeSerializer
class ManagerSearchAPI(generics.ListAPIView):
serializer_class = serializers.HostSerializer
permission_classes = [IsAuthenticated]

def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def get_queryset(self):
query_list = self.request.query_params.dict()
del query_list['order']
del query_list['offset']
del query_list['limit']
if len(query_list) == 0:
return {}
else:
return hostQuery(**query_list)
17 changes: 16 additions & 1 deletion apps/manager/query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import models
import json
from django.db.models import Q
def systemtypeQuery():
list=[]
choices=models.Host.SYSTEM_CHOICES
Expand All @@ -19,4 +20,18 @@ def groupQuery():
dit['name']=group.name
dit['value']=group.hosts.count()
list.append(dit)
return json.dumps(list, ensure_ascii=False, encoding='UTF-8')
return json.dumps(list, ensure_ascii=False, encoding='UTF-8')

def cleanQuery(**kwargs):
list = []
for key in kwargs:
list.append((key+'__contains',kwargs[key]),)
return list

def hostQuery(*args,**kwargs):
list = cleanQuery(**kwargs)
q = Q()
q.connector = 'AND'
q.children=list
return models.Host.objects.filter(q)

15 changes: 9 additions & 6 deletions apps/manager/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ class Meta:
model = models.Group
fields = ['id', 'hosts']

class SystemTypeSerializer(serializers.CharField):
name=serializers.CharField()
percentage=serializers.IntegerField()
class HostSearchSerializer(serializers.ModelSerializer):
groups__name = serializers.CharField()
storages__disk_size = serializers.CharField()
storages__disk_path = serializers.CharField()
class Meta:
fields= ['name','percentage']


model=models.Host
fields = ('manage_ip','service_ip','outer_ip','server_position','hostname','groups__name',
'normal_user','sshpasswd','sshport','coreness','memory','root_disk','info'
,'storages__disk_size','storages__disk_path'
)
22 changes: 22 additions & 0 deletions apps/manager/templates/manager/detail_host.html
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ <h3 class="box-title"><i class="fa fa-cogs"></i>配置信息</h3>
</div>
</div>
</div>
<div class="col-md-1">
<button class="btn btn-app" id="flush_host">
<i class="fa fa-plus"></i> 刷新主机信息
</button>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -277,5 +282,22 @@ <h3 class="box-title"><i class="fa fa-life-ring"></i>应用信息</h3>
function historyBack() {
window.history.go(-1);
}
$(document).ready(function () {
})
.on('click','#flush_host',function () {
$.ajax({
mimeType: 'text/html; charset=utf-8', // ! Need set mimeType only when run from local file
url: '/api-execute/v1/update/host/{{ host.id }}/',
type: 'GET',
success: function(data) {
location.reload();
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
},
dataType: "html",
async: false
});
})
</script>
{% endblock %}
5 changes: 0 additions & 5 deletions apps/manager/templates/manager/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ <h3 class="box-title">主机列表</h3>
<th data-field="server_position">服务器位置</th>
<th data-field="info">用途</th>
<th data-field="id" data-formatter="detailFormatter">细节</th>
<th data-field="id" data-formatter="flushFormatter">刷新</th>
</tr>
</thead>
<tbody id="hosttbody">
Expand Down Expand Up @@ -101,10 +100,6 @@ <h3 class="box-title" style="text-align: center">主机信息</h3>
var label_a='<a class="btn btn-default" href="/manager/host/'+value+'/detail/">'+'<i class="fa fa-paperclip"></i>'+'</a>';
return [label_a].join('');
}
function flushFormatter(value,row,index) {
var label_a='<a class="btn btn-default" href="/api-execute/v1/update/host/'+value+'/">'+'<i class="fa fa-refresh"></i>'+'</a>';
return [label_a].join('');
}
$(document).ready(function () {
$('.select2').select2();
var table=$('#host_tb').bootstrapTable({
Expand Down
98 changes: 82 additions & 16 deletions apps/manager/templates/manager/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1>
<!--Main Content-->
<section class="content container page-search">
<div class="row">
<h1><i class="fa fa-search"></i> devEops</h1>
<h1><i class="fa fa-search"></i> devEops - 资产搜索 </h1>
</div>
<div class="row">
<div class="col-md-3">
Expand All @@ -30,36 +30,102 @@ <h1><i class="fa fa-search"></i> devEops</h1>
<h3 class="box-title" style="text-align: center">操作</h3>
</div>
<div class="box-body">
<dt>请使用以下规则使用搜索</dt>
<dt>key=value</dt><dd>单个Item项通过key和value组成</dd>
<dt>Item;Item</dt><dd>多个Item项通过;号分隔</dd>
<dt>Example</dt><dd>port=3306;prefix=/usr/local</dd>
<dt>可选用的key值</dt><dd>port,prefix,version,online</dd>
<dt>请使用以下规则使用搜索</dt>
<dt>key=value</dt><dd>单个Item项通过key和value组成</dd>
<dt>Item;Item</dt><dd>多个Item项通过;号分隔</dd>
<dt>Example</dt><dd>manage_ip=10.100.100;sshport=52000</dd>
<dt>可选用的key值</dt><dd>manage_ip,service_ip,outer_ip,
server_position,hostname,
groups__name,normal_user,sshport,
coreness,memory,root_disk,info,
storages__disk_size,
storages__disk_path</dd>
</div>
</div>
</div>
<div class="col-md-9">
<div class="input-group">
<input type="text" id='manager-search-info' class="form-control manager-search-input">
<span class="input-group-btn">
<button type="button" class="btn btn-info" onclick="searchManager()">Search</button>
</span>
</div>
<div class="row">
<div class="col-md-12">
<div class="input-group">
<input type="text" id='manager-search-info' class="form-control" placeholder="">
<span class="input-group-btn">
<button type="button" class="btn btn-info" id="btn-search">Search</button>
</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8" disabled="none">
<div class="box" disabled="none">
<div class="box-header">
<h3 class="box-title" style="text-align: center">搜索结果</h3>
</div>
<div class="box-body">
<table id="search_tb" class="table table-bordered table-striped">
<thead>
<tr>
<th data-field="service_ip">服务IP</th>
<th data-field="info">用途</th>
<th data-field="id" data-formatter="detailFormatter">细节</th>
</tr>
</thead>
<tbody id="hosttbody">
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!--Main Content-->
{% endblock %}
{% block foot_js %}
<script src="{% static 'js/jquery-3.2.1.min.js' %}"></script>
<script src="{% static 'js/bootstrap-table.min.js'%}"></script>
<script>
function detailFormatter(value,row,index) {
var label_a='<a class="btn btn-default" href="/manager/host/'+value+'/detail/">'+'<i class="fa fa-paperclip"></i>'+'</a>';
return [label_a].join('');
}
function historyBack() {
window.history.go(-1);
}
function searchManager(){
var Element=document.getElementById('manager-search-info');
var string=Element.value;
//$.ajax()
function cleanGetData(string){
var get_data = "?";
var strs = new Array();
strs = string.split(';');
for(var i=0;i<strs.length;i++){
var st = strs[i].split('=');
get_data+= st[0].replace(/(^\s*)|(\s*$)/g, "") + '=' + st[1].replace(/(^\s*)|(\s*$)/g, "");
if(i!=strs.length-1){
get_data +="&";
}
}
return get_data;
}
$(document).ready(function () {
var table=$('#search_tb').bootstrapTable({
url : '/api-manager/v1/search/',
method : 'GET',
uniqueId : 'id',
pagination : true,
sidePagination : 'client',
dataType : 'json',
pageSize : 5,
pageList : [5],
exportDataType : "basic",
});
})
.on('click','#btn-search',function () {
var Element=document.getElementById('manager-search-info');
var string=Element.value;
var data =cleanGetData(string);
var url = '/api-manager/v1/search/'+data;
var table=$('#search_tb').bootstrapTable('refresh', {url : url});
})
</script>
{% endblock %}
11 changes: 5 additions & 6 deletions apps/manager/urls/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from .. import api
urlpatterns=[
# Resource group api
url(r'^v1/group/', api.GroupListAPI.as_view()),
url(r'^v1/group/', api.ManagerGroupListAPI.as_view()),

# Resource host api
url(r'^v1/hostbygroup/(?P<pk>[0-9]+)',api.HostListByGroupAPI.as_view()),
url(r'^v1/hostbygroup/(?P<pk>[0-9]+)',api.ManagerHostListByGroupAPI.as_view()),

# Resource storage api
url(r'^v1/storage/', api.StorageListAPI.as_view()),

# Resource systemtype api
url(r'^v1/dashboard/systemtype',api.SystemTypeAPI.as_view()),
url(r'^v1/storage/', api.ManagerStorageListAPI.as_view()),

# Resource search api
url(r'^v1/search/',api.ManagerSearchAPI.as_view()),
]
1 change: 0 additions & 1 deletion apps/operation/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class ScriptRemoveArgsAPI(generics.DestroyAPIView):
permission_classes = [IsAuthenticated]

def delete(self, request, *args, **kwargs):
# return self.destroy(self,request,args,kwargs)
if models.Script.objects.get(id=int(kwargs['pk'])).scriptargs.filter(args_name=request.data['args_name']).exists():
models.Script.objects.get(id=int(kwargs['pk'])).scriptargs.filter(args_name=request.data['args_name']).delete()
return Response({'info':'删除成功'},status=status.HTTP_201_CREATED)
Expand Down
Loading

0 comments on commit f79a801

Please sign in to comment.