Пример #1
0
 def _free_vlans():
     free_vlans = set(
         range(int(settings.VLANS_RANGE_START),
               int(settings.VLANS_RANGE_END))) - set(used_vlans)
     if not free_vlans or len(free_vlans) < len(networks_metadata):
         raise errors.OutOfVLANs()
     return sorted(list(free_vlans))
Пример #2
0
 def _free_vlans():
     free_vlans = set(
         range(
             *global_params.parameters["vlan_range"]
         )
     ) - set(used_vlans)
     if not free_vlans or len(free_vlans) < len(networks_list):
         raise errors.OutOfVLANs()
     return sorted(list(free_vlans))
Пример #3
0
    def create_network_groups(self, cluster_id):
        '''
        Method for creation of network groups for cluster.

        :param cluster_id: Cluster database ID.
        :type  cluster_id: int
        :returns: None
        :raises: errors.OutOfVLANs, errors.OutOfIPs,
        errors.NoSuitableCIDR
        '''
        used_nets = [n.cidr for n in self.db.query(Network).all()]
        used_vlans = [v.id for v in self.db.query(Vlan).all()]
        cluster_db = self.db.query(Cluster).get(cluster_id)

        networks_metadata = cluster_db.release.networks_metadata

        free_vlans = set(
            range(int(settings.VLANS_RANGE_START), int(
                settings.VLANS_RANGE_END))) - set(used_vlans)

        if not free_vlans or len(free_vlans) < len(networks_metadata):
            raise errors.OutOfVLANs()

        for network in networks_metadata:
            vlan_start = sorted(list(free_vlans))[0]
            logger.debug(u"Found free vlan: %s", vlan_start)
            pool = settings.NETWORK_POOLS.get(network['access'])
            if not pool:
                raise errors.InvalidNetworkAccess(
                    u"Invalid access '{0}' for network '{1}'".format(
                        network['access'], network['name']))
            nets_free_set = IPSet(pool) -\
                IPSet(settings.NET_EXCLUDE) -\
                IPSet(
                    IPRange(
                        settings.ADMIN_NETWORK["first"],
                        settings.ADMIN_NETWORK["last"]
                    )
                ) -\
                IPSet(used_nets)
            if not nets_free_set:
                raise errors.OutOfIPs()

            free_cidrs = sorted(list(nets_free_set._cidrs))
            new_net = None
            for fcidr in free_cidrs:
                for n in fcidr.subnet(24, count=1):
                    new_net = n
                    break
                if new_net:
                    break
            if not new_net:
                raise errors.NoSuitableCIDR()

            new_ip_range = IPAddrRange(first=str(new_net[2]),
                                       last=str(new_net[-2]))

            nw_group = NetworkGroup(release=cluster_db.release.id,
                                    name=network['name'],
                                    access=network['access'],
                                    cidr=str(new_net),
                                    netmask=str(new_net.netmask),
                                    gateway=str(new_net[1]),
                                    cluster_id=cluster_id,
                                    vlan_start=vlan_start,
                                    amount=1)
            self.db.add(nw_group)
            self.db.commit()
            nw_group.ip_ranges.append(new_ip_range)
            self.db.commit()
            self.create_networks(nw_group)

            free_vlans = free_vlans - set([vlan_start])
            used_nets.append(str(new_net))