def action(request): ''' 接收参数,并调用salt-api,得到结果返回数据 :param request: :return: ''' group_gl_obj = group_gl() if request.method == 'GET': #验证用户是否已经登陆,检查用户session if request.session.get('user', False): all_group = group_gl_obj.show_all_group() return render(request, 'action.html', {'all_group': all_group}) else: return redirect('/login/') if request.method == 'POST': group_name = request.POST.get('hosts') #接收模块 mudule_func = request.POST.get('module') #接收参数列表 args = request.POST.get('args') args = json.loads(args) #调用api salt_api_obj = salt_api() res = salt_api_obj.cmd(group_name, mudule_func, args) data = correct_Data(res) try: return HttpResponse(json.dumps(data), content_type='application/json') except UnicodeDecodeError: # data = correct_Data(res) return HttpResponse(json.dumps(data), content_type='application/json')
def cmd(self,group_name,mudules,args): ''' 命令执行方法 :param hosts: :param mudules: :param args: :return: ''' group_gl_obj = group_gl() if group_name == '*': hosts = group_name else: hosts = group_gl_obj.show_hosts(group_name) print(6,hosts,mudules,args) if type(hosts) == list : if len(hosts) > 100: host_number = len(hosts) self.timeout += int(host_number / 50) data = self.__Client.cmd(hosts, mudules, args,expr_form='list',timeout=self.timeout) self.log_write(log_content="对%s执行了%s命令,参数为%s" % (group_name,mudules,args),type='action') else: all_host = self.__key.list_keys()['minions'] self.timeout += int(len(all_host) / 50) data = self.__Client.cmd(hosts,mudules,args,timeout=self.timeout) self.log_write(log_content="对%s执行了%s命令,参数为%s" % (group_name,mudules,args),type='action') return data
def group(request): ''' 组管理页的方法 :param request: :return: ''' group_gl_obj = group_gl() if request.method == 'GET': all_group = group_gl_obj.show_all_group() return render(request, 'group.html', {'all_group': all_group}) elif request.method == 'POST': group_name = request.POST.get('del_group') result = group_gl_obj.del_group(group_name) return HttpResponse(result)
def edit_group(request, group=None): ''' 新建组的方法 :param request: :return: ''' host_gl_obj = host_gl() group_gl_obj = group_gl() if request.method == 'GET': if group == None: host_list = host_gl_obj.show_host() return render(request, 'edit_group.html', {'host_list': host_list}) else: host_list = host_gl_obj.show_host() group_obj = group_gl_obj.Scolopendra_db.group.find_one( {'group_name': group}) group_info = { 'group_name': group_obj['group_name'], 'group_description': group_obj['group_description'], 'group_hosts': group_obj['group_hosts'] } return render(request, 'edit_group.html', { 'host_list': host_list, 'group_info': json.dumps(group_info) }) elif request.method == 'POST': action = request.POST.get('action') group_name = request.POST.get('group_name') group_hosts_str = request.POST.get('group_table') group_hosts = eval(group_hosts_str) group_description = request.POST.get('group_description') group_hosts_number = len(group_hosts) print(action, group_name, group_hosts, group_description) group = { 'group_name': group_name, 'group_hosts': group_hosts, 'group_description': group_description, 'group_hosts_number': group_hosts_number } if hasattr(group_gl_obj, action): func = getattr(group_gl_obj, action) status = func(group) else: status = 'Function Exist' return HttpResponse(status)
def detail_group(request, group): group_gl_obj = group_gl() if request.method == 'GET': group_info = group_gl_obj.Scolopendra_db.group.find_one( {'group_name': group}) return render(request, "detail_group.html", {'group_info': group_info})