Exemplo n.º 1
0
    def PUT(self, cluster_id):
        """
        :IMPORTANT: this method should be rewritten to be more RESTful

        :returns: JSONized Task object.
        :http: * 202 (network checking task failed)
               * 200 (network verification task started)
               * 404 (cluster not found in db)
        """
        cluster = self.get_object_or_404(Cluster, cluster_id)

        try:
            data = self.validator.validate_networks_update(web.data())
        except web.webapi.badrequest as exc:
            task = Task(name='check_networks', cluster=cluster)
            db().add(task)
            db().commit()
            TaskHelper.set_error(task.uuid, exc.data)
            logger.error(traceback.format_exc())

            json_task = build_json_response(TaskHandler.render(task))
            raise web.accepted(data=json_task)

        vlan_ids = [{
            'name': n['name'],
            'vlans': NetworkGroup.generate_vlan_ids_list(n)
        } for n in data['networks']]

        task_manager = VerifyNetworksTaskManager(cluster_id=cluster.id)
        task = task_manager.execute(data, vlan_ids)

        return TaskHandler.render(task)
Exemplo n.º 2
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',
         'access': 'private',
         'vlan_start': 200,
         'amount': 25,
         'cluster_id': cluster.id
     }
     ng = NetworkGroup(**kw)
     self.db.add(ng)
     self.db.commit()
     self.env.network_manager.create_networks(ng)
     nets_db = self.db.query(Network).filter(
         not_(Network.name == "fuelweb_admin")).all()
     self.assertEquals(len(nets_db), kw['amount'])
     self.assertEquals(nets_db[0].vlan_id, kw['vlan_start'])
     self.assertEquals(nets_db[kw['amount'] - 1].vlan_id,
                       kw['vlan_start'] + kw['amount'] - 1)
     self.assertEquals(all(x.name == kw['name'] for x in nets_db), True)
     self.assertEquals(all(x.access == kw['access'] for x in nets_db), True)
     vlans_db = self.db.query(Vlan).all()
     self.assertEquals(len(vlans_db), kw['amount'] + 1)  # + 1 for admin net
Exemplo n.º 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
        """
        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])

            nw_group = NetworkGroup(release=cluster_db.release.id,
                                    name=network['name'],
                                    cidr=network['cidr'],
                                    netmask=network['netmask'],
                                    gateway=network['gateway'],
                                    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()
            self.create_networks(nw_group)
Exemplo n.º 4
0
    def PUT(self, cluster_id):
        """:IMPORTANT: this method should be rewritten to be more RESTful

        :returns: JSONized Task object.
        :http: * 202 (network checking task failed)
               * 200 (network verification task started)
               * 404 (cluster not found in db)
        """
        cluster = self.get_object_or_404(Cluster, cluster_id)

        try:
            data = self.validator.validate_networks_update(web.data())
        except web.webapi.badrequest as exc:
            task = Task(name='check_networks', cluster=cluster)
            db().add(task)
            db().commit()
            TaskHelper.set_error(task.uuid, exc.data)
            logger.error(traceback.format_exc())

            json_task = build_json_response(TaskHandler.render(task))
            raise web.accepted(data=json_task)

        vlan_ids = [{
            'name': n['name'],
            'vlans': NetworkGroup.generate_vlan_ids_list(n)
        } for n in data['networks']]

        task_manager = VerifyNetworksTaskManager(cluster_id=cluster.id)
        task = task_manager.execute(data, vlan_ids)

        return TaskHandler.render(task)
Exemplo n.º 5
0
    def PUT(self, cluster_id):
        cluster = self.get_object_or_404(Cluster, cluster_id)
        data = self.validator.validate_networks_update(web.data())
        vlan_ids = [{
            'name': n['name'],
            'vlans': NetworkGroup.generate_vlan_ids_list(n)
        } for n in data['networks']]
        task_manager = VerifyNetworksTaskManager(cluster_id=cluster.id)
        task = task_manager.execute(data, vlan_ids)

        return TaskHandler.render(task)
Exemplo n.º 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.create_networks(ng)
     self.db.refresh(ng)
     self.assertEquals(ng.cidr, "172.0.0.0/8")
Exemplo n.º 7
0
 def test_network_group_does_not_squeezes_base_cidr(self):
     cluster = self.env.create_cluster(api=False)
     kw = {
         'release': cluster.release_id,
         'cidr': '172.0.0.0/24',
         'netmask': '255.255.255.0',
         'network_size': 64,
         'name': 'fixed',
         'access': 'private',
         'vlan_start': 200,
         'amount': 3,
         'cluster_id': cluster.id
     }
     ng = NetworkGroup(**kw)
     self.db.add(ng)
     self.db.commit()
     self.env.network_manager.create_networks(ng)
     self.db.refresh(ng)
     self.assertEquals(ng.cidr, "172.0.0.0/24")
Exemplo n.º 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.create_networks(ng)
     nets_db = self.db.query(Network).filter(
         not_(Network.name == "fuelweb_admin")).all()
     self.assertEquals(len(nets_db), 1)
     self.assertEquals(nets_db[0].vlan_id, kw['vlan_start'])
     self.assertEquals(nets_db[0].name, kw['name'])
     self.assertEquals(nets_db[0].cidr, kw['cidr'])
Exemplo n.º 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.create_networks(ng)
     nets_db = self.db.query(Network).filter(
         not_(Network.name == "fuelweb_admin")).all()
     self.assertEquals(len(nets_db), kw['amount'])
     self.assertEquals(nets_db[0].gateway, "10.0.0.5")
     self.assertEquals(nets_db[1].gateway, "10.0.0.5")
Exemplo n.º 10
0
    def PUT(self, cluster_id):
        cluster = self.get_object_or_404(Cluster, cluster_id)

        try:
            data = self.validator.validate_networks_update(web.data())
        except web.webapi.badrequest as exc:
            task = Task(name='check_networks', cluster=cluster)
            db().add(task)
            db().commit()
            TaskHelper.set_error(task.uuid, exc.data)
            logger.error(traceback.format_exc())

            json_task = build_json_response(TaskHandler.render(task))
            raise web.accepted(data=json_task)

        vlan_ids = [{
            'name': n['name'],
            'vlans': NetworkGroup.generate_vlan_ids_list(n)
        } for n in data['networks']]

        task_manager = VerifyNetworksTaskManager(cluster_id=cluster.id)
        task = task_manager.execute(data, vlan_ids)

        return TaskHandler.render(task)
Exemplo n.º 11
0
 def test_network_group_slices_cidr_for_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': 128,
         'name': 'fixed',
         'access': 'private',
         'vlan_start': 200,
         'amount': 2,
         'cluster_id': cluster.id
     }
     ng = NetworkGroup(**kw)
     self.db.add(ng)
     self.db.commit()
     self.env.network_manager.create_networks(ng)
     nets_db = self.db.query(Network).filter(
         not_(Network.name == "fuelweb_admin")).all()
     self.assertEquals(len(nets_db), kw['amount'])
     self.assertEquals(nets_db[0].cidr, '10.0.0.0/25')
     self.assertEquals(nets_db[1].cidr, '10.0.0.128/25')
     self.db.refresh(ng)
     self.assertEquals(ng.cidr, '10.0.0.0/8')
Exemplo n.º 12
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))
Exemplo n.º 13
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 = []
        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=self.get_admin_network_group_id()).all()[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_metadata):
                raise errors.OutOfVLANs()
            return sorted(list(free_vlans))

        public_vlan = _free_vlans()[0]
        used_vlans.append(public_vlan)
        for network in networks_metadata:
            free_vlans = _free_vlans()
            vlan_start = public_vlan if network.get("use_public_vlan") \
                else 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()

            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'],
                                    cidr=str(new_net),
                                    netmask=str(new_net.netmask),
                                    gateway=str(new_net[1]),
                                    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()
            self.create_networks(nw_group)

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