Ejemplo n.º 1
0
def server_update(request,id):
    """
    更新上报数据
    """
    context = {}
    edit_id = Host.objects.get(id=id)
    token_api_id = token_id()
    list_all = salt_api_token(
    {
    'client': 'local_async',
    'fun': 'grains.items',
    'tgt': edit_id.node_name,
                   },
    salt_api_url,
    {"X-Auth-Token": token_api_id}
    )
    list_all = list_all.run()
    for i in list_all["return"]:
        try:
            context["jid"] = i["jid"]
            context["minions"] = i["minions"]
        except KeyError:
            return render_to_response('assets/update_error.html', context, context_instance=RequestContext(request))
    jobs_id = context["jid"]
    jobs_url = salt_api_url + "/jobs/" + jobs_id
    minions_list_all = salt_api_jobs(
    jobs_url,
    {"X-Auth-Token": token_api_id}
    )
    voilet_test = minions_list_all.run()
    for i in voilet_test["return"]:
        update_keys = i.keys()
        try:
            update_key = update_keys[0]
        except IndexError:
            return render_to_response('assets/update_error.html', context, context_instance=RequestContext(request))
        context["cmd_run"] = i[update_key]
    # context["eth0"] = context["cmd_run"]["ip_interfaces"]["eth0"][0]
    # try:
    #
    if "eth0" in context["cmd_run"]["ip_interfaces"]:
        edit_id.eth1 = context["cmd_run"]["ip_interfaces"]["eth0"][0]
    elif "em1" in context["cmd_run"]["ip_interfaces"]:
        edit_id.eth1 = context["cmd_run"]["ip_interfaces"]["em1"][0]
    else:
        edit_id.eth1 = "127.0.0.1"
    if "eth1" in context["cmd_run"]["ip_interfaces"]:
    # try:
        edit_id.eth2 = context["cmd_run"]["ip_interfaces"]["eth1"][0]
    # except IndexError:
    else:
        edit_id.eth2 = ""
    edit_id.memory = context["cmd_run"]["mem_total"]
    edit_id.core_num = int(context["cmd_run"]["num_cpus"])
    edit_id.system = context["cmd_run"]["os"]
    edit_id.system_version = context["cmd_run"]["lsb_distrib_release"]
    edit_id.cpu = context["cmd_run"]["cpu_model"]
    edit_id.save()
    #日志入库
    return HttpResponse('<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><h4>数据更新完成,三秒后关闭</h4></div>')
