示例#1
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)
示例#2
0
def vm_snap_create_view(req):
    arg_vmid = req.GET.get('vmid')
    obj_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid})
    if obj_res['res']:
        obj = obj_res['info']
    else:
        return HttpResponseRedirect('/vmadmin/vm/list/')

    dicts = {}
    if req.method == 'POST':
        group_id = obj['group_id']
        remarks = req.POST.get('remarks', None)

        create_res = api_vm_create_snap({
            'req_user': req.user,
            'uuid': arg_vmid,
            'remarks': remarks
        })

        if create_res['res']:
            dicts['alert_msg'] = "快照创建成功"
        else:
            err_msg = ERROR_CN[create_res['err']] if create_res[
                'err'] in ERROR_CN else create_res['err']
            dicts['alert_msg'] = '快照创建失败: %s' % err_msg

    dicts['vmobj'] = obj

    return render(req, 'vmadmin_snap_create.html', dicts)
示例#3
0
def vm_reset_view(req):
    dicts = {}
    arg_vmid = req.GET.get('vmid')
    if req.method == 'POST':
        arg_vmid = req.POST.get('vmid')
        arg_image = req.POST.get('image')
        status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid})
        if status_res['res'] and not status_res['status'] in (1, 2, 3, 7):
            reset_res = api_vm_reset({
                'req_user': req.user,
                'uuid': arg_vmid,
                'image_id': arg_image
            })
            if reset_res['res']:
                dicts['result'] = '重置成功。'
            else:
                err_msg = ERROR_CN[reset_res['err']] if reset_res[
                    'err'] in ERROR_CN else reset_res['err']
                dicts['result'] = '重置失败: %s' % err_msg

        else:
            dicts['result'] = '重置失败:只能在关机状态重置。'

    vm_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid})

    if vm_res['res']:
        dicts['vmobj'] = vm_res['info']
    else:
        return HttpResponseRedirect(reverse("vm_list"))

    image_res = api_image_get_list({
        'req_user': req.user,
        'ceph_id': dicts['vmobj']['ceph_id'],
        'enable': True
    })
    image_dic = {}
    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

    return render(req, 'vmadmin_reset.html', dicts)
示例#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))
示例#5
0
def vm_migrate_view(req):
    dicts = {}
    arg_vmid = req.GET.get('vmid')
    if req.method == 'POST':
        arg_vmid = req.POST.get('vmid')
        arg_host = req.POST.get('host')
        status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid})
        if status_res['res'] and not status_res['status'] in (1, 2, 3, 7):
            migrate_res = api_vm_migrate({
                'req_user': req.user,
                'uuid': arg_vmid,
                'host_id': arg_host
            })
            if migrate_res['res']:
                dicts['result'] = '迁移成功。'
            else:
                err_msg = ERROR_CN[migrate_res['err']] if migrate_res[
                    'err'] in ERROR_CN else migrate_res['err']
                dicts['result'] = '迁移失败: %s' % err_msg
        else:
            dicts['result'] = '迁移失败:只能在关机状态迁移。'

    vm_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid})

    if vm_res['res']:
        dicts['vmobj'] = vm_res['info']
    else:
        return HttpResponseRedirect(reverse("vm_list"))

    group_res = api_group_get_list({
        'req_user': req.user,
        'center_id': dicts['vmobj']['center_id']
    })
    if group_res['res']:
        host_list = []
        for group in group_res['list']:
            host_res = api_host_get_list({
                'req_user': req.user,
                'group_id': group['id']
            })
            if host_res['res']:
                for host in host_res['list']:
                    if dicts['vmobj']['host_id'] == host['id'] or host[
                            'enable'] == False:
                        continue
                    host['group_name'] = group['name']
                    host_list.append(host)
        dicts['host_list'] = host_list
    return render(req, 'vmadmin_migrate.html', dicts)
示例#6
0
def vm_edit_view(req):
    dicts = {}
    arg_vmid = req.GET.get('vmid')

    if req.method == 'POST':
        arg_cpu = req.POST.get('cpu')
        arg_mem = req.POST.get('mem')

        status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid})

        if status_res['res'] and status_res['status'] != 1:
            if arg_cpu.isdigit() and arg_mem.isdigit():
                edit_res = api_vm_edit({
                    'req_user': req.user,
                    'uuid': arg_vmid,
                    'vcpu': arg_cpu,
                    'mem': arg_mem
                })
                if not edit_res['res']:
                    dicts['warning'] = '修改失败。'
        else:
            dicts['warning'] = '只能修改处于关机状态的虚拟机。'

    obj_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid})
    if obj_res['res']:
        obj = obj_res['info']
    else:
        return HttpResponseRedirect('../list/')
    dicts['vmobj'] = obj

    host_res = api_host_get({'req_user': req.user, 'host_id': obj['host_id']})
    if host_res['res']:
        host = host_res['info']
        dicts['remain_mem'] = host['mem_total'] - \
                              host['mem_allocated'] - \
                              host['mem_reserved']
        if dicts['remain_mem'] < 0:
            dicts['remain_mem'] = 0
        dicts['remain_cpu'] = host['vcpu_total'] - host['vcpu_allocated']
        if dicts['remain_cpu'] < 0:
            dicts['remain_cpu'] = 0

    return render_to_response('vmadmin_edit.html',
                              dicts,
                              context_instance=RequestContext(req))
