root / snf-cyclades-app / synnefo / management / pprint.py @ 6a439d95
History | View | Annotate | Download (13.8 kB)
1 |
#Copyright (C) 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 |
import sys |
35 |
from snf_django.management.utils import pprint_table |
36 |
from synnefo.lib.ordereddict import OrderedDict |
37 |
from snf_django.lib.astakos import UserCache |
38 |
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN, |
39 |
ASTAKOS_AUTH_URL) |
40 |
from synnefo.db.models import Backend, pooled_rapi_client |
41 |
from synnefo.db.pools import bitarray_to_map |
42 |
|
43 |
from synnefo.logic.rapi import GanetiApiError |
44 |
from synnefo.logic.reconciliation import nics_from_instance |
45 |
from synnefo.management.common import get_image |
46 |
|
47 |
|
48 |
def pprint_network(network, display_mails=False, stdout=None, title=None): |
49 |
if stdout is None: |
50 |
stdout = sys.stdout |
51 |
if title is None: |
52 |
title = "State of Network %s in DB" % network.id
|
53 |
|
54 |
ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) |
55 |
userid = network.userid |
56 |
|
57 |
db_network = OrderedDict([ |
58 |
("name", network.name),
|
59 |
("backend-name", network.backend_id),
|
60 |
("state", network.state),
|
61 |
("userid", userid),
|
62 |
("username", ucache.get_name(userid) if display_mails else None), |
63 |
("public", network.public),
|
64 |
("floating_ip_pool", network.floating_ip_pool),
|
65 |
("external_router", network.external_router),
|
66 |
("drained", network.drained),
|
67 |
("MAC prefix", network.mac_prefix),
|
68 |
("flavor", network.flavor),
|
69 |
("link", network.link),
|
70 |
("mode", network.mode),
|
71 |
("deleted", network.deleted),
|
72 |
("tags", "), ".join(network.backend_tag)), |
73 |
("action", network.action)])
|
74 |
|
75 |
pprint_table(stdout, db_network.items(), None, separator=" | ", |
76 |
title=title) |
77 |
|
78 |
|
79 |
def pprint_network_subnets(network, stdout=None, title=None): |
80 |
if stdout is None: |
81 |
stdout = sys.stdout |
82 |
if title is None: |
83 |
title = "Subnets of network %s" % network.id
|
84 |
|
85 |
subnets = list(network.subnets.values_list("id", "name", "ipversion", |
86 |
"cidr", "gateway", "dhcp", |
87 |
"deleted"))
|
88 |
headers = ["ID", "Name", "Version", "CIDR", "Gateway", "DHCP", |
89 |
"Deleted"]
|
90 |
pprint_table(stdout, subnets, headers, separator=" | ",
|
91 |
title=title) |
92 |
|
93 |
|
94 |
def pprint_network_backends(network, stdout=None, title=None): |
95 |
if stdout is None: |
96 |
stdout = sys.stdout |
97 |
if title is None: |
98 |
title = "State of Network %s in DB for each backend" % network.id
|
99 |
bnets = list(network.backend_networks.values_list(
|
100 |
"backend__clustername",
|
101 |
"operstate", "deleted", "backendjobid", |
102 |
"backendopcode", "backendjobstatus")) |
103 |
headers = ["Backend", "State", "Deleted", "JobID", "Opcode", |
104 |
"JobStatus"]
|
105 |
pprint_table(stdout, bnets, headers, separator=" | ",
|
106 |
title=title) |
107 |
|
108 |
|
109 |
def pprint_network_in_ganeti(network, stdout=None): |
110 |
if stdout is None: |
111 |
stdout = sys.stdout |
112 |
for backend in Backend.objects.exclude(offline=True): |
113 |
with pooled_rapi_client(backend) as client: |
114 |
try:
|
115 |
g_net = client.GetNetwork(network.backend_id) |
116 |
ip_map = g_net.pop("map")
|
117 |
pprint_table(stdout, g_net.items(), None,
|
118 |
title="State of network in backend: %s" %
|
119 |
backend.clustername) |
120 |
if network.subnet4 is not None: |
121 |
pprint_pool(None, ip_map, 80, stdout) |
122 |
except GanetiApiError as e: |
123 |
if e.code == 404: |
124 |
stdout.write('Network does not exist in backend %s\n' %
|
125 |
backend.clustername) |
126 |
else:
|
127 |
raise e
|
128 |
|
129 |
|
130 |
def pprint_subnet_in_db(subnet, stdout=None, title=None): |
131 |
if stdout is None: |
132 |
stdout = sys.stdout |
133 |
if title is None: |
134 |
title = "State of Subnet %s in DB" % subnet.id
|
135 |
info = OrderedDict([("ID", subnet.id),
|
136 |
("Network_ID", subnet.network.id),
|
137 |
# If a user names his subnet "-", what happens then?
|
138 |
("User_ID", subnet.userid),
|
139 |
("Name", "-" if subnet.name == "" else subnet.name), |
140 |
("IP_Version", subnet.ipversion),
|
141 |
("CIDR", subnet.cidr),
|
142 |
("Gateway", subnet.gateway),
|
143 |
("Public", subnet.public),
|
144 |
("DHCP/SLAAC", subnet.dhcp),
|
145 |
("Host_Routes", subnet.host_routes),
|
146 |
("DNS_Nameservers", subnet.dns_nameservers)])
|
147 |
pprint_table(stdout, info.items(), None, separator=" | ", title=title) |
148 |
|
149 |
|
150 |
def pprint_ippool(subnet, stdout=None, title=None): |
151 |
"""Pretty print IP Pools of a subnet. Only IPv4 subnets have IP Pools"""
|
152 |
|
153 |
if int(subnet.ipversion) != 4: |
154 |
return 0 |
155 |
|
156 |
if stdout is None: |
157 |
stdout = sys.stdout |
158 |
|
159 |
stdout.write("IP Pools of subnet %s:\n\n" % subnet.id)
|
160 |
|
161 |
for pool in subnet.get_ip_pools(): |
162 |
size = pool.pool_size |
163 |
available = pool.available.count() |
164 |
info = OrderedDict([("First_IP", pool.return_start()),
|
165 |
("Last_IP", pool.return_end()),
|
166 |
("Size", size),
|
167 |
("Available", available)])
|
168 |
pprint_table(stdout, info.items(), None, separator=" | ", title=None) |
169 |
|
170 |
reserved = [pool.index_to_value(index) |
171 |
for index, ip in enumerate(pool.reserved[:size]) |
172 |
if ip is False] |
173 |
|
174 |
if reserved != []:
|
175 |
stdout.write("\nExternally Reserved IPs:\n\n")
|
176 |
stdout.write(", ".join(reserved) + "\n") |
177 |
|
178 |
ip_sum = pool.available[:size] & pool.reserved[:size] |
179 |
pprint_pool(None, bitarray_to_map(ip_sum), 80, stdout) |
180 |
stdout.write("\n\n")
|
181 |
|
182 |
|
183 |
def pool_map_chunks(smap, step=64): |
184 |
for i in xrange(0, len(smap), step): |
185 |
yield smap[i:i + step]
|
186 |
|
187 |
|
188 |
def splitPoolMap(s, count): |
189 |
chunks = pool_map_chunks(s, count) |
190 |
acc = [] |
191 |
count = 0
|
192 |
for chunk in chunks: |
193 |
chunk_len = len(chunk)
|
194 |
acc.append(str(count).rjust(3) + ' ' + chunk + ' ' + |
195 |
str(count + chunk_len - 1).ljust(4)) |
196 |
count += chunk_len |
197 |
return '\n' + '\n'.join(acc) |
198 |
|
199 |
|
200 |
def pprint_pool(name, pool_map, step=80, stdout=None): |
201 |
if stdout is None: |
202 |
stdout = sys.stdout |
203 |
if name is not None: |
204 |
stdout.write("Pool: %s\n" % name)
|
205 |
stdout.write(splitPoolMap(pool_map, count=step)) |
206 |
stdout.write("\n")
|
207 |
|
208 |
|
209 |
def pprint_port(port, stdout=None, title=None): |
210 |
if stdout is None: |
211 |
stdout = sys.stdout |
212 |
if title is None: |
213 |
title = "State of Port %s in DB" % port.id
|
214 |
port = OrderedDict([ |
215 |
("id", port.id),
|
216 |
("name", port.name),
|
217 |
("userid", port.userid),
|
218 |
("server", port.machine_id),
|
219 |
("network", port.network_id),
|
220 |
("device_owner", port.device_owner),
|
221 |
("mac", port.mac),
|
222 |
("state", port.state)])
|
223 |
|
224 |
pprint_table(stdout, port.items(), None, separator=" | ", |
225 |
title=title) |
226 |
|
227 |
|
228 |
def pprint_port_ips(port, stdout=None, title=None): |
229 |
if stdout is None: |
230 |
stdout = sys.stdout |
231 |
if title is None: |
232 |
title = "IP Addresses of Port %s" % port.id
|
233 |
ips = list(port.ips.values_list("address", "network_id", "subnet_id", |
234 |
"subnet__cidr", "floating_ip")) |
235 |
headers = ["Address", "Network", "Subnet", "CIDR", "is_floating"] |
236 |
pprint_table(stdout, ips, headers, separator=" | ",
|
237 |
title=title) |
238 |
|
239 |
|
240 |
def pprint_port_in_ganeti(port, stdout=None, title=None): |
241 |
if stdout is None: |
242 |
stdout = sys.stdout |
243 |
if title is None: |
244 |
title = "State of Port %s in Ganeti" % port.id
|
245 |
|
246 |
vm = port.machine |
247 |
if vm is None: |
248 |
stdout.write("Port is not attached to any instance.\n")
|
249 |
return
|
250 |
|
251 |
client = vm.get_client() |
252 |
try:
|
253 |
vm_info = client.GetInstance(vm.backend_vm_id) |
254 |
except GanetiApiError as e: |
255 |
if e.code == 404: |
256 |
stdout.write("Port seems attached to server %s, but"
|
257 |
" server does not exist in backend.\n"
|
258 |
% vm) |
259 |
return
|
260 |
raise e
|
261 |
|
262 |
nics = nics_from_instance(vm_info) |
263 |
try:
|
264 |
gnt_nic = filter(lambda nic: nic.get("name") == port.backend_uuid, |
265 |
nics)[0]
|
266 |
gnt_nic["instance"] = vm_info["name"] |
267 |
except IndexError: |
268 |
stdout.write("Port %s is not attached to instance %s\n" %
|
269 |
(port.id, vm.id)) |
270 |
return
|
271 |
pprint_table(stdout, gnt_nic.items(), None, separator=" | ", |
272 |
title=title) |
273 |
|
274 |
vm.put_client(client) |
275 |
|
276 |
|
277 |
def pprint_server(server, display_mails=False, stdout=None, title=None): |
278 |
if stdout is None: |
279 |
stdout = sys.stdout |
280 |
if title is None: |
281 |
title = "State of Server %s in DB" % server.id
|
282 |
|
283 |
ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) |
284 |
userid = server.userid |
285 |
|
286 |
try:
|
287 |
image = get_image(server.imageid, server.userid)['name']
|
288 |
except:
|
289 |
image = server.imageid |
290 |
|
291 |
server_dict = OrderedDict([ |
292 |
("id", server.id),
|
293 |
("name", server.name),
|
294 |
("userid", server.userid),
|
295 |
("username", ucache.get_name(userid) if display_mails else None), |
296 |
("flavor_id", server.flavor_id),
|
297 |
("flavor_name", server.flavor.name),
|
298 |
("imageid", server.imageid),
|
299 |
("image_name", image),
|
300 |
("state", server.operstate),
|
301 |
("backend", server.backend),
|
302 |
("deleted", server.deleted),
|
303 |
("action", server.action),
|
304 |
("task", server.task),
|
305 |
("task_job_id", server.task_job_id),
|
306 |
("backendjobid", server.backendjobid),
|
307 |
("backendopcode", server.backendopcode),
|
308 |
("backendjobstatus", server.backendjobstatus),
|
309 |
("backend_time", server.backendtime),
|
310 |
]) |
311 |
|
312 |
pprint_table(stdout, server_dict.items(), None, separator=" | ", |
313 |
title=title) |
314 |
|
315 |
|
316 |
def pprint_server_nics(server, stdout=None, title=None): |
317 |
if title is None: |
318 |
title = "Ports of Server %s" % server.id
|
319 |
if stdout is None: |
320 |
stdout = sys.stdout |
321 |
|
322 |
nics = [] |
323 |
for nic in server.nics.all(): |
324 |
nics.append((nic.id, nic.name, nic.index, nic.mac, nic.ipv4_address, |
325 |
nic.ipv6_address, nic.network, nic.firewall_profile, |
326 |
nic.state)) |
327 |
|
328 |
headers = ["ID", "Name", "Index", "MAC", "IPv4 Address", "IPv6 Address", |
329 |
"Network", "Firewall", "State"] |
330 |
pprint_table(stdout, nics, headers, separator=" | ",
|
331 |
title=title) |
332 |
|
333 |
|
334 |
def pprint_server_in_ganeti(server, print_jobs=False, stdout=None, title=None): |
335 |
if stdout is None: |
336 |
stdout = sys.stdout |
337 |
if title is None: |
338 |
title = "State of Server %s in Ganeti" % server.id
|
339 |
|
340 |
client = server.get_client() |
341 |
try:
|
342 |
server_info = client.GetInstance(server.backend_vm_id) |
343 |
except GanetiApiError as e: |
344 |
if e.code == 404: |
345 |
stdout.write("Server '%s' does not exist in backend '%s'\n"
|
346 |
% (server.id, server.backend.clustername)) |
347 |
return
|
348 |
raise e
|
349 |
server.put_client(client) |
350 |
|
351 |
GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status', |
352 |
'pnode', 'snode', 'network_port', |
353 |
'disk_template', 'disk_usage', |
354 |
'oper_ram', 'oper_vcpus', 'mtime') |
355 |
server_dict = OrderedDict([(k, server_info.get(k)) |
356 |
for k in GANETI_INSTANCE_FIELDS]) |
357 |
|
358 |
pprint_table(stdout, server_dict.items(), None, separator=" | ", |
359 |
title=title) |
360 |
stdout.write("\n")
|
361 |
|
362 |
nics = nics_from_instance(server_info) |
363 |
nics_keys = ["ip", "mac", "name", "network"] |
364 |
nics_values = [[nic[key] for key in nics_keys] for nic in nics] |
365 |
pprint_table(stdout, nics_values, nics_keys, separator=" | ",
|
366 |
title="NICs of Server %s in Ganeti" % server.id)
|
367 |
|
368 |
if not print_jobs: |
369 |
return
|
370 |
|
371 |
client = server.get_client() |
372 |
jobs = client.GetJobs(bulk=True)
|
373 |
server_jobs = filter(
|
374 |
lambda x: server.backend_vm_id in (" ".join(x.get("summary"))), jobs) |
375 |
|
376 |
GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus', |
377 |
'oplog', 'start_ts', 'end_ts') |
378 |
for server_job in server_jobs: |
379 |
stdout.write("\n")
|
380 |
values = [server_job.get(k) for k in GANETI_JOB_FIELDS] |
381 |
pprint_table(stdout, zip(GANETI_JOB_FIELDS, values), None, |
382 |
separator=" | ",
|
383 |
title="Ganeti Job %s" % server_job["id"]) |
384 |
server.put_client(client) |