root / snf-cyclades-app / synnefo / logic / management / commands / network-inspect.py @ a859190b
History | View | Annotate | Download (5.6 kB)
1 |
# Copyright 2012-2013 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 |
from optparse import make_option |
35 |
|
36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
from synnefo.management.common import get_network |
38 |
|
39 |
from synnefo.db.models import (Backend, pooled_rapi_client) |
40 |
from synnefo.logic.rapi import GanetiApiError |
41 |
from snf_django.lib.astakos import UserCache |
42 |
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN, |
43 |
ASTAKOS_BASE_URL) |
44 |
from util import pool_map_chunks |
45 |
from snf_django.management.utils import pprint_table |
46 |
from synnefo.lib.ordereddict import OrderedDict |
47 |
|
48 |
|
49 |
class Command(BaseCommand): |
50 |
help = "Inspect a network on DB and Ganeti."
|
51 |
|
52 |
option_list = BaseCommand.option_list + ( |
53 |
make_option( |
54 |
'--displayname',
|
55 |
action='store_true',
|
56 |
dest='displayname',
|
57 |
default=False,
|
58 |
help="Display both uuid and display name"),
|
59 |
) |
60 |
|
61 |
def handle(self, *args, **options): |
62 |
write = self.stdout.write
|
63 |
if len(args) != 1: |
64 |
raise CommandError("Please provide a network ID.") |
65 |
|
66 |
network = get_network(args[0])
|
67 |
|
68 |
ucache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN) |
69 |
|
70 |
displayname = options['displayname']
|
71 |
|
72 |
userid = network.userid |
73 |
db_network = OrderedDict([ |
74 |
("name", network.name),
|
75 |
("backend-name", network.backend_id),
|
76 |
("state", network.state),
|
77 |
("userid", userid),
|
78 |
("username", ucache.get_name(userid) if displayname else ""), |
79 |
("public", network.public),
|
80 |
("floating_ip_pool", network.floating_ip_pool),
|
81 |
("external_router", network.external_router),
|
82 |
("drained", network.drained),
|
83 |
("MAC prefix", network.mac_prefix),
|
84 |
("flavor", network.flavor),
|
85 |
("link", network.link),
|
86 |
("mode", network.mode),
|
87 |
("deleted", network.deleted),
|
88 |
("tags", "), ".join(network.backend_tag)), |
89 |
("action", network.action)])
|
90 |
|
91 |
pprint_table(self.stdout, db_network.items(), None, separator=" | ", |
92 |
title="State of Network in DB")
|
93 |
|
94 |
subnets = list(network.subnets.values_list("id", "name", "ipversion", |
95 |
"cidr", "gateway", "dhcp", |
96 |
"deleted"))
|
97 |
headers = ["ID", "Name", "Version", "CIDR", "Gateway", "DHCP", |
98 |
"Deleted"]
|
99 |
write("\n\n")
|
100 |
pprint_table(self.stdout, subnets, headers, separator=" | ", |
101 |
title="Subnets")
|
102 |
|
103 |
bnets = list(network.backend_networks.values_list(
|
104 |
"backend__clustername",
|
105 |
"operstate", "deleted", "backendjobid", |
106 |
"backendopcode", "backendjobstatus")) |
107 |
headers = ["Backend", "State", "Deleted", "JobID", "Opcode", |
108 |
"JobStatus"]
|
109 |
write("\n\n")
|
110 |
pprint_table(self.stdout, bnets, headers, separator=" | ", |
111 |
title="Backend Networks")
|
112 |
|
113 |
write("\n\n")
|
114 |
|
115 |
for backend in Backend.objects.exclude(offline=True): |
116 |
with pooled_rapi_client(backend) as client: |
117 |
try:
|
118 |
g_net = client.GetNetwork(network.backend_id) |
119 |
ip_map = g_net.pop("map")
|
120 |
pprint_table(self.stdout, g_net.items(), None, |
121 |
title="State of network in backend: %s" %
|
122 |
backend.clustername) |
123 |
write(splitPoolMap(ip_map, 80) + "\n\n") |
124 |
except GanetiApiError as e: |
125 |
if e.code == 404: |
126 |
write('Network does not exist in backend %s\n' %
|
127 |
backend.clustername) |
128 |
else:
|
129 |
raise e
|
130 |
|
131 |
|
132 |
def splitPoolMap(s, count): |
133 |
chunks = pool_map_chunks(s, count) |
134 |
acc = [] |
135 |
count = 0
|
136 |
for chunk in chunks: |
137 |
chunk_len = len(chunk)
|
138 |
acc.append(str(count).rjust(3) + ' ' + chunk + ' ' + |
139 |
str(count + chunk_len - 1).ljust(4)) |
140 |
count += chunk_len |
141 |
return '\n' + '\n'.join(acc) |