Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (13.3 kB)

1 d5a4a8d1 Christos Stavrakakis
#Copyright (C) 2013 GRNET S.A. All rights reserved.
2 d5a4a8d1 Christos Stavrakakis
#
3 d5a4a8d1 Christos Stavrakakis
#Redistribution and use in source and binary forms, with or
4 d5a4a8d1 Christos Stavrakakis
#without modification, are permitted provided that the following
5 d5a4a8d1 Christos Stavrakakis
#conditions are met:
6 d5a4a8d1 Christos Stavrakakis
#
7 d5a4a8d1 Christos Stavrakakis
#  1. Redistributions of source code must retain the above
8 d5a4a8d1 Christos Stavrakakis
#     copyright notice, this list of conditions and the following
9 d5a4a8d1 Christos Stavrakakis
#     disclaimer.
10 d5a4a8d1 Christos Stavrakakis
#
11 d5a4a8d1 Christos Stavrakakis
#  2. Redistributions in binary form must reproduce the above
12 d5a4a8d1 Christos Stavrakakis
#     copyright notice, this list of conditions and the following
13 d5a4a8d1 Christos Stavrakakis
#     disclaimer in the documentation and/or other materials
14 d5a4a8d1 Christos Stavrakakis
#     provided with the distribution.
15 d5a4a8d1 Christos Stavrakakis
#
16 d5a4a8d1 Christos Stavrakakis
#THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 d5a4a8d1 Christos Stavrakakis
#OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 d5a4a8d1 Christos Stavrakakis
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 d5a4a8d1 Christos Stavrakakis
#PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A. OR
20 d5a4a8d1 Christos Stavrakakis
#CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 d5a4a8d1 Christos Stavrakakis
#SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 d5a4a8d1 Christos Stavrakakis
#LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 d5a4a8d1 Christos Stavrakakis
#USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 d5a4a8d1 Christos Stavrakakis
#AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 d5a4a8d1 Christos Stavrakakis
#LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 d5a4a8d1 Christos Stavrakakis
#ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 d5a4a8d1 Christos Stavrakakis
#POSSIBILITY OF SUCH DAMAGE.
28 d5a4a8d1 Christos Stavrakakis
#
29 d5a4a8d1 Christos Stavrakakis
#The views and conclusions contained in the software and
30 d5a4a8d1 Christos Stavrakakis
#documentation are those of the authors and should not be
31 d5a4a8d1 Christos Stavrakakis
#interpreted as representing official policies, either expressed
32 d5a4a8d1 Christos Stavrakakis
#or implied, of GRNET S.A.
33 d5a4a8d1 Christos Stavrakakis
34 d5a4a8d1 Christos Stavrakakis
import sys
35 d5a4a8d1 Christos Stavrakakis
from snf_django.management.utils import pprint_table
36 d5a4a8d1 Christos Stavrakakis
from synnefo.lib.ordereddict import OrderedDict
37 d5a4a8d1 Christos Stavrakakis
from snf_django.lib.astakos import UserCache
38 d5a4a8d1 Christos Stavrakakis
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN,
39 9835a70d Christos Stavrakakis
                              ASTAKOS_AUTH_URL)
40 d5a4a8d1 Christos Stavrakakis
from synnefo.db.models import Backend, pooled_rapi_client
41 e4758367 Dionysis Grigoropoulos
from synnefo.db.pools import bitarray_to_map
42 e4758367 Dionysis Grigoropoulos
43 d5a4a8d1 Christos Stavrakakis
from synnefo.logic.rapi import GanetiApiError
44 d5a4a8d1 Christos Stavrakakis
from synnefo.logic.reconciliation import nics_from_instance
45 8b178e6b Christos Stavrakakis
from synnefo.management.common import get_image
46 d5a4a8d1 Christos Stavrakakis
47 d5a4a8d1 Christos Stavrakakis
48 d5a4a8d1 Christos Stavrakakis
def pprint_network(network, display_mails=False, stdout=None, title=None):
49 d5a4a8d1 Christos Stavrakakis
    if stdout is None:
