接上篇《授之以渔-运维平台Saltstack Web 管理二(Returnner篇)》,今天介绍下sls文件生成。
一、 top.sls思路
1.哪些主机可以执行哪些模块(数据库)
2.前端哪些主机修改了过哪些模块(checkbox获取更改主机及模块,更新数据库)
3.生成top.sls(遍历数据库,生成top.sls文件)
4.调用salt.client 执行salt xxx主机highstate.state
二、 食用过程
1.salt_create_topsls(tops生成)
from cmdb.models import *
import os
def salt_create_topsls():
try:
if os.path.exists("/srv/salt/top.sls"):
os.remove("/srv/salt/top.sls")
topsls = file("/srv/salt/top.sls", "a+")
title = "base:\n"
content = ''
salt_hosts = Salt_hosts.objects.all()
for i in salt_hosts:
salt_model_content = ''
if i.salt_hosts_models_group_id == None or i.salt_hosts_models_group_id == '':
continue
else:
salt_model_list = Salt_models_group.objects.get(id=i.salt_hosts_models_group_id).salt_models_group_member.split(',')
for ii in salt_model_list:
salt_model_content += " - " + ii + "\n"
content += " " + "'" + i.salt_hosts_name + "':\n" + salt_model_content
topsls.writelines(title)
topsls.writelines(content)
topsls.close()
return '{"status":1}'
except Exception as err:
return '{"status":3,"err":"%s"}' % err
2.Salt_model_group代码如下:
class Salt_models_group (models.Model):
class Meta:
verbose_name = 'Salt模块组'
verbose_name_plural = verbose_name
salt_models_group_member = models.CharField('Salt模块组成员',max_length=40)
salt_models_group_name = models.CharField('Salt模块组名称',max_length=40)
salt_models_group_change_at = models.DateTimeField('最后更改时间',auto_now=True)
def __unicode__(self):
return str(self.salt_models_group_name)