def handleDhcp(request): if request.method == "POST": op = request.POST.get('op') dhcp_id = request.POST.get('id') if op in ['delete', 'enable', 'disable', 'start', 'stop']: try: vMdhcp = VmDHCP.objects.get(id=dhcp_id) except: return JsonResponse({ "code": 500, "data": None, "msg": "DHCP配置不存在。" }) DHCP = DHCPConfig() if op == 'enable': if vMdhcp.isAlive == 1: status = DHCP.enableNets(netnsName=vMdhcp.mode, brName=vMdhcp.brName, port=vMdhcp.dhcp_port, ip=vMdhcp.server_ip, drive=vMdhcp.drive) if status[0] == 0: VmDHCP.objects.filter(id=vMdhcp.id).update(isAlive=0) return JsonResponse({ "code": 200, "msg": "激活成功。", "data": None }) else: return JsonResponse({ "code": 500, "msg": "激活失败。", "data": status[1] }) else: return JsonResponse({ "code": 500, "msg": "配置已是激活状态。", "data": None }) elif op == 'disable': if vMdhcp.isAlive == 0: status = DHCP.disableNets(netnsName=vMdhcp.mode, brName=vMdhcp.brName, port=vMdhcp.dhcp_port, drive=vMdhcp.drive) if status[0] == 0: VmDHCP.objects.filter(id=vMdhcp.id).update(isAlive=1) return JsonResponse({ "code": 200, "msg": "禁用成功。", "data": None }) else: return JsonResponse({ "code": 500, "msg": "禁用失败。", "data": status[1] }) else: return JsonResponse({ "code": 500, "msg": "配置已是非激活状态。", "data": None }) elif op == 'start': if vMdhcp.isAlive == 0 and vMdhcp.status == 1: if vMdhcp.mode == 'dhcp-ext': status = DHCP.start(netnsName=vMdhcp.mode, iprange=vMdhcp.ip_range, port=vMdhcp.dhcp_port, mode='ext', brName=vMdhcp.brName, drive=vMdhcp.drive, gateway=vMdhcp.gateway, dns=vMdhcp.dns) elif vMdhcp.mode == 'dhcp-int': status = DHCP.start(netnsName=vMdhcp.mode, iprange=vMdhcp.ip_range, drive=vMdhcp.drive, port=vMdhcp.dhcp_port, mode='int', brName=vMdhcp.brName) if status[0] == 0: VmDHCP.objects.filter(id=vMdhcp.id).update(status=0) return JsonResponse({ "code": 200, "msg": "DHCP服务启动成功。", "data": None }) else: return JsonResponse({ "code": 500, "msg": "DHCP服务启动失败。", "data": status[1] }) else: return JsonResponse({ "code": 500, "msg": "请先激活DHCP配置或者DHCP服务已是启动状态。", "data": None }) elif op == 'stop': if vMdhcp.isAlive == 0 and vMdhcp.status == 0: if vMdhcp.mode == 'dhcp-ext': status = DHCP.stop(mode='ext') elif vMdhcp.mode == 'dhcp-int': status = DHCP.stop(mode='int') if status[0] == 0: VmDHCP.objects.filter(id=vMdhcp.id).update(status=1) return JsonResponse({ "code": 200, "msg": "DHCP服关闭成功。", "data": None }) else: return JsonResponse({ "code": 500, "msg": "DHCP服务关闭失败。", "data": status[1] }) else: return JsonResponse({ "code": 500, "msg": "请先激活DHCP配置或者DHCP服务已是关闭状态。", "data": None }) elif op == 'delete': if vMdhcp.isAlive == 0 and vMdhcp.status == 0: if vMdhcp.mode == 'dhcp-ext': status = DHCP.stop(mode='ext') elif vMdhcp.mode == 'dhcp-int': status = DHCP.stop(mode='int') if status[0] == 0: status = DHCP.disableNets(netnsName=vMdhcp.mode, brName=vMdhcp.brName, port=vMdhcp.dhcp_port, drive=vMdhcp.drive) if status[0] == 0: result = VmDHCP.objects.filter(id=vMdhcp.id).delete() elif vMdhcp.isAlive == 0 and vMdhcp.status == 1: status = DHCP.disableNets(netnsName=vMdhcp.mode, brName=vMdhcp.brName, port=vMdhcp.dhcp_port, drive=vMdhcp.drive) # if status[0] == 0: result = VmDHCP.objects.filter(id=vMdhcp.id).delete() else: result = VmDHCP.objects.filter(id=vMdhcp.id).delete() if result: return JsonResponse({ "code": 500, "msg": "DHCP服务删除失败。", "data": None }) else: return JsonResponse({ "code": 200, "msg": "DHCP服务删除成功。", "data": None }) else: return JsonResponse({"code": 500, "msg": "不支持的操作。", "data": None})
def startDhcpServer(): DHCP = DHCPConfig() for dh in VmDHCP.objects.all(): if dh.isAlive == 0 and dh.status == 0: alive = DHCP.netnsIsAlive(dh.mode) if alive[0] > 0: DHCP.enableNets(netnsName=dh.mode, brName=dh.brName, port=dh.dhcp_port, ip=dh.server_ip, drive=dh.drive) if dh.mode == 'dhcp-int': status = DHCP.status(mode='int') if status[0] > 0: DHCP.start(netnsName=dh.mode, iprange=dh.ip_range, port=dh.dhcp_port, drive=dh.drive, mode='int', brName=dh.brName, gateway=dh.gateway, dns=dh.dns) elif dh.mode == 'dhcp-ext': status = DHCP.status(mode='ext') if status[0] > 0: DHCP.start(netnsName=dh.mode, iprange=dh.ip_range, port=dh.dhcp_port, drive=dh.drive, mode='ext', brName=dh.brName, gateway=dh.gateway, dns=dh.dns)
def handleDhcp(request): if request.method == "POST": op = request.POST.get('op') dhcp_id = request.POST.get('id') if op in ['delete', 'enable', 'disable', 'start', 'stop']: try: vMdhcp = VmDHCP.objects.get(id=dhcp_id) except: return JsonResponse({ "code": 500, "data": None, "msg": "DHCP configuration does not exist." }) DHCP = DHCPConfig() if op == 'enable': if vMdhcp.isAlive == 1: status = DHCP.enableNets(netnsName=vMdhcp.mode, brName=vMdhcp.brName, port=vMdhcp.dhcp_port, ip=vMdhcp.server_ip, drive=vMdhcp.drive) if status[0] == 0: VmDHCP.objects.filter(id=vMdhcp.id).update(isAlive=0) return JsonResponse({ "code": 200, "msg": "Activation was successful.", "data": None }) else: return JsonResponse({ "code": 500, "msg": "Activation fails.", "data": status[1] }) else: return JsonResponse({ "code": 500, "msg": "Configuration is already active.", "data": None }) elif op == 'disable': if vMdhcp.isAlive == 0: status = DHCP.disableNets(netnsName=vMdhcp.mode, brName=vMdhcp.brName, port=vMdhcp.dhcp_port, drive=vMdhcp.drive) if status[0] == 0: VmDHCP.objects.filter(id=vMdhcp.id).update(isAlive=1) return JsonResponse({ "code": 200, "msg": "Disabled successfully.", "data": None }) else: return JsonResponse({ "code": 500, "msg": "Disabled disabled.", "data": status[1] }) else: return JsonResponse({ "code": 500, "msg": "The configuration is already inactive.", "data": None }) elif op == 'start': if vMdhcp.isAlive == 0 and vMdhcp.status == 1: if vMdhcp.mode == 'dhcp-ext': status = DHCP.start(netnsName=vMdhcp.mode, iprange=vMdhcp.ip_range, port=vMdhcp.dhcp_port, mode='ext', brName=vMdhcp.brName, drive=vMdhcp.drive, gateway=vMdhcp.gateway, dns=vMdhcp.dns) elif vMdhcp.mode == 'dhcp-int': status = DHCP.start(netnsName=vMdhcp.mode, iprange=vMdhcp.ip_range, drive=vMdhcp.drive, port=vMdhcp.dhcp_port, mode='int', brName=vMdhcp.brName) if status[0] == 0: VmDHCP.objects.filter(id=vMdhcp.id).update(status=0) return JsonResponse({ "code": 200, "msg": "The DHCP service started successfully.", "data": None }) else: return JsonResponse({ "code": 500, "msg": "The DHCP service failed to start.", "data": status[1] }) else: return JsonResponse({ "code": 500, "msg": "Please activate the DHCP configuration or the DHCP service is already started.", "data": None }) elif op == 'stop': if vMdhcp.isAlive == 0 and vMdhcp.status == 0: if vMdhcp.mode == 'dhcp-ext': status = DHCP.stop(mode='ext') elif vMdhcp.mode == 'dhcp-int': status = DHCP.stop(mode='int') if status[0] == 0: VmDHCP.objects.filter(id=vMdhcp.id).update(status=1) return JsonResponse({ "code": 200, "msg": "DHCP service closed successfully.", "data": None }) else: return JsonResponse({ "code": 500, "msg": "The DHCP service failed to close.", "data": status[1] }) else: return JsonResponse({ "code": 500, "msg": "Please activate DHCP configuration or the DHCP service is closed.", "data": None }) elif op == 'delete': if vMdhcp.isAlive == 0 and vMdhcp.status == 0: if vMdhcp.mode == 'dhcp-ext': status = DHCP.stop(mode='ext') elif vMdhcp.mode == 'dhcp-int': status = DHCP.stop(mode='int') if status[0] == 0: status = DHCP.disableNets(netnsName=vMdhcp.mode, brName=vMdhcp.brName, port=vMdhcp.dhcp_port, drive=vMdhcp.drive) if status[0] == 0: result = VmDHCP.objects.filter(id=vMdhcp.id).delete() elif vMdhcp.isAlive == 0 and vMdhcp.status == 1: status = DHCP.disableNets(netnsName=vMdhcp.mode, brName=vMdhcp.brName, port=vMdhcp.dhcp_port, drive=vMdhcp.drive) # if status[0] == 0: result = VmDHCP.objects.filter(id=vMdhcp.id).delete() else: result = VmDHCP.objects.filter(id=vMdhcp.id).delete() if result: return JsonResponse({ "code": 500, "msg": "Failed to delete the DHCP service.", "data": None }) else: return JsonResponse({ "code": 200, "msg": "The DHCP service was deleted successfully.", "data": None }) else: return JsonResponse({ "code": 500, "msg": "Unsupported operation.", "data": None })