Exemplo n.º 1
0
def remove_vm_from_network(user, vm, network, operation_type):
    # 虚拟机必须是开机状态
    vm_if = NetInterface.objects.get(vm=vm)

    if_no = 0
    if vm_if.eth1_network == network:
        vm_if.eth1_type = NULL
        vm_if.eth1_network = None
        if_no = 1
    elif vm_if.eth2_network == network:
        vm_if.eth2_type = NULL
        vm_if.eth2_network = None
        if_no = 2
    elif vm_if.eth3_network == network:
        vm_if.eth3_type = NULL
        vm_if.eth3_network = None
        if_no = 3
    else:
        pass

    if_code = calculate_if_code(vm_if)
    machines = json.loads(network.machines)
    machines.remove(vm.info_id)
    network.machines = json.dumps(machines)
    network.save()
    data_dict = dict(request_type="network", request_id=random_str(), request_userid=user.id,
                     operation_type=operation_type, net_name=network.name, vm_name=vm.name, if_no=if_no,
                     if_code=if_code)
    communicate(data_dict, vm.host.ip, vm.host.vm_manager_port)
    print(vm.name)
Exemplo n.º 2
0
def create_intnet(user, host, net_name, ip, netmask, lower_ip, upper_ip):
    data_dict = dict(request_type="network", request_id=random_str(), request_userid=user.id,
                     operation_type=CREATE_INTNET, net_name=net_name,
                     ip=ip, netmask=netmask, lower_ip=lower_ip, upper_ip=upper_ip)

    communicate(data_dict, host.ip, host.vm_manager_port)
    network = Network(name=net_name, type=INTNET, host=host, ip=ip, netmask=netmask, lower_ip=lower_ip,
                      upper_ip=upper_ip, machines=json.dumps([]))
    network.save()
Exemplo n.º 3
0
def add_vm_to_hostonly(user, network, vm):
    if_code, if_no, vm_interface = set_vm_network(vm, network)
    if if_no > 0:
        data_dict = dict(request_type="network", request_id=random_str(), request_userid=user.id,
                         operation_type=ADD_VM_TO_HOSTONLY, net_name=network.name, vm_name=vm.name, if_code=if_code,
                         if_no=if_no)
        communicate(data_dict, vm.host.ip, vm.host.vm_manager_port)
        vm_interface.save()
        machines = json.loads(network.machines)
        machines.append(vm.info_id)
        network.machines = json.dumps(machines)
        network.save()
Exemplo n.º 4
0
def delete_intnet(user, host, network):
    data_dict = dict(request_type="network", request_id=random_str(), request_userid=user.id,
                     operation_type=DELETE_INTNET, net_name=network.name)
    communicate(data_dict, host.ip, host.vm_manager_port)
    network_to_delete = Network.objects.get(name=network.name)
    network_to_delete.delete()
Exemplo n.º 5
0
def delete_hostonly(user, host, network):
    data_dict = dict(request_type="network", request_id=random_str(), request_userid=user.id,
                     operation_type=DELETE_HOSTONLY, net_name=network.name)
    communicate(data_dict, host.ip, host.vm_manager_port)
    network.delete()