Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / management / pprint.py @ 1040b85b

History | View | Annotate | Download (16 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
                                          disks_from_instance)
46
from synnefo.management.common import get_image
47

    
48

    
49
def pprint_network(network, display_mails=False, stdout=None, title=None):
50
    if stdout is None:
51
        stdout = sys.stdout
52
    if title is None:
53
        title = "State of Network %s in DB" % network.id
54

    
55
    ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN)
56
    userid = network.userid
57

    
58
    db_network = OrderedDict([
59
        ("name", network.name),
60
        ("backend-name", network.backend_id),
61
        ("state", network.state),
62
        ("userid", userid),
63
        ("username", ucache.get_name(userid) if display_mails else None),
64
        ("public", network.public),
65
        ("floating_ip_pool", network.floating_ip_pool),
66
        ("external_router", network.external_router),
67
        ("drained", network.drained),
68
        ("MAC prefix", network.mac_prefix),
69
        ("flavor", network.flavor),
70
        ("link", network.link),
71
        ("mode", network.mode),
72
        ("deleted", network.deleted),
73
        ("tags", "), ".join(network.backend_tag)),
74
        ("action", network.action)])
75

    
76
    pprint_table(stdout, db_network.items(), None, separator=" | ",
77
                 title=title)
78

    
79

    
80
def pprint_network_subnets(network, stdout=None, title=None):
81
    if stdout is None:
82
        stdout = sys.stdout
83
    if title is None:
84
        title = "Subnets of network %s" % network.id
85

    
86
    subnets = list(network.subnets.values_list("id", "name", "ipversion",
87
                                               "cidr", "gateway", "dhcp",
88
                                               "deleted"))
89
    headers = ["ID", "Name", "Version", "CIDR", "Gateway", "DHCP",
90
               "Deleted"]
91
    pprint_table(stdout, subnets, headers, separator=" | ",
92
                 title=title)
93

    
94

    
95
def pprint_network_backends(network, stdout=None, title=None):
96
    if stdout is None:
97
        stdout = sys.stdout
98
    if title is None:
99
        title = "State of Network %s in DB for each backend" % network.id
100
    bnets = list(network.backend_networks.values_list(
101
        "backend__clustername",
102
        "operstate", "deleted", "backendjobid",
103
        "backendopcode", "backendjobstatus"))
104
    headers = ["Backend", "State", "Deleted", "JobID", "Opcode",
105
               "JobStatus"]
106
    pprint_table(stdout, bnets, headers, separator=" | ",
107
                 title=title)
108

    
109

    
110
def pprint_network_in_ganeti(network, stdout=None):
111
    if stdout is None:
112
        stdout = sys.stdout
113
    for backend in Backend.objects.exclude(offline=True):
114
        with pooled_rapi_client(backend) as client:
115
            try:
116
                g_net = client.GetNetwork(network.backend_id)
117
                ip_map = g_net.pop("map")
118
                pprint_table(stdout, g_net.items(), None,
119
                             title="State of network in backend: %s" %
120
                                   backend.clustername)
121
                if network.subnet4 is not None:
122
                    pprint_pool(None, ip_map, 80, stdout)
123
            except GanetiApiError as e:
124
                if e.code == 404:
125
                    stdout.write('Network does not exist in backend %s\n' %
126
                                 backend.clustername)
127
                else:
128
                    raise e
129

    
130

    
131
def pprint_subnet_in_db(subnet, stdout=None, title=None):
132
    if stdout is None:
133
        stdout = sys.stdout
134
    if title is None:
135
        title = "State of Subnet %s in DB" % subnet.id
136
    info = OrderedDict([("ID", subnet.id),
137
                        ("Network_ID", subnet.network.id),
138
                        # If a user names his subnet "-", what happens then?
139
                        ("User_ID", subnet.userid),
140
                        ("Name", "-" if subnet.name == "" else subnet.name),
141
                        ("IP_Version", subnet.ipversion),
142
                        ("CIDR", subnet.cidr),
143
                        ("Gateway", subnet.gateway),
144
                        ("Public", subnet.public),
145
                        ("DHCP/SLAAC", subnet.dhcp),
146
                        ("Host_Routes", subnet.host_routes),
147
                        ("DNS_Nameservers", subnet.dns_nameservers)])
