Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / network-inspect.py @ 8283d6c1

History | View | Annotate | Download (5.1 kB)

1 00051a6a Christos Stavrakakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 00051a6a Christos Stavrakakis
#
3 00051a6a Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
4 00051a6a Christos Stavrakakis
# without modification, are permitted provided that the following
5 00051a6a Christos Stavrakakis
# conditions are met:
6 00051a6a Christos Stavrakakis
#
7 00051a6a Christos Stavrakakis
#   1. Redistributions of source code must retain the above
8 00051a6a Christos Stavrakakis
#      copyright notice, this list of conditions and the following
9 00051a6a Christos Stavrakakis
#      disclaimer.
10 00051a6a Christos Stavrakakis
#
11 00051a6a Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
12 00051a6a Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 00051a6a Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
14 00051a6a Christos Stavrakakis
#      provided with the distribution.
15 00051a6a Christos Stavrakakis
#
16 00051a6a Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 00051a6a Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 00051a6a Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 00051a6a Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 00051a6a Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 00051a6a Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 00051a6a Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 00051a6a Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 00051a6a Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 00051a6a Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 00051a6a Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 00051a6a Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
28 00051a6a Christos Stavrakakis
#
29 00051a6a Christos Stavrakakis
# The views and conclusions contained in the software and
30 00051a6a Christos Stavrakakis
# documentation are those of the authors and should not be
31 00051a6a Christos Stavrakakis
# interpreted as representing official policies, either expressed
32 00051a6a Christos Stavrakakis
# or implied, of GRNET S.A.
33 00051a6a Christos Stavrakakis
34 00051a6a Christos Stavrakakis
import json
35 00051a6a Christos Stavrakakis
36 c51f3a08 Stratos Psomadakis
from optparse import make_option
37 c51f3a08 Stratos Psomadakis
38 00051a6a Christos Stavrakakis
from django.core.management.base import BaseCommand, CommandError
39 8283d6c1 Stratos Psomadakis
from synnefo.management.common import get_network, UserCache, Omit
40 00051a6a Christos Stavrakakis
41 b84ed662 Christos Stavrakakis
from synnefo.db.models import (Backend, BackendNetwork,
42 3524241a Christos Stavrakakis
                               pooled_rapi_client)
43 3524241a Christos Stavrakakis
from synnefo.logic.rapi import GanetiApiError
44 5e1a5f9b Christos Stavrakakis
from util import pool_map_chunks
45 00051a6a Christos Stavrakakis
46 3524241a Christos Stavrakakis
47 00051a6a Christos Stavrakakis
class Command(BaseCommand):
48 00051a6a Christos Stavrakakis
    help = "Inspect a network on DB and Ganeti."
49 00051a6a Christos Stavrakakis
50 c51f3a08 Stratos Psomadakis
    option_list = BaseCommand.option_list + (
51 4500650c Stratos Psomadakis
        make_option('--displayname',
52 c51f3a08 Stratos Psomadakis
            action='store_true',
53 4500650c Stratos Psomadakis
            dest='displayname',
54 c51f3a08 Stratos Psomadakis
            default=False,
55 4500650c Stratos Psomadakis
            help="Display both uuid and display name"),
56 4500650c Stratos Psomadakis
    )
57 c51f3a08 Stratos Psomadakis
58 00051a6a Christos Stavrakakis
    def handle(self, *args, **options):
59 cc92b70f Christos Stavrakakis
        write = self.stdout.write
60 00051a6a Christos Stavrakakis
        if len(args) != 1:
61 00051a6a Christos Stavrakakis
            raise CommandError("Please provide a network ID.")
62 00051a6a Christos Stavrakakis
63 b84ed662 Christos Stavrakakis
        net = get_network(args[0])
64 00051a6a Christos Stavrakakis
65 8283d6c1 Stratos Psomadakis
        ucache = UserCache()
66 4500650c Stratos Psomadakis
67 4500650c Stratos Psomadakis
        displayname = options['displayname']
68 4500650c Stratos Psomadakis
69 00051a6a Christos Stavrakakis
        sep = '-' * 80 + '\n'
70 4500650c Stratos Psomadakis
        labels = filter(lambda x: x is not Omit,
71 4500650c Stratos Psomadakis
                        ['name', 'backend-name', 'state', 'owner uuid',
72 4500650c Stratos Psomadakis
                         'owner_name' if displayname else Omit, 'subnet',
73 4500650c Stratos Psomadakis
                         'gateway', 'mac_prefix', 'link', 'public', 'dhcp',
74 4500650c Stratos Psomadakis
                         'flavor', 'deleted', 'action', 'pool'])
75 4500650c Stratos Psomadakis
76 4500650c Stratos Psomadakis
        uuid = net.userid
77 4500650c Stratos Psomadakis
        if displayname:
