Esempio n. 1
0
def server_edit(request, uuid):
    """
    修改主机
    """
    content = {}
    edit_id = Host.objects.get(uuid=uuid)
    test_name = edit_id.node_name
    old_ip = edit_id.eth1
    idc_name = IDC.objects.all()
    server_type = Project.objects.all()
    uf = HostForm(instance=edit_id)
    if request.method == 'POST':    
        uf = HostForm(request.POST, instance=edit_id)   
        if uf.is_valid(): 
            zw = uf.save(commit=False)
            zw.edit_username = request.user.username
            zw.mac = str(request.POST.getlist("mac")[0]).replace(':', '-').strip(" ")
            zw.save()
            uf.save_m2m()
            user_all = request.user
            # idc_log(user_all.username, edit_id.node_name, "修改", edit_id.edit_username, edit_id.edit_datetime, id, user_all.id)
            return HttpResponse(json.dumps({"status": "ok"}, ensure_ascii=False, indent=4,))

    else:
        uf = HostForm(instance=edit_id)
        business = Project.objects.all()
        business_group = edit_id.business.all()

        return render_to_response('assets/host_edit.html', locals(), context_instance=RequestContext(request))
Esempio n. 2
0
def addhost(request):
    if request.method == 'POST':
        host_form = HostForm(request.POST)
        if host_form.is_valid():
            clean_data = host_form.cleaned_data
            ip = clean_data['ip']
            hostname = clean_data['hostname']
            product = clean_data['product']
            app = clean_data['application']
            jg = clean_data['idc_jg']
            status = clean_data['status']
            remark = clean_data['remark']
            host_list = HostList.objects.create(ip=ip,
                                                hostname=hostname,
                                                product=product,
                                                application=app,
                                                idc_jg=jg,
                                                status=status,
                                                remark=remark)
            return HttpResponseRedirect('/assets/',
                                        {'host_form': host_form})
    else:
        host_form = HostForm()

    return render(request, 'assets/hostlist.html',
                  {'host_form': host_form})
Esempio n. 3
0
def HostAdd(request):
    if request.method == 'POST':
        hostform = HostForm(request.POST)
        if hostform.is_valid():
            host = hostform.save()
            if host is not None:
                return HttpResponseRedirect(reverse('host_list'))
        else:
            return render(request, 'asset/host_add.html', locals())
    else:
        hostform = HostForm()
    return render(request, 'asset/host_add.html', locals())
Esempio n. 4
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/")
Esempio n. 5
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/")