Ejemplo n.º 1
0
def salt_update_node(request):
    context = {}
    update_name = request.GET['node_name']
    if request.method == 'POST':    
        uf = Host_from(request.POST)
        if uf.is_valid():   
            # 查询数据库中是否有此数据
            zw = uf.save(commit=False)
            zw.edit_username = request.user.username
            zw.status = 1
            zw.vm = int(request.POST["vm"])
            business_id = request.POST.get('business')
            if business_id:
                business = Project.objects.get(id=business_id)
                zw.business = business
            # zw.save()
            uf = Host_from()
            context['uf'] = uf
            context.update(csrf(request))
            return HttpResponseRedirect("/salt/status/")
            # return render_to_response('assets/host_list.html',context,context_instance=RequestContext(request))
        else:
            print "save error"
            uf = Host_from()
            context["server_type"] = Project.objects.all()
            context['uf'] = uf
            context.update(csrf(request))
            return HttpResponseRedirect("/salt/status/")

    if len(update_name) > 0 and Host.objects.filter(node_name=update_name).count() == 0:
        token_api_id = token_id()
        #同步本地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()
        #执行模块
        list = salt_api_token({'client': 'local', 'fun': 'grains.items', 'tgt': update_name, 'timeout': 100}, salt_api_url, {"X-Auth-Token": token_api_id})
        master_status = list.run()
        uf = Host_from()
        for i in master_status["return"]:
            system_os = i[update_name]["os"]
            osarch = i[update_name]["osarch"]
            ipinfo = i[update_name]["hwaddr_interfaces"]["eth0"]
            mac = ipinfo.replace(":", "-")
            if "eth0" in i[update_name]["ip_interfaces"]:
                context["eth0"] = i[update_name]["ip_interfaces"]["eth0"][0]
            elif "em1" in i[update_name]["ip_interfaces"]:
                context["eth0"] = i[update_name]["ip_interfaces"]["em1"][0]
            else:
                context["eth0"] = "127.0.0.1"
            if "eth1" in i[update_name]["ip_interfaces"]:
            # try:
                context["eth1"] = i[update_name]["ip_interfaces"]["eth1"][0]
            # except IndexError:
            else:
                context["eth1"] = False
            # ip = request.POST.get("eth1")
            # data = select_node(context["eth0"])
            try:
                data = Host.objects.get(eth1=context["eth0"])
                data.node_name = i[update_name]["fqdn"]
                data.mac = mac
                data.core_num = i[update_name]["num_cpus"]
                data.system_version = i[update_name]["osrelease"]
                data.system_cpuarch = i[update_name]["cpuarch"]
                data.cpu = i[update_name]["cpu_model"]
                data.memory = i[update_name]["mem_total"]
                data.save()
                return HttpResponseRedirect("/salt/status/")
                # return render_to_response('saltstack/node_add.html', context, context_instance=RequestContext(request))

            except:
                context["vm"] = i[update_name]["virtual"]
                context["cpu_model"] = i[update_name]["cpu_model"]
                context["mem_total"] = i[update_name]["mem_total"]
                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["system_usage"] = System_usage
                context["edit_system_arch"] = system_arch
                context["server_type"] = Project.objects.all()
                # print context
                context.update(csrf(request))
        return render_to_response('saltstack/node_add.html', context, context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect("/salt/status/")
Ejemplo n.º 2
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.º 3
0
def salt_update_node(request):
    context = {}
    update_name = request.GET['node_name']
    if request.method == 'POST':
        uf = HostForm(request.POST)
        if uf.is_valid():
            # 查询数据库中是否有此数据
            zw = uf.save(commit=False)
            zw.edit_username = request.user.username
            zw.status = 1
            business_id = request.POST.get('business')
            if business_id:
                business = Project.objects.get(id=business_id)
                zw.business = business
            zw.save()
            uf = HostForm()
            context['uf'] = uf
            context.update(csrf(request))
            return HttpResponseRedirect("/salt/status/")
            # return render_to_response('assets/host_list.html',context,context_instance=RequestContext(request))
        else:
            uf = HostForm()
            context["server_type"] = Project.objects.all()
            context['uf'] = uf
            context.update(csrf(request))
            return HttpResponseRedirect("/salt/status/")

    if len(update_name) > 0 and Host.objects.filter(
            node_name=update_name).count() == 0:
        token_api_id = token_id()
        #同步本地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()
        #执行模块
        list = salt_api_token(
            {
                'client': 'local',
                'fun': 'grains.items',
                'tgt': update_name,
                'timeout': 100
            }, salt_api_url, {"X-Auth-Token": token_api_id}, False)
        master_status = list.run()
        uf = HostForm()
        for i in master_status["return"]:
            system_os = i[update_name]["os"]
            osarch = i[update_name]["osarch"]
            ipinfo = i[update_name]["hwaddr_interfaces"]["eth0"]
            mac = ipinfo.replace(":", "-")
            if "eth0" in i[update_name]["ip_interfaces"]:
                context["eth0"] = i[update_name]["ip_interfaces"]["eth0"][0]
            elif "em1" in i[update_name]["ip_interfaces"]:
                context["eth0"] = i[update_name]["ip_interfaces"]["em1"][0]
            else:
                context["eth0"] = "127.0.0.1"
            print i[update_name]["ip_interfaces"]
            if "eth1" in i[update_name]["ip_interfaces"]:
                # try:
                if len(i[update_name]["ip_interfaces"]["eth1"]) > 0:
                    context["eth1"] = i[update_name]["ip_interfaces"]["eth1"][
                        0]
            # except IndexError:
            else:
                context["eth1"] = ""
            # ip = request.POST.get("eth1")
            # data = select_node(context["eth0"])
            try:
                data = Host.objects.get(eth1=context["eth0"])
                data.node_name = i[update_name]["fqdn"]
                data.mac = mac
                data.core_num = i[update_name]["num_cpus"]
                data.system_version = i[update_name]["osrelease"]
                data.system_cpuarch = i[update_name]["cpuarch"]
                data.cpu = i[update_name]["cpu_model"]
                data.memory = i[update_name]["mem_total"]
                data.save()
                return HttpResponseRedirect("/salt/status/")
                # return render_to_response('saltstack/node_add.html', context, context_instance=RequestContext(request))

            except:
                context["vm"] = i[update_name]["virtual"]
                context["cpu_model"] = i[update_name]["cpu_model"]
                context["mem_total"] = i[update_name]["mem_total"]
                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["system_usage"] = System_usage
                context["edit_system_arch"] = system_arch
                context["server_type"] = Project.objects.all()
                # print context
                context.update(csrf(request))

        return render_to_response('saltstack/node_add.html',
                                  context,
                                  context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect("/salt/status/")
Ejemplo n.º 4
0
def salt_delete_key(request):
    delete_name = request.GET['node_name']
    token_api_id = token_id()
    list = salt_api_token({'client' : 'wheel', 'fun': 'key.delete', 'match':delete_name}, salt_api_url, {"X-Auth-Token": token_api_id})
    list.run()
    return HttpResponseRedirect("/salt/1/")