Beispiel #1
0
 def get_dynamic_maasipset(self, exclude_ip_ranges: list = None):
     if exclude_ip_ranges is None:
         exclude_ip_ranges = []
     dynamic_ranges = MAASIPSet(iprange.get_MAASIPRange()
                                for iprange in self.get_dynamic_ranges()
                                if iprange not in exclude_ip_ranges)
     return dynamic_ranges
Beispiel #2
0
 def get_dynamic_maasipset(self, exclude_ip_ranges: list = None):
     if exclude_ip_ranges is None:
         exclude_ip_ranges = []
     dynamic_ranges = MAASIPSet(iprange.get_MAASIPRange()
                                for iprange in self.iprange_set.all()
                                if iprange.type == IPRANGE_TYPE.DYNAMIC
                                and iprange not in exclude_ip_ranges)
     return dynamic_ranges
Beispiel #3
0
 def get_reserved_maasipset(self, exclude_ip_ranges: list = None):
     if exclude_ip_ranges is None:
         exclude_ip_ranges = []
     reserved_ranges = MAASIPSet(iprange.get_MAASIPRange()
                                 for iprange in self.iprange_set.all()
                                 if iprange.type == IPRANGE_TYPE.RESERVED
                                 and iprange not in exclude_ip_ranges)
     return reserved_ranges
def convert_static_ipranges_to_reserved(IPRange, subnet, ranges, created_time,
                                        range_description):
    unreserved_range_set = MAASIPSet(ranges)
    unreserved_ranges = unreserved_range_set.get_unused_ranges(
        subnet.cidr, purpose="reserved")
    for iprange in unreserved_ranges:
        start_ip = str(IPAddress(iprange.first))
        end_ip = str(IPAddress(iprange.last))
        IPRange.objects.get_or_create(
            created=created_time,
            updated=datetime.now(),
            subnet=subnet,
            start_ip=start_ip,
            end_ip=end_ip,
            type=IPRANGE_TYPE.RESERVED,
            comment="Migrated from static range: %s on %s." %
            (range_description, subnet.cidr))
Beispiel #5
0
    def get_maasipset_for_neighbours(self) -> MAASIPSet:
        """Return the observed neighbours in this subnet.

        :return: MAASIPSet of neighbours (with the "neighbour" purpose).
        """
        # Circular imports.
        from maasserver.models import Discovery
        # Note: we only need unknown IP addresses here, because the known
        # IP addresses should already be covered by get_ipranges_in_use().
        neighbours = Discovery.objects.filter(subnet=self).by_unknown_ip()
        neighbour_set = {
            make_iprange(neighbour.ip, purpose="neighbour")
            for neighbour in neighbours
        }
        return MAASIPSet(neighbour_set)
Beispiel #6
0
    def get_ipranges_in_use(
        self,
        exclude_addresses: IPAddressExcludeList = None,
        ranges_only: bool = False,
        include_reserved: bool = True,
        with_neighbours: bool = False,
        ignore_discovered_ips: bool = False,
        exclude_ip_ranges: list = None,
        cached_staticroutes: list = None,
    ) -> MAASIPSet:
        """Returns a `MAASIPSet` of `MAASIPRange` objects which are currently
        in use on this `Subnet`.

        :param exclude_addresses: Additional addresses to consider "in use".
        :param ignore_discovered_ips: DISCOVERED addresses are not "in use".
        :param ranges_only: if True, filters out gateway IPs, static routes,
            DNS servers, and `exclude_addresses`.
        :param with_neighbours: If True, includes addresses learned from
            neighbour observation.
        """
        if exclude_addresses is None:
            exclude_addresses = []
        ranges = set()
        network = self.get_ipnetwork()
        if network.version == 6:
            # For most IPv6 networks, automatically reserve the range:
            #     ::1 - ::ffff:ffff
            # We expect the administrator will be using ::1 through ::ffff.
            # We plan to reserve ::1:0 through ::ffff:ffff for use by MAAS,
            # so that we can allocate addresses in the form:
            #     ::<node>:<child>
            # For now, just make sure IPv6 addresses are allocated from
            # *outside* both ranges, so that they won't conflict with addresses
            # reserved from this scheme in the future.
            first = str(IPAddress(network.first))
            first_plus_one = str(IPAddress(network.first + 1))
            second = str(IPAddress(network.first + 0xFFFFFFFF))
            if network.prefixlen == 64:
                ranges |= {
                    make_iprange(first_plus_one, second, purpose="reserved")
                }
            # Reserve the subnet router anycast address, except for /127 and
            # /128 networks. (See RFC 6164, and RFC 4291 section 2.6.1.)
            if network.prefixlen < 127:
                ranges |= {
                    make_iprange(first, first, purpose="rfc-4291-2.6.1")
                }
        if not ranges_only:
            if (self.gateway_ip is not None and self.gateway_ip != ""
                    and self.gateway_ip in network):
                ranges |= {make_iprange(self.gateway_ip, purpose="gateway-ip")}
            if self.dns_servers is not None:
                ranges |= set(
                    make_iprange(server, purpose="dns-server")
                    for server in self.dns_servers if server in network)
            if cached_staticroutes is not None:
                static_routes = [
                    static_route for static_route in cached_staticroutes
                    if static_route.source == self
                ]
            else:
                static_routes = StaticRoute.objects.filter(source=self)
            for static_route in static_routes:
                ranges |= {
                    make_iprange(static_route.gateway_ip, purpose="gateway-ip")
                }
            ranges |= self._get_ranges_for_allocated_ips(
                network, ignore_discovered_ips)
            ranges |= set(
                make_iprange(address, purpose="excluded")
                for address in exclude_addresses if address in network)
        if include_reserved:
            ranges |= self.get_reserved_maasipset(
                exclude_ip_ranges=exclude_ip_ranges)
        ranges |= self.get_dynamic_maasipset(
            exclude_ip_ranges=exclude_ip_ranges)
        if with_neighbours:
            ranges |= self.get_maasipset_for_neighbours()
        return MAASIPSet(ranges)
Beispiel #7
0
 def get_dynamic_maasipset(self):
     dynamic_ranges = MAASIPSet(iprange.get_MAASIPRange()
                                for iprange in self.get_dynamic_ranges())
     return dynamic_ranges
Beispiel #8
0
 def get_reserved_maasipset(self):
     reserved_ranges = MAASIPSet(iprange.get_MAASIPRange()
                                 for iprange in self.get_reserved_ranges())
     return reserved_ranges