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 #20 from YoLoveLife/devel
Browse files Browse the repository at this point in the history
完成资产信息获取功能
  • Loading branch information
YoLoveLife authored Sep 21, 2017
2 parents d9f70ef + c56616a commit 104437b
Show file tree
Hide file tree
Showing 28 changed files with 146 additions and 71 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ travis & django TestCase</br>

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

*如果您有相关的问题或者建议请在issues和我讨论 :heart: 期待收到您的信息*
*关于我开发中遇到的问题 我会写在issues当中供有需要的朋友 :mag: 查询*</br>
*这些问题都是在查询了诸多资料并亲身尝试大量解决方案 :grimacing: 最后得出的结论*</br>
*您可以在issues中搜索question和help wanted查看我写的相关问题*</br>
*如果未找到您关注的问题或者您对我的项目有建议请在issues和我讨论 :heart: 期待收到您的信息*</br>

## Contents
* [介绍](#introduce)
Expand Down
23 changes: 12 additions & 11 deletions apps/deveops/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@
url(r'^404/',views.ErrorView.as_view(),name='404'),
url(r'^permission/',views.PermissionView.as_view(),name='permission'),
url(r'^validate/', include('validate.urls.views_urls', namespace='validate')),
# url(r'^manager/', include('manager.urls.views_urls', namespace='manager')),
# url(r'^operation/',include('operation.urls.views_urls',namespace='operation')),
# url(r'^timeline/',include('timeline.urls.views_urls',namespace='timeline')),
# url(r'^authority/',include('authority.urls.views_urls',namespace='authority')),
# url(r'^application/',include('application.urls.views_urls',namespace='application')),
# url(r'^concert/',include('concert.urls.views_urls',namespace='concert')),
url(r'^manager/', include('manager.urls.views_urls', namespace='manager')),
url(r'^operation/',include('operation.urls.views_urls',namespace='operation')),
url(r'^timeline/',include('timeline.urls.views_urls',namespace='timeline')),
url(r'^authority/',include('authority.urls.views_urls',namespace='authority')),
url(r'^application/',include('application.urls.views_urls',namespace='application')),
url(r'^concert/',include('concert.urls.views_urls',namespace='concert')),

# API
# url(r'^api-manager/', include('manager.urls.api_urls', namespace='api-manager')),
# url(r'^api-operation/',include('operation.urls.api_urls',namespace='api-operation')),
# url(r'^api-authority/',include('authority.urls.api_urls',namespace='api-authority')),
# url(r'^api-application/',include('application.urls.api_urls',namespace='api-application')),
# url(r'^api-concert/',include('concert.urls.api_urls',namespace='api-concert')),
url(r'^api-manager/', include('manager.urls.api_urls', namespace='api-manager')),
url(r'^api-operation/',include('operation.urls.api_urls',namespace='api-operation')),
url(r'^api-authority/',include('authority.urls.api_urls',namespace='api-authority')),
url(r'^api-application/',include('application.urls.api_urls',namespace='api-application')),
url(r'^api-concert/',include('concert.urls.api_urls',namespace='api-concert')),
url(r'^api-execute/',include('execute.urls.api_urls',namespace='api-execute')),
]