Ejemplo n.º 2
0
def salt_cmd(request):
    context = {}
    type_node = ""
    node_list = []
    if request.method == 'POST':
        salt_text = request.POST
        service_type = salt_text.getlist("business_node")
        # for i in service_type:
        #     service_name_type = get_object_or_404(MyForm,service_name = i)
        #     server_list = service_name_type.host_set.all()
        #     for s in server_list:
        #         type_node += "%s," % (s.node_name)
        # context["type_node"] = type_node
        for i in service_type:
            i = '%s' % (i)
            node_list.append(i.encode("utf-8"))
        salt_api_type = salt_text['comm_shell']
        if salt_api_type == "cmd":
            salt_cmd_lr = salt_text['salt_cmd']
            if len(service_type) > 0:
                salt_node_name = node_list
            elif len(salt_text["salt_node_name"]) >0:
                for i in salt_text["salt_node_name"]:
                    i = '%s ' % (i)
                    node_list.append(i.encode("utf-8"))
            else:
                salt_node_name = "*"
            token_api_id = token_id()
            list_all = salt_api_token({'fun': 'cmd.run', 'tgt': salt_node_name, 'arg': salt_cmd_lr, 'expr_form':'list'}, salt_api_url, { 'X-Auth-Token' : token_api_id})
            list_all = list_all.run()
            for i in list_all["return"]:
                context["jid"] =  i["jid"]
                context["minions"] = i["minions"]
            jobs_id = context["jid"]
            jobs_url = salt_api_url + "/jobs/" + jobs_id
            minions_list_all = salt_api_jobs(
            jobs_url,
            {"X-Auth-Token": token_api_id}
            )
            voilet_test = minions_list_all.run()
            for i in voilet_test["return"]:
                context["cmd_run"] = i
            context["cmd_Advanced"] = False
            context["salt_cmd"] = salt_text['salt_cmd']
            context["len_node"] = len(context["minions"])
            context.update(csrf(request))
            # print yaml.dump(context["cmd_run"])
            #日志入库
            salt_log(request.user.username, context["minions"], int(jobs_id), salt_api_type, context["len_node"], salt_cmd_lr, context["cmd_run"])
            return render_to_response('saltstack/salt_cmd_run.html', context, context_instance=RequestContext(request))
            #     #return HttpResponse(json.dumps(cmd))
        elif salt_api_type == "grains" :
            salt_cmd_lr = salt_text['salt_cmd']
            if len(salt_text["salt_node_name"]) >0:
                salt_node_name = salt_text["salt_node_name"]
            else:
                salt_node_name = "*"
            token_api_id = token_id()
            list_all = salt_api_token(
            {
            # 'client': 'local',
            'fun': 'grains.item',
            'tgt':salt_node_name,
            'arg':salt_cmd_lr ,
                           },
            salt_api_url,
            {"X-Auth-Token": token_api_id}
            )
            list_all = list_all.run()
            for i in list_all["return"]:
                context["jid"] =  i["jid"]
                context["minions"] = i["minions"]
            jobs_id = context["jid"]
            jobs_url = salt_api_url + "/jobs/" + jobs_id
            minions_list_all = salt_api_jobs(
            jobs_url,
            {"X-Auth-Token": token_api_id}
            )
            voilet_test = minions_list_all.run()
            for i in voilet_test["return"]:
                print type(i)
                context["cmd_run"] = i
            context["cmd_Advanced"]=False
            context["salt_cmd"]=salt_text['salt_cmd']
            context["len_node"] = len(context["minions"])
            context.update(csrf(request))
            #日志入库
            salt_log(request.user.username, context["minions"], int(jobs_id), salt_api_type, context["len_node"], salt_cmd_lr, context["cmd_run"])
            return render_to_response('saltstack/salt_cmd_grains_run.html', context, context_instance=RequestContext(request))
        elif salt_api_type == "ping":
            salt_cmd_lr = salt_text['salt_cmd']
            if len(salt_text["salt_node_name"]) > 0:
                salt_node_name = salt_text["salt_node_name"]
            else:
                salt_node_name = "*"
            token_api_id = token_id()
            list_all = salt_api_token(
            {
            # 'client': 'local',
            'fun': 'test.ping',
            'tgt':salt_node_name,
                           },
            salt_api_url,
            {"X-Auth-Token": token_api_id}
            )
            list_all = list_all.run()
            for i in list_all["return"]:
                context["jid"] =  i["jid"]
                context["minions"] = i["minions"]
            jobs_id = context["jid"]
            jobs_url = salt_api_url + "/jobs/" + jobs_id
            minions_list_all = salt_api_jobs(
            jobs_url,
            {"X-Auth-Token": token_api_id}
            )
            voilet_test = minions_list_all.run()
            for i in voilet_test["return"]:
                print type(i)
                context["cmd_run"] = i
            context["cmd_Advanced"]=True
            context["salt_cmd"]=salt_text['salt_cmd']
            context["len_node"] = len(context["minions"])
            context.update(csrf(request))
            #日志入库
            salt_log(request.user.username, context["minions"], int(jobs_id), salt_api_type, context["len_node"], salt_cmd_lr, context["cmd_run"])
            return render_to_response('saltstack/test_ping.html', context, context_instance=RequestContext(request))
        else:
            return render_to_response('saltstack/salt_cmd_run.html', context, context_instance=RequestContext(request))