148
    pprint_table(stdout, info.items(), None, separator=" | ", title=title)
149

    
150

    
151
def pprint_ippool(subnet, stdout=None, title=None):
152
    """Pretty print IP Pools of a subnet. Only IPv4 subnets have IP Pools"""
153

    
154
    if int(subnet.ipversion) != 4:
155
        return 0
156

    
157
    if stdout is None:
158
        stdout = sys.stdout
159

    
160
    stdout.write("IP Pools of subnet %s:\n\n" % subnet.id)
161

    
162
    for pool in subnet.get_ip_pools():
163
        size = pool.pool_size
164
        available = pool.available.count()
165
        info = OrderedDict([("First_IP", pool.return_start()),
166
                            ("Last_IP", pool.return_end()),
167
                            ("Size", size),
168
                            ("Available", available)])
169
        pprint_table(stdout, info.items(), None, separator=" | ", title=None)
170

    
171
        reserved = [pool.index_to_value(index)
172
                    for index, ip in enumerate(pool.reserved[:size])
173
                    if ip is False]
174

    
175
        if reserved != []:
176
            stdout.write("\nExternally Reserved IPs:\n\n")
177
            stdout.write(", ".join(reserved) + "\n")
178

    
179
        ip_sum = pool.available[:size] & pool.reserved[:size]
180
        pprint_pool(None, bitarray_to_map(ip_sum), 80, stdout)
181
        stdout.write("\n\n")
182

    
183

    
184
def pool_map_chunks(smap, step=64):
185
    for i in xrange(0, len(smap), step):
186
        yield smap[i:i + step]
187

    
188

    
189
def splitPoolMap(s, count):
190
    chunks = pool_map_chunks(s, count)
191
    acc = []
192
    count = 0
193
    for chunk in chunks:
194
        chunk_len = len(chunk)
195
        acc.append(str(count).rjust(3) + ' ' + chunk + ' ' +
196
                   str(count + chunk_len - 1).ljust(4))
197
        count += chunk_len
198
    return '\n' + '\n'.join(acc)
199

    
200

    
201
def pprint_pool(name, pool_map, step=80, stdout=None):
202
    if stdout is None:
203
        stdout = sys.stdout
204
    if name is not None:
205
        stdout.write("Pool: %s\n" % name)
206
    stdout.write(splitPoolMap(pool_map, count=step))
207
    stdout.write("\n")
208

    
209

    
210
def pprint_port(port, stdout=None, title=None):
211
    if stdout is None:
212
        stdout = sys.stdout
213
    if title is None:
214
        title = "State of Port %s in DB" % port.id
215
    port = OrderedDict([
216
        ("id", port.id),
217
        ("name", port.name),
218
        ("userid", port.userid),
219
        ("server", port.machine_id),
220
        ("network", port.network_id),
221
        ("device_owner", port.device_owner),
222
        ("mac", port.mac),
223
        ("state", port.state)])
224

    
225
    pprint_table(stdout, port.items(), None, separator=" | ",
226
                 title=title)
227

    
228

    
229
def pprint_port_ips(port, stdout=None, title=None):
230
    if stdout is None:
231
        stdout = sys.stdout
232
    if title is None:
233
        title = "IP Addresses of Port %s" % port.id
234
    ips = list(port.ips.values_list("address", "network_id", "subnet_id",
235
                                    "subnet__cidr", "floating_ip"))
236
    headers = ["Address", "Network", "Subnet", "CIDR", "is_floating"]
237
    pprint_table(stdout, ips, headers, separator=" | ",
238
                 title=title)