50 d5a4a8d1 Christos Stavrakakis
        stdout = sys.stdout
51 d5a4a8d1 Christos Stavrakakis
    if title is None:
52 d5a4a8d1 Christos Stavrakakis
        title = "State of Network %s in DB" % network.id
53 d5a4a8d1 Christos Stavrakakis
54 9835a70d Christos Stavrakakis
    ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN)
55 d5a4a8d1 Christos Stavrakakis
    userid = network.userid
56 d5a4a8d1 Christos Stavrakakis
57 d5a4a8d1 Christos Stavrakakis
    db_network = OrderedDict([
58 d5a4a8d1 Christos Stavrakakis
        ("name", network.name),
59 d5a4a8d1 Christos Stavrakakis
        ("backend-name", network.backend_id),
60 d5a4a8d1 Christos Stavrakakis
        ("state", network.state),
61 d5a4a8d1 Christos Stavrakakis
        ("userid", userid),
62 8b178e6b Christos Stavrakakis
        ("username", ucache.get_name(userid) if display_mails else None),
63 d5a4a8d1 Christos Stavrakakis
        ("public", network.public),
64 d5a4a8d1 Christos Stavrakakis
        ("floating_ip_pool", network.floating_ip_pool),
65 d5a4a8d1 Christos Stavrakakis
        ("external_router", network.external_router),
66 d5a4a8d1 Christos Stavrakakis
        ("drained", network.drained),
67 d5a4a8d1 Christos Stavrakakis
        ("MAC prefix", network.mac_prefix),
68 d5a4a8d1 Christos Stavrakakis
        ("flavor", network.flavor),
69 d5a4a8d1 Christos Stavrakakis
        ("link", network.link),
70 d5a4a8d1 Christos Stavrakakis
        ("mode", network.mode),
71 d5a4a8d1 Christos Stavrakakis
        ("deleted", network.deleted),
72 d5a4a8d1 Christos Stavrakakis
        ("tags", "), ".join(network.backend_tag)),
73 d5a4a8d1 Christos Stavrakakis
        ("action", network.action)])
74 d5a4a8d1 Christos Stavrakakis
75 d5a4a8d1 Christos Stavrakakis
    pprint_table(stdout, db_network.items(), None, separator=" | ",
76 d5a4a8d1 Christos Stavrakakis
                 title=title)
77 d5a4a8d1 Christos Stavrakakis
78 d5a4a8d1 Christos Stavrakakis
79 d5a4a8d1 Christos Stavrakakis
def pprint_network_subnets(network, stdout=None, title=None):
80 d5a4a8d1 Christos Stavrakakis
    if stdout is None:
81 d5a4a8d1 Christos Stavrakakis
        stdout = sys.stdout
82 d5a4a8d1 Christos Stavrakakis
    if title is None:
83 d5a4a8d1 Christos Stavrakakis
        title = "Subnets of network %s" % network.id
84 d5a4a8d1 Christos Stavrakakis
85 d5a4a8d1 Christos Stavrakakis
    subnets = list(network.subnets.values_list("id", "name", "ipversion",
86 d5a4a8d1 Christos Stavrakakis
                                               "cidr", "gateway", "dhcp",
87 d5a4a8d1 Christos Stavrakakis
                                               "deleted"))
88 d5a4a8d1 Christos Stavrakakis
    headers = ["ID", "Name", "Version", "CIDR", "Gateway", "DHCP",
89 d5a4a8d1 Christos Stavrakakis
               "Deleted"]
90 d5a4a8d1 Christos Stavrakakis
    pprint_table(stdout, subnets, headers, separator=" | ",
91 d5a4a8d1 Christos Stavrakakis
                 title=title)
92 d5a4a8d1 Christos Stavrakakis
93 d5a4a8d1 Christos Stavrakakis
94 d5a4a8d1 Christos Stavrakakis
def pprint_network_backends(network, stdout=None, title=None):
95 d5a4a8d1 Christos Stavrakakis
    if stdout is None:
