Exemplo n.º 1
0
def handleNetwork(request):
    if request.method == "POST":
        op = request.POST.get('op')
        server_id = request.POST.get('server_id')
        netkName = request.POST.get('netkName')
        if op in ['delete'] and request.user.has_perm('VManagePlatform.change_vmserverinstance'):
            try:
                vmServer = VMServer.selectOneHost(id=server_id)
            except:
                return JsonResponse({"code":500,"data":None,"msg":"主机不存在。"})  
            try:
                VMS = LibvirtManage(uri=vmServer.uri)
                SSH = BRManage(hostname=vmServer.server_ip,port=22)
                OVS = SSH.genre(model='ovs')
                BRCTL = SSH.genre(model='brctl')                
            except Exception,e:
                return  JsonResponse({"code":500,"msg":"服务器连接失败。。","data":e})             
            try:
                NETWORK = VMS.genre(model='network')
                netk = NETWORK.getNetwork(netk_name=netkName)
                if op == 'delete':
                    try:
                        if netkName.startswith('ovs'):OVS.ovsDelBr(brName=netkName)
                        elif netkName.startswith('br'):
                            BRCTL.brctlDownBr(brName=netkName)
#                             BRCTL.brctlDelBr(brName=netkName)
                        SSH.close()
                    except:
                        pass
                    status = NETWORK.deleteNetwork(netk)
                    VMS.close() 
                    if status == 0:return JsonResponse({"code":200,"data":None,"msg":"网络删除成功"})  
                    else:return JsonResponse({"code":500,"data":None,"msg":"网络删除失败"})     
            except Exception,e:
                return JsonResponse({"code":500,"msg":"获取网络失败。","data":e}) 
