else: return JsonResponse({ "code": 500, "data": None, "msg": "不支持的操作或者您没有权限添加虚拟机" }) @login_required def modfInstance(request, id): try: vServer = VmServer.objects.get(id=id) except Exception, e: return JsonResponse({"code": 500, "msg": "找不到主机资源", "data": e}) if request.method == "POST": if CommTools.argsCkeck(args=['op', 'server_id', 'vm_name'], data=request.POST) and request.user.has_perm( 'VManagePlatform.change_vmserverinstance'): LIBMG = LibvirtManage(vServer.server_ip, vServer.username, vServer.passwd, vServer.vm_type) SERVER = LIBMG.genre(model='server') STROAGE = LIBMG.genre(model='storage') INSTANCE = LIBMG.genre(model='instance') 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": "虚拟机不存在,或者已经被删除。"
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": "暂时不支持的操作或者您没有权限操作操作此项。" })
return JsonResponse({"code":500,"data":None,"msg":dom}) except: return JsonResponse({"code":500,"data":None,"msg":"虚拟主机添加失败。"}) else: return JsonResponse({"code":500,"data":None,"msg":"不支持的操作或者您没有权限创建虚拟机"}) @login_required def modfInstance(request,id): try: vServer = VmServer.objects.get(id=id) except Exception,e: return JsonResponse({"code":500,"msg":"找不到主机资源","data":e}) if request.method == "POST": if CommTools.argsCkeck(args=['op','server_id','vm_name'], data=request.POST) and request.user.has_perm('VManagePlatform.change_vmserverinstance'): LIBMG = LibvirtManage(vServer.server_ip,vServer.username, vServer.passwd,vServer.vm_type) SERVER = LIBMG.genre(model='server') STORAGE = LIBMG.genre(model='storage') INSTANCE = LIBMG.genre(model='instance') 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':