96 d5a4a8d1 Christos Stavrakakis
        stdout = sys.stdout
97 d5a4a8d1 Christos Stavrakakis
    if title is None:
98 d5a4a8d1 Christos Stavrakakis
        title = "State of Network %s in DB for each backend" % network.id
99 d5a4a8d1 Christos Stavrakakis
    bnets = list(network.backend_networks.values_list(
100 d5a4a8d1 Christos Stavrakakis
        "backend__clustername",
101 d5a4a8d1 Christos Stavrakakis
        "operstate", "deleted", "backendjobid",
102 d5a4a8d1 Christos Stavrakakis
        "backendopcode", "backendjobstatus"))
103 d5a4a8d1 Christos Stavrakakis
    headers = ["Backend", "State", "Deleted", "JobID", "Opcode",
104 d5a4a8d1 Christos Stavrakakis
               "JobStatus"]
105 d5a4a8d1 Christos Stavrakakis
    pprint_table(stdout, bnets, headers, separator=" | ",
106 d5a4a8d1 Christos Stavrakakis
                 title=title)
107 d5a4a8d1 Christos Stavrakakis
108 d5a4a8d1 Christos Stavrakakis
109 d5a4a8d1 Christos Stavrakakis
def pprint_network_in_ganeti(network, stdout=None):
110 d5a4a8d1 Christos Stavrakakis
    if stdout is None:
111 d5a4a8d1 Christos Stavrakakis
        stdout = sys.stdout
112 d5a4a8d1 Christos Stavrakakis
    for backend in Backend.objects.exclude(offline=True):
113 d5a4a8d1 Christos Stavrakakis
        with pooled_rapi_client(backend) as client:
114 d5a4a8d1 Christos Stavrakakis
            try:
115 d5a4a8d1 Christos Stavrakakis
                g_net = client.GetNetwork(network.backend_id)
116 d5a4a8d1 Christos Stavrakakis
                ip_map = g_net.pop("map")
117 d5a4a8d1 Christos Stavrakakis
                pprint_table(stdout, g_net.items(), None,
118 d5a4a8d1 Christos Stavrakakis
                             title="State of network in backend: %s" %
119 d5a4a8d1 Christos Stavrakakis
                                   backend.clustername)
120 bac6ed1e Christos Stavrakakis
                if network.subnet4 is not None:
121 bac6ed1e Christos Stavrakakis
                    pprint_pool(None, ip_map, 80, stdout)
122 d5a4a8d1 Christos Stavrakakis
            except GanetiApiError as e:
123 d5a4a8d1 Christos Stavrakakis
                if e.code == 404:
124 d5a4a8d1 Christos Stavrakakis
                    stdout.write('Network does not exist in backend %s\n' %
125 d5a4a8d1 Christos Stavrakakis
                                 backend.clustername)
126 d5a4a8d1 Christos Stavrakakis
                else:
127 d5a4a8d1 Christos Stavrakakis
                    raise e
128 d5a4a8d1 Christos Stavrakakis
129 d5a4a8d1 Christos Stavrakakis
130 e4758367 Dionysis Grigoropoulos
def pprint_subnet_in_db(subnet, stdout=None, title=None):
131 e4758367 Dionysis Grigoropoulos
    if stdout is None:
132 e4758367 Dionysis Grigoropoulos
        stdout = sys.stdout
133 e4758367 Dionysis Grigoropoulos
    if title is None:
134 e4758367 Dionysis Grigoropoulos
        title = "State of Subnet %s in DB" % subnet.id