239

    
240

    
241
def pprint_port_in_ganeti(port, stdout=None, title=None):
242
    if stdout is None:
243
        stdout = sys.stdout
244
    if title is None:
245
        title = "State of Port %s in Ganeti" % port.id
246

    
247
    vm = port.machine
248
    if vm is None:
249
        stdout.write("Port is not attached to any instance.\n")
250
        return
251

    
252
    client = vm.get_client()
253
    try:
254
        vm_info = client.GetInstance(vm.backend_vm_id)
255
    except GanetiApiError as e:
256
        if e.code == 404:
257
            stdout.write("Port seems attached to server %s, but"
258
                         " server does not exist in backend.\n"
259
                         % vm)
260
            return
261
        raise e
262

    
263
    disks = disks_from_instance(vm_info)
264
    try:
265
        gnt_disk = filter(lambda disk: disk.get("name") == port.backend_uuid,
266
                          disks)[0]
267
        gnt_disk["instance"] = vm_info["name"]
268
    except IndexError:
269
        stdout.write("Port %s is not attached to instance %s\n" %
270
                     (port.id, vm.id))
271
        return
272
    pprint_table(stdout, gnt_disk.items(), None, separator=" | ",
273
                 title=title)
274

    
275
    vm.put_client(client)
276

    
277

    
278
def pprint_server(server, display_mails=False, stdout=None, title=None):
279
    if stdout is None:
280
        stdout = sys.stdout
281
    if title is None:
282
        title = "State of Server %s in DB" % server.id
283

    
284
    ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN)
285
    userid = server.userid
286

    
287
    try:
288
        image = get_image(server.imageid, server.userid)['name']
289
    except:
290
        image = server.imageid
291

    
292
    server_dict = OrderedDict([
293
        ("id", server.id),
294
        ("name", server.name),
295
        ("userid", server.userid),
296
        ("username", ucache.get_name(userid) if display_mails else None),
297
        ("flavor_id", server.flavor_id),
298
        ("flavor_name", server.flavor.name),
299
        ("imageid", server.imageid),
300
        ("image_name", image),
301
        ("state", server.operstate),
302
        ("backend", server.backend),
303
        ("deleted", server.deleted),
304
        ("action", server.action),
305
        ("task", server.task),
306
        ("task_job_id", server.task_job_id),
307
        ("backendjobid", server.backendjobid),
308
        ("backendopcode", server.backendopcode),
309
        ("backendjobstatus", server.backendjobstatus),
310
        ("backend_time", server.backendtime),
311
        ])
312

    
313
    pprint_table(stdout, server_dict.items(), None, separator=" | ",
314
                 title=title)
315

    
316

    
317
def pprint_server_nics(server, stdout=None, title=None):
318
    if title is None:
319
        title = "Ports of Server %s" % server.id
320
    if stdout is None:
321
        stdout = sys.stdout
322

    
323
    nics = []
324
    for nic in server.nics.all():
325
        nics.append((nic.id, nic.name, nic.index, nic.mac, nic.ipv4_address,
326
                     nic.ipv6_address, nic.network, nic.firewall_profile,
327
                     nic.state))
328

    
329
    headers = ["ID", "Name", "Index", "MAC", "IPv4 Address", "IPv6 Address",
330
               "Network", "Firewall", "State"]
331
    pprint_table(stdout, nics, headers, separator=" | ",
332
                 title=title)
333

    
334

    
335
def pprint_server_in_ganeti(server, print_jobs=False, stdout=None, title=None):
336
    if stdout is None:
337
        stdout = sys.stdout
338
    if title is None:
339
        title = "State of Server %s in Ganeti" % server.id
340

    
341
    client = server.get_client()
342
    try:
343
        server_info = client.GetInstance(server.backend_vm_id)
344
    except GanetiApiError as e:
345
        if e.code == 404:
346
            stdout.write("Server '%s' does not exist in backend '%s'\n"
347
                         % (server.id, server.backend.clustername))
348
            return
