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)
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
def vm_create_view(req): dicts = {} center_dic = api_center_get_list({'req_user': req.user}) if center_dic['res']: dicts['center_list'] = center_dic['list'] arg_center = req.GET.get('center') if not arg_center and len(dicts['center_list']) > 0: arg_center = dicts['center_list'][0]['id'] if arg_center: #创建虚拟机 #by lzx: 把post处理放在获取渲染页面数据的操作之前,以使得获取准确的用于渲染html的数据 if req.method == 'POST': arg_group = req.POST.get('group') arg_host = req.POST.get('host') arg_image = req.POST.get('image') arg_net = req.POST.get('net') arg_vlan = req.POST.get('vlan') arg_ip = req.POST.get('ip') arg_vcpu = req.POST.get('vcpu') arg_mem = req.POST.get('mem') arg_num = req.POST.get('num') arg_remarks = req.POST.get('remarks') posted_data = { 'group': arg_group, 'host': arg_host, 'image': arg_image, 'net': arg_net, 'vlan': arg_vlan, 'ip': arg_ip, 'vcpu': arg_vcpu, 'mem': arg_mem, 'num': arg_num, 'remarks': arg_remarks } try: dicts['posted_data_json'] = json.dumps(posted_data) except Exception as e: dicts['posted_data_json'] = json.dumps(None) print(e) res = [] if (arg_group and arg_group.isdigit() and arg_image and arg_image.isdigit() and arg_vcpu and arg_vcpu.isdigit() and arg_mem and arg_mem.isdigit() and arg_num and arg_num.isdigit()): if int(arg_num) > 1: arg_ip = None for i in range(int(arg_num)): create_res = api_vm_create({ 'req_user': req.user, 'group_id': arg_group, 'host_id': arg_host, 'image_id': arg_image, 'net_type_id': arg_net, 'vlan_id': arg_vlan, 'vcpu': arg_vcpu, 'mem': arg_mem, 'remarks': arg_remarks, 'ipv4': arg_ip or None }) if create_res['res'] == False: if create_res['err'] in ERROR_CN: create_res['error'] = ERROR_CN[create_res['err']] else: create_res['error'] = create_res['err'] res.append(create_res) dicts['res'] = res else: dicts['error'] = '参数错误。' #获取集群列表 group_dic = api_group_get_list({ 'req_user': req.user, 'center_id': arg_center }) if group_dic['res']: dicts['group_list'] = group_dic['list'] else: dicts['group_list'] = [] #获取宿主机列表 hosts = {} for g in dicts['group_list']: host_dic = api_host_get_list({ 'req_user': req.user, 'group_id': g['id'] }) if host_dic['res']: # hosts[g['id']] = host_dic['list'] hosts[g['id']] = [] for host in host_dic['list']: if host['enable']: hosts[g['id']].append(host) dicts['hosts_json'] = json.dumps(hosts) #获取镜像列表 image_dic = {} cephpool_dic = api_ceph_get_list({ 'req_user': req.user, 'center_id': arg_center }) if cephpool_dic['res']: for cephpool in cephpool_dic['list']: image_res = api_image_get_list({ 'req_user': req.user, 'ceph_id': cephpool['id'], 'enable': True }) if image_res['res']: for image in image_res['list']: if image['type'] in image_dic: image_dic[image['type']].append(image) else: image_dic[image['type']] = [image] image_type_list_info = api_image_get_type_list() if image_type_list_info['res']: image_type_list = [t['name'] for t in image_type_list_info['list']] else: image_type_list = list(image_dic.keys()) image_list_ordered = [] for image_type in image_type_list: if image_type in image_dic: image_list_ordered.append( (image_type, image_list_sort(image_dic[image_type]))) dicts['image_list_ordered'] = image_list_ordered #获取网络类型 vlan_type_dic = {} vlans = {} vlan_ip_dic = {} for group in dicts['group_list']: if group['id'] not in vlans: vlans[group['id']] = {} vlan_dic = api_net_get_vlan_list({ 'req_user': req.user, 'group_id': group['id'] }) if vlan_dic['res']: #vlan sorting vlan_list = vlan_list_sort(vlan_dic['list']) for vlan in vlan_list: if vlan['type_code'] not in vlan_type_dic: vlan_type_dic[vlan['type_code']] = vlan['type'] if vlan['type_code'] not in vlans[group['id']]: vlans[group['id']][vlan['type_code']] = [vlan] else: vlans[group['id']][vlan['type_code']].append(vlan) macip_info = api_net_get_vlan_ip_list({ 'vlan_id': vlan['id'], 'req_user': req.user }) if macip_info['res']: vlan_ip_dic[vlan['id']] = [ macip['ipv4'] for macip in macip_info['list'] if (macip['vmid'] == "" or macip['vmid'] is None) and macip['enable'] is True ] #VLAN_type sorting vlan_type_list_info = api_net_get_vlan_type_list( {'req_user': req.user}) if vlan_type_list_info['res']: vlan_type_list = [(t['code'], t['name']) for t in vlan_type_list_info['list'] if t['code'] in vlan_type_dic] else: vlan_type_list = [(t[0], t[1]) for t in list(vlan_type_dic.items())] dicts['vlan_type_list'] = vlan_type_list print(vlan_type_list_info) dicts['vlans_json'] = json.dumps(vlans) # 各个vlan的可用ip列表 dicts['vlan_ip_json'] = json.dumps(vlan_ip_dic) #return render_to_response('vmadmin_create.html', dicts, context_instance=RequestContext(req)) return render(req, 'vmadmin_create.html', dicts)