135 e4758367 Dionysis Grigoropoulos
    info = OrderedDict([("ID", subnet.id),
136 e4758367 Dionysis Grigoropoulos
                        ("Network_ID", subnet.network.id),
137 e4758367 Dionysis Grigoropoulos
                        # If a user names his subnet "-", what happens then?
138 e4758367 Dionysis Grigoropoulos
                        ("Name", "-" if subnet.name == "" else subnet.name),
139 e4758367 Dionysis Grigoropoulos
                        ("IP_Version", subnet.ipversion),
140 e4758367 Dionysis Grigoropoulos
                        ("CIDR", subnet.cidr),
141 e4758367 Dionysis Grigoropoulos
                        ("Gateway", subnet.gateway),
142 e4758367 Dionysis Grigoropoulos
                        ("DHCP/SLAAC", subnet.dhcp),
143 e4758367 Dionysis Grigoropoulos
                        ("Host_Routes", subnet.host_routes),
144 e4758367 Dionysis Grigoropoulos
                        ("DNS_Nameservers", subnet.dns_nameservers)])
145 e4758367 Dionysis Grigoropoulos
    pprint_table(stdout, info.items(), None, separator=" | ", title=title)
146 e4758367 Dionysis Grigoropoulos
147 e4758367 Dionysis Grigoropoulos
148 e4758367 Dionysis Grigoropoulos
def pprint_ippool(subnet, stdout=None, title=None):
149 e4758367 Dionysis Grigoropoulos
    """Pretty print IP Pools of a subnet. Only IPv4 subnets have IP Pools"""
150 e4758367 Dionysis Grigoropoulos
151 e4758367 Dionysis Grigoropoulos
    if int(subnet.ipversion) != 4:
152 e4758367 Dionysis Grigoropoulos
        return 0
153 e4758367 Dionysis Grigoropoulos
154 e4758367 Dionysis Grigoropoulos
    if stdout is None:
155 e4758367 Dionysis Grigoropoulos
        stdout = sys.stdout
156 e4758367 Dionysis Grigoropoulos
157 aab200c6 Dionysis Grigoropoulos
    stdout.write("IP Pools of subnet %s:\n\n" % subnet.id)
158 e4758367 Dionysis Grigoropoulos
159 e4758367 Dionysis Grigoropoulos
    for pool in subnet.get_ip_pools():
160 e4758367 Dionysis Grigoropoulos
        size = pool.pool_size
161 e4758367 Dionysis Grigoropoulos
        available = pool.available.count()
162 e4758367 Dionysis Grigoropoulos
        info = OrderedDict([("First_IP", pool.return_start()),
163 e4758367 Dionysis Grigoropoulos
                            ("Last_IP", pool.return_end()),
164 e4758367 Dionysis Grigoropoulos
                            ("Size", size),
165 e4758367 Dionysis Grigoropoulos
                            ("Available", available)])
166 e4758367 Dionysis Grigoropoulos
        pprint_table(stdout, info.items(), None, separator=" | ", title=None)
167 aab200c6 Dionysis Grigoropoulos
        pprint_pool(None, bitarray_to_map(pool.available[:size]), 80, stdout)
168 e4758367 Dionysis Grigoropoulos
        stdout.write("\n\n")
169 e4758367 Dionysis Grigoropoulos
170 e4758367 Dionysis Grigoropoulos
171 d5a4a8d1 Christos Stavrakakis
def pool_map_chunks(smap, step=64):
172 d5a4a8d1 Christos Stavrakakis
    for i in xrange(0, len(smap), step):
173 d5a4a8d1 Christos Stavrakakis
        yield smap[i:i + step]
174 d5a4a8d1 Christos Stavrakakis
175 d5a4a8d1 Christos Stavrakakis
176 d5a4a8d1 Christos Stavrakakis
def splitPoolMap(s, count):
177 d5a4a8d1 Christos Stavrakakis
    chunks = pool_map_chunks(s, count)
178 d5a4a8d1 Christos Stavrakakis
    acc = []
179 d5a4a8d1 Christos Stavrakakis
    count = 0
180 d5a4a8d1 Christos Stavrakakis
    for chunk in chunks:
181 d5a4a8d1 Christos Stavrakakis
        chunk_len = len(chunk)
