def volume_create_view(req): dicts = {} if req.method == 'POST': group_id = req.POST.get('group', None) size = req.POST.get('size', None) remarks = req.POST.get('remarks', None) print(group_id, size, remarks) if not group_id or not group_id.isdigit() or not size or not size.isdigit() or float(size) <= 0: dicts['alert_msg'] = '参数错误' size = int(float(size) * 1024) res = api_volume_create({'req_user':req.user, 'group_id': int(group_id), 'size': size}) if res['res']: return HttpResponseRedirect('../list/') else: if res['err'] in ERROR_CN: dicts['alert_msg'] = ERROR_CN[res['err']] else: dicts['alert_msg'] = res['err'] center_dic = api_center_get_list({'req_user': req.user}) if center_dic['res']: dicts['center_list'] = center_dic['list'] center_id = req.GET.get('center', None) if not center_id and len(dicts['center_list']) > 0: center_id = dicts['center_list'][0]['id'] if center_id: group_list_info = api_group_get_list({'req_user': req.user, 'center_id': center_id}) group_list = [] if group_list_info['res']: group_list = group_list_info['list'] dicts['group_list'] = group_list return render_to_response('vmadmin_volume_create.html', dicts, context_instance=RequestContext(req))
def volume_create_view(req): dicts = {} if req.method == 'POST': group_id = req.POST.get('group', None) pool_id = req.POST.get('pool', None) size = req.POST.get('size', None) remarks = req.POST.get('remarks', None) go_to_mount = req.POST.get('go_to_mount', '0') print(group_id, pool_id, size, remarks,go_to_mount) if not group_id or not group_id.isdigit() or not pool_id or not pool_id.isdigit() or not size or not size.isdigit() or float(size) <= 0: dicts['alert_msg'] = '参数错误' else: size = int(float(size) * 1024) res = api_volume_create({'req_user':req.user, 'group_id': int(group_id), 'size': size, 'pool_id': int(pool_id)}) if res['res']:#by lzx:2018-06-25 api_volume_set_remark({ 'req_user': req.user, 'volume_id': res['volume_id'], 'remarks': remarks}) if res['res']: if go_to_mount == '1': return HttpResponseRedirect('/vmadmin/volume/mount/?volume_id=' + res['volume_id']) return HttpResponseRedirect('../list/') else: if res['err'] in ERROR_CN: dicts['alert_msg'] = ERROR_CN[res['err']] else: dicts['alert_msg'] = res['err'] center_dic = api_center_get_list({'req_user': req.user}) if center_dic['res']: dicts['center_list'] = center_dic['list'] center_id = req.GET.get('center', None) if not center_id and len(dicts['center_list']) > 0: center_id = dicts['center_list'][0]['id'] if center_id: group_list_info = api_group_get_list({'req_user': req.user, 'center_id': center_id}) group_list = [] if group_list_info['res']: group_list = group_list_info['list'] dicts['group_list'] = group_list pool_list_info = api_ceph_get_list({'req_user': req.user, 'center_id': center_id}) pool_list = [] if pool_list_info['res']: pool_list = [p for p in pool_list_info['list'] if p['type'] == CEPH_VOLUME_POOL_FLAG] dicts['pool_list'] = pool_list return render(req, 'vmadmin_volume_create.html', dicts)
def volume_create_view(req): dicts = {} if req.method == 'POST': group_id = req.POST.get('group', None) size = req.POST.get('size', None) remarks = req.POST.get('remarks', None) print(group_id, size, remarks) if not group_id or not group_id.isdigit( ) or not size or not size.isdigit() or float(size) <= 0: dicts['alert_msg'] = '参数错误' size = int(float(size) * 1024) res = api_volume_create({ 'req_user': req.user, 'group_id': int(group_id), 'size': size }) if res['res']: return HttpResponseRedirect('../list/') else: if res['err'] in ERROR_CN: dicts['alert_msg'] = ERROR_CN[res['err']] else: dicts['alert_msg'] = res['err'] center_dic = api_center_get_list({'req_user': req.user}) if center_dic['res']: dicts['center_list'] = center_dic['list'] center_id = req.GET.get('center', None) if not center_id and len(dicts['center_list']) > 0: center_id = dicts['center_list'][0]['id'] if center_id: group_list_info = api_group_get_list({ 'req_user': req.user, 'center_id': center_id }) group_list = [] if group_list_info['res']: group_list = group_list_info['list'] dicts['group_list'] = group_list return render_to_response('vmadmin_volume_create.html', dicts, context_instance=RequestContext(req))
def vm_list_view(req): dicts = {} #获取GET参数 arg_center = req.GET.get('center') arg_group = req.GET.get('group') arg_host = req.GET.get('host') arg_ip = req.GET.get('ip') arg_creator = req.GET.get('creator') arg_text = req.GET.get('text') #参数校验 if arg_center: try: arg_center = int(arg_center) except: arg_center = None if arg_group: try: arg_group = int(arg_group) except: arg_group = None if arg_host: try: arg_host = int(arg_host) except: arg_host = None if arg_ip: arg_ip = arg_ip.strip() if arg_text: arg_text = arg_text.strip() #获取分中心列表 center_info = api_center_get_list({'req_user': req.user}) if center_info['res']: dicts['center_list'] = center_info['list'] else: dicts['center_list'] = [] #根据分中心获取集群列表 if arg_center: group_info = api_group_get_list({ 'req_user': req.user, 'center_id': arg_center }) if group_info['res']: dicts['group_list'] = group_info['list'] #根据集群获取宿主机列表 if arg_group: host_info = api_host_get_list({ 'req_user': req.user, 'group_id': arg_group }) if host_info['res']: dicts['host_list'] = host_info['list'] #虚拟机列表筛选 vm_list = [] #根据主机筛选 if arg_host and arg_group and arg_center: vms_info = api_vm_get_list({ 'req_user': req.user, 'group_id': arg_group }) if vms_info['res']: for vm in vms_info['list']: if vm['host_id'] == int(arg_host): vm_list.append(vm) #根据集群筛选 elif arg_group and arg_center: vms_info = api_vm_get_list({ 'req_user': req.user, 'group_id': arg_group }) if vms_info['res']: vm_list = vms_info['list'] #根据分中心筛选 elif arg_center: vm_list = get_vm_list_by_center(req.user, arg_center) else: for center in dicts['center_list']: vm_list += get_vm_list_by_center(req.user, center['id']) #从虚拟机列表中获取创建者集合 creator_dic = {} for vm in vm_list: if vm['creator'] not in creator_dic: creator_dic[vm['creator']] = 1 dicts['creator_list'] = list(creator_dic.keys()) #根据创建者筛选 if arg_creator: i = 0 l = len(vm_list) while True: if i >= l: break if vm_list[i]['creator'] != arg_creator: del (vm_list[i]) l -= 1 else: i += 1 #根据ip地址筛选 if arg_ip: for i in range(len(vm_list) - 1, -1, -1): #反向循环,删除列表中不合要求项 by hai if vm_list[i]['ipv4'].find(arg_ip) == -1: del (vm_list[i]) #根据 主机编号-ip-备注 综合筛选 if arg_text: for i in range(len(vm_list) - 1, -1, -1): #反向循环,删除列表中不合要求项 if vm_list[i]['ipv4'].find(arg_text) == -1: if vm_list[i]['remarks'].find(arg_text) == -1: if vm_list[i]['uuid'].find(arg_text) == -1: del (vm_list[i]) vm_list = vm_list_sort(vm_list) dicts['p'] = get_page(vm_list, req) for vm in dicts['p'].object_list: vm['can_operate'] = req.user.is_superuser or vm[ 'creator'] == req.user.username res = api_volume_get_list({ 'req_user': req.user, 'vm_uuid': vm['uuid'] }) if res['res']: vm['volumes'] = res['list'] else: vm['volumes'] = [] return render(req, 'vmadmin_list.html', dicts)
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)
def vm_list_view(req): dicts = {} #获取GET参数 arg_center = req.GET.get('center') arg_group = req.GET.get('group') arg_host = req.GET.get('host') arg_ip = req.GET.get('ip') arg_creator = req.GET.get('creator') #参数校验 if arg_center: try: arg_center = int(arg_center) except: arg_center = None if arg_group: try: arg_group = int(arg_group) except: arg_group = None if arg_host: try: arg_host = int(arg_host) except: arg_host = None if arg_ip: arg_ip = arg_ip.strip() #获取分中心列表 center_info = api_center_get_list({'req_user': req.user}) if center_info['res']: dicts['center_list'] = center_info['list'] else: dicts['center_list'] = [] #根据分中心获取集群列表 if arg_center: group_info = api_group_get_list({'req_user': req.user, 'center_id': arg_center}) if group_info['res']: dicts['group_list'] = group_info['list'] #根据集群获取宿主机列表 if arg_group: host_info = api_host_get_list({'req_user': req.user, 'group_id': arg_group}) if host_info['res']: dicts['host_list'] = host_info['list'] #虚拟机列表筛选 vm_list = [] #根据主机筛选 if arg_host and arg_group and arg_center: vms_info = api_vm_get_list({'req_user': req.user, 'group_id': arg_group}) if vms_info['res']: for vm in vms_info['list']: if vm['host_id'] == int(arg_host): vm_list.append(vm) #根据集群筛选 elif arg_group and arg_center: vms_info = api_vm_get_list({'req_user': req.user, 'group_id': arg_group}) if vms_info['res']: vm_list = vms_info['list'] #根据分中心筛选 elif arg_center: vm_list = get_vm_list_by_center(req.user, arg_center) else: for center in dicts['center_list']: vm_list += get_vm_list_by_center(req.user, center['id']) #从虚拟机列表中获取创建者集合 creator_dic = {} for vm in vm_list: if vm['creator'] not in creator_dic: creator_dic[vm['creator']] = 1 dicts['creator_list'] = list(creator_dic.keys()) #根据创建者筛选 if arg_creator: i = 0 l = len(vm_list) while True: if i >= l: break if vm_list[i]['creator'] != arg_creator: del(vm_list[i]) l -= 1 else: i += 1 #根据ip地址筛选 if arg_ip: i = 0 l = len(vm_list) while True: if i >= l: break if vm_list[i]['ipv4'].find(arg_ip) == -1: del(vm_list[i]) l -= 1 else: i += 1 vm_list = vm_list_sort(vm_list) dicts['p'] = get_page(vm_list, req) for vm in dicts['p'].object_list: vm['can_operate'] = req.user.is_superuser or vm['creator'] == req.user.username return render_to_response('vmadmin_list.html', dicts, context_instance=RequestContext(req))
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: #获取集群列表 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']}) 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 = {} 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) #VLAN_type sorting vlan_type_list_info = api_net_get_vlan_type_list() 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 dicts['vlans_json'] = json.dumps(vlans) #创建虚拟机 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_vcpu = req.POST.get('vcpu') arg_mem = req.POST.get('mem') arg_num = req.POST.get('num') arg_remarks = req.POST.get('remarks') 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()): 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}) if create_res['res'] == False: if create_res['err'] in ERROR_CN: create_res['error'] = ERROR_CN[create_res['err']] res.append(create_res) dicts['res'] = res else: dicts['error'] = '参数错误。' return render_to_response('vmadmin_create.html', dicts, context_instance=RequestContext(req))