Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.7 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import json
35

    
36
from django.core.management.base import BaseCommand, CommandError
37

    
38
from synnefo.db.models import (Backend, Network, BackendNetwork,
39
                               pooled_rapi_client)
40
from synnefo.logic.rapi import GanetiApiError
41
from util import pool_map_chunks
42

    
43

    
44
class Command(BaseCommand):
45
    help = "Inspect a network on DB and Ganeti."
46

    
47
    def handle(self, *args, **options):
48
        if len(args) != 1:
49
            raise CommandError("Please provide a network ID.")
50
        try:
51
            net_id = int(args[0])
52
        except ValueError:
53
            raise CommandError("Invalid network ID.")
54

    
55
        try:
56
            net = Network.objects.get(id=net_id)
57
        except Network.DoesNotExist:
58
            raise CommandError("Network not found in DB.")
59

    
60
        sep = '-' * 80 + '\n'
61
        labels = ('name', 'backend-name', 'state', 'owner', 'subnet', 'gateway',
62
                  'mac_prefix', 'link', 'public', 'dhcp', 'type', 'deleted',
63
                  'action', 'pool')
64
        fields = (net.name, net.backend_id, net.state, str(net.userid),
65
                  str(net.subnet), str(net.gateway), str(net.mac_prefix),
66
                  str(net.link), str(net.public),  str(net.dhcp),
67
                  str(net.type), str(net.deleted), str(net.action),
68
                  str(splitPoolMap(net.get_pool().to_map(), 64)))
69

    
70
        self.stdout.write(sep)
71
        self.stdout.write('State of Network in DB\n')
72
        self.stdout.write(sep)
73
        for l, f in zip(labels, fields):
74
            self.stdout.write(l.ljust(20) + ': ' + f.ljust(20) + '\n')
75

    
76
        labels = ('Backend', 'State', 'Deleted', 'JobID', 'OpCode',
77
                  'JobStatus')
78
        for back_net in BackendNetwork.objects.filter(network=net):
79
            self.stdout.write('\n')
80
            fields = (back_net.backend.clustername, back_net.operstate,
81
                     str(back_net.deleted),  str(back_net.backendjobid),
82
                     str(back_net.backendopcode),
83
                     str(back_net.backendjobstatus))
84
            for l, f in zip(labels, fields):
85
                self.stdout.write(l.ljust(20) + ': ' + f.ljust(20) + '\n')
86
        self.stdout.write('\n')
87

    
88
        self.stdout.write(sep)
89
        self.stdout.write('State of Network in Ganeti\n')
90
        self.stdout.write(sep)
91

    
92
        for backend in Backend.objects.exclude(offline=True):
93
            with pooled_rapi_client(backend) as client:
94
                try:
95
                    g_net = client.GetNetwork(net.backend_id)
96
                    self.stdout.write("Backend: %s\n" % backend.clustername)
97
                    print json.dumps(g_net, indent=2)
98
                    self.stdout.write(sep)
99
                except GanetiApiError as e:
100
                    if e.code == 404:
101
                        self.stdout.write('Network does not exist in backend %s\n' %
102
                                          backend.clustername)
103
                    else:
104
                        raise e
105

    
106

    
107
def splitPoolMap(s, count):
108
    chunks = pool_map_chunks(s, count)
109
    acc = []
110
    count = 0
111
    for chunk in chunks:
112
        chunk_len = len(chunk)
113
        acc.append(str(count).rjust(3) + ' ' + chunk + ' ' +
114
                   str(count + chunk_len - 1).ljust(4))
115
        count += chunk_len
116
    return '\n' + '\n'.join(acc)