Ejemplo n.º 3
0
def salt_cmd(request):
    """CMD选项功能"""
    context = {}
    salt_node_name = ""
    salt_node_name_null = ""
    node_list = []
    if request.method == 'POST':
        salt_text = request.POST
        business_node = salt_text.getlist("business_node")
        for i in business_node:
            i = '%s' % (i)
            node_list.append(i.encode("utf-8"))
        salt_api_type = salt_text['comm_shell']
        # 选择cmd类型执行方法
        if salt_api_type == "cmd":
            salt_cmd_lr = salt_text['salt_cmd']
            # 如果使用危险命令则返回无权限
            if str(salt_cmd_lr).split()[0] in auth_content:
                context["auth_content"] = True
                context.update(csrf(request))
                return render_to_response('saltstack/salt_cmd_run.html', context,
                                          context_instance=RequestContext(request))
            # 是可指定主机,注意,此权限较高
            if "salt_node_name" in salt_text:
                if len(business_node) > 0 and len(salt_text["salt_node_name"]) > 0:
                    node_list.append(salt_text["salt_node_name"])
                elif len(business_node) > 0 and len(salt_text["salt_node_name"]) == 0:
                    node_list = business_node
                elif len(business_node) == 0 and len(salt_text["salt_node_name"]) > 0:
                    salt_node_name_null += salt_text["salt_node_name"]
                elif len(salt_text["salt_node_name"]) > 0:
                    node_list = salt_text["salt_node_name"]
                else:
                    salt_node_name_null += "*"
            else:
                node_list = business_node
            token_api_id = token_id()
            if len(node_list) >= 2:
                list_all = salt_api_token({'fun': 'cmd.run', 'tgt': node_list,
                                           'arg': salt_cmd_lr, 'expr_form': 'list'},
                                          salt_api_url, {'X-Auth-Token': token_api_id})
                list_all = list_all.run()
                # time.sleep(5)
                for i in list_all["return"]:
                    context["jid"] = i["jid"]
                    context["minions"] = i["minions"]
                    jobs_id = context["jid"]
                    jobs_url = salt_api_url + "/jobs/" + jobs_id
                    minions_list_all = salt_api_jobs(
                        jobs_url,
                        {"X-Auth-Token": token_api_id}
                    )
                    voilet_test = minions_list_all.run()
                    for i in voilet_test["return"]:
                        context["cmd_run"] = i
                        context["cmd_Advanced"] = False
                        context["salt_cmd"] = salt_text['salt_cmd']
                        context["len_node"] = len(i.keys())
                    context.update(csrf(request))
                    # 日志入库
                    salt_log(request.user.username, context["minions"], int(jobs_id),
                             salt_api_type, context["len_node"], salt_cmd_lr, context["cmd_run"])
                    return render_to_response('saltstack/salt_cmd_run.html', context,
                                              context_instance=RequestContext(request))
            elif len(node_list) == 1:
                list_all = salt_api_token({'client': 'local', 'fun': 'cmd.run',
                                           'tgt': node_list, 'arg': salt_cmd_lr},
                                          salt_api_url, {'X-Auth-Token': token_api_id})
                voilet_test = list_all.run()
                for i in voilet_test["return"]:
                    # print i.keys()
                    context["cmd_run"] = i
                    context["cmd_Advanced"] = False
                    context["salt_cmd"] = salt_text['salt_cmd']
                    context["len_node"] = len(i.keys())
                    context["minions"] = i.keys()
                context.update(csrf(request))
                # 日志入库
                salt_log(request.user.username, context["minions"], '', salt_api_type,
                         context["len_node"], salt_cmd_lr, context["cmd_run"])
                return render_to_response('saltstack/salt_cmd_run.html', context,
                                          context_instance=RequestContext(request))
            elif salt_node_name:
                list_all = salt_api_token({'client': 'local', 'fun': 'cmd.run',
                                           'tgt': salt_node_name, 'arg': salt_cmd_lr},
                                          salt_api_url, {'X-Auth-Token': token_api_id})
                voilet_test = list_all.run()
                for i in voilet_test["return"]:
                    context["cmd_run"] = i
                    context["cmd_Advanced"] = False
                    context["salt_cmd"] = salt_text['salt_cmd']
                    context["len_node"] = len(i.keys())
                    context["minions"] = i.keys()
                context.update(csrf(request))
                # 日志入库
                salt_log(request.user.username, context["minions"], '',
                         salt_api_type, context["len_node"], salt_cmd_lr, context["cmd_run"])
                return render_to_response('saltstack/salt_cmd_run.html', context,
                                          context_instance=RequestContext(request))
            elif salt_node_name_null:
                list_all = salt_api_token({'client': 'local', 'fun': 'cmd.run',
                                           'tgt': salt_node_name_null, 'arg': salt_cmd_lr},
                                          salt_api_url, {'X-Auth-Token': token_api_id})
                list_all = list_all.run()
                for i in list_all["return"]:
                    context["cmd_run"] = i
                    context["cmd_Advanced"] = False
                    context["salt_cmd"] = salt_text['salt_cmd']
                    context["len_node"] = len(i.keys())
                    context["minions"] = i.keys()
                context.update(csrf(request))
                # print yaml.dump(context["cmd_run"])
                # 日志入库
                salt_log(request.user.username, context["minions"], '', salt_api_type, context["len_node"], salt_cmd_lr,
                         context["cmd_run"])
                return render_to_response('saltstack/salt_cmd_run.html', context,
                                          context_instance=RequestContext(request))
            elif len(node_list) == 0:
                context["cmd_Advanced"] = False
                context.update(csrf(request))
                return render_to_response('saltstack/salt_cmd_run.html', context,
                                          context_instance=RequestContext(request))
        # 如果选择类型是grains
        elif salt_api_type == "grains":
            salt_cmd_lr = salt_text['salt_cmd']
            if "salt_node_name" in salt_text:
                if len(business_node) > 0 and len(salt_text["salt_node_name"]) > 0:
                    node_list.append(salt_text["salt_node_name"])
                elif len(business_node) > 0 and len(salt_text["salt_node_name"]) == 0:
                    node_list = business_node
                elif len(business_node) == 0 and len(salt_text["salt_node_name"]) > 0:
                    salt_node_name += salt_text["salt_node_name"]
                elif len(salt_text["salt_node_name"]) > 0:
                    node_list = salt_text["salt_node_name"]
                else:
                    salt_node_name_null += "*"
            else:
                node_list = business_node
            token_api_id = token_id()
            if len(node_list) >= 2:
                list_all = salt_api_token({'fun': 'grains.item', 'tgt': node_list,
                                           'arg': salt_cmd_lr, 'expr_form': 'list'},
                                          salt_api_url, {'X-Auth-Token': token_api_id})
                list_all = list_all.run()
                for i in list_all["return"]:
                    context["jid"] = i["jid"]
                    context["minions"] = i["minions"]
                    jobs_id = context["jid"]
                    jobs_url = salt_api_url + "/jobs/" + jobs_id
                    minions_list_all = salt_api_jobs(
                        jobs_url,
                        {"X-Auth-Token": token_api_id}
                    )
                    voilet_test = minions_list_all.run()
                    for i in voilet_test["return"]:
                        context["cmd_run"] = i
                        context["cmd_Advanced"] = False
                        context["salt_cmd"] = salt_text['salt_cmd']
                        context["len_node"] = len(i.keys())
                    context.update(csrf(request))
                    # 日志入库
                    salt_log(request.user.username, context["minions"], int(jobs_id),
                             salt_api_type, context["len_node"], salt_cmd_lr, context["cmd_run"])
                    return render_to_response('saltstack/salt_cmd_grains_run.html',
                                              context, context_instance=RequestContext(request))
            elif len(node_list) == 1:
                list_all = salt_api_token({'client': 'local', 'fun': 'grains.item',
                                           'tgt': node_list, 'arg': salt_cmd_lr},
                                          salt_api_url, {'X-Auth-Token': token_api_id})
                voilet_test = list_all.run()
                for i in voilet_test["return"]:
                    # print i.keys()
                    context["cmd_run"] = i
                    context["cmd_Advanced"] = False
                    context["salt_cmd"] = salt_text['salt_cmd']
                    context["len_node"] = len(i.keys())
                    context["minions"] = i.keys()
                context.update(csrf(request))
                # 日志入库
                salt_log(request.user.username, context["minions"], '', salt_api_type,
                         context["len_node"], salt_cmd_lr, context["cmd_run"])
                return render_to_response('saltstack/salt_cmd_grains_run.html', context,
                                          context_instance=RequestContext(request))
            elif salt_node_name:
                list_all = salt_api_token({'client': 'local', 'fun': 'grains.item',
                                           'tgt': salt_node_name, 'arg': salt_cmd_lr},
                                          salt_api_url, {'X-Auth-Token': token_api_id})
                voilet_test = list_all.run()
                for i in voilet_test["return"]:
                    context["cmd_run"] = i
                    context["cmd_Advanced"] = False
                    context["salt_cmd"] = salt_text['salt_cmd']
                    context["len_node"] = len(i.keys())
                    context["minions"] = i.keys()
                context.update(csrf(request))
                # 日志入库
                salt_log(request.user.username, context["minions"], '',
                         salt_api_type, context["len_node"], salt_cmd_lr, context["cmd_run"])
                return render_to_response('saltstack/salt_cmd_grains_run.html', context,
                                          context_instance=RequestContext(request))
            elif salt_node_name_null:
                list_all = salt_api_token({'client': 'local', 'fun': 'grains.item',
                                           'tgt': salt_node_name_null, 'arg': salt_cmd_lr},
                                          salt_api_url, {'X-Auth-Token': token_api_id})
                list_all = list_all.run()
                for i in list_all["return"]:
                    context["cmd_run"] = i
                    context["cmd_Advanced"] = False
                    context["salt_cmd"] = salt_text['salt_cmd']
                    context["len_node"] = len(i.keys())
                    context["minions"] = i.keys()
                context.update(csrf(request))
                # 日志入库
                salt_log(request.user.username, context["minions"], '',
                         salt_api_type, context["len_node"], salt_cmd_lr, context["cmd_run"])
                return render_to_response('saltstack/salt_cmd_grains_run.html', context,
                                          context_instance=RequestContext(request))
            elif len(node_list) == 0:
                context["cmd_Advanced"] = False
                context.update(csrf(request))
                return render_to_response('saltstack/salt_cmd_grains_run.html',
                                          context, context_instance=RequestContext(request))
        else:
            return render_to_response('saltstack/salt_cmd_run.html', context, context_instance=RequestContext(request))
