Exemplo n.º 1
0
 def update_initial_state(self, callback):
     addresses = ip_lib.get_devices_with_ip(self.ha_namespace,
                                            name=self.get_ha_device_name())
     cidrs = (address['cidr'] for address in addresses)
     ha_cidr = self._get_primary_vip()
     state = 'master' if ha_cidr in cidrs else 'backup'
     self.ha_state = state
     callback(self.router_id, state)
Exemplo n.º 2
0
 def update_initial_state(self, callback):
     addresses = ip_lib.get_devices_with_ip(self.ha_namespace,
                                            name=self.get_ha_device_name())
     cidrs = (address['cidr'] for address in addresses)
     ha_cidr = self._get_primary_vip()
     state = 'master' if ha_cidr in cidrs else 'backup'
     self.ha_state = state
     callback(self.router_id, state)
Exemplo n.º 3
0
 def get_ipv6_llas(self, device_name, namespace):
     kwargs = {
         'family': utils.get_socket_address_family(constants.IP_VERSION_6),
         'scope': 'link'
     }
     return ip_lib.get_devices_with_ip(namespace,
                                       name=device_name,
                                       **kwargs)
Exemplo n.º 4
0
    def test_get_devices_with_ip_name(self):
        for idx in range(self.num_devices_with_ip):
            dev_name = 'test_device_%s' % idx
            ip_addresses = ip_lib.get_devices_with_ip(self.namespace,
                                                      name=dev_name)
            ip_addresses = self._remove_loopback_interface(ip_addresses)
            ip_addresses = self._remove_ipv6_scope_link(ip_addresses)

            for cidr in self.cidrs:
                cidr = (str(cidr.ip + idx) + '/' +
                        str(cidr.netmask.netmask_bits()))
                self._pop_ip_address(ip_addresses, cidr)

            self.assertEqual(0, len(ip_addresses))

        for idx in range(self.num_devices_with_ip, self.num_devices):
            dev_name = 'test_device_%s' % idx
            ip_addresses = ip_lib.get_devices_with_ip(self.namespace,
                                                      name=dev_name)
            ip_addresses = self._remove_loopback_interface(ip_addresses)
            ip_addresses = self._remove_ipv6_scope_link(ip_addresses)
            self.assertEqual(0, len(ip_addresses))
Exemplo n.º 5
0
 def get_centralized_fip_cidr_set(self):
     """Returns the fip_cidr set for centralized floatingips."""
     ex_gw_port = self.get_ex_gw_port()
     # Don't look for centralized FIP cidrs if gw_port not exists or
     # this is not snat host
     if (not ex_gw_port or not self._is_this_snat_host() or
             not self.snat_namespace.exists()):
         return set()
     interface_name = self.get_snat_external_device_interface_name(
             ex_gw_port)
     return set([addr['cidr'] for addr in ip_lib.get_devices_with_ip(
                                              self.snat_namespace.name,
                                              name=interface_name)])
Exemplo n.º 6
0
 def get_centralized_fip_cidr_set(self):
     """Returns the fip_cidr set for centralized floatingips."""
     ex_gw_port = self.get_ex_gw_port()
     # Don't look for centralized FIP cidrs if gw_port not exists or
     # this is not snat host
     if (not ex_gw_port or not self._is_this_snat_host() or
             not self.snat_namespace.exists()):
         return set()
     interface_name = self.get_snat_external_device_interface_name(
             ex_gw_port)
     return set([addr['cidr'] for addr in ip_lib.get_devices_with_ip(
                                              self.snat_namespace.name,
                                              name=interface_name)])
Exemplo n.º 7
0
    def _setup_interface_ip(self, ip, interface='lo'):
        """Sets up an IP address on the target interface

        Args:
            ip(String): ip address with cidr
            interface(String): network interface, 'lo' by default
        Return:
            None
        """
        dev = ip_lib.IPDevice(interface)
        dev.addr = ip_lib.IpAddrCommand(dev)
        existing_addreses = ip_lib.get_devices_with_ip(None, name=dev.name)
        existing_ips = [addr['cidr'] for addr in existing_addreses]
        if ip not in existing_ips:
            LOG.info("Adding %s to %s interface" % (ip, dev.name))
            dev.addr.add(cidr=ip)
        else:
            LOG.debug("%s interface already have %s ip" % (dev.name, ip))
Exemplo n.º 8
0
    def _ensure_dev_having_ip(self, target_dev, ip):
        """Ensure target device have ip

        Args:
            target_dev(ip_lib.IPDevice):
            ip(String): ip address with cidr
        Return:
            None
        """
        existing_addreses = ip_lib.get_devices_with_ip(None,
                                                       name=target_dev.name)
        existing_ips = [addr['cidr'] for addr in existing_addreses]
        LOG.debug("The existing address of dev %s are %s" %
                  (target_dev.name, existing_ips))
        if ip not in existing_ips:
            target_dev.addr.add(cidr=ip, additional_args=[
                'noprefixroute',
            ])
        else:
            LOG.debug("%s already have ip %s" % (target_dev.name, ip))
Exemplo n.º 9
0
    def handle_initial_state(self):
        try:
            state = 'backup'
            cidr = common_utils.ip_to_cidr(self.cidr)
            # NOTE(ralonsoh): "get_devices_with_ip" without passing an IP
            # address performs one single pyroute2 command. Because the number
            # of interfaces in the namespace is reduced, this is faster.
            for address in ip_lib.get_devices_with_ip(self.namespace):
                if (address['name'] == self.interface
                        and address['cidr'] == cidr):
                    state = 'primary'
                    break

            if not self.initial_state:
                self.write_state_change(state)
                self.notify_agent(state)
        except Exception:
            if not self.initial_state:
                LOG.exception('Failed to get initial status of router %s',
                              self.router_id)
Exemplo n.º 10
0
 def get_ipv6_llas(self, device_name, namespace):
     kwargs = {'family': utils.get_socket_address_family(
                             constants.IP_VERSION_6),
               'scope': 'link'}
     return ip_lib.get_devices_with_ip(namespace, name=device_name,
                                       **kwargs)