Django开发运维后台(五):formview结合salt执行命令
远程执行命令我使用saltstack这个运维工具,执行一些系统命令可以直接使用salt,执行一些相关的游戏脚本,则可以使用salt的模块功能,最后还可以把结果存入数据库,太方便了
saltstack 自定义modules:http://lihuipeng.blog.51cto.com/3064864/1396279
saltstack return的使用:http://lihuipeng.blog.51cto.com/3064864/1403387
saltstack API的使用:http://docs.saltstack.cn/ref/clients/index.html#localclient
我这里使用了多数据库,django多数据库的配置参考:http://lihuipeng.blog.51cto.com/3064864/1415141
models.py(这个salt官方需要的两个表)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class QjshsaJids(models.Model):
_database = 'kaifu'
id = models.IntegerField(primary_key = True )
jid = models.CharField(unique = True , max_length = 255 )
load = models.TextField()
class Meta:
managed = False
db_table = 'QjshSa_jids'
class QjshsaSaltreturns(models.Model):
_database = 'kaifu'
id = models.IntegerField(primary_key = True )
fun = models.CharField(max_length = 50 )
jid = models.CharField(max_length = 255 )
result = models.TextField()
host = models.CharField(max_length = 255 )
success = models.CharField(max_length = 10 )
full_ret = models.TextField()
class Meta:
managed = False
db_table = 'QjshSa_saltreturns'
|
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
systemcommand = [
( 'df -lh' , 'df -lh' ),
( 'free -m' , 'free -m' ),
( 'uptime' , 'uptime' ),
( 'netstat -tunlp' , 'netstat -tunlp' ),
( 'ps aux | grep "daemon"' , 'ps aux | grep "daemon"' ),
( 'crontab -l' , 'crontab -l' ),
]
class ExecuteSystemForm(forms.Form):
plat = forms.CharField(required = True ,
widget = forms.TextInput(attrs = { 'readonly' : 'True' , 'class' : "form-control" }))
server_id = forms.CharField(required = True ,
widget = forms.TextInput(attrs = { 'readonly' : 'True' , 'class' : "form-control" }))
server_name = forms.CharField(required = True ,
widget = forms.TextInput(attrs = { 'readonly' : 'True' , 'class' : "form-control" }))
host = forms.CharField(required = True ,
widget = forms.TextInput(attrs = { 'readonly' : 'True' , 'class' : "form-control" }))
command = forms.ChoiceField(required = True ,
choices = systemcommand,
widget = forms.Select(attrs = { 'class' : "form-control" }))
|
我限定了可以执行某些命令,感觉这样我安心一些。。。
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
class ExecuteSystem(FormView):
template_name = 'execute_system.jinja.html'
form_class = ExecuteSystemForm
def get_initial( self , * * kwargs):
initial = {}
kaifuid = self .kwargs[ 'pk' ]
serverinfo = ServerList.objects.get(kaifu_id = kaifuid)
initial[ 'plat' ] = serverinfo.plat
initial[ 'server_id' ] = serverinfo.server_id
initial[ 'server_name' ] = serverinfo.server_name
initial[ 'host' ] = serverinfo.dx_ip
return initial
def form_valid( self , form):
formdata = form.cleaned_data
host = formdata[ 'host' ]
command = formdata[ 'command' ]
saltconnection = salt.client.LocalClient()
result = saltconnection.cmd(host, 'cmd.run' , [command], ret = 'qjsh_mysql' )
result = result[host]
messages.success( self .request, '执行成功!' )
return render_to_response( self .template_name,
{ 'form' :form, 'kaifuid' : self .kwargs[ 'pk' ], 'result' :result},
context_instance = RequestContext( self .request))
def get_context_data( self , * * kwargs):
context = super (ExecuteSystem, self ).get_context_data( * * kwargs)
context[ 'kaifuid' ] = self .kwargs[ 'pk' ]
return context
|
get_inital是初始化form
form_valid是表单通过验证后执行的
execute_system.jinja.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
{% from "form_macro.html" import render_field, render_checkbox%} {% extends "execute_cmd_index.jinja.html" %} {% set active_page2 = 'executesystem' -%} {% block content %} < div class = "row" >
< div class = "col-md-6" >
< form class = "form-inline" role = "form" method = "POST" >{% csrf_token %}
< table class = "table table-bordered table-hover" >
< tr >
< td >平台名称:</ td >
< td >{{ form.plat|safe }}</ td >
</ tr >
< tr >
< td >服务器ID:</ td >
< td >{{ form.server_id|safe }}</ td >
</ tr >
< tr >
< td >服务器名称:</ td >
< td >{{ form.server_name|safe }}</ td >
</ tr >
< tr >
< td >执行IP:</ td >
< td >{{ form.host|safe }}</ td >
</ tr >
< tr >
< td >执行命令:</ td >
< td >
< div class = "form-group" >
{{ form.command|safe }}
</ div >
</ td >
</ tr >
< tr >
< td colspan = "2" >< center >< button type = "submit" class = "btn btn-info" >确定</ button ></ center ></ td >
</ tr >
</ table >
</ form >
</ div >
</ div >
{% if result %}
< hr >
< div class = "row" >
< div class = "panel panel-primary" >
< div class = "panel-heading" >
< h3 class = "panel-title" >执行结果:</ h3 >
</ div >
< div class = "panel-body" >
< pre >< core >`result`</ core ></ pre >
</ div >
</ div >
</ div >
{% endif %}
{% endblock %} |
最后我实现的效果:
本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1440954如需转载请自行联系原作者
lihuipeng