'''
Expand Down
16 changes: 16 additions & 0 deletions apps/execute/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding:utf-8 -*-
import models,serializers
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from execute.service.catch.basic import BasicAnsibleService
from operation.models import PlayBook
from manager.models import Host
from service.catch.basic import BasicAnsibleService
class UpdateHostAPI(generics.ListAPIView):
serializer_class = serializers.ExecuteSerializer
permission_classes = [IsAuthenticated]
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])
5 changes: 4 additions & 1 deletion apps/execute/callback/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ def v2_runner_on_unreachable(self, result):
# str="h:%s r:unreach module:%s arvg:%s"%(host,json.dumps(result._result['invocation']["module_name"]),json.dumps(result._result['invocation']["module_args"]))
# print(str)
# print(json.dumps(result._result))
return super(ResultCallback,self).v2_runner_on_unreachable(result)
return super(ResultCallback,self).v2_runner_on_unreachable(result)

def ResultExtract(self):
pass
8 changes: 6 additions & 2 deletions apps/execute/callback/catch/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class BasicResultCallback(ResultCallback):
def v2_runner_on_ok(self, result, **kwargs):
c = Callback()
c.info=json.dumps(result._result)
c.history = History.objects.get(id=1)
self.result = result._result
c.save()
return c.id
self.c = c
return

def ResultExtract(self):
return self.result['stdout_lines']
6 changes: 6 additions & 0 deletions apps/execute/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import models
from rest_framework import serializers
class ExecuteSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Callback
fields = ('id')
25 changes: 16 additions & 9 deletions apps/execute/service/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
# -*- coding:utf-8 -*-
from inventory.maker import Maker
from yosible.tasks.tasks import Task
from yosible.run.ansiblerun import Ansible
from yosible.run.playbook import Playbook
from yosible.tasks.tasks import Tasks
from operation.models import Script
__metaclass__ = type
class AnsibleService():
def __init__(self,name):
self.ansible = Ansible()
self.maker = Maker()
self.playbook = Playbook(pbname=name,pbfacts='no')
def __init__(self,filename):
self.ansible = Ansible(filename)
self.playbook = Playbook(pbname='null',pbfacts='no')
self.tasks = Tasks()
self.push_tasks()
self.push_playbook()

def run(self,hostlist,tasklist):
def run(self,tasklist,maker):
for task in tasklist :
t = Task(module = task.module,args=task.args)
#如果为脚本 则单独将脚本文件写出来
if task.module == u'script':
script = Script.objects.get(id=int(str(task.args)))
t.args = maker.script_maker(script_id=str(script.id),script=script.formatScript())
self.tasks.push_task(t)
result = self.ansible.run_playbook()
maker.inventory_clear()

self.maker.inventory_maker(hostlist)
self.ansible.run_playbook()
return
return result

def push_makername(self):
return self.maker.filename

def push_tasks(self):
self.playbook.push_tasks(self.tasks)
Expand Down
26 changes: 20 additions & 6 deletions apps/execute/service/catch/basic.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
# -*- coding:utf-8 -*-
from execute.service import AnsibleService
from execute.callback.catch import basic

from execute.models import Callback
from inventory.maker import Maker
__metaclass__ = type
class BasicAnsibleService(AnsibleService):

def __init__(self,name):
super(BasicAnsibleService,self).__init__(self,name)
def __init__(self,hostlist):
maker = Maker()
self.maker = maker
self.maker.inventory_maker(hostlist)
super(BasicAnsibleService,self).__init__(maker.filename)

def run(self,hostlist,tasklist):
self.push_callback(basic.BasicResultCallback())
return super(BasicAnsibleService,self).run(hostlist,tasklist)
callback = basic.BasicResultCallback()
self.push_callback(callback)
super(BasicAnsibleService,self).run(tasklist,self.maker)
list = callback.ResultExtract()
self.update(list,hostlist)

def update(self,list,hostlist):
for host in hostlist:
host.coreness = list[0]
host.memory = list[1]
host.root_disk = list[2]
host.hostname = list[3]
host.save()

Empty file added apps/execute/urls/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions apps/execute/urls/api_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.conf.urls import url
from .. import api
urlpatterns=[
# Resource update api
url(r'^v1/update/host/(?P<pk>[0-9]+)/', api.UpdateHostAPI.as_view()),
]
11 changes: 11 additions & 0 deletions apps/execute/urls/views_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
# Time 10 15:38
# Author Yo
# Email [email protected]
from django.conf.urls import url
from ..views import group,host,storage,dashboard
urlpatterns = [
#Resource dashboard url
# url(r'^dashboard/$', dashboard.ManagerDashboardView.as_view(), name='dashboard'),
]
12 changes: 0 additions & 12 deletions apps/manager/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import models,serializers
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from execute.service.catch.basic import BasicAnsibleService
class GroupListAPI(generics.ListAPIView):
module = models.Group
serializer_class = serializers.GroupSerializer
Expand All @@ -23,17 +22,6 @@ def get_queryset(self):
queryset=models.Group.objects.get(id=self.kwargs['pk']).hosts
return queryset

class HostFlushAPI(generics.ListAPIView):
serializer_class = serializers.HostSerializer
permission_classes = [IsAuthenticated]
def get(self, request, *args, **kwargs):
host = models.Host.objects.get(id=self.kwargs['pk'])

bas = BasicAnsibleService('update host')
bas.run(hostlist=[host],tasklist=[])
return self.list(request, *args, **kwargs)


class StorageListAPI(generics.ListAPIView):
module = models.Storage
serializer_class = serializers.StorageSerializer
Expand Down
5 changes: 5 additions & 0 deletions apps/manager/templates/manager/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ <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 @@ -100,6 +101,10 @@ <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
1 change: 0 additions & 1 deletion apps/manager/urls/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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

# Resource storage api
url(r'^v1/storage/', api.StorageListAPI.as_view()),
Expand Down
2 changes: 1 addition & 1 deletion apps/operation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MUDULE_OPTION = {
MODULE_OPTION = {
'0':'none',
'1': 'shell',
'2': 'copy',
Expand Down
3 changes: 1 addition & 2 deletions apps/operation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def formatScript(self):
kwargs[args.args_name] = args.args_value
string = string + utils.bash_writer(self.author.email,'now',**kwargs)
string = string + utils.html2bash(self.script)
print(string)
return string

class ScriptArgs(models.Model):
Expand Down Expand Up @@ -55,7 +54,7 @@ class PlayBook(models.Model):

class Task(models.Model):
id = models.AutoField(primary_key=True)
mudule = models.CharField(default='hostname',max_length=20)
module = models.CharField(default='hostname',max_length=20)
args = models.CharField(default='',max_length=100)
sort = models.IntegerField(default=0)
playbook = models.ForeignKey(PlayBook,default=1,related_name='tasks')
Expand Down
2 changes: 1 addition & 1 deletion apps/operation/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ class Meta:
class TaskSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Task
fields= ('id','mudule','args','sort')
fields= ('id','module','args','sort')
4 changes: 2 additions & 2 deletions apps/operation/templates/operation/module/copy.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="id_mudule">模块</label>
<input id="id_mudule" class="form-control" name="mudule" disabled="yes" value="copy" required="no" max_length="100" placeholder="模块" type="text">
<label class="control-label" for="id_module">模块</label>
<input id="id_module" class="form-control" name="module" disabled="yes" value="copy" required="no" max_length="100" placeholder="模块" type="text">
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions apps/operation/templates/operation/module/file.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="id_mudule">模块</label>
<input id="id_mudule" class="form-control" name="mudule" disabled="yes" value="file" required="no" max_length="100" placeholder="模块" type="text">
<label class="control-label" for="id_module">模块</label>
<input id="id_module" class="form-control" name="module" disabled="yes" value="file" required="no" max_length="100" placeholder="模块" type="text">
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions apps/operation/templates/operation/module/script.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="id_mudule">模块</label>
<input id="id_mudule" class="form-control" name="mudule" disabled="yes" value="script" required="no" max_length="100" placeholder="模块" type="text">
<label class="control-label" for="id_module">模块</label>
<input id="id_module" class="form-control" name="module" disabled="yes" value="script" required="no" max_length="100" placeholder="模块" type="text">
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions apps/operation/templates/operation/module/service.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="id_mudule">模块</label>
<input id="id_mudule" class="form-control" name="mudule" disabled="yes" value="service" required="no" max_length="100" placeholder="模块" type="text">
<label class="control-label" for="id_module">模块</label>
<input id="id_module" class="form-control" name="module" disabled="yes" value="service" required="no" max_length="100" placeholder="模块" type="text">
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions apps/operation/templates/operation/module/shell.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="id_mudule">模块</label>
<input id="id_mudule" class="form-control" name="mudule" disabled="yes" value="shell" required="no" max_length="100" placeholder="模块" type="text">
<label class="control-label" for="id_module">模块</label>
<input id="id_module" class="form-control" name="module" disabled="yes" value="shell" required="no" max_length="100" placeholder="模块" type="text">
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions apps/operation/templates/operation/module/yum.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="id_mudule">模块</label>
<input id="id_mudule" class="form-control" name="mudule" disabled="yes" value="yum" required="no" max_length="100" placeholder="模块" type="text">
<label class="control-label" for="id_module">模块</label>
<input id="id_module" class="form-control" name="module" disabled="yes" value="yum" required="no" max_length="100" placeholder="模块" type="text">
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ <h3 class="box-title">剧本内容</h3>
<tr>
<th data-field="check" data-radio="true">#</th>
<th data-field="sort">剧本顺序</th>
<th data-field="mudule">模块名称</th>
<th data-field="module">模块名称</th>
<th data-field="args">模块参数</th>
</tr>
</thead>
Expand Down
2 changes: 1 addition & 1 deletion apps/operation/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ def html2bash(str):
if __name__ == "__main__":
kwargs={}
print(bash_writer(author='Yo',time='2017-8-11 16:44:13',**kwargs))
string = "<p>#!/bin/bash<br># Time 2017-9-12 17:32:27<br># Author Yo<br># Github github.com/YoLoveLife/scrRipt<br># processor</p><p>hostname<br>cat /proc/cpuinfo |grep processor|wc -l<br>free -m|sed -n '2p' |awk '{ print $2 }'<br>df -h |grep '/$' |awk '{ print $2 }'</p>"
string = "<p>hostname<br>cat /proc/cpuinfo |grep processor|wc -l<br>free -m|sed -n '2p' |awk '{ print $2 }'<br>df -h |grep '/$' |awk '{ print $2 }'</p>"
print(html2bash(string))
4 changes: 2 additions & 2 deletions apps/operation/views/playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView,UpdateView
from django.views.generic.detail import DetailView
from .. import MUDULE_OPTION
from .. import MODULE_OPTION
class OperationPlaybookListView(LoginRequiredMixin,TemplateView):
template_name= 'operation/playbook.html'

Expand Down Expand Up @@ -88,7 +88,7 @@ def get_context_data(self, **kwargs):
context.update({
'scripts' : scripts
})
self.template_name = self.template_dir + MUDULE_OPTION[module]+self.postfix
self.template_name = self.template_dir + MODULE_OPTION[module]+self.postfix
return context

def get(self,request,*args, **kwargs):
Expand Down
Loading

0 comments on commit 104437b

Please sign in to comment.