Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / pool-list.py @ be1dca04

History | View | Annotate | Download (3.2 kB)

1 0eed6cf8 Christos Stavrakakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 0eed6cf8 Christos Stavrakakis
#
3 0eed6cf8 Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
4 0eed6cf8 Christos Stavrakakis
# without modification, are permitted provided that the following
5 0eed6cf8 Christos Stavrakakis
# conditions are met:
6 0eed6cf8 Christos Stavrakakis
#
7 0eed6cf8 Christos Stavrakakis
#   1. Redistributions of source code must retain the above
8 0eed6cf8 Christos Stavrakakis
#      copyright notice, this list of conditions and the following
9 0eed6cf8 Christos Stavrakakis
#      disclaimer.
10 0eed6cf8 Christos Stavrakakis
#
11 0eed6cf8 Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
12 0eed6cf8 Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 0eed6cf8 Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
14 0eed6cf8 Christos Stavrakakis
#      provided with the distribution.
15 0eed6cf8 Christos Stavrakakis
#
16 0eed6cf8 Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 0eed6cf8 Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 0eed6cf8 Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 0eed6cf8 Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 0eed6cf8 Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 0eed6cf8 Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 0eed6cf8 Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 0eed6cf8 Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 0eed6cf8 Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 0eed6cf8 Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 0eed6cf8 Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 0eed6cf8 Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
28 0eed6cf8 Christos Stavrakakis
#
29 0eed6cf8 Christos Stavrakakis
# The views and conclusions contained in the software and
30 0eed6cf8 Christos Stavrakakis
# documentation are those of the authors and should not be
31 0eed6cf8 Christos Stavrakakis
# interpreted as representing official policies, either expressed
32 0eed6cf8 Christos Stavrakakis
# or implied, of GRNET S.A.
33 0eed6cf8 Christos Stavrakakis
34 7f9f3018 Christos Stavrakakis
from django.core.management.base import BaseCommand
35 0eed6cf8 Christos Stavrakakis
from optparse import make_option
36 0eed6cf8 Christos Stavrakakis
37 0eed6cf8 Christos Stavrakakis
from util import pool_table_from_type
38 0eed6cf8 Christos Stavrakakis
39 0eed6cf8 Christos Stavrakakis
POOL_CHOICES = ['bridge', 'mac-prefix']
40 0eed6cf8 Christos Stavrakakis
41 cc92b70f Christos Stavrakakis
42 0eed6cf8 Christos Stavrakakis
class Command(BaseCommand):
43 0eed6cf8 Christos Stavrakakis
    help = "List available pools"
44 7f9f3018 Christos Stavrakakis
    output_transaction = True
45 0eed6cf8 Christos Stavrakakis
    option_list = BaseCommand.option_list + (
46 0eed6cf8 Christos Stavrakakis
        make_option('--type', dest='type',
47 0eed6cf8 Christos Stavrakakis
                    choices=POOL_CHOICES,
48 0eed6cf8 Christos Stavrakakis
                    help="Type of pool"
49 0eed6cf8 Christos Stavrakakis
                    ),
50 0eed6cf8 Christos Stavrakakis
    )
51 0eed6cf8 Christos Stavrakakis
52 0eed6cf8 Christos Stavrakakis
    def handle(self, *args, **options):
53 0eed6cf8 Christos Stavrakakis
        type_ = options['type']
54 0eed6cf8 Christos Stavrakakis
55 0eed6cf8 Christos Stavrakakis
        if type_:
56 0eed6cf8 Christos Stavrakakis
            pool_tables = [pool_table_from_type(type_)]
57 0eed6cf8 Christos Stavrakakis
        else:
58 0eed6cf8 Christos Stavrakakis
            pool_tables = [pool_table_from_type(x) for x in POOL_CHOICES]
59 0eed6cf8 Christos Stavrakakis
60 0eed6cf8 Christos Stavrakakis
        for pool_table in pool_tables:
61 0eed6cf8 Christos Stavrakakis
            self.stdout.write("-" * 80 + '\n')
62 0eed6cf8 Christos Stavrakakis
            pl = pool_table.__name__.replace("Table", "")
63 0eed6cf8 Christos Stavrakakis
            self.stdout.write(pl + '\n')
64 0eed6cf8 Christos Stavrakakis
            self.stdout.write("-" * 80 + '\n')
65 7f9f3018 Christos Stavrakakis
            keys = ["id", "size", "base", "offset", "available", "reserved"]
66 7f9f3018 Christos Stavrakakis
            for key in keys:
67 7f9f3018 Christos Stavrakakis
                self.stdout.write(("%s" % key).rjust(12))
68 7f9f3018 Christos Stavrakakis
            self.stdout.write("\n")
69 0eed6cf8 Christos Stavrakakis
            for pool_table_row in pool_table.objects.all():
70 0eed6cf8 Christos Stavrakakis
                pool = pool_table_row.pool
71 0eed6cf8 Christos Stavrakakis
72 0eed6cf8 Christos Stavrakakis
                kv = {
73 0eed6cf8 Christos Stavrakakis
                    'id': pool_table_row.id,
74 0eed6cf8 Christos Stavrakakis
                    'offset': pool_table_row.offset,
75 0eed6cf8 Christos Stavrakakis
                    'base': pool_table_row.base,
76 0eed6cf8 Christos Stavrakakis
                    'size': pool_table_row.size,
77 0eed6cf8 Christos Stavrakakis
                    'available': pool.count_available(),
78 0eed6cf8 Christos Stavrakakis
                    'reserved': pool.count_reserved(),
79 0eed6cf8 Christos Stavrakakis
                }
80 0eed6cf8 Christos Stavrakakis
81 0eed6cf8 Christos Stavrakakis
                for key in keys:
82 0eed6cf8 Christos Stavrakakis
                    self.stdout.write(("%s" % kv[key]).rjust(12))
83 0eed6cf8 Christos Stavrakakis
                self.stdout.write("\n")