Exemplo n.º 1
0
    def handle(self, *args, **options):
        type_ = options['type']

        if not type_:
            raise CommandError("Type of pool is mandatory")

        pool_table = common.pool_table_from_type(type_)

        try:
            pool_id = int(args[0])
            pool_row = pool_table.objects.get(id=pool_id)
        except (ValueError, pool_table.DoesNotExist):
            raise CommandError("Invalid pool ID")

        pool = pool_row.pool

        kv = {
            'id': pool_row.id,
            'offset': pool_row.offset,
            'base': pool_row.base,
            'size': pool_row.size,
            'available': pool.count_available(),
            'reserved': pool.count_reserved(),
        }

        for key, val in sorted(kv.items()):
            line = '%s: %s\n' % (key.rjust(16), val)
            self.stdout.write(line.encode('utf8'))

        step = (type_ == 'bridge') and 64 or 80
        pprint.pprint_pool('Available', pool.to_map(), step, self.stdout)
        pprint.pprint_pool('Reserved',
                           bitarray_to_map(pool.reserved[:pool_row.size]),
                           step, self.stdout)
Exemplo n.º 2
0
    def handle(self, *args, **options):
        type_ = options['type']

        if not type_:
            raise CommandError("Type of pool is mandatory")

        pool_table = common.pool_table_from_type(type_)

        try:
            pool_id = int(args[0])
            pool_row = pool_table.objects.get(id=pool_id)
        except (ValueError, pool_table.DoesNotExist):
            raise CommandError("Invalid pool ID")

        pool = pool_row.pool

        kv = {
            'id': pool_row.id,
            'offset': pool_row.offset,
            'base': pool_row.base,
            'size': pool_row.size,
            'available': pool.count_available(),
            'reserved': pool.count_reserved(),
        }

        for key, val in sorted(kv.items()):
            line = '%s: %s\n' % (key.rjust(16), val)
            self.stdout.write(line.encode('utf8'))

        step = (type_ == 'bridge') and 64 or 80
        pprint.pprint_pool('Available', pool.to_map(), step, self.stdout)
        pprint.pprint_pool('Reserved',
                           bitarray_to_map(pool.reserved[:pool_row.size]),
                           step, self.stdout)
Exemplo n.º 3
0
def pprint_ippool(subnet, stdout=None, title=None):
    """Pretty print IP Pools of a subnet. Only IPv4 subnets have IP Pools"""

    if int(subnet.ipversion) != 4:
        return 0

    if stdout is None:
        stdout = sys.stdout

    stdout.write("IP Pools of subnet %s:\n\n" % subnet.id)

    for pool in subnet.get_ip_pools():
        size = pool.pool_size
        available = pool.available.count()
        info = OrderedDict([("First_IP", pool.return_start()),
                            ("Last_IP", pool.return_end()), ("Size", size),
                            ("Available", available)])
        pprint_table(stdout, info.items(), None, separator=" | ", title=None)

        reserved = [
            pool.index_to_value(index)
            for index, ip in enumerate(pool.reserved[:size]) if ip is False
        ]

        if reserved != []:
            stdout.write("\nExternally Reserved IPs:\n\n")
            stdout.write(", ".join(reserved) + "\n")

        ip_sum = pool.available[:size] & pool.reserved[:size]
        pprint_pool(None, bitarray_to_map(ip_sum), 80, stdout)
        stdout.write("\n\n")
Exemplo n.º 4
0
    def handle(self, *args, **options):
        type_ = options["type"]

        if not type_:
            raise CommandError("Type of pool is mandatory")

        pool_table = common.pool_table_from_type(type_)

        try:
            pool_id = int(args[0])
            pool_row = pool_table.objects.get(id=pool_id)
        except IndexError:
            raise CommandError("Please provide a pool ID")
        except (ValueError, pool_table.DoesNotExist):
            raise CommandError("Invalid pool ID")

        pool = pool_row.pool

        kv = {
            "id": pool_row.id,
            "offset": pool_row.offset,
            "base": pool_row.base,
            "size": pool_row.size,
            "available": pool.count_available(),
            "reserved": pool.count_reserved(),
        }

        for key, val in sorted(kv.items()):
            line = "%s: %s\n" % (key.rjust(16), val)
            self.stdout.write(line.encode("utf8"))

        step = (type_ == "bridge") and 64 or 80
        pprint.pprint_pool("Available", pool.to_map(), step, self.stdout)
        pprint.pprint_pool("Reserved", bitarray_to_map(pool.reserved[: pool_row.size]), step, self.stdout)
Exemplo n.º 5
0
def pprint_ippool(subnet, stdout=None, title=None):
    """Pretty print IP Pools of a subnet. Only IPv4 subnets have IP Pools"""

    if int(subnet.ipversion) != 4:
        return 0

    if stdout is None:
        stdout = sys.stdout

    stdout.write("IP Pools of subnet %s:\n\n" % subnet.id)

    for pool in subnet.get_ip_pools():
        size = pool.pool_size
        available = pool.available.count()
        info = OrderedDict([("First_IP", pool.return_start()),
                            ("Last_IP", pool.return_end()),
                            ("Size", size),
                            ("Available", available)])
        pprint_table(stdout, info.items(), None, separator=" | ", title=None)

        reserved = [pool.index_to_value(index)
                    for index, ip in enumerate(pool.reserved[:size])
                    if ip is False]

        if reserved != []:
            stdout.write("\nExternally Reserved IPs:\n\n")
            stdout.write(", ".join(reserved) + "\n")

        ip_sum = pool.available[:size] & pool.reserved[:size]
        pprint_pool(None, bitarray_to_map(ip_sum), 80, stdout)
        stdout.write("\n\n")
Exemplo n.º 6
0
 def test_bitarray_to_map(self):
     bt = bitarray('01001100101')
     map_ = bitarray_to_map(bt)
     self.assertEqual(map_, 'X.XX..XX.X.')
Exemplo n.º 7
0
 def test_bitarray_to_map(self):
     bt = bitarray('01001100101')
     map_ = bitarray_to_map(bt)
     self.assertEqual(map_, 'X.XX..XX.X.')