def get_vm_list_by_center(user, center_id): '''根据center_id获取虚拟机列表''' group_info = api_group_get_list({'req_user': user, 'center_id': center_id}) vm_list = [] if group_info['res']: for group in group_info['list']: vms_info = api_vm_get_list({'req_user': user, 'group_id': group['id']}) if vms_info['res']: vm_list += vms_info['list'] else: print(vms_info) return vm_list
def get_vm_list_by_center(user, center_id): '''根据center_id获取虚拟机列表''' group_info = api_group_get_list({'req_user': user, 'center_id': center_id}) vm_list = [] if group_info['res']: for group in group_info['list']: vms_info = api_vm_get_list({ 'req_user': user, 'group_id': group['id'] }) if vms_info['res']: vm_list += vms_info['list'] else: print(vms_info) return vm_list
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 volume_mount_ceph_view(req): dicts = {} if req.method == 'POST': print(req.POST) vm_uuid = req.POST.get('vm_uuid', None) volume_id = req.POST.get('volume_id', None) volume = _get_volume_by_id(req.user, volume_id) if not volume: dicts['alert_msg'] = '参数错误' else: if volume['vm']: dicts['alert_msg'] = '云硬盘已挂载' else: res = api_volume_mount({'req_user': req.user, 'vm_uuid':vm_uuid, 'volume_id':volume_id}) if res['res']: dicts['alert_msg'] = '挂载成功!' elif res['err'] in ERROR_CN: dicts['alert_msg'] = ERROR_CN[res['err']] else: dicts['alert_msg'] = '挂载失败' volume_id = req.GET.get('volume_id', None) vm_id = req.GET.get('vm_id', None) volume = None vm = None if volume_id != None: volume = _get_volume_by_id(req.user, volume_id) if vm_id != None: vm = _get_vm_by_id(req.user, vm_id) volume_list = [] vm_list = [] if vm: volume_list_info = api_volume_get_list({'req_user': req.user, 'group_id': vm['group_id']}) if volume_list_info['res']: for vol in volume_list_info['list']: if not vol['enable']: continue if vol['group_id'] != vm['group_id']: continue if vol['vm']: continue if req.user.is_superuser or vol['user_name'] == req.user.username: volume_list.append(vol) # cephpool_list = _get_cephpool_list_by_center_id(req.user, vm['center_id']) # for cephpool in cephpool_list: # volume_list_info = api_volume_get_list({'req_user': req.user, 'cephpool_id': cephpool['id']}) # if volume_list_info['res']: # volume_list += volume_list_info['list'] mounted_volume_list_info = api_volume_get_list({'req_user': req.user, 'vm_uuid':vm['uuid']}) if mounted_volume_list_info['res']: vm['volumes'] = mounted_volume_list_info['list'] elif volume: vm_list_info = api_vm_get_list({'req_user': req.user, 'group_id': volume['group_id']}) if vm_list_info['res']: if req.user.is_superuser: vm_list += vm_list_info['list'] else: for vm_info in vm_list_info['list']: if vm_info['creator'] == req.user.username: vm_list.append(vm_info) # volume_cephpool_info = api_ceph_get({'req_user': req.user, 'cephpool_id': volume['cephpool_id']}) # if volume_cephpool_info['res']: # group_list_info = api_group_get_list({'req_user': req.user, 'center_id': volume_cephpool_info['info']['center_id']}) # if group_list_info['res']: # for group in group_list_info['list']: # vm_list_info = api_vm_get_list({'req_user': req.user, 'group_id': group['id']}) # if vm_list_info['res']: # if req.user.is_superuser: # vm_list += vm_list_info['list'] # else: # for vm_info in vm_list_info['list']: # if vm_info['creator'] == req.user.username: # vm_list.append(vm_info) dicts['volume'] = volume dicts['vm'] = vm dicts['volume_list'] = volume_list dicts['vm_list'] = vm_list if volume and volume['enable'] == False: dicts['alert_msg'] = '该设备不可用' return render_to_response('vmadmin_volume_mount_ceph.html', dicts, context_instance=RequestContext(req))
def gpu_mount_view(req): dicts = {} if req.method == "POST": gpu_id = req.POST.get('gpu_id', None) vm_id = req.POST.get('vm_id', None) if gpu_id == None or vm_id == None: dicts['alert_msg'] = '参数错误' else: gpu = _get_gpu_by_id(req.user, gpu_id) if gpu['enable'] == False: dicts['alert_msg'] = '该设备不可用' elif gpu['vm']: dicts['alert_msg'] = '该设备已被占用' else: vm = _get_vm_by_id(req.user, vm_id) res = api_gpu_mount({'req_user': req.user, 'vm_id':vm_id, 'gpu_id':gpu_id}) if res['res']: dicts['alert_msg'] = '挂载成功' elif res['err'] in ERROR_CN: dicts['alert_msg'] = ERROR_CN[res['err']] else: dicts['alert_msg'] = '挂载失败(' + res['err'] + ')' # return HttpResponseRedirect('../detail/?gpu_id=' + gpu_id) gpu_id = req.GET.get('gpu_id', None) vm_id = req.GET.get('vm_id', None) host_id = req.GET.get('host_id', None) gpu = None vm = None host = None if gpu_id != None: gpu = _get_gpu_by_id(req.user, gpu_id) if vm_id != None: vm = _get_vm_by_id(req.user, vm_id) if host_id != None: host = _get_host_by_id(req.user, host_id) if host == None: if vm != None: host_id_from_vm_or_gpu = vm['host_id'] elif gpu != None: host_id_from_vm_or_gpu = gpu['host_id'] else: host_id_from_vm_or_gpu = None if host_id_from_vm_or_gpu: host = _get_host_by_id(req.user, host_id_from_vm_or_gpu) if host != None: gpu_list = [] if gpu == None: gpu_list_info = api_gpu_get_list({'req_user': req.user, 'host_id':host['id']}) if gpu_list_info['res']: gpu_list = gpu_list_info['list'] vm_list = [] if vm == None: vm_list_info = api_vm_get_list({'req_user': req.user, 'group_id': host['group_id']}) if vm_list_info['res']: for v in vm_list_info['list']: if v['host_id'] == host['id'] and (req.user.is_superuser or req.user.username == v['creator']): vm_list.append(v) dicts['host'] = host dicts['vm'] = vm dicts['gpu'] = gpu dicts['gpu_list'] = gpu_list dicts['vm_list'] = vm_list if gpu and gpu['enable'] == False: dicts['alert_msg'] = '该设备不可用' return render_to_response('vmadmin_gpu_mount.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') #参数校验 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 volume_mount_ceph_view(req): dicts = {} if req.method == 'POST': print(req.POST) vm_uuid = req.POST.get('vm_uuid', None) volume_id = req.POST.get('volume_id', None) volume = _get_volume_by_id(req.user, volume_id) if not volume: dicts['alert_msg'] = '参数错误' else: if volume['vm']: dicts['alert_msg'] = '云硬盘已挂载' else: res = api_volume_mount({ 'req_user': req.user, 'vm_uuid': vm_uuid, 'volume_id': volume_id }) if res['res']: dicts['alert_msg'] = '挂载成功!' elif res['err'] in ERROR_CN: dicts['alert_msg'] = ERROR_CN[res['err']] else: dicts['alert_msg'] = '挂载失败' volume_id = req.GET.get('volume_id', None) vm_id = req.GET.get('vm_id', None) volume = None vm = None if volume_id != None: volume = _get_volume_by_id(req.user, volume_id) if vm_id != None: vm = _get_vm_by_id(req.user, vm_id) volume_list = [] vm_list = [] if vm: volume_list_info = api_volume_get_list({ 'req_user': req.user, 'group_id': vm['group_id'] }) if volume_list_info['res']: for vol in volume_list_info['list']: if not vol['enable']: continue if vol['group_id'] != vm['group_id']: continue if vol['vm']: continue if req.user.is_superuser or vol[ 'user_name'] == req.user.username: volume_list.append(vol) # cephpool_list = _get_cephpool_list_by_center_id(req.user, vm['center_id']) # for cephpool in cephpool_list: # volume_list_info = api_volume_get_list({'req_user': req.user, 'cephpool_id': cephpool['id']}) # if volume_list_info['res']: # volume_list += volume_list_info['list'] mounted_volume_list_info = api_volume_get_list({ 'req_user': req.user, 'vm_uuid': vm['uuid'] }) if mounted_volume_list_info['res']: vm['volumes'] = mounted_volume_list_info['list'] elif volume: vm_list_info = api_vm_get_list({ 'req_user': req.user, 'group_id': volume['group_id'] }) if vm_list_info['res']: if req.user.is_superuser: vm_list += vm_list_info['list'] else: for vm_info in vm_list_info['list']: if vm_info['creator'] == req.user.username: vm_list.append(vm_info) # volume_cephpool_info = api_ceph_get({'req_user': req.user, 'cephpool_id': volume['cephpool_id']}) # if volume_cephpool_info['res']: # group_list_info = api_group_get_list({'req_user': req.user, 'center_id': volume_cephpool_info['info']['center_id']}) # if group_list_info['res']: # for group in group_list_info['list']: # vm_list_info = api_vm_get_list({'req_user': req.user, 'group_id': group['id']}) # if vm_list_info['res']: # if req.user.is_superuser: # vm_list += vm_list_info['list'] # else: # for vm_info in vm_list_info['list']: # if vm_info['creator'] == req.user.username: # vm_list.append(vm_info) dicts['volume'] = volume dicts['vm'] = vm dicts['volume_list'] = volume_list dicts['vm_list'] = vm_list if volume and volume['enable'] == False: dicts['alert_msg'] = '该设备不可用' return render_to_response('vmadmin_volume_mount_ceph.html', dicts, context_instance=RequestContext(req))
def volume_mount_ceph_view(req): dicts = {} if req.method == 'POST': print(req.POST) vm_uuid = req.POST.get('vm_uuid', None) volume_id = req.POST.get('volume_id', None) volume = _get_volume_by_id(req.user, volume_id) if not volume: dicts['alert_msg'] = '参数错误' else: if volume['vm']: dicts['alert_msg'] = '云硬盘已挂载' else: res = api_volume_mount({ 'req_user': req.user, 'vm_uuid': vm_uuid, 'volume_id': volume_id }) if res['res']: dicts['alert_msg'] = '挂载成功!' elif res['err'] in ERROR_CN: dicts['alert_msg'] = ERROR_CN[res['err']] else: dicts['alert_msg'] = '挂载失败' volume_id = req.GET.get('volume_id', None) vm_id = req.GET.get('vm_id', None) volume = None vm = None if volume_id != None: volume = _get_volume_by_id(req.user, volume_id) if vm_id != None: vm = _get_vm_by_id(req.user, vm_id) volume_list = [] vm_list = [] if vm: template = 'vmadmin_volume_mount_ceph.html' volume_list_info = api_volume_get_list({ 'req_user': req.user, 'group_id': vm['group_id'] }) if volume_list_info['res']: for vol in volume_list_info['list']: if not vol['enable']: continue if vol['group_id'] != vm['group_id']: continue if vol['vm']: continue if req.user.is_superuser or vol[ 'user_name'] == req.user.username: volume_list.append(vol) # cephpool_list = _get_cephpool_list_by_center_id(req.user, vm['center_id']) # for cephpool in cephpool_list: # volume_list_info = api_volume_get_list({'req_user': req.user, 'cephpool_id': cephpool['id']}) # if volume_list_info['res']: # volume_list += volume_list_info['list'] mounted_volume_list_info = api_volume_get_list({ 'req_user': req.user, 'vm_uuid': vm['uuid'] }) if mounted_volume_list_info['res']: vm['volumes'] = mounted_volume_list_info['list'] elif volume: template = 'vmadmin_volume_mount_to_vm.html' vm_list_info = api_vm_get_list({ 'req_user': req.user, 'group_id': volume['group_id'] }) if vm_list_info['res']: if req.user.is_superuser: vm_list += vm_list_info['list'] else: for vm_info in vm_list_info['list']: if vm_info['creator'] == req.user.username: vm_list.append(vm_info) else: raise Http404() def attach_mounted_volumes(vm_info): mounted_volume_info = api_volume_get_list({ 'req_user': req.user, 'vm_uuid': vm_info['uuid'] }) if mounted_volume_info['res']: vm_info['volumes'] = mounted_volume_info['list'] return vm_info vm_list = filter(attach_mounted_volumes, vm_list) dicts['volume'] = volume dicts['vm'] = vm dicts['volume_list'] = volume_list dicts['vm_list'] = vm_list if volume and volume['enable'] == False: dicts['alert_msg'] = '该设备不可用' return render(req, template, dicts)