写在片头:2011年接触到了Puppet ,感叹老美对自动化的理解高了咱不是一个档次。2015年接触到了Saltstack,再次感叹。当时Saltstack还没有类似Puppet Foreman之类的Web管理,,从那时起,我就有了一个小小目标,我要山寨Foreman.....
Saltstack Web分为几块,下面用与Puppet Foreman对比的方式进行说明:
一、 环境
- Master和遍历程序在同一台服务器(Saltstack是否有对应的API可以远程操作models,暂时没研究)
- Django运行版本1.6.1
- Python运行版本2.7.1
二、 食用过程
思路:在Saltstack master 写好相应的模块,按照一定的格式放到对应的目录中,通过遍历将类的名称,路径,sls名称写入Mysql中,然后通过Web方式进行展现。先取出所有本地模块列表,组成一个本地模块列表。然后用本地模块列表的数量和远端数据库内所存的模块列表数量进行比较。本地多了,就在远端数据库添加,本地少了,就在远端数据库删除。
1.导入模块代码如下(根据实际情况调整目录)
def salt_model_import(request):
if request.method == 'POST':
os_list = []
add_count = 0
del_count = 0
try:
for i in os.listdir('/srv/salt/'):
""""遍历一级目录,判断目录下是否为目录"""
if os.path.isdir('/srv/salt/%s/'%i):
for ii in os.listdir('/srv/salt/%s/'%i):
""""遍历二级目录,判断目录下是否为文件且为sls结尾"""
if os.path.isfile('/srv/salt/%s/%s' %(i,ii)) and str('/srv/salt/%s/%s' %(i,ii)).endswith('sls'):
os_list.append('%s.%s' %(i,ii.split('.')[0]))
else:
pass
for i in os.listdir('/srv/salt/'):
""""遍历一级目录,判断目录下是否为目录"""
if os.path.isdir('/srv/salt/%s/'%i):
for ii in os.listdir('/srv/salt/%s/'%i):
""""遍历二级目录,判断目录下是否为文件且为sls结尾"""
if os.path.isfile('/srv/salt/%s/%s' %(i,ii)) and str('/srv/salt/%s/%s' %(i,ii)).endswith('sls'):
"""与数据库内容进行匹对"""
if len(os_list) > Salt_model.objects.all().count():
if Salt_model.objects.filter(salt_model_name=i, salt_model_sls_name=ii, salt_model_dir='/srv/salt/%s/%s' %(i,ii)):
pass
else:
Salt_model(salt_model_name=i, salt_model_sls_name=ii, salt_model_dir='/srv/salt/%s/%s' %(i,ii)).save()
add_count = add_count + 1
elif len(os_list) < Salt_model.objects.all().count():
for i in Salt_model.objects.all():
if '%s.%s'%(i.salt_model_name,i.salt_model_sls_name.split('.')[0]) not in os_list:
Salt_model.objects.filter(salt_model_name=i.salt_model_name, salt_model_sls_name=i.salt_model_sls_name).delete()
del_count = del_count + 1
else:
pass
else:
pass
else:
pass
return HttpResponse('{"status":1,"add_action":%s,"del_action":%s}' % (add_count, del_count))
except Exception as err:
return HttpResponse('{"status":0,"err":%s}' % err)
else:
return HttpResponse('{"status":0}')
数据结果存储如下:
2.模块组代码如下(根据实际情况调整目录)
思路:数据库已经存储了对应的模块名称和路径,我们只需要借助模块组的概念,让目标主机挨个通过State.sls挨个去执行对应的模块即可。
def salt_models_group_edit(request):
if request.method == 'GET':
edit_group_id = request.GET['edit_group_id']
salt_models_group = Salt_models_group.objects.get(id = edit_group_id)
salt_models_group_member_list = salt_models_group.salt_models_group_member.split(',')
salt_model = Salt_model.objects.all().order_by('salt_model_name')
salt_model_list = []
for i in salt_model:
salt_model_list.append('%s.%s'%(i.salt_model_name,i.salt_model_sls_name.split(".")[0]))
list = []
for ii in salt_model_list:
if ii in salt_models_group_member_list:
list.append('<option selected>%s</option>' % str(ii))
else:
list.append('<option>%s</option>' % str(ii))
return render_to_response("mysalt/salt_models_group_edit.html",{'list':list},context_instance=RequestContext(request))
else:
edit_group_id = request.META['QUERY_STRING'].split('=')[-1]
salt_models_select_list = request.POST.getlist('salt_models_select',[])
salt_models_group = Salt_models_group.objects.get(id = edit_group_id)
salt_models_group_member_list = salt_models_group.salt_models_group_member.split(',')
for i in salt_models_select_list:
if i not in salt_models_group_member_list:
salt_models_group_member_list.append(i)
for i in salt_models_group_member_list:
if i not in salt_models_select_list:
salt_models_group_member_list.remove(i)
Salt_models_group.objects.filter(id=edit_group_id).update(salt_models_group_member=(','.join(salt_models_group_member_list)).lstrip(','))
return HttpResponseRedirect('/cmdb/salt_models_group/')
def salt_models_group_add (request):
"""1为添加成功,0为不成功"""
if request.method == 'POST':
salt_models_group_name = request.POST['salt_models_group_name']
Salt_models_group(salt_models_group_name = salt_models_group_name).save()
return HttpResponse('{"status":1}')
else:
return HttpResponse('{"status":0}')
def salt_models_group_del (request):
"""1为添加成功,0为不成功"""
if 'salt_models_group' in request.GET:
salt_models_group = request.GET['salt_models_group']
Salt_models_group.objects.filter(salt_models_group_name = salt_models_group).delete()
return HttpResponse('{"status":1}')
else:
return HttpResponse('{"status":0}')
数据结果存储如下: