Пример #1
0
    def test_check_untagged_intersection_failed(self, mocked_db):
        cluster = self.env.create(
            nodes_kwargs=[
                {'roles': ['controller'], 'pending_addition': True}
            ]
        )
        cluster_db = self.db.query(Cluster).get(cluster['id'])

        ng1 = NetworkGroup()
        ng1.name = consts.NETWORKS.storage
        ng2 = NetworkGroup()
        ng2.name = consts.NETWORKS.management
        self.env.db().add_all([ng1, ng2])
        self.env.db().flush()

        checker = NetworkCheck(FakeTask(cluster_db), {})
        checker.networks = [{'id': ng1.id,
                             'cidr': '192.168.0.0/24',
                             'name': ng1.name,
                             'vlan_start': None,
                             'meta': {'notation': consts.NETWORK_NOTATION.cidr}
                             },
                            {'id': ng2.id,
                             'cidr': '192.168.0.0/26',
                             'name': ng2.name,
                             'vlan_start': None,
                             'meta': {'notation': consts.NETWORK_NOTATION.cidr}
                             }]
        checker.cluster.nodes[0].interfaces[0].assigned_networks_list = \
            [ng1, ng2]
        checker.cluster.nodes[0].interfaces[1].assigned_networks_list = \
            [ng1, ng2]
        self.env.db.flush()
        self.assertRaises(errors.NetworkCheckError,
                          checker.check_untagged_intersection)
Пример #2
0
    def test_check_untagged_intersection(self):
        cluster = self.env.create(nodes_kwargs=[{
            'roles': ['controller'],
            'pending_addition': True
        }])
        cluster_db = self.db.query(Cluster).get(cluster['id'])
        checker = NetworkCheck(FakeTask(cluster_db), {})
        checker.networks = [{
            'id': 1,
            'cidr': '192.168.0.0/24',
            'name': 'fake1',
            'vlan_start': None,
            'meta': {
                'notation': 'cidr'
            }
        }]
        ng1 = NetworkGroup()
        ng1.name = 'fake3'
        ng1.id = 3
        ng2 = NetworkGroup()
        ng2.name = 'fake4'
        ng2.id = 4
        checker.cluster.nodes[0].interfaces[0].assigned_networks_list = \
            [ng1, ng2]

        self.assertNotRaises(errors.NetworkCheckError,
                             checker.check_untagged_intersection)
Пример #3
0
    def create_network_groups(cls, 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
        """
        cluster_db = db().query(Cluster).get(cluster_id)
        networks_metadata = \
            cluster_db.release.networks_metadata["nova_network"]

        for network in networks_metadata["networks"]:
            new_ip_range = IPAddrRange(first=network["ip_range"][0],
                                       last=network["ip_range"][1])
            gw = network['gateway'] if network.get('use_gateway') else None

            nw_group = NetworkGroup(release=cluster_db.release.id,
                                    name=network['name'],
                                    cidr=network['cidr'],
                                    netmask=network['netmask'],
                                    gateway=gw,
                                    cluster_id=cluster_id,
                                    vlan_start=network['vlan_start'],
                                    amount=1,
                                    network_size=network['network_size']
                                    if 'network_size' in network else 256)
            db().add(nw_group)
            db().commit()
            nw_group.ip_ranges.append(new_ip_range)
            db().commit()
            cls.cleanup_network_group(nw_group)
Пример #4
0
    def create_network_groups(cls, cluster, neutron_segment_type, gid=None):
        """Method for creation of network groups for cluster.

        :param cluster: Cluster instance.
        :type  cluster: instance
        :returns: None
        """
        group_id = gid or objects.Cluster.get_default_group(cluster).id
        networks_metadata = cluster.release.networks_metadata
        networks_list = networks_metadata[cluster.net_provider]["networks"]
        used_nets = [IPNetwork(cls.get_admin_network_group().cidr)]

        def check_range_in_use_already(cidr_range):
            for n in used_nets:
                if cls.is_range_intersection(n, cidr_range):
                    logger.warn(
                        "IP range {0} is in use already".format(cidr_range))
                    break
            used_nets.append(cidr_range)

        for net in networks_list:
            if "seg_type" in net \
                    and neutron_segment_type != net['seg_type']:
                continue
            vlan_start = net.get("vlan_start")
            cidr, gw, cidr_gw = None, None, None
            if net.get("notation"):
                if net.get("cidr"):
                    cidr = IPNetwork(net["cidr"]).cidr
                    cidr_gw = str(cidr[1])
                if net["notation"] == 'cidr' and cidr:
                    new_ip_range = IPAddrRange(first=str(cidr[2]),
                                               last=str(cidr[-2]))
                    if net.get('use_gateway'):
                        gw = cidr_gw
                    else:
                        new_ip_range.first = cidr_gw
                    check_range_in_use_already(cidr)
                elif net["notation"] == 'ip_ranges' and net.get("ip_range"):
                    new_ip_range = IPAddrRange(first=net["ip_range"][0],
                                               last=net["ip_range"][1])
                    gw = net.get('gateway') or cidr_gw \
                        if net.get('use_gateway') else None
                    check_range_in_use_already(
                        IPRange(new_ip_range.first, new_ip_range.last))
            if net['name'] == "management":
                gw = "192.168.0.254"  #set default manage gateway
            nw_group = NetworkGroup(release=cluster.release.id,
                                    name=net['name'],
                                    cidr=str(cidr) if cidr else None,
                                    gateway=gw,
                                    group_id=group_id,
                                    vlan_start=vlan_start,
                                    meta=net)
            db().add(nw_group)
            db().flush()
            if net.get("notation"):
                nw_group.ip_ranges.append(new_ip_range)
                db().flush()
                cls.cleanup_network_group(nw_group)
Пример #5
0
 def create_admin_network_group(cls, cluster_id, group_id):
     cluster_db = objects.Cluster.get_by_uid(cluster_id)
     admin_ng = cls.get_admin_network_group()
     new_admin = NetworkGroup(release=cluster_db.release.id,
                              name='fuelweb_admin',
                              cidr='9.9.9.0/24',
                              gateway='9.9.9.1',
                              group_id=group_id,
                              vlan_start=None,
                              meta=admin_ng.meta)
     db().add(new_admin)
     db().flush()
Пример #6
0
 def test_network_group_does_not_squeezes_base_cidr_if_amount_1(self):
     cluster = self.env.create_cluster(api=False)
     kw = {
         'release': cluster.release_id,
         'cidr': '172.0.0.0/8',
         'netmask': '255.0.0.0',
         'network_size': 256,
         'name': 'public',
         'vlan_start': 200,
         'amount': 1,
         'cluster_id': cluster.id
     }
     ng = NetworkGroup(**kw)
     self.db.add(ng)
     self.db.commit()
     self.env.network_manager.cleanup_network_group(ng)
     self.db.refresh(ng)
     self.assertEquals(ng.cidr, "172.0.0.0/8")
Пример #7
0
 def test_network_group_creates_several_networks(self):
     cluster = self.env.create_cluster(api=False)
     kw = {
         'release': cluster.release_id,
         'cidr': '10.0.0.0/8',
         'netmask': '255.0.0.0',
         'network_size': 256,
         'name': 'fixed',
         'vlan_start': 200,
         'amount': 25,
         'cluster_id': cluster.id
     }
     ng = NetworkGroup(**kw)
     self.db.add(ng)
     self.db.commit()
     self.env.network_manager.cleanup_network_group(ng)
     nets_db = self.db.query(NetworkGroup).filter(
         not_(NetworkGroup.name == "fuelweb_admin")).all()
     self.assertEquals(nets_db[0].amount, kw['amount'])
     self.assertEquals(nets_db[0].vlan_start, kw['vlan_start'])
     self.assertEquals(all(x.name == kw['name'] for x in nets_db), True)
Пример #8
0
 def test_network_group_size_of_1_creates_1_network(self):
     cluster = self.env.create_cluster(api=False)
     kw = {
         'release': cluster.release_id,
         'cidr': '10.0.0.0/24',
         'netmask': '255.255.255.0',
         'network_size': 256,
         'name': 'fixed',
         'vlan_start': 200,
         'cluster_id': cluster.id
     }
     ng = NetworkGroup(**kw)
     self.db.add(ng)
     self.db.commit()
     self.env.network_manager.cleanup_network_group(ng)
     nets_db = self.db.query(NetworkGroup).filter(
         not_(NetworkGroup.name == "fuelweb_admin")).all()
     self.assertEquals(len(nets_db), 1)
     self.assertEquals(nets_db[0].vlan_start, kw['vlan_start'])
     self.assertEquals(nets_db[0].name, kw['name'])
     self.assertEquals(nets_db[0].cidr, kw['cidr'])
Пример #9
0
 def test_network_group_sets_correct_gateway_for_few_nets(self):
     cluster = self.env.create_cluster(api=False)
     kw = {
         'release': cluster.release_id,
         'cidr': '10.0.0.0/8',
         'netmask': '255.0.0.0',
         'network_size': 128,
         'name': 'fixed',
         'vlan_start': 200,
         'amount': 2,
         'gateway': "10.0.0.5",
         'cluster_id': cluster.id
     }
     ng = NetworkGroup(**kw)
     self.db.add(ng)
     self.db.commit()
     self.env.network_manager.cleanup_network_group(ng)
     nets_db = self.db.query(NetworkGroup).filter(
         not_(NetworkGroup.name == "fuelweb_admin")).all()
     self.assertEquals(nets_db[0].amount, kw['amount'])
     self.assertEquals(nets_db[0].gateway, "10.0.0.5")
Пример #10
0
    def create_network_groups(cls, cluster_id):
        """Method for creation of network groups for cluster.

        :param cluster_id: Cluster database ID.
        :type  cluster_id: int
        :returns: None
        :raises: errors.InvalidNetworkCIDR
        """
        cluster_db = db().query(Cluster).get(cluster_id)
        networks_metadata = cluster_db.release.networks_metadata
        networks_list = networks_metadata[cluster_db.net_provider]["networks"]
        used_nets = [IPNetwork(cls.get_admin_network_group().cidr)]

        def check_range_in_use_already(cidr_range):
            for n in used_nets:
                if cls.is_range_intersection(n, cidr_range):
                    logger.warn(
                        "IP range {0} is in use already".format(cidr_range))
                    break
            used_nets.append(cidr_range)

        for net in networks_list:
            if "seg_type" in net and \
                    cluster_db.net_segment_type != net['seg_type']:
                continue
            vlan_start = net.get("vlan_start")
            net_size = net.get('network_size')
            cidr, gw, cidr_gw, netmask = None, None, None, None
            if net.get("notation"):
                if net.get("cidr"):
                    cidr = IPNetwork(net["cidr"]).cidr
                    cidr_gw = str(cidr[1])
                    netmask = str(cidr.netmask)
                    net_size = net_size or cidr.size
                if net["notation"] == 'cidr' and cidr:
                    new_ip_range = IPAddrRange(first=str(cidr[2]),
                                               last=str(cidr[-2]))
                    if net.get('use_gateway'):
                        gw = cidr_gw
                    else:
                        new_ip_range.first = cidr_gw
                    check_range_in_use_already(cidr)
                elif net["notation"] == 'ip_ranges' and net.get("ip_range"):
                    new_ip_range = IPAddrRange(first=net["ip_range"][0],
                                               last=net["ip_range"][1])
                    gw = net.get('gateway') or cidr_gw \
                        if net.get('use_gateway') else None
                    netmask = net.get('netmask') or netmask
                    check_range_in_use_already(
                        IPRange(new_ip_range.first, new_ip_range.last))

            nw_group = NetworkGroup(release=cluster_db.release.id,
                                    name=net['name'],
                                    cidr=str(cidr) if cidr else None,
                                    netmask=netmask,
                                    gateway=gw,
                                    cluster_id=cluster_id,
                                    vlan_start=vlan_start,
                                    amount=1,
                                    network_size=net_size or 1,
                                    meta=net)
            db().add(nw_group)
            db().commit()
            if net.get("notation"):
                nw_group.ip_ranges.append(new_ip_range)
                db().commit()
                cls.cleanup_network_group(nw_group)
Пример #11
0
    def create_network_groups(cls, 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 = []
        used_vlans = []

        global_params = db().query(GlobalParameters).first()

        cluster_db = db().query(Cluster).get(cluster_id)

        networks_metadata = cluster_db.release.networks_metadata

        admin_network_range = db().query(IPAddrRange).filter_by(
            network_group_id=cls.get_admin_network_group_id()).all()[0]

        networks_list = networks_metadata["neutron"]["networks"]

        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))

        for network in networks_list:
            free_vlans = _free_vlans()
            vlan_start = free_vlans[0] if "vlan_start" not in network \
                else network.get("vlan_start")
            if vlan_start and vlan_start not in free_vlans:
                vlan_start = free_vlans[0]

            logger.debug("Found free vlan: %s", vlan_start)
            pool = network.get('pool')
            if not pool:
                raise errors.InvalidNetworkPool(
                    u"Invalid pool '{0}' for network '{1}'".format(
                        pool, network['name']))

            nets_free_set = IPSet(pool) -\
                IPSet(
                    IPNetwork(global_params.parameters["net_exclude"])
                ) -\
                IPSet(
                    IPRange(
                        admin_network_range.first,
                        admin_network_range.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()

            if network.get("ip_range"):
                new_ip_range = IPAddrRange(first=network["ip_range"][0],
                                           last=network["ip_range"][1])
            else:
                new_ip_range = IPAddrRange(first=str(new_net[2]),
                                           last=str(new_net[-2]))
            gw = str(new_net[1]) if network.get('use_gateway') else None

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

            used_vlans.append(vlan_start)
            used_nets.append(str(new_net))

        if cluster_db.net_segment_type == 'vlan':
            private_network_group = NetworkGroup(release=cluster_db.release.id,
                                                 name="private",
                                                 cluster_id=cluster_id,
                                                 netmask='32',
                                                 vlan_start=None,
                                                 amount=1)
            db().add(private_network_group)
            db().commit()