182 d5a4a8d1 Christos Stavrakakis
        acc.append(str(count).rjust(3) + ' ' + chunk + ' ' +
183 d5a4a8d1 Christos Stavrakakis
                   str(count + chunk_len - 1).ljust(4))
184 d5a4a8d1 Christos Stavrakakis
        count += chunk_len
185 d5a4a8d1 Christos Stavrakakis
    return '\n' + '\n'.join(acc)
186 d5a4a8d1 Christos Stavrakakis
187 d5a4a8d1 Christos Stavrakakis
188 d5a4a8d1 Christos Stavrakakis
def pprint_pool(name, pool_map, step=80, stdout=None):
189 d5a4a8d1 Christos Stavrakakis
    if stdout is None:
190 d5a4a8d1 Christos Stavrakakis
        stdout = sys.stdout
191 d5a4a8d1 Christos Stavrakakis
    if name is not None:
192 d5a4a8d1 Christos Stavrakakis
        stdout.write("Pool: %s\n" % name)
193 d5a4a8d1 Christos Stavrakakis
    stdout.write(splitPoolMap(pool_map, count=step))
194 d5a4a8d1 Christos Stavrakakis
    stdout.write("\n")
195 d5a4a8d1 Christos Stavrakakis
196 d5a4a8d1 Christos Stavrakakis
197 d5a4a8d1 Christos Stavrakakis
def pprint_port(port, stdout=None, title=None):
198 d5a4a8d1 Christos Stavrakakis
    if stdout is None:
199 d5a4a8d1 Christos Stavrakakis
        stdout = sys.stdout
200 d5a4a8d1 Christos Stavrakakis
    if title is None:
201 d5a4a8d1 Christos Stavrakakis
        title = "State of Port %s in DB" % port.id
202 d5a4a8d1 Christos Stavrakakis
    port = OrderedDict([
203 d5a4a8d1 Christos Stavrakakis
        ("id", port.id),
204 d5a4a8d1 Christos Stavrakakis
        ("name", port.name),
205 d5a4a8d1 Christos Stavrakakis
        ("userid", port.userid),
206 d5a4a8d1 Christos Stavrakakis
        ("server", port.machine_id),
207 d5a4a8d1 Christos Stavrakakis
        ("network", port.network_id),
208 d5a4a8d1 Christos Stavrakakis
        ("device_owner", port.device_owner),
209 d5a4a8d1 Christos Stavrakakis
        ("mac", port.mac),
210 d5a4a8d1 Christos Stavrakakis
        ("state", port.state)])
211 d5a4a8d1 Christos Stavrakakis
212 d5a4a8d1 Christos Stavrakakis
    pprint_table(stdout, port.items(), None, separator=" | ",
213 d5a4a8d1 Christos Stavrakakis
                 title=title)
214 d5a4a8d1 Christos Stavrakakis
215 d5a4a8d1 Christos Stavrakakis
216 d5a4a8d1 Christos Stavrakakis
def pprint_port_ips(port, stdout=None, title=None):
217 d5a4a8d1 Christos Stavrakakis
    if stdout is None:
218 d5a4a8d1 Christos Stavrakakis
        stdout = sys.stdout
219 d5a4a8d1 Christos Stavrakakis
    if title is None:
220 d5a4a8d1 Christos Stavrakakis
        title = "IP Addresses of Port %s" % port.id
221 d5a4a8d1 Christos Stavrakakis
    ips = list(port.ips.values_list("address", "network_id", "subnet_id",
222 d5a4a8d1 Christos Stavrakakis
                                    "subnet__cidr", "floating_ip"))
223 d5a4a8d1 Christos Stavrakakis
    headers = ["Address", "Network", "Subnet", "CIDR", "is_floating"]
224 d5a4a8d1 Christos Stavrakakis
    pprint_table(stdout, ips, headers, separator=" | ",
225 d5a4a8d1 Christos Stavrakakis
                 title=title)
