Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.9 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, CYCLADES_ASTAKOS_SERVICE_TOKEN,
48
                              ASTAKOS_URL)
49
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient
50
from synnefo.lib import astakos
51

    
52

    
53
def format_bool(b):
54
    return 'YES' if b else 'NO'
55

    
56

    
57
def parse_bool(string):
58
    if string == "True":
59
        return True
60
    elif string == "False":
61
        return False
62
    else:
63
        raise Exception("Can not parse string %s to bool" % string)
64

    
65

    
66
def format_date(d):
67
    if not d:
68
        return ''
69

    
70
    if d < datetime.now():
71
        return timesince(d) + ' ago'
72
    else:
73
        return 'in ' + timeuntil(d)
74

    
75

    
76
def format_vm_state(vm):
77
    if vm.operstate == "BUILD":
78
        return "BUILD(" + str(vm.buildpercentage) + "%)"
79
    else:
80
        return vm.operstate
81

    
82

    
83
def validate_network_info(options):
84
    subnet = options['subnet']
85
    gateway = options['gateway']
86
    subnet6 = options['subnet6']
87
    gateway6 = options['gateway6']
88

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

    
103
    try:
104
        subnet6 and ipaddr.IPv6Network(subnet6) or None
105
    except ValueError:
106
        raise CommandError('Malformed subnet6')
107

    
108
    try:
109
        gateway6 and ipaddr.IPv6Address(gateway6) or None
110
    except ValueError:
111
        raise CommandError('Malformed gateway6')
112

    
113
    return subnet, gateway, subnet6, gateway6
114

    
115

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

    
127

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

    
139

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

    
151

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

    
163

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

    
175

    
176
def filter_results(objects, filter_by):
177
    filter_list = filter_by.split(",")
178
    filter_dict = {}
179
    exclude_dict = {}
180

    
181
    def map_field_type(query):
182
        def fix_bool(val):
183
            if val.lower() in ("yes", "true", "t"):
184
                return True
185
            if val.lower() in ("no", "false", "f"):
186
                return False
187
            return val
188

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

    
208
    map(lambda x: map_field_type(x), filter_list)
209

    
210
    try:
211
        objects = objects.filter(**filter_dict)
212
        return objects.exclude(**exclude_dict)
213
    except FieldError as e:
214
        raise CommandError(e)
215
    except Exception as e:
216
        raise CommandError("Can not filter results: %s" % e)
217

    
218

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

    
228
    info = client.GetInfo()
229
    info_name = info['name']
230
    if info_name != clustername:
231
        raise CommandError("Invalid clustername value. Please use the"
232
                           " Ganeti Cluster name: %s" % info_name)
233

    
234

    
235
def pprint_table(out, table, headers=None, separator=None):
236
    """Print a pretty, aligned string representation of table.
237

238
    Works by finding out the max width of each column and padding to data
239
    to this value.
240
    """
241

    
242
    sep = separator if separator else "  "
243

    
244
    if headers:
245
        table.insert(0, headers)
246

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

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

    
259
    # print the rest table
260
    for row in table:
261
        print >> out, sep.join((val.rjust(width).encode('utf8') \
262
                                for val, width in zip(row, widths)))
263

    
264

    
265
class UUIDCache(object):
266
    def __init__(self):
267
        self.users = {}
268

    
269
    def get_user(self, uuid):
270
        if not uuid in self.users:
271
            astakos_url = ASTAKOS_URL.replace("im/authenticate",
272
                                              "service/api/user_catalogs")
273
            self.users[uuid] = \
274
            astakos.get_displayname(token=CYCLADES_ASTAKOS_SERVICE_TOKEN,
275
                                    url=astakos_url, uuid=uuid)
276

    
277
        return self.users[uuid]