78 8283d6c1 Stratos Psomadakis
            dname = ucache.get_name(uuid)
79 4500650c Stratos Psomadakis
80 4500650c Stratos Psomadakis
        fields = filter(lambda x: x is not Omit,
81 4500650c Stratos Psomadakis
                        [net.name, net.backend_id, net.state, uuid or '-',
82 4500650c Stratos Psomadakis
                         dname or '-' if displayname else Omit,
83 4500650c Stratos Psomadakis
                         str(net.subnet), str(net.gateway), str(net.mac_prefix),
84 4500650c Stratos Psomadakis
                         str(net.link), str(net.public),  str(net.dhcp),
85 4500650c Stratos Psomadakis
                         str(net.flavor), str(net.deleted), str(net.action),
86 4500650c Stratos Psomadakis
                         str(splitPoolMap(net.get_pool().to_map(), 64))])
87 00051a6a Christos Stavrakakis
88 cc92b70f Christos Stavrakakis
        write(sep)
89 cc92b70f Christos Stavrakakis
        write('State of Network in DB\n')
90 cc92b70f Christos Stavrakakis
        write(sep)
91 00051a6a Christos Stavrakakis
        for l, f in zip(labels, fields):
92 cc92b70f Christos Stavrakakis
            write(l.ljust(20) + ': ' + f.ljust(20) + '\n')
93 00051a6a Christos Stavrakakis
94 00051a6a Christos Stavrakakis
        labels = ('Backend', 'State', 'Deleted', 'JobID', 'OpCode',
95 00051a6a Christos Stavrakakis
                  'JobStatus')
96 00051a6a Christos Stavrakakis
        for back_net in BackendNetwork.objects.filter(network=net):
97 cc92b70f Christos Stavrakakis
            write('\n')
98 00051a6a Christos Stavrakakis
            fields = (back_net.backend.clustername, back_net.operstate,
99 cc92b70f Christos Stavrakakis
                      str(back_net.deleted),  str(back_net.backendjobid),
100 cc92b70f Christos Stavrakakis
                      str(back_net.backendopcode),
101 cc92b70f Christos Stavrakakis
                      str(back_net.backendjobstatus))
102 00051a6a Christos Stavrakakis
            for l, f in zip(labels, fields):
103 cc92b70f Christos Stavrakakis
                write(l.ljust(20) + ': ' + f.ljust(20) + '\n')
104 cc92b70f Christos Stavrakakis
        write('\n')
105 00051a6a Christos Stavrakakis
106 cc92b70f Christos Stavrakakis
        write(sep)
107 cc92b70f Christos Stavrakakis
        write('State of Network in Ganeti\n')
108 cc92b70f Christos Stavrakakis
        write(sep)
109 00051a6a Christos Stavrakakis
110 00051a6a Christos Stavrakakis
        for backend in Backend.objects.exclude(offline=True):
111 3524241a Christos Stavrakakis
            with pooled_rapi_client(backend) as client:
112 3524241a Christos Stavrakakis
                try:
113 3524241a Christos Stavrakakis
                    g_net = client.GetNetwork(net.backend_id)
114 cc92b70f Christos Stavrakakis
                    write("Backend: %s\n" % backend.clustername)
115 3524241a Christos Stavrakakis
                    print json.dumps(g_net, indent=2)
116 cc92b70f Christos Stavrakakis
                    write(sep)
117 3524241a Christos Stavrakakis
                except GanetiApiError as e:
118 3524241a Christos Stavrakakis
                    if e.code == 404:
119 cc92b70f Christos Stavrakakis
                        write('Network does not exist in backend %s\n' %
120 cc92b70f Christos Stavrakakis
                              backend.clustername)
121 3524241a Christos Stavrakakis
                    else:
122 3524241a Christos Stavrakakis
                        raise e
123 439ff929 Christos Stavrakakis
124 439ff929 Christos Stavrakakis
125 439ff929 Christos Stavrakakis
def splitPoolMap(s, count):
126 5e1a5f9b Christos Stavrakakis
    chunks = pool_map_chunks(s, count)
127 439ff929 Christos Stavrakakis
    acc = []
128 5e1a5f9b Christos Stavrakakis
    count = 0
129 5e1a5f9b Christos Stavrakakis
    for chunk in chunks:
130 5e1a5f9b Christos Stavrakakis
        chunk_len = len(chunk)
131 5e1a5f9b Christos Stavrakakis
        acc.append(str(count).rjust(3) + ' ' + chunk + ' ' +
132 5e1a5f9b Christos Stavrakakis
                   str(count + chunk_len - 1).ljust(4))
133 5e1a5f9b Christos Stavrakakis
        count += chunk_len
134 439ff929 Christos Stavrakakis
    return '\n' + '\n'.join(acc)