226 d5a4a8d1 Christos Stavrakakis
227 d5a4a8d1 Christos Stavrakakis
228 d5a4a8d1 Christos Stavrakakis
def pprint_port_in_ganeti(port, stdout=None, title=None):
229 d5a4a8d1 Christos Stavrakakis
    if stdout is None:
230 d5a4a8d1 Christos Stavrakakis
        stdout = sys.stdout
231 d5a4a8d1 Christos Stavrakakis
    if title is None:
232 d5a4a8d1 Christos Stavrakakis
        title = "State of Port %s in Ganeti" % port.id
233 d5a4a8d1 Christos Stavrakakis
234 d5a4a8d1 Christos Stavrakakis
    vm = port.machine
235 d5a4a8d1 Christos Stavrakakis
    if vm is None:
236 d5a4a8d1 Christos Stavrakakis
        stdout.write("Port is not attached to any instance.\n")
237 d5a4a8d1 Christos Stavrakakis
        return
238 d5a4a8d1 Christos Stavrakakis
239 d5a4a8d1 Christos Stavrakakis
    client = vm.get_client()
240 d5a4a8d1 Christos Stavrakakis
    try:
241 d5a4a8d1 Christos Stavrakakis
        vm_info = client.GetInstance(vm.backend_vm_id)
242 d5a4a8d1 Christos Stavrakakis
    except GanetiApiError as e:
243 d5a4a8d1 Christos Stavrakakis
        if e.code == 404:
244 a9c1db43 Christos Stavrakakis
            stdout.write("Port seems attached to server %s, but"
245 d5a4a8d1 Christos Stavrakakis
                         " server does not exist in backend.\n"
246 d5a4a8d1 Christos Stavrakakis
                         % vm)
247 d5a4a8d1 Christos Stavrakakis
            return
248 d5a4a8d1 Christos Stavrakakis
        raise e
249 d5a4a8d1 Christos Stavrakakis
250 d5a4a8d1 Christos Stavrakakis
    nics = nics_from_instance(vm_info)
251 d5a4a8d1 Christos Stavrakakis
    try:
252 d5a4a8d1 Christos Stavrakakis
        gnt_nic = filter(lambda nic: nic.get("name") == port.backend_uuid,
253 d5a4a8d1 Christos Stavrakakis
                         nics)[0]
254 d5a4a8d1 Christos Stavrakakis
        gnt_nic["instance"] = vm_info["name"]
255 d5a4a8d1 Christos Stavrakakis
    except IndexError:
256 a9c1db43 Christos Stavrakakis
        stdout.write("Port %s is not attached to instance %s" % (port, vm))
257 d5a4a8d1 Christos Stavrakakis
        return
258 d5a4a8d1 Christos Stavrakakis
    pprint_table(stdout, gnt_nic.items(), None, separator=" | ",
259 d5a4a8d1 Christos Stavrakakis
                 title=title)
260 d5a4a8d1 Christos Stavrakakis
261 d5a4a8d1 Christos Stavrakakis
    vm.put_client(client)
262 8b178e6b Christos Stavrakakis
263 8b178e6b Christos Stavrakakis
264 8b178e6b Christos Stavrakakis
def pprint_server(server, display_mails=False, stdout=None, title=None):
265 8b178e6b Christos Stavrakakis
    if stdout is None:
266 8b178e6b Christos Stavrakakis
        stdout = sys.stdout
267 8b178e6b Christos Stavrakakis
    if title is None:
268 8b178e6b Christos Stavrakakis
        title = "State of Server %s in DB" % server.id
269 8b178e6b Christos Stavrakakis
270 9835a70d Christos Stavrakakis
    ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN)
271 8b178e6b Christos Stavrakakis
    userid = server.userid
272 8b178e6b Christos Stavrakakis
273 8b178e6b Christos Stavrakakis
    try:
274 8b178e6b Christos Stavrakakis
        image = get_image(server.imageid, server.userid)['name']
275 8b178e6b Christos Stavrakakis
    except:
276 8b178e6b Christos Stavrakakis
        image = server.imageid
