Esempio n. 1
0
def __associate_floating_ip_neutron(server_id):
    """
    associate floating ip to vm in neutron
    """
    # 如果 vm 直接连接到外部网络,那么返回 vm 在外部网络上的 ip
    direct_external_ip = _get_vm_direct_external_ip(server_id)
    if direct_external_ip:
        # 在本地数据库删除此 IP
        floating_ip_manager.delete_floating_ip_address(direct_external_ip)
        return direct_external_ip

    # 否则找到一个与外部网络有路由关系的内部网络,再分配 floating ip
    external_net_id = get_vm_external_net_id(server_id)
    if not external_net_id:
        return None

    ip = floating_ip_manager.allocate_ip(external_net_id)
    ip_address = ip['address']
    ip_id = ip['id']
    update_dict = {}
    update_dict['port_id'] = get_vm_net_port_id(server_id)
    try:
        NEUTRON_CLI.update_floatingip(ip_id, {'floatingip': update_dict})
    except:
        floating_ip_manager.reclaim_ip(ip_address)
        raise
    return ip_address
Esempio n. 2
0
def __disassociate_floating_ip_neutron(server_id):
    """
    disassociate floating ip from vm in neutron
    """
    vm = NOVA_CLI.servers.get(server_id)
    if vm:
        port_id = get_vm_net_port_id(server_id)
        param = {'port_id': port_id}
        floating_ips = NEUTRON_CLI.list_floatingips(**param)
        if floating_ips['floatingips']:
            for ip in floating_ips['floatingips']:
                floating_ip_manager.reclaim_ip(ip['floating_ip_address'])
                update_dict = {}
                update_dict['port_id'] = None
                NEUTRON_CLI.update_floatingip(ip['id'],
                                              {'floatingip': update_dict})
Esempio n. 3
0
def __disassociate_floating_ip_nova_network(server):
    """
    disassociate floating ip with server in nova network
    """
    vm = NOVA_CLI.servers.get(server)
    addresses = vm.addresses if vm.addresses else None
    if addresses:
        net_name = list(addresses.keys())[0]
        net_addresses = addresses[net_name]

        floating_ip = None
        for addr in net_addresses:
            if addr['OS-EXT-IPS:type'] == 'floating':
                floating_ip = addr['addr']
                break

        if floating_ip:
            NOVA_CLI.servers.remove_floating_ip(vm.id, floating_ip)
            floating_ip_manager.reclaim_ip(floating_ip)