Exemplo n.º 2
0
         "vmServer":
         vServer,
         "netList":
         netList,
         "insList":
         insList
     },
                               context_instance=RequestContext(request))
 elif request.method == "POST" and request.user.has_perm(
         'VManagePlatform.change_vmserverinstance'):
     try:
         VMS = LibvirtManage(vServer.server_ip, vServer.username,
                             vServer.passwd, vServer.vm_type)
         NETWORK = VMS.genre(model='network')
         if request.POST.get('network-mode') == 'bridge':
             SSH = BRManage(hostname=vServer.server_ip, port=22)
             OVS = SSH.genre(model='ovs')
             BRCTL = SSH.genre(model='brctl')
             if NETWORK and OVS:
                 status = NETWORK.getNetwork(
                     netk_name=request.POST.get('bridge-name'))
                 if status:
                     VMS.close()
                     return JsonResponse({
                         "code": 500,
                         "msg": "The network already exists.",
                         "data": None
                     })
                 else:
                     if request.POST.get('mode') == 'openvswitch':
                         status = OVS.ovsAddBr(brName=request.POST.get(
Exemplo n.º 3
0
def modfInstance(request):
    if request.method == "POST":
        if CommTools.argsCkeck(args=['op', 'server_id', 'vm_name'],
                               data=request.POST) and request.user.has_perm(
                                   'VManagePlatform.change_vmserverinstance'):
            vMserver = VMServer.selectOneHost(id=request.POST.get('server_id'))
            LIBMG = LibvirtManage(uri=vMserver.uri)
            SERVER = LIBMG.genre(model='server')
            STROAGE = LIBMG.genre(model='storage')
            INSTANCE = LIBMG.genre(model='instance')
            NETWORK = LIBMG.genre(model='network')
            if SERVER:
                instance = INSTANCE.queryInstance(
                    name=str(request.POST.get('vm_name')))
                if instance is False:
                    LIBMG.close()
                    return JsonResponse({
                        "code": 404,
                        "data": None,
                        "msg": "虚拟机不存在,或者已经被删除。"
                    })
            else:
                return JsonResponse({
                    "code": 500,
                    "data": None,
                    "msg": "虚拟主机链接失败。"
                })
            #调整磁盘
            if request.POST.get('device') == 'disk':
                if request.POST.get('op') == 'attach':
                    if instance.state()[0] == 5:
                        return JsonResponse({
                            "code": 500,
                            "data": None,
                            "msg": "请先启动虚拟机。"
                        })
                    storage = STROAGE.getStoragePool(
                        pool_name=request.POST.get('pool_name'))
                    if storage:
                        volume = STROAGE.createVolumes(
                            pool=storage,
                            volume_name=request.POST.get('vol_name'),
                            drive=request.POST.get('vol_drive'),
                            volume_capacity=request.POST.get('vol_size'))
                        if volume:
                            volPath = volume.path()
                            volume_name = volume.name()
                        else:
                            LIBMG.close()
                            return JsonResponse({
                                "code": 500,
                                "data": None,
                                "msg": "卷已经存在。"
                            })
                        status = INSTANCE.addInstanceDisk(instance, volPath)
                        LIBMG.close()
                        if status:
                            recordLogs.delay(
                                user=request.user,
                                action='attach_disk',
                                status=0,
                                vm_name=request.POST.get('vm_name'))
                            return JsonResponse({
                                "code": 200,
                                "data": None,
                                "msg": "操作成功。"
                            })
                        else:
                            STROAGE.deleteVolume(storage, volume_name)
                            recordLogs.delay(
                                user=request.user,
                                action='attach_disk',
                                status=1,
                                vm_name=request.POST.get('vm_name'))
                            return JsonResponse({
                                "code": 500,
                                "data": None,
                                "msg": "操作失败。"
                            })
                    else:
                        LIBMG.close()
                        return JsonResponse({
                            "code": 404,
                            "data": None,
                            "msg": "存储池不存在,或者已经被删除。"
                        })
                elif request.POST.get('op') == 'detach':
                    status = INSTANCE.delInstanceDisk(
                        instance, volPath=request.POST.get('disk'))
                    LIBMG.close()
                    recordLogs.delay(user=request.user,
                                     action='detach_disk',
                                     status=status,
                                     vm_name=request.POST.get('vm_name'))
                    if status == 0:
                        return JsonResponse({
                            "code": 200,
                            "data": None,
                            "msg": "操作成功。"
                        })
                    else:
                        LIBMG.close()
                        return JsonResponse({
                            "code": 500,
                            "data": status,
                            "msg": "操作失败。"
                        })
            #调整网卡
            elif request.POST.get('device') == 'netk':
                if request.POST.get('op') == 'attach':
                    result = INSTANCE.addInstanceInterface(
                        instance, brName=request.POST.get('netk_name'))
                    recordLogs.delay(user=request.user,
                                     action='attach_netk',
                                     status=result,
                                     vm_name=request.POST.get('vm_name'))
                    if result == 0:
                        return JsonResponse({
                            "code": 200,
                            "data": None,
                            "msg": "操作成功。"
                        })
                    else:
                        return JsonResponse({
                            "code": 500,
                            "data": status,
                            "msg": "添加失败。"
                        })
                elif request.POST.get('op') == 'detach':
                    result = INSTANCE.delInstanceInterface(
                        instance, interName=request.POST.get('netk_name'))
                    recordLogs.delay(user=request.user,
                                     action='detach_netk',
                                     status=result,
                                     vm_name=request.POST.get('vm_name'))
                    if result == 0:
                        return JsonResponse({
                            "code": 200,
                            "data": None,
                            "msg": "操作成功。"
                        })
                    else:
                        return JsonResponse({
                            "code": 500,
                            "data": status,
                            "msg": "添加失败。"
                        })
            #调整内存大小
            elif request.POST.get('device') == 'mem':
                if request.POST.get('op') == 'attach':
                    result = INSTANCE.setMem(instance,
                                             mem=int(request.POST.get('mem')))
                    recordLogs.delay(user=request.user,
                                     action='attach_mem',
                                     status=result,
                                     vm_name=request.POST.get('vm_name'))
                    if result == 0:
                        return JsonResponse({
                            "code": 200,
                            "data": None,
                            "msg": "操作成功。"
                        })
                    else:
                        return JsonResponse({
                            "code": 500,
                            "data": None,
                            "msg": "不能设置虚拟机内存超过宿主机机器的物理内存"
                        })
            #调整cpu个数
            elif request.POST.get('device') == 'cpu':
                if request.POST.get('op') == 'attach':
                    result = INSTANCE.setVcpu(instance,
                                              cpu=int(request.POST.get('cpu')))
                    LIBMG.close()
                    recordLogs.delay(user=request.user,
                                     action='attach_cpu',
                                     status=result,
                                     vm_name=request.POST.get('vm_name'))
                    if result == 0:
                        return JsonResponse({
                            "code": 200,
                            "data": None,
                            "msg": "操作成功。"
                        })
                    else:
                        return JsonResponse({
                            "code": 500,
                            "data": None,
                            "msg": "不能设置虚拟机CPU超过宿主机机器的物理CPU个数"
                        })
            #调整带宽
            elif request.POST.get('device') == 'bandwidth':
                SSH = BRManage(hostname=vMserver.server_ip, port=22)
                OVS = SSH.genre(model='ovs')
                BRCTL = SSH.genre(model='brctl')
                mode = INSTANCE.getInterFace(instance,
                                             request.POST.get('netk_name'))
                if request.POST.get('op') == 'attach':
                    if mode.get('type') == 'openvswitch':
                        if int(request.POST.get('bandwidth')) == 0:
                            result = OVS.ovsCleanBandwidth(
                                port=request.POST.get('netk_name'))
                        else:
                            result = OVS.ovsConfBandwidth(
                                port=request.POST.get('netk_name'),
                                bandwidth=request.POST.get('bandwidth'))
                    else:
                        if int(request.POST.get('bandwidth')) == 0:
                            result = INSTANCE.cleanInterfaceBandwidth(
                                instance, request.POST.get('netk_name'))
                        result = INSTANCE.setInterfaceBandwidth(
                            instance,
                            port=request.POST.get('netk_name'),
                            bandwidth=request.POST.get('bandwidth'))
                    SSH.close()
                    LIBMG.close()
                    if result.get('status') == 'success':
                        return JsonResponse({
                            "code": 200,
                            "data": None,
                            "msg": "操作成功。"
                        })
                    else:
                        return JsonResponse({
                            "code": 500,
                            "data": None,
                            "msg": "未设置带宽,不需要清除"
                        })

            LIBMG.close()
        else:
            return JsonResponse({
                "code": 500,
                "data": None,
                "msg": "暂时不支持的操作或者您没有权限操作操作此项。"
            })