示例#7
0
def volume_vm_create_view(req):
    vmid = req.GET.get('vmid')
    obj_res = api_vm_get({'req_user': req.user, 'uuid': vmid})
    if obj_res['res']:
        obj = obj_res['info']
    else:
        return HttpResponseRedirect('/vmadmin/vm/list/')

    dicts = {}
    if req.method == 'POST':
        size = req.POST.get('size', None)
        group_id = obj['group_id']
        backend = req.POST.get('backend', 'GFS')
        remarks = req.POST.get('remarks', None)
        if 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,
            'host_id': obj['host_id'],
            'group_id': int(group_id),
            'size': size,
            'backend': backend
        })

        if res['res']:
            mount_res = api_volume_mount({
                'req_user': req.user,
                'vm_uuid': vmid,
                'volume_id': res['volume_id']
            })
            if not mount_res['res']:
                messages.error(
                    req,
                    '挂载失败:' + ERROR_CN.get(mount_res['err'], mount_res['err']))
            return HttpResponseRedirect('/vmadmin/vm/list/')
        else:
            if res['err'] in ERROR_CN:
                dicts['alert_msg'] = ERROR_CN[res['err']]
            else:
                dicts['alert_msg'] = res['err']

    dicts['vmobj'] = obj

    return render(req, 'vmadmin_volume_vm_create.html', dicts)
示例#8
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))
示例#9
0
def volume_list_view(req):
    dicts = {}

    volume_info = api_volume_get_list({'req_user': req.user})
    ret_list= []
    if volume_info['res']:
        ret_list += volume_info['list']

    #查询并设置挂载到的vm的ip信息   
    for volume in ret_list: 
        volume['vm_ipv4'] = None
        if volume['vm']:
            vm_info = api_vm_get({'req_user': req.user, 'uuid': volume['vm']})
            if vm_info['res']:
                volume['vm_ipv4'] = vm_info['info']['ipv4']

    dicts['p'] = get_page(ret_list, req)
    #return render_to_response('vmadmin_volume_list.html', dicts, context_instance=RequestContext(req))
    return render(req,'vmadmin_volume_list.html', dicts)
示例#10
0
def vm_edit_view(req):
    dicts = {}
    arg_vmid = req.GET.get('vmid')
    
    if req.method == 'POST':
        arg_cpu = req.POST.get('cpu')
        arg_mem = req.POST.get('mem')
        
        status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid})
        
        if status_res['res'] and status_res['status'] != 1:
            if arg_cpu.isdigit() and arg_mem.isdigit():
                edit_res = api_vm_edit({'req_user': req.user,
                                        'uuid': arg_vmid,
                                        'vcpu': arg_cpu, 
                                        'mem': arg_mem})
                if not edit_res['res']:
                    dicts['warning'] = '修改失败。'
        else:
            dicts['warning'] = '只能修改处于关机状态的虚拟机。'
            
    obj_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid})
    if obj_res['res']:
        obj = obj_res['info']
    else:
        return HttpResponseRedirect('../list/')
    dicts['vmobj'] = obj
    
    host_res = api_host_get({'req_user': req.user, 'host_id': obj['host_id']})
    if host_res['res']:
        host = host_res['info']
        dicts['remain_mem'] = host['mem_total'] - \
                              host['mem_allocated'] - \
                              host['mem_reserved']   
        if dicts['remain_mem'] < 0:
            dicts['remain_mem'] = 0
        dicts['remain_cpu'] = host['vcpu_total'] - host['vcpu_allocated']
        if dicts['remain_cpu'] < 0:
            dicts['remain_cpu'] = 0
    
    return render_to_response('vmadmin_edit.html', dicts, context_instance=RequestContext(req))
示例#11
0
def vm_migrate_view(req):
    dicts = {}
    arg_vmid = req.GET.get('vmid')
    if req.method == 'POST':
        arg_vmid = req.POST.get('vmid')
        arg_host = req.POST.get('host')
        status_res = api_vm_status({'req_user': req.user, 'uuid': arg_vmid})
        if status_res['res'] and not status_res['status'] in (1, 2, 3, 7):
            migrate_res = api_vm_migrate({'req_user': req.user, 'uuid': arg_vmid, 'host_id': arg_host})
            if migrate_res['res']:
                dicts['result'] = '迁移成功。'
            else:
                dicts['result'] = '迁移失败: %s' % migrate_res['err']
        else:
            dicts['result'] = '迁移失败:只能在关机状态迁移。'
            
    vm_res = api_vm_get({'req_user': req.user, 'uuid': arg_vmid})
    
    if vm_res['res']:
        dicts['vmobj'] = vm_res['info']
    else:
        return HttpResponseRedirect('../list/')
    
    group_res = api_group_get_list({'req_user': req.user, 'center_id': dicts['vmobj']['center_id']})
    if group_res['res']:
        host_list = []
        for group in group_res['list']:
            host_res = api_host_get_list({'req_user': req.user, 'group_id': group['id']})
            if host_res['res']:
                for host in host_res['list']:
                    if dicts['vmobj']['host_id'] == host['id'] or host['enable'] == False:
                        continue
                    host['group_name'] = group['name']
                    host_list.append(host)
        dicts['host_list'] = host_list
    return render_to_response('vmadmin_migrate.html', dicts, context_instance=RequestContext(req))
示例#12
0
def _get_vm_by_id(user, vm_id):
    vm_info = api_vm_get({'req_user': user, 'uuid': vm_id})
    if vm_info['res']:
        return vm_info['info']
    return None
示例#13
0
def _get_vm_by_id(user, vm_id):
    vm_info = api_vm_get({'req_user': user, 'uuid': vm_id})
    if vm_info['res']:
        return vm_info['info']
    return None