277 8b178e6b Christos Stavrakakis
278 8b178e6b Christos Stavrakakis
    server_dict = OrderedDict([
279 8b178e6b Christos Stavrakakis
        ("id", server.id),
280 8b178e6b Christos Stavrakakis
        ("name", server.name),
281 8b178e6b Christos Stavrakakis
        ("userid", server.userid),
282 8b178e6b Christos Stavrakakis
        ("username", ucache.get_name(userid) if display_mails else None),
283 8b178e6b Christos Stavrakakis
        ("flavor_id", server.flavor_id),
284 8b178e6b Christos Stavrakakis
        ("flavor_name", server.flavor.name),
285 8b178e6b Christos Stavrakakis
        ("imageid", server.imageid),
286 8b178e6b Christos Stavrakakis
        ("image_name", image),
287 8b178e6b Christos Stavrakakis
        ("state", server.operstate),
288 8b178e6b Christos Stavrakakis
        ("backend", server.backend),
289 8b178e6b Christos Stavrakakis
        ("deleted", server.deleted),
290 8b178e6b Christos Stavrakakis
        ("action", server.action),
291 8b178e6b Christos Stavrakakis
        ("task", server.task),
292 8b178e6b Christos Stavrakakis
        ("task_job_id", server.task_job_id),
293 8b178e6b Christos Stavrakakis
        ("backendjobid", server.backendjobid),
294 8b178e6b Christos Stavrakakis
        ("backendopcode", server.backendopcode),
295 8b178e6b Christos Stavrakakis
        ("backendjobstatus", server.backendjobstatus),
296 8b178e6b Christos Stavrakakis
        ("backend_time", server.backendtime),
297 8b178e6b Christos Stavrakakis
        ])
298 8b178e6b Christos Stavrakakis
299 8b178e6b Christos Stavrakakis
    pprint_table(stdout, server_dict.items(), None, separator=" | ",
300 8b178e6b Christos Stavrakakis
                 title=title)
301 8b178e6b Christos Stavrakakis
302 8b178e6b Christos Stavrakakis
303 8b178e6b Christos Stavrakakis
def pprint_server_nics(server, stdout=None, title=None):
304 8b178e6b Christos Stavrakakis
    if title is None:
305 a9c1db43 Christos Stavrakakis
        title = "Ports of Server %s" % server.id
306 8b178e6b Christos Stavrakakis
    if stdout is None:
307 8b178e6b Christos Stavrakakis
        stdout = sys.stdout
308 8b178e6b Christos Stavrakakis
309 8b178e6b Christos Stavrakakis
    nics = []
310 8b178e6b Christos Stavrakakis
    for nic in server.nics.all():
311 db908bb1 Christos Stavrakakis
        nics.append((nic.id, nic.name, nic.index, nic.mac, nic.ipv4_address,
312 8b178e6b Christos Stavrakakis
                     nic.ipv6_address, nic.network, nic.firewall_profile,
313 8b178e6b Christos Stavrakakis
                     nic.state))
314 8b178e6b Christos Stavrakakis
315 db908bb1 Christos Stavrakakis
    headers = ["ID", "Name", "Index", "MAC", "IPv4 Address", "IPv6 Address",
316 8b178e6b Christos Stavrakakis
               "Network", "Firewall", "State"]
317 8b178e6b Christos Stavrakakis
    pprint_table(stdout, nics, headers, separator=" | ",
318 8b178e6b Christos Stavrakakis
                 title=title)
319 8b178e6b Christos Stavrakakis
320 8b178e6b Christos Stavrakakis
321 8b178e6b Christos Stavrakakis
def pprint_server_in_ganeti(server, print_jobs=False, stdout=None, title=None):
322 8b178e6b Christos Stavrakakis
    if stdout is None:
323 8b178e6b Christos Stavrakakis
        stdout = sys.stdout
324 8b178e6b Christos Stavrakakis
    if title is None:
