Revision b0e7f310 snf-cyclades-app/synnefo/logic/management/commands/backend-list.py

b/snf-cyclades-app/synnefo/logic/management/commands/backend-list.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from optparse import make_option
35
from django.core.management.base import BaseCommand, CommandError
36
from synnefo.management.common import pprint_table
34
from synnefo.db.models import Backend, IPPoolTable
35
from synnefo.webproject.management.commands import ListCommand
37 36

  
38
from synnefo.db.models import Backend
39 37

  
38
class Command(ListCommand):
39
    help = "List Ganeti backends"
40
    object_class = Backend
40 41

  
41
class Command(BaseCommand):
42
    help = "List backends"
42
    def get_vms(backend):
43
        return backend.virtual_machines.filter(deleted=False).count()
43 44

  
44
    option_list = BaseCommand.option_list + (
45
        make_option('-c',
46
                    action='store_true',
47
                    dest='csv',
48
                    default=False,
49
                    help="Use pipes to separate values"),
50
    )
45
    def get_mem(backend):
46
        return "%s/%s" % (backend.mfree, backend.mtotal)
51 47

  
52
    def handle(self, *args, **options):
53
        if args:
54
            raise CommandError("Command doesn't accept any arguments")
48
    def get_disk(backend):
49
        return "%s/%s" % (backend.dfree, backend.dtotal)
55 50

  
56
        backends = Backend.objects.order_by('id')
51
    def get_ips(backend):
52
        free_ips = 0
53
        total_ips = 0
54
        for bnet in backend.networks.filter(deleted=False,
55
                                            network__public=True,
56
                                            network__deleted=False):
57
            network = bnet.network
58
            try:
59
                pool = IPPoolTable.objects.get(id=network.pool_id).pool
60
                free_ips += pool.count_available()
61
                total_ips += pool.pool_size
62
            except IPPoolTable.DoesNotExist:
63
                pass
64
        return "%s/%s" % (free_ips, total_ips)
57 65

  
58
        headers = ('id', 'clustername', 'port', 'username', "VMs", 'drained',
59
                   'offline')
60
        table = []
61
        for backend in backends:
62
            id = str(backend.id)
63
            vms = str(backend.virtual_machines.filter(deleted=False).count())
64
            fields = (id, backend.clustername, str(backend.port),
65
                      backend.username, vms, str(backend.drained),
66
                      str(backend.offline))
67
            table.append(fields)
66
    FIELDS = {
67
        "id": ("id", "Backend's unique ID"),
68
        "clustername": ("clustername", "The name of the Ganeti cluster"),
69
        "port": ("port", ""),
70
        "username": ("username", "The RAPI user"),
71
        "drained": ("drained", "Whether backend is marked as drained"),
72
        "offline": ("offline", "Whether backend if marked as offline"),
73
        "vms": (get_vms, "Number of VMs that this backend hosts"),
74
        "ips": (get_ips, "free/total number of public IPs"),
75
        "mem": (get_mem, "free/total memory (MB)"),
76
        "disk": (get_mem, "free/total disk (GB)"),
77
    }
68 78

  
69
        separator = " | " if options['csv'] else None
70
        pprint_table(self.stdout, table, headers, separator)
79
    fields = ["id", "clustername", "port", "username", "drained", "offline",
80
              "vms", "ips"]

Also available in: Unified diff