349
        raise e
350
    server.put_client(client)
351

    
352
    GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status',
353
                              'pnode', 'snode', 'network_port',
354
                              'disk_template', 'disk_usage',
355
                              'oper_ram', 'oper_vcpus', 'mtime')
356
    server_dict = OrderedDict([(k, server_info.get(k))
357
                              for k in GANETI_INSTANCE_FIELDS])
358

    
359
    pprint_table(stdout, server_dict.items(), None, separator=" | ",
360
                 title=title)
361
    stdout.write("\n")
362

    
363
    nics = nics_from_instance(server_info)
364
    nics_keys = ["ip", "mac", "name", "network"]
365
    nics_values = [[nic[key] for key in nics_keys] for nic in nics]
366
    pprint_table(stdout, nics_values, nics_keys, separator=" | ",
367
                 title="NICs of Server %s in Ganeti" % server.id)
368

    
369
    if not print_jobs:
370
        return
371

    
372
    client = server.get_client()
373
    jobs = client.GetJobs(bulk=True)
374
    server_jobs = filter(
375
        lambda x: server.backend_vm_id in (" ".join(x.get("summary"))), jobs)
376

    
377
    GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus',
378
                         'oplog', 'start_ts', 'end_ts')
379
    for server_job in server_jobs:
380
        stdout.write("\n")
381
        values = [server_job.get(k) for k in GANETI_JOB_FIELDS]
382
        pprint_table(stdout, zip(GANETI_JOB_FIELDS, values), None,
383
                     separator=" | ",
384
                     title="Ganeti Job %s" % server_job["id"])
385
    server.put_client(client)
386

    
387

    
388
def pprint_volume(volume, display_mails=False, stdout=None, title=None):
389
    if stdout is None:
390
        stdout = sys.stdout
391
    if title is None:
392
        title = "State of volume %s in DB" % volume.id
393

    
394
    ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN)
395
    userid = volume.userid
396

    
397
    volume_dict = OrderedDict([
398
        ("id", volume.id),
399
        ("size", volume.size),
400
        ("disk_template", volume.disk_template),
401
        ("disk_provider", volume.disk_provider),
402
        ("server_id", volume.machine_id),
403
        ("userid", volume.userid),
404
        ("username", ucache.get_name(userid) if display_mails else None),
405
        ("index", volume.index),
406
        ("name", volume.name),
407
        ("state", volume.status),
408
        ("deleted", volume.deleted),
409
        ("backendjobid", volume.backendjobid),
410
        ])
411

    
412
    pprint_table(stdout, volume_dict.items(), None, separator=" | ",
413
                 title=title)
414

    
415

    
416
def pprint_volume_in_ganeti(volume, stdout=None, title=None):
417
    if stdout is None:
418
        stdout = sys.stdout
419
    if title is None:
420
        title = "State of volume %s in Ganeti" % volume.id
421

    
422
    vm = volume.machine
423
    if vm is None:
424
        stdout.write("volume is not attached to any instance.\n")
425
        return
426

    
427
    client = vm.get_client()
428
    try:
429
        vm_info = client.GetInstance(vm.backend_vm_id)
430
    except GanetiApiError as e:
431
        if e.code == 404:
432
            stdout.write("Volume seems attached to server %s, but"
433
                         " server does not exist in backend.\n"
434
                         % vm)
435
            return
436
        raise e
437

    
438
    disks = disks_from_instance(vm_info)
439
    try:
440
        gnt_disk = filter(lambda disk:
441
                          disk.get("name") == volume.backend_volume_uuid,
442
                          disks)[0]
443
        gnt_disk["instance"] = vm_info["name"]
444
    except IndexError:
445
        stdout.write("Volume %s is not attached to instance %s\n" % (volume.id,
446
                                                                     vm.id))
447
        return
448
    pprint_table(stdout, gnt_disk.items(), None, separator=" | ",
449
                 title=title)
450

    
451
    vm.put_client(client)