325 8b178e6b Christos Stavrakakis
        title = "State of Server %s in Ganeti" % server.id
326 8b178e6b Christos Stavrakakis
327 8b178e6b Christos Stavrakakis
    client = server.get_client()
328 8b178e6b Christos Stavrakakis
    try:
329 8b178e6b Christos Stavrakakis
        server_info = client.GetInstance(server.backend_vm_id)
330 8b178e6b Christos Stavrakakis
    except GanetiApiError as e:
331 8b178e6b Christos Stavrakakis
        if e.code == 404:
332 a9c1db43 Christos Stavrakakis
            stdout.write("Server '%s' does not exist in backend '%s'\n"
333 a9c1db43 Christos Stavrakakis
                         % (server.id, server.backend.clustername))
334 8b178e6b Christos Stavrakakis
            return
335 8b178e6b Christos Stavrakakis
        raise e
336 8b178e6b Christos Stavrakakis
    server.put_client(client)
337 8b178e6b Christos Stavrakakis
338 8b178e6b Christos Stavrakakis
    GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status',
339 8b178e6b Christos Stavrakakis
                              'pnode', 'snode', 'network_port',
340 8b178e6b Christos Stavrakakis
                              'disk_template', 'disk_usage',
341 8b178e6b Christos Stavrakakis
                              'oper_ram', 'oper_vcpus', 'mtime')
342 8b178e6b Christos Stavrakakis
    server_dict = OrderedDict([(k, server_info.get(k))
343 8b178e6b Christos Stavrakakis
                              for k in GANETI_INSTANCE_FIELDS])
344 8b178e6b Christos Stavrakakis
345 8b178e6b Christos Stavrakakis
    pprint_table(stdout, server_dict.items(), None, separator=" | ",
346 a9c1db43 Christos Stavrakakis
                 title=title)
347 8b178e6b Christos Stavrakakis
    stdout.write("\n")
348 8b178e6b Christos Stavrakakis
349 8b178e6b Christos Stavrakakis
    nics = nics_from_instance(server_info)
350 8b178e6b Christos Stavrakakis
    nics_keys = ["ip", "mac", "name", "network"]
351 8b178e6b Christos Stavrakakis
    nics_values = [[nic[key] for key in nics_keys] for nic in nics]
352 8b178e6b Christos Stavrakakis
    pprint_table(stdout, nics_values, nics_keys, separator=" | ",
353 a9c1db43 Christos Stavrakakis
                 title="NICs of Server %s in Ganeti" % server.id)
354 8b178e6b Christos Stavrakakis
355 8b178e6b Christos Stavrakakis
    if not print_jobs:
356 8b178e6b Christos Stavrakakis
        return
357 8b178e6b Christos Stavrakakis
358 8b178e6b Christos Stavrakakis
    client = server.get_client()
359 8b178e6b Christos Stavrakakis
    jobs = client.GetJobs(bulk=True)
360 8b178e6b Christos Stavrakakis
    server_jobs = filter(
361 8b178e6b Christos Stavrakakis
        lambda x: server.backend_vm_id in (" ".join(x.get("summary"))), jobs)
362 8b178e6b Christos Stavrakakis
363 8b178e6b Christos Stavrakakis
    GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus',
364 8b178e6b Christos Stavrakakis
                         'oplog', 'start_ts', 'end_ts')
365 8b178e6b Christos Stavrakakis
    for server_job in server_jobs:
366 8b178e6b Christos Stavrakakis
        stdout.write("\n")
367 8b178e6b Christos Stavrakakis
        values = [server_job.get(k) for k in GANETI_JOB_FIELDS]
368 8b178e6b Christos Stavrakakis
        pprint_table(stdout, zip(GANETI_JOB_FIELDS, values), None,
369 8b178e6b Christos Stavrakakis
                     separator=" | ",
370 8b178e6b Christos Stavrakakis
                     title="Ganeti Job %s" % server_job["id"])
371 8b178e6b Christos Stavrakakis
    server.put_client(client)