Ejemplo n.º 1
0
def net_vlan_ip_list_view(req):
    dicts = {}
    arg_vlan = req.GET.get('vlan')
    #参数校验
    if arg_vlan:
        try:
            arg_vlan = int(arg_vlan)
        except:
            arg_vlan = None
    ip_list = []
    dicts['vlan_info'] = None
    if arg_vlan:
        vlan_info = api_net_get_vlan({
            'req_user': req.user,
            'vlan_id': arg_vlan
        })
        if vlan_info['res']:
            dicts['vlan_info'] = vlan_info['info']
            macips_info = api_net_get_vlan_ip_list({
                'req_user': req.user,
                'vlan_id': arg_vlan
            })
            if macips_info['res']:
                ip_list = macips_info['list']
    dicts['p'] = get_page(ip_list, req)
    return render(req, 'vmadmin_net_vlan_ip_list.html', dicts)
Ejemplo n.º 2
0
def vm_detail_view(req):
    arg_vmid = req.GET.get('vmid')
    vm_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid})
    dicts = {}
    if vm_res['res']:
        dicts['vmobj'] = vm_res['info']
        vlan = api_net_get_vlan({'req_user': req.user, 'vlan_id': dicts['vmobj']['vlan_id']})
        if vlan['res']:
            dicts['vmobj']['br'] = vlan['info']['br']

        # 添加操作功能和挂载云硬盘 2018-07-12 yanghao
        dicts['vmobj']['can_operate'] = req.user.is_superuser or dicts['vmobj']['center_name'] == req.user.username

        from api.volume import get_volume_available
        volume_info = get_volume_available({'req_user': req.user,'group_id':dicts['vmobj']['group_id'],'vm_uuid':dicts['vmobj']['uuid']})
        # volume_info = api_volume_get_list({'req_user': req.user,'group_id':dicts['vmobj']['group_id']})
        ret_list = []
        if volume_info['res']:
            ret_list += volume_info['list']
        dicts['p'] = get_page(ret_list, req)
        
    else:
        if settings.DEBUG: print(vm_res['err'])
        return HttpResponseRedirect('../list/')
    return render(req,'vmadmin_detail.html', dicts)
Ejemplo n.º 3
0
def vm_detail_view(req):
    arg_vmid = req.GET.get('vmid')
    vm_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid})
    dicts = {}
    if vm_res['res']:
        dicts['vmobj'] = vm_res['info']
        vlan = api_net_get_vlan({'req_user': req.user, 'vlan_id': dicts['vmobj']['vlan_id']})
        if vlan['res']:
            dicts['vmobj']['br'] = vlan['info']['br'] 
        
    else:
        if settings.DEBUG: print(vm_res['err'])
        return HttpResponseRedirect('../list/')
    return render_to_response('vmadmin_detail.html', dicts, context_instance=RequestContext(req))
Ejemplo n.º 4
0
def vm_detail_view(req):
    arg_vmid = req.GET.get('vmid')
    vm_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid})
    dicts = {}
    if vm_res['res']:
        dicts['vmobj'] = vm_res['info']
        vlan = api_net_get_vlan({
            'req_user': req.user,
            'vlan_id': dicts['vmobj']['vlan_id']
        })
        if vlan['res']:
            dicts['vmobj']['br'] = vlan['info']['br']

    else:
        if settings.DEBUG: print(vm_res['err'])
        return HttpResponseRedirect('../list/')
    return render_to_response('vmadmin_detail.html',
                              dicts,
                              context_instance=RequestContext(req))
Ejemplo n.º 5
0
def net_vlan_conf_file_view(req):
    dicts = {}
    arg_vlan = req.GET.get('vlan')
    #参数校验
    if arg_vlan:
        try:
            arg_vlan = int(arg_vlan)
        except:
            arg_vlan = None
    ip_list = []
    dicts['vlan_info'] = None
    conf_file_str = ""
    if arg_vlan:
        vlan_info = api_net_get_vlan({
            'req_user': req.user,
            'vlan_id': arg_vlan
        })
        if vlan_info['res']:
            dicts['vlan_info'] = vlan_info['info']
            macips_info = api_net_get_vlan_ip_list({
                'req_user': req.user,
                'vlan_id': arg_vlan
            })
            if macips_info['res']:
                ip_list = macips_info['list']
            conf_file_str = _write_vlan_conf_content(
                vlan_info['info']['subnetip'], vlan_info['info']['netmask'],
                ip_list)

    the_file_name = "dhcpd.conf"
    if dicts['vlan_info']:
        the_file_name = dicts['vlan_info']['subnetip'] + "_" + the_file_name
    response = StreamingHttpResponse(conf_file_str)
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(
        the_file_name)
    return response
