Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / management / pprint.py @ 9835a70d

History | View | Annotate | Download (13.3 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
                        ("Name", "-" if subnet.name == "" else subnet.name),
139
                        ("IP_Version", subnet.ipversion),
140
                        ("CIDR", subnet.cidr),
141
                        ("Gateway", subnet.gateway),
142
                        ("DHCP/SLAAC", subnet.dhcp),
143
                        ("Host_Routes", subnet.host_routes),
144
                        ("DNS_Nameservers", subnet.dns_nameservers)])
145
    pprint_table(stdout, info.items(), None, separator=" | ", title=title)
146

    
147

    
148
def pprint_ippool(subnet, stdout=None, title=None):
149
    """Pretty print IP Pools of a subnet. Only IPv4 subnets have IP Pools"""
150

    
151
    if int(subnet.ipversion) != 4:
152
        return 0
153

    
154
    if stdout is None:
155
        stdout = sys.stdout
156

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

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

    
170

    
171
def pool_map_chunks(smap, step=64):
172
    for i in xrange(0, len(smap), step):
173
        yield smap[i:i + step]
174

    
175

    
176
def splitPoolMap(s, count):
177
    chunks = pool_map_chunks(s, count)
178
    acc = []
179
    count = 0
180
    for chunk in chunks:
181
        chunk_len = len(chunk)
182
        acc.append(str(count).rjust(3) + ' ' + chunk + ' ' +
183
                   str(count + chunk_len - 1).ljust(4))
184
        count += chunk_len
185
    return '\n' + '\n'.join(acc)
186

    
187

    
188
def pprint_pool(name, pool_map, step=80, stdout=None):
189
    if stdout is None:
190
        stdout = sys.stdout
191
    if name is not None:
192
        stdout.write("Pool: %s\n" % name)
193
    stdout.write(splitPoolMap(pool_map, count=step))
194
    stdout.write("\n")
195

    
196

    
197
def pprint_port(port, stdout=None, title=None):
198
    if stdout is None:
199
        stdout = sys.stdout
200
    if title is None:
201
        title = "State of Port %s in DB" % port.id
202
    port = OrderedDict([
203
        ("id", port.id),
204
        ("name", port.name),
205
        ("userid", port.userid),
206
        ("server", port.machine_id),
207
        ("network", port.network_id),
208
        ("device_owner", port.device_owner),
209
        ("mac", port.mac),
210
        ("state", port.state)])
211

    
212
    pprint_table(stdout, port.items(), None, separator=" | ",
213
                 title=title)
214

    
215

    
216
def pprint_port_ips(port, stdout=None, title=None):
217
    if stdout is None:
218
        stdout = sys.stdout
219
    if title is None:
220
        title = "IP Addresses of Port %s" % port.id
221
    ips = list(port.ips.values_list("address", "network_id", "subnet_id",
222
                                    "subnet__cidr", "floating_ip"))
223
    headers = ["Address", "Network", "Subnet", "CIDR", "is_floating"]
224
    pprint_table(stdout, ips, headers, separator=" | ",
225
                 title=title)
226

    
227

    
228
def pprint_port_in_ganeti(port, stdout=None, title=None):
229
    if stdout is None:
230
        stdout = sys.stdout
231
    if title is None:
232
        title = "State of Port %s in Ganeti" % port.id
233

    
234
    vm = port.machine
235
    if vm is None:
236
        stdout.write("Port is not attached to any instance.\n")
237
        return
238

    
239
    client = vm.get_client()
240
    try:
241
        vm_info = client.GetInstance(vm.backend_vm_id)
242
    except GanetiApiError as e:
243
        if e.code == 404:
244
            stdout.write("Port seems attached to server %s, but"
245
                         " server does not exist in backend.\n"
246
                         % vm)
247
            return
248
        raise e
249

    
250
    nics = nics_from_instance(vm_info)
251
    try:
252
        gnt_nic = filter(lambda nic: nic.get("name") == port.backend_uuid,
253
                         nics)[0]
254
        gnt_nic["instance"] = vm_info["name"]
255
    except IndexError:
256
        stdout.write("Port %s is not attached to instance %s" % (port, vm))
257
        return
258
    pprint_table(stdout, gnt_nic.items(), None, separator=" | ",
259
                 title=title)
260

    
261
    vm.put_client(client)