Ejemplo n.º 4
0
def salt_update_node(request):
    context = {}
    update_name = request.GET['node_name']
    if request.method == 'POST':    #验证post方法
        uf = Host_from(request.POST)   #绑定POST动作
        if uf.is_valid(): #验证数据有效性
            # uf.save()
            zw = uf.save(commit=False)
            zw.edit_username = request.user.username
            zw.auto_install = 1
            zw.save()
            uf.save_m2m()
            uf = Host_from()
            context['uf'] = uf
            context.update(csrf(request))
            return HttpResponseRedirect("/assets/server/list/")
            # return render_to_response('server_idc/index.html',context,context_instance=RequestContext(request))
        else:
            print "save error"
            uf = Host_from()
            context["server_type"] = MyForm.objects.all()
            context['uf'] = uf
            context.update(csrf(request))
            return render_to_response('server_idc/index.html',context,context_instance=RequestContext(request))

    if len(update_name) >0 and Host.objects.filter(node_name = update_name).count() == 0 :
        token_api_id = token_id()
        list = salt_api_token({'fun': 'grains.items', 'tgt':update_name}, salt_api_url, {"X-Auth-Token": token_api_id})
        master_status =list.run()
        #同步本地grains
        salt_garins_sync = salt_api_token({'fun': 'saltutil.sync_all', 'tgt':update_name, 'client':'local'}, salt_api_url, {"X-Auth-Token": token_api_id})
        salt_garins_sync.run()
        #重新加载模块
        salt_garins_reload = salt_api_token({'fun': 'sys.reload_modules', 'tgt':update_name, 'client':'local'}, salt_api_url, {"X-Auth-Token": token_api_id})
        salt_garins_reload.run()
        for i in master_status["return"]:
                context["jid"] =  i["jid"]
                context["minions"] = i["minions"]
        jobs_id = context["jid"]
        jobs_url = salt_api_url + "/jobs/" + jobs_id
        minions_list_all = salt_api_jobs(
        jobs_url,
        {"X-Auth-Token": token_api_id}
        )
        voilet_test = minions_list_all.run()
        uf = Host_from()
        for i in voilet_test["return"]:
            system_os =i[update_name]["os"]
            osarch = i[update_name]["osarch"]
            try:
                ipinfo = i[update_name]["ipinfo"]
                for mac in ipinfo:
                    mac = mac["MAC"].replace(":", "-")
            except KeyError:
                mac = ""
            context["eth0"] = i[update_name]["ipv4"][1]
            try:
                context["eth1"] = i[update_name]["ipv4"][2]
            except IndexError:
                context["eth1"] = False
            context["update_name"] = update_name
            context["mac"] = mac
            context["system_os"] = system_os
            context["osarch"] = osarch
            context["uf"] = uf
            context["os"] = i[update_name]["os"]
            context["edit_brand"] = Server_System
            context["edit_Cores"] = Cores
            context["edit_system"] = System_os
            context["edit_system_arch"] = system_arch
            context["server_type"] = MyForm.objects.all()
            context.update(csrf(request))
        return render_to_response('saltstack/node_add.html',context,context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect("/assets/server/list/")
Ejemplo n.º 5
0
def server_update(request,id):
    context = {}
    edit_id = Host.objects.get(id = id)
    #机房名称
    idc_name = IDC.objects.all()
    server_type = MyForm.objects.all()
    if request.method == 'POST':    #验证post方法
        uf = Host_from(request.POST)   #绑定POST动作
        if uf.is_valid(): #验证数据有效性
            uf.auto_id = edit_id.id
            zw = uf.save(commit=False)
            zw.edit_username = request.user.username
            zw.old_editname = edit_id.edit_username
            zw.old_editdatetime = edit_id.edit_datetime
            zw.create_time = edit_id.create_time
            zw.auto_install = edit_id.auto_install
            zw.id=edit_id.id
            zw.save()
            uf.save_m2m()
            print "保存数据"
            # print request.user.username, edit_id.node_name,"saltstack更新", edit_id.edit_username, edit_id.edit_datetime, edit_id.id, request.user.id
            idc_log(request.user.username, edit_id.node_name,"saltstack更新", edit_id.edit_username, edit_id.edit_datetime, edit_id.id, request.user.id)
            return HttpResponseRedirect('/assets/server/node_id/'+id)
        else:
            print "is over"
    else:
        uf = Host_from()
        context["uf"] = uf
        context["edit_brand"] = Server_System
        context["edit_Cores"] = Cores
        context["edit_system"] = System_os
        context["edit_system_cpuarch"] = system_arch
        context["server_type"] = server_type
        context["edit_id"] = edit_id
        context["server_name"] = idc_name
        context["edit_usage"] = System_usage
        token_api_id = token_id()
        list_all = salt_api_token(
        {
        # 'client': 'local',
        'fun': 'grains.items',
        'tgt':edit_id.node_name,
        # 'arg':salt_cmd_lr ,
                       },
        salt_api_url,
        {"X-Auth-Token": token_api_id}
        )
        list_all = list_all.run()
        for i in list_all["return"]:
            try:
                context["jid"] =  i["jid"]
                context["minions"] = i["minions"]
            except KeyError:
                return render_to_response('server_idc/update_error.html',context,context_instance=RequestContext(request))
        jobs_id = context["jid"]
        jobs_url = salt_api_url + "/jobs/" + jobs_id
        minions_list_all = salt_api_jobs(
        jobs_url,
        {"X-Auth-Token": token_api_id}
        )
        voilet_test = minions_list_all.run()
        for i in voilet_test["return"]:
            update_keys = i.keys()
            try:
                update_key = update_keys[0]
            except IndexError:
                return render_to_response('server_idc/update_error.html',context,context_instance=RequestContext(request))
            context["cmd_run"] = i[update_key]
        context["eth0"] = context["cmd_run"]["ipv4"][1]
        try:
            context["eth1"] = context["cmd_run"]["ipv4"][2]
        except IndexError:
            context["eth1"] = False
        context["mem_total"] = context["cmd_run"]["mem_total"]
        context["num_cpus"] = int(context["cmd_run"]["num_cpus"])
        context["os"] = context["cmd_run"]["os"]
        context["lsb_distrib_release"] = context["cmd_run"]["lsb_distrib_release"]
        context["cpu_model"] = context["cmd_run"]["cpu_model"]
        context.update(csrf(request))
        #日志入库
        return render_to_response('server_idc/update.html',context,context_instance=RequestContext(request))
Ejemplo n.º 6
0
def salt_cmd(request):
    context = {}
    type_node = ""
    node_list = []
    if request.method == 'POST':
        salt_text = request.POST
        service_type = salt_text.getlist("business_node")
        # for i in service_type:
        #     service_name_type = get_object_or_404(Project,service_name = i)
        #     server_list = service_name_type.host_set.all()
        #     for s in server_list:
        #         type_node += "%s," % (s.node_name)
        # context["type_node"] = type_node
        for i in service_type:
            i = '%s' % (i)
            node_list.append(i.encode("utf-8"))
        salt_api_type = salt_text['comm_shell']
        if salt_api_type == "cmd":
            salt_cmd_lr = salt_text['salt_cmd']
            if len(service_type) > 0:
                salt_node_name = node_list
            elif len(salt_text["salt_node_name"]) > 0:
                for i in salt_text["salt_node_name"]:
                    i = '%s ' % (i)
                    node_list.append(i.encode("utf-8"))
            else:
                salt_node_name = "*"
            token_api_id = token_id()
            list_all = salt_api_token(
                {
                    'fun': 'cmd.run',
                    'tgt': salt_node_name,
                    'arg': salt_cmd_lr,
                    'expr_form': 'list'
                }, salt_api_url, {'X-Auth-Token': token_api_id})
            list_all = list_all.run()
            for i in list_all["return"]:
                context["jid"] = i["jid"]
                context["minions"] = i["minions"]
            jobs_id = context["jid"]
            jobs_url = salt_api_url + "/jobs/" + jobs_id
            minions_list_all = salt_api_jobs(jobs_url,
                                             {"X-Auth-Token": token_api_id})
            voilet_test = minions_list_all.run()
            for i in voilet_test["return"]:
                context["cmd_run"] = i
            context["cmd_Advanced"] = False
            context["salt_cmd"] = salt_text['salt_cmd']
            context["len_node"] = len(context["minions"])
            context.update(csrf(request))
            # print yaml.dump(context["cmd_run"])
            #日志入库
            salt_log(request.user.username, context["minions"], int(jobs_id),
                     salt_api_type, context["len_node"], salt_cmd_lr,
                     context["cmd_run"])
            return render_to_response('saltstack/salt_cmd_run.html',
                                      context,
                                      context_instance=RequestContext(request))
            #     #return HttpResponse(json.dumps(cmd))
        elif salt_api_type == "grains":
            salt_cmd_lr = salt_text['salt_cmd']
            if len(salt_text["salt_node_name"]) > 0:
                salt_node_name = salt_text["salt_node_name"]
            else:
                salt_node_name = "*"
            token_api_id = token_id()
            list_all = salt_api_token(
                {
                    # 'client': 'local',
                    'fun': 'grains.item',
                    'tgt': salt_node_name,
                    'arg': salt_cmd_lr,
                },
                salt_api_url,
                {"X-Auth-Token": token_api_id})
            list_all = list_all.run()
            for i in list_all["return"]:
                context["jid"] = i["jid"]
                context["minions"] = i["minions"]
            jobs_id = context["jid"]
            jobs_url = salt_api_url + "/jobs/" + jobs_id
            minions_list_all = salt_api_jobs(jobs_url,
                                             {"X-Auth-Token": token_api_id})
            voilet_test = minions_list_all.run()
            for i in voilet_test["return"]:
                print type(i)
                context["cmd_run"] = i
            context["cmd_Advanced"] = False
            context["salt_cmd"] = salt_text['salt_cmd']
            context["len_node"] = len(context["minions"])
            context.update(csrf(request))
            #日志入库
            salt_log(request.user.username, context["minions"], int(jobs_id),
                     salt_api_type, context["len_node"], salt_cmd_lr,
                     context["cmd_run"])
            return render_to_response('saltstack/salt_cmd_grains_run.html',
                                      context,
                                      context_instance=RequestContext(request))
        elif salt_api_type == "ping":
            salt_cmd_lr = salt_text['salt_cmd']
            if len(salt_text["salt_node_name"]) > 0:
                salt_node_name = salt_text["salt_node_name"]
            else:
                salt_node_name = "*"
            token_api_id = token_id()
            list_all = salt_api_token(
                {
                    # 'client': 'local',
                    'fun': 'test.ping',
                    'tgt': salt_node_name,
                },
                salt_api_url,
                {"X-Auth-Token": token_api_id})
            list_all = list_all.run()
            for i in list_all["return"]:
                context["jid"] = i["jid"]
                context["minions"] = i["minions"]
            jobs_id = context["jid"]
            jobs_url = salt_api_url + "/jobs/" + jobs_id
            minions_list_all = salt_api_jobs(jobs_url,
                                             {"X-Auth-Token": token_api_id})
            voilet_test = minions_list_all.run()
            for i in voilet_test["return"]:
                print type(i)
                context["cmd_run"] = i
            context["cmd_Advanced"] = True
            context["salt_cmd"] = salt_text['salt_cmd']
            context["len_node"] = len(context["minions"])
            context.update(csrf(request))
            #日志入库
            salt_log(request.user.username, context["minions"], int(jobs_id),
                     salt_api_type, context["len_node"], salt_cmd_lr,
                     context["cmd_run"])
            return render_to_response('saltstack/test_ping.html',
                                      context,
                                      context_instance=RequestContext(request))
        else:
            return render_to_response('saltstack/salt_cmd_run.html',
                                      context,
                                      context_instance=RequestContext(request))