Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / management / common.py @ f1ffc695

History | View | Annotate | Download (9.3 kB)

1
# Copyright 2012 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

    
35
import ipaddr
36
from datetime import datetime
37

    
38
from django.utils.timesince import timesince, timeuntil
39

    
40
from django.core.management import CommandError
41
from synnefo.db.models import Backend, VirtualMachine, Network, Flavor
42
from synnefo.api.util import get_image as backend_get_image
43
from synnefo.api.faults import ItemNotFound
44
from django.core.exceptions import FieldError
45

    
46
from synnefo.api.util import validate_network_size
47
from synnefo.settings import (MAX_CIDR_BLOCK,
48
                              CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN,
49
                              ASTAKOS_URL)
50
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient
51
from synnefo.lib import astakos
52

    
53
import logging
54
log = logging.getLogger(__name__)
55

    
56

    
57
def format_bool(b):
58
    return 'YES' if b else 'NO'
59

    
60

    
61
def parse_bool(string):
62
    if string == "True":
63
        return True
64
    elif string == "False":
65
        return False
66
    else:
67
        raise Exception("Can not parse string %s to bool" % string)
68

    
69

    
70
def format_date(d):
71
    if not d:
72
        return ''
73

    
74
    if d < datetime.now():
75
        return timesince(d) + ' ago'
76
    else:
77
        return 'in ' + timeuntil(d)
78

    
79

    
80
def format_vm_state(vm):
81
    if vm.operstate == "BUILD":
82
        return "BUILD(" + str(vm.buildpercentage) + "%)"
83
    else:
84
        return vm.operstate
85

    
86

    
87
def validate_network_info(options):
88
    subnet = options['subnet']
89
    gateway = options['gateway']
90
    subnet6 = options['subnet6']
91
    gateway6 = options['gateway6']
92

    
93
    try:
94
        net = ipaddr.IPv4Network(subnet)
95
        prefix = net.prefixlen
96
        if not validate_network_size(prefix):
97
            raise CommandError("Unsupport network mask %d."
98
                               " Must be in range (%s,29] "
99
                               % (prefix, MAX_CIDR_BLOCK))
100
    except ValueError:
101
        raise CommandError('Malformed subnet')
102
    try:
103
        gateway and ipaddr.IPv4Address(gateway) or None
104
    except ValueError:
105
        raise CommandError('Malformed gateway')
106

    
107
    try:
108
        subnet6 and ipaddr.IPv6Network(subnet6) or None
109
    except ValueError:
110
        raise CommandError('Malformed subnet6')
111

    
112
    try:
113
        gateway6 and ipaddr.IPv6Address(gateway6) or None
114
    except ValueError:
115
        raise CommandError('Malformed gateway6')
116

    
117
    return subnet, gateway, subnet6, gateway6
118

    
119

    
120
def get_backend(backend_id):
121
    try:
122
        backend_id = int(backend_id)
123
        return Backend.objects.get(id=backend_id)
124
    except ValueError:
125
        raise CommandError("Invalid Backend ID: %s" % backend_id)
126
    except Backend.DoesNotExist:
127
        raise CommandError("Backend with ID %s not found in DB. "
128
                           " Use snf-manage backend-list to find"
129
                           " out available backend IDs." % backend_id)
130

    
131

    
132
def get_image(image_id, user_id):
133
    if image_id:
134
        try:
135
            return backend_get_image(image_id, user_id)
136
        except ItemNotFound:
137
            raise CommandError("Image with ID %s not found."
138
                               " Use snf-manage image-list to find"
139
                               " out available image IDs." % image_id)
140
    else:
141
        raise CommandError("image-id is mandatory")
142

    
143

    
144
def get_vm(server_id):
145
    try:
146
        server_id = int(server_id)
147
        return VirtualMachine.objects.get(id=server_id)
148
    except ValueError:
149
        raise CommandError("Invalid server ID: %s", server_id)
150
    except VirtualMachine.DoesNotExist:
151
        raise CommandError("Server with ID %s not found in DB."
152
                           " Use snf-manage server-list to find out"
153
                           " available server IDs." % server_id)
154

    
155

    
156
def get_network(network_id):
157
    try:
158
        network_id = int(network_id)
159
        return Network.objects.get(id=network_id)
160
    except ValueError:
161
        raise CommandError("Invalid network ID: %s", network_id)
162
    except Network.DoesNotExist:
