Statistics
| Branch: | Tag: | Revision:

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

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_BASE_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_BASE_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
                pprint_pool(None, ip_map, 80, stdout)
121
            except GanetiApiError as e:
122
                if e.code == 404:
123
                    stdout.write('Network does not exist in backend %s\n' %
124
                                 backend.clustername)
125
                else:
126
                    raise e
127

    
128

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

    
146

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

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

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

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

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

    
169

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

    
174

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

    
186

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

    
195

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

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

    
214

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

    
226

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

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

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

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

    
260
    vm.put_client(client)
261

    
262

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

    
269
    ucache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN)
270
    userid = server.userid
271

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

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

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

    
301

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

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

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

    
319

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

    
326
    client = server.get_client()
327
    try:
328
        server_info = client.GetInstance(server.backend_vm_id)
329
    except GanetiApiError as e:
330
        if e.code == 404:
331
            stdout.write("NIC seems attached to server %s, but"
332
                         " server does not exist in backend.\n"
333
                         % server)
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="NICs of Server %s in Ganeti" % server.id)
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=title)
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)