Ejemplo n.º 6
0
def net_batch_create_macip(req):
    dicts = {}
    res = True
    err = None

    #获取GET参数
    arg_vlan = req.GET.get('vlan')  #vlan_id
    arg_kind = req.GET.get('kind')  #生成方式
    arg_subnetip = req.GET.get('subnetip')  #子网ip
    arg_netmask = req.GET.get('netmask')
    arg_gateway = req.GET.get('gateway')
    arg_startip = req.GET.get('startip')
    arg_endip = req.GET.get('endip')

    #参数校验
    if arg_vlan:
        try:
            arg_vlan = int(arg_vlan)
        except:
            arg_vlan = None

    if arg_kind:
        arg_kind = arg_kind.strip()
    if arg_subnetip:
        arg_subnetip = arg_subnetip.strip()
    if arg_netmask:
        arg_netmask = arg_netmask.strip()
    if arg_gateway:
        arg_gateway = arg_gateway.strip()
    if arg_startip:
        arg_startip = arg_startip.strip()
    if arg_endip:
        arg_endip = arg_endip.strip()

    dicts['vlan_info'] = None
    macip_list = []
    if arg_vlan:
        vlan_info = api_net_get_vlan({
            'req_user': req.user,
            'vlan_id': arg_vlan
        })
    if arg_vlan and vlan_info['res']:
        dicts['vlan_info'] = vlan_info['info']
        if vlan_info['info']['ip_count'] > 0:
            dicts['err'] = "该VLAN已添加过ip"
        elif not vlan_info['info']['enable']:
            dicts['err'] = "该VLAN未启用"
        else:
            try:
                #根据子网ip和掩码生成ip列表
                if arg_kind == "netmask" and arg_subnetip and arg_netmask:
                    ip_str = arg_subnetip + "/" + arg_netmask
                    subnetip_obj = ipaddress.IPv4Network(ip_str)
                    for ip1 in subnetip_obj:
                        if str(ip1) == arg_gateway:
                            continue
                        r1, macip = _ip2macip(str(ip1))
                        if r1:
                            macip_list.append(macip)
                    if len(macip_list) > 0:
                        macip_list.pop(0)
                    if len(macip_list) > 0:
                        macip_list.pop(len(macip_list) - 1)
                elif arg_kind == "iprange" and arg_startip and arg_endip:
                    #根据ip范围生成ip列表
                    s_ip = ipaddress.IPv4Address(arg_startip)
                    e_ip = ipaddress.IPv4Address(arg_endip)
                    while e_ip >= s_ip:
                        r1, macip = _ip2macip(str(s_ip))
                        if r1:
                            macip_list.append(macip)
                        s_ip = s_ip + 1
            except Exception as e:
                print(e)

    else:
        dicts['err'] = "无法获取VLAN信息"

    if req.method == "GET":
        dicts['p'] = get_page(macip_list, req)
        return render(req, 'vmadmin_net_vlan_ip_batch_create.html', dicts)
    elif req.method == 'POST':  #添加入数据库
        success_count = 0
        for i in range(len(macip_list)):
            macip = macip_list[i]
            r2 = api_net_add_vlan_ip({
                'req_user': req.user,
                'vlan_id': arg_vlan,
                'mac': macip['mac'],
                'ipv4': macip['ipv4'],
                'enable': True
            })

            if r2['res']:
                macip_list[i]['res'] = True
                success_count += 1
            else:
                macip_list[i]['res'] = False
        fail_count = len(macip_list) - success_count
        dic = {
            "macip_list": macip_list,
            "success_count": success_count,
            "fail_count": fail_count
        }
        return HttpResponse(json.dumps(dic), content_type='application/json')