262

    
263

    
264
def pprint_server(server, display_mails=False, stdout=None, title=None):
265
    if stdout is None:
266
        stdout = sys.stdout
267
    if title is None:
268
        title = "State of Server %s in DB" % server.id
269

    
270
    ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN)
271
    userid = server.userid
272

    
273
    try:
274
        image = get_image(server.imageid, server.userid)['name']
275
    except:
276
        image = server.imageid
277

    
278
    server_dict = OrderedDict([
279
        ("id", server.id),
280
        ("name", server.name),
281
        ("userid", server.userid),
282
        ("username", ucache.get_name(userid) if display_mails else None),
283
        ("flavor_id", server.flavor_id),
284
        ("flavor_name", server.flavor.name),
285
        ("imageid", server.imageid),
286
        ("image_name", image),
287
        ("state", server.operstate),
288
        ("backend", server.backend),
289
        ("deleted", server.deleted),
290
        ("action", server.action),
291
        ("task", server.task),
292
        ("task_job_id", server.task_job_id),
293
        ("backendjobid", server.backendjobid),
294
        ("backendopcode", server.backendopcode),
295
        ("backendjobstatus", server.backendjobstatus),
296
        ("backend_time", server.backendtime),
297
        ])
298

    
299
    pprint_table(stdout, server_dict.items(), None, separator=" | ",
300
                 title=title)
301

    
302

    
303
def pprint_server_nics(server, stdout=None, title=None):
304
    if title is None:
305
        title = "Ports of Server %s" % server.id
306
    if stdout is None:
307
        stdout = sys.stdout
308

    
309
    nics = []
310
    for nic in server.nics.all():
311
        nics.append((nic.id, nic.name, nic.index, nic.mac, nic.ipv4_address,
312
                     nic.ipv6_address, nic.network, nic.firewall_profile,
313
                     nic.state))
314

    
315
    headers = ["ID", "Name", "Index", "MAC", "IPv4 Address", "IPv6 Address",
316
               "Network", "Firewall", "State"]
317
    pprint_table(stdout, nics, headers, separator=" | ",
318
                 title=title)
319

    
320

    
321
def pprint_server_in_ganeti(server, print_jobs=False, stdout=None, title=None):
322
    if stdout is None:
323
        stdout = sys.stdout
324
    if title is None:
325
        title = "State of Server %s in Ganeti" % server.id
326

    
327
    client = server.get_client()
328
    try:
329
        server_info = client.GetInstance(server.backend_vm_id)
330
    except GanetiApiError as e:
331
        if e.code == 404:
332
            stdout.write("Server '%s' does not exist in backend '%s'\n"
333
                         % (server.id, server.backend.clustername))
334
            return
335
        raise e
336
    server.put_client(client)
337

    
338
    GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status',
339
                              'pnode', 'snode', 'network_port',
340
                              'disk_template', 'disk_usage',
341
                              'oper_ram', 'oper_vcpus', 'mtime')
342
    server_dict = OrderedDict([(k, server_info.get(k))
343
                              for k in GANETI_INSTANCE_FIELDS])
344

    
345
    pprint_table(stdout, server_dict.items(), None, separator=" | ",
346
                 title=title)
347
    stdout.write("\n")
348

    
349
    nics = nics_from_instance(server_info)
350
    nics_keys = ["ip", "mac", "name", "network"]
351
    nics_values = [[nic[key] for key in nics_keys] for nic in nics]
352
    pprint_table(stdout, nics_values, nics_keys, separator=" | ",
353
                 title="NICs of Server %s in Ganeti" % server.id)
354

    
355
    if not print_jobs:
356
        return
357

    
358
    client = server.get_client()
359
    jobs = client.GetJobs(bulk=True)
360
    server_jobs = filter(
361
        lambda x: server.backend_vm_id in (" ".join(x.get("summary"))), jobs)
362

    
363
    GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus',
364
                         'oplog', 'start_ts', 'end_ts')
365
    for server_job in server_jobs:
366
        stdout.write("\n")
367
        values = [server_job.get(k) for k in GANETI_JOB_FIELDS]
368
        pprint_table(stdout, zip(GANETI_JOB_FIELDS, values), None,
369
                     separator=" | ",
370
                     title="Ganeti Job %s" % server_job["id"])
371
    server.put_client(client)