163
        raise CommandError("Network with ID %s not found in DB."
164
                           " Use snf-manage network-list to find out"
165
                           " available network IDs." % network_id)
166

    
167

    
168
def get_flavor(flavor_id):
169
    try:
170
        flavor_id = int(flavor_id)
171
        return Flavor.objects.get(id=flavor_id)
172
    except ValueError:
173
        raise CommandError("Invalid flavor ID: %s", flavor_id)
174
    except Flavor.DoesNotExist:
175
        raise CommandError("Flavor with ID %s not found in DB."
176
                           " Use snf-manage flavor-list to find out"
177
                           " available flavor IDs." % flavor_id)
178

    
179

    
180
def filter_results(objects, filter_by):
181
    filter_list = filter_by.split(",")
182
    filter_dict = {}
183
    exclude_dict = {}
184

    
185
    def map_field_type(query):
186
        def fix_bool(val):
187
            if val.lower() in ("yes", "true", "t"):
188
                return True
189
            if val.lower() in ("no", "false", "f"):
190
                return False
191
            return val
192

    
193
        if "!=" in query:
194
            key, val = query.split("!=")
195
            exclude_dict[key] = fix_bool(val)
196
            return
197
        OP_MAP = {
198
            ">=": "__gte",
199
            "=>": "__gte",
200
            ">":  "__gt",
201
            "<=": "__lte",
202
            "=<": "__lte",
203
            "<":  "__lt",
204
            "=":  "",
205
        }
206
        for op, new_op in OP_MAP.items():
207
            if op in query:
208
                key, val = query.split(op)
209
                filter_dict[key + new_op] = fix_bool(val)
210
                return
211

    
212
    map(lambda x: map_field_type(x), filter_list)
213

    
214
    try:
215
        objects = objects.filter(**filter_dict)
216
        return objects.exclude(**exclude_dict)
217
    except FieldError as e:
218
        raise CommandError(e)
219
    except Exception as e:
220
        raise CommandError("Can not filter results: %s" % e)
221

    
222

    
223
def check_backend_credentials(clustername, port, username, password):
224
    try:
225
        client = GanetiRapiClient(clustername, port, username, password)
226
        # This command will raise an exception if there is no
227
        # write-access
228
        client.ModifyCluster()
229
    except GanetiApiError as e:
230
        raise CommandError(e)
231

    
232
    info = client.GetInfo()
233
    info_name = info['name']
234
    if info_name != clustername:
235
        raise CommandError("Invalid clustername value. Please use the"
236
                           " Ganeti Cluster name: %s" % info_name)
237

    
238

    
239
def pprint_table(out, table, headers=None, separator=None):
240
    """Print a pretty, aligned string representation of table.
241

242
    Works by finding out the max width of each column and padding to data
243
    to this value.
244
    """
245

    
246
    sep = separator if separator else "  "
247

    
248
    if headers:
249
        table.insert(0, headers)
250

    
251
    # Find out the max width of each column
252
    widths = [max(map(len, col)) for col in zip(*table)]
253

    
254
    t_length = sum(widths) + len(sep) * (len(widths) - 1)
255
    if headers:
256
        # pretty print the headers
257
        print >> out, sep.join((val.rjust(width)
258
                               for val, width in zip(headers, widths)))
259
        print >> out, "-" * t_length
260
        # remove headers
261
        table = table[1:]
262

    
263
    # print the rest table
264
    for row in table:
265
        print >> out, sep.join((val.rjust(width).encode('utf8')
266
                               for val, width in zip(row, widths)))
267

    
268

    
269
class UUIDCache(object):
270
    """UUID-to-email cache"""
271

    
272
    user_catalogs_url = ASTAKOS_URL.replace("im/authenticate",
273
                                            "service/api/user_catalogs")
274

    
275
    def __init__(self):
276
        self.users = {}
277

    
278
    def get_user(self, uuid):
279
        """Do the uuid-to-email resolving"""
280

    
281
        if not uuid in self.users:
282
            try:
283
                self.users[uuid] = \
284
                    astakos.get_displayname(token=ASTAKOS_TOKEN,
285
                                            url=UUIDCache.user_catalogs_url,
286
                                            uuid=uuid)
287
            except Exception as e:
288
                log.error("Can not get display name for uuid %s: %s", uuid, e)
289
                return uuid
290

    
291
        return self.users[uuid]