Esempio n. 1
0
File: cmd.py Progetto: haiya512/cmdb
def cmd_run(request):
    if request.method == 'POST':
        data = request.POST.copy()
        node_list = data.getlist("node_name")  # minions
        cmd_type = request.GET.get("args", False)
        cmd = data.get("salt_cmd", None)  #
        token_api_id = token_id()
        project = data.get("project")
        if cmd_type == "cmd":
            # 如果使用危险命令则返回无权限
            der_cmd = cmd.replace(";", " ").split()
            if len(cmd) == 0:
                salt_cmd = cmd
                return render_to_response('autoinstall/cmd_run_status.html',
                                          locals(), context_instance=RequestContext(request))
            for drop_shell in der_cmd:
                drop = drop_shell.split(";")
                drop_data = "<h3>操作人: %s<br>主机列表:%s<br><br>命令: %s</h3><br>" % (request.user.username, node_list, cmd)

                if drop[0] in auth_content:
                    return render_to_response('autoinstall/cmd_run.html', locals(),
                                              context_instance=RequestContext(request))

            list_all = salt_api_token({'fun': 'cmd.run', 'tgt': node_list,
                                       'arg': cmd},
                                      salt_api_url, {'X-Auth-Token': token_api_id})
            list_all = list_all.CmdRun()
            result = list_all["return"][0]
            fqdn_sum = len(node_list)
            return render_to_response('autoinstall/cmd_run.html', locals(), context_instance=RequestContext(request))

        # 如果选择类型是grains
        elif cmd_type == "grains":
            if len(cmd) == 0:
                salt_cmd = cmd
                return render_to_response('autoinstall/cmd_run_status.html',
                                          locals(), context_instance=RequestContext(request))
            list_all = salt_api_token({'fun': 'grains.item', 'tgt': node_list,
                                       'arg': cmd,},
                                      salt_api_url, {'X-Auth-Token': token_api_id})
            list_all = list_all.run()
            result = list_all["return"][0]
            jid = result['jid']
            # 日志入库
            salt_log(request.user.username, node_list, int(jid), "推送配置", len(node_list), cmd, jid)

            return render_to_response('autoinstall/cmd_run.html',
                                      locals(), context_instance=RequestContext(request))
        else:
            a = ''
            return render_to_response('autoinstall/cmd_run_status.html',
                                      locals(), context_instance=RequestContext(request))
    else:
        return render_to_response('autoinstall/cmd_run.html',
                                      locals(), context_instance=RequestContext(request))
Esempio 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(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))
Esempio n. 3
0
def cmd_run(request):
    if request.method == 'POST':
        data = request.POST.copy()
        node_list = data.getlist("node_name")  # minions
        cmd_type = request.GET.get("args", False)
        cmd = data.get("salt_cmd", None)  #
        token_api_id = token_id()
        project = data.get("project")
        if cmd_type == "cmd":
            # 如果使用危险命令则返回无权限
            der_cmd = cmd.replace(";", " ").split()
            if len(cmd) == 0:
                salt_cmd = cmd
                return render_to_response(
                    'autoinstall/cmd_run_status.html',
                    locals(),
                    context_instance=RequestContext(request))
            for drop_shell in der_cmd:
                drop = drop_shell.split(";")
                drop_data = "<h3>操作人: %s<br>主机列表:%s<br><br>命令: %s</h3><br>" % (
                    request.user.username, node_list, cmd)

                if drop[0] in auth_content:
                    return render_to_response(
                        'autoinstall/cmd_run.html',
                        locals(),
                        context_instance=RequestContext(request))

            list_all = salt_api_token(
                {
                    'fun': 'cmd.run',
                    'tgt': node_list,
                    'arg': cmd
                }, salt_api_url, {'X-Auth-Token': token_api_id})
            list_all = list_all.CmdRun()
            result = list_all["return"][0]
            fqdn_sum = len(node_list)
            return render_to_response('autoinstall/cmd_run.html',
                                      locals(),
                                      context_instance=RequestContext(request))

        # 如果选择类型是grains
        elif cmd_type == "grains":
            if len(cmd) == 0:
                salt_cmd = cmd
                return render_to_response(
                    'autoinstall/cmd_run_status.html',
                    locals(),
                    context_instance=RequestContext(request))
            list_all = salt_api_token(
                {
                    'fun': 'grains.item',
                    'tgt': node_list,
                    'arg': cmd,
                }, salt_api_url, {'X-Auth-Token': token_api_id})
            list_all = list_all.run()
            result = list_all["return"][0]
            jid = result['jid']
            # 日志入库
            salt_log(request.user.username, node_list, int(jid), "推送配置",
                     len(node_list), cmd, jid)

            return render_to_response('autoinstall/cmd_run.html',
                                      locals(),
                                      context_instance=RequestContext(request))
        else:
            return render_to_response('autoinstall/cmd_run_status.html',
                                      locals(),
                                      context_instance=RequestContext(request))
Esempio n. 4
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))
Esempio n. 5
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))
Esempio n. 6
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))