Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / errors.py @ 2005b18e

History | View | Annotate | Download (20.7 kB)

1
# Copyright 2011-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.command
33

    
34
from traceback import print_stack, print_exc
35
import logging
36

    
37
from kamaki.clients import ClientError
38
from kamaki.cli.errors import CLIError, raiseCLIError, CLISyntaxError
39
from kamaki.cli import _debug, kloger
40
from kamaki.cli.utils import format_size
41

    
42
sendlog = logging.getLogger('clients.send')
43
datasendlog = logging.getLogger('data.send')
44
recvlog = logging.getLogger('clients.recv')
45
datarecvlog = logging.getLogger('data.recv')
46

    
47

    
48
class generic(object):
49

    
50
    @classmethod
51
    def all(this, foo):
52
        def _raise(self, *args, **kwargs):
53
            try:
54
                return foo(self, *args, **kwargs)
55
            except Exception as e:
56
                if _debug:
57
                    print_stack()
58
                    print_exc(e)
59
                raiseCLIError(e)
60
        return _raise
61

    
62
    @classmethod
63
    def _connection(this, foo, base_url):
64
        def _raise(self, *args, **kwargs):
65
            try:
66
                foo(self, *args, **kwargs)
67
            except ClientError as ce:
68
                ce_msg = ('%s' % ce).lower()
69
                if ce.status == 401:
70
                    raiseCLIError(ce, 'Authorization failed', details=[
71
                        'Make sure a valid token is provided:',
72
                        '  to check if token is valid: /astakos authenticate',
73
                        '  to set token: /config set [.server.]token <token>',
74
                        '  to get current token: /config get [server.]token'])
75
                elif ce.status in range(-12, 200) + [302, 401, 403, 500]:
76
                    raiseCLIError(ce, importance=3, details=[
77
                        'Check if service is up or set to url %s' % base_url,
78
                        '  to get url: /config get %s' % base_url,
79
                        '  to set url: /config set %s <URL>' % base_url])
80
                elif ce.status == 404 and 'kamakihttpresponse' in ce_msg:
81
                    client = getattr(self, 'client', None)
82
                    if not client:
83
                        raise
84
                    url = getattr(client, 'base_url', '<empty>')
85
                    msg = 'Invalid service url %s' % url
86
                    raiseCLIError(ce, msg, details=[
87
                        'Please, check if service url is correctly set',
88
                        '* to get current url: /config get compute.url',
89
                        '* to set url: /config set compute.url <URL>'])
90
                raise
91
        return _raise
92

    
93

    
94
class astakos(object):
95

    
96
    _token_details = [
97
        'To check default token: /config get token',
98
        'If set/update a token:',
99
        '*  (permanent):    /config set token <token>',
100
        '*  (temporary):    re-run with <token> parameter']
101

    
102
    @classmethod
103
    def load(this, foo):
104
        def _raise(self, *args, **kwargs):
105
            r = foo(self, *args, **kwargs)
106
            try:
107
                client = getattr(self, 'client')
108
            except AttributeError as ae:
109
                raiseCLIError(ae, 'Client setup failure', importance=3)
110
            if not getattr(client, 'token', False):
111
                kloger.warning(
112
                    'No permanent token (try: kamaki config set token <tkn>)')
113
            if not getattr(client, 'base_url', False):
114
                msg = 'Missing astakos server URL'
115
                raise CLIError(msg, importance=3, details=[
116
                    'Check if astakos.url is set correctly',
117
                    'To get astakos url:   /config get astakos.url',
118
                    'To set astakos url:   /config set astakos.url <URL>'])
119
            return r
120
        return _raise
121

    
122
    @classmethod
123
    def authenticate(this, foo):
124
        def _raise(self, *args, **kwargs):
125
            try:
126
                r = foo(self, *args, **kwargs)
127
            except ClientError as ce:
128
                if ce.status == 401:
129
                    token = kwargs.get('custom_token', 0) or self.client.token
130
                    msg = (
131
                        'Authorization failed for token %s' % token
132
                    ) if token else 'No token provided',
133
                    details = [] if token else this._token_details
134
                    raiseCLIError(ce, msg, details=details)
135
            self._raise = foo
136
            return r
137
        return _raise
138

    
139

    
140
class history(object):
141
    @classmethod
142
    def init(this, foo):
143
        def _raise(self, *args, **kwargs):
144
            r = foo(self, *args, **kwargs)
145
            if not hasattr(self, 'history'):
146
                raise CLIError('Failed to load history', importance=2)
147
            return r
148
        return _raise
149

    
150
    @classmethod
151
    def _get_cmd_ids(this, foo):
152
        def _raise(self, cmd_ids, *args, **kwargs):
153
            if not cmd_ids:
154
                raise CLISyntaxError(
155
                    'Usage: <id1|id1-id2> [id3|id3-id4] ...',
156
                    details=self.__doc__.split('\n'))
157
            return foo(self, cmd_ids, *args, **kwargs)
158
        return _raise
159

    
160

    
161
class cyclades(object):
162
    about_flavor_id = [
163
        'How to pick a valid flavor id:',
164
        '* get a list of flavor ids: /flavor list',
165
        '* details of flavor: /flavor info <flavor id>']
166

    
167
    about_network_id = [
168
        'How to pick a valid network id:',
169
        '* get a list of network ids: /network list',
170
        '* details of network: /network info <network id>']
171

    
172
    @classmethod
173
    def connection(this, foo):
174
        return generic._connection(foo, 'compute.url')
175

    
176
    @classmethod
177
    def date(this, foo):
178
        def _raise(self, *args, **kwargs):
179
            try:
180
                return foo(self, *args, **kwargs)
181
            except ClientError as ce:
182
                if ce.status == 400 and 'changes-since' in ('%s' % ce):
183
                    raise CLIError(
184
                        'Incorrect date format for --since',
185
                        details=['Accepted date format: d/m/y'])
186
                raise
187
        return _raise
188

    
189
    @classmethod
190
    def network_id(this, foo):
191
        def _raise(self, *args, **kwargs):
192
            network_id = kwargs.get('network_id', None)
193
            try:
194
                network_id = int(network_id)
195
                return foo(self, *args, **kwargs)
196
            except ValueError as ve:
197
                msg = 'Invalid network id %s ' % network_id
198
                details = ['network id must be a positive integer']
199
                raiseCLIError(ve, msg, details=details, importance=1)
200
            except ClientError as ce:
201
                if network_id and ce.status == 404 and (
202
                    'network' in ('%s' % ce).lower()
203
                ):
204
                    msg = 'No network with id %s found' % network_id,
205
                    raiseCLIError(ce, msg, details=this.about_network_id)
206
                raise
207
        return _raise
208

    
209
    @classmethod
210
    def network_max(this, foo):
211
        def _raise(self, *args, **kwargs):
212
            try:
213
                return foo(self, *args, **kwargs)
214
            except ClientError as ce:
215
                if ce.status == 413:
216
                    msg = 'Cannot create another network',
217
                    details = [
218
                        'Maximum number of networks reached',
219
                        '* to get a list of networks: /network list',
220
                        '* to delete a network: /network delete <net id>']
221
                    raiseCLIError(ce, msg, details=details)
222
                raise
223
        return _raise
224

    
225
    @classmethod
226
    def network_in_use(this, foo):
227
        def _raise(self, *args, **kwargs):
228
            network_id = kwargs.get('network_id', None)
229
            try:
230
                return foo(self, *args, **kwargs)
231
            except ClientError as ce:
232
                if network_id and ce.status == 400:
233
                    msg = 'Network with id %s does not exist' % network_id,
234
                    raiseCLIError(ce, msg, details=self.about_network_id)
235
                elif network_id or ce.status == 421:
236
                    msg = 'Network with id %s is in use' % network_id,
237
                    raiseCLIError(ce, msg, details=[
238
                        'Disconnect all nics/VMs of this network first',
239
                        '* to get nics: /network info %s' % network_id,
240
                        '.  (under "attachments" section)',
241
                        '* to disconnect: /network disconnect <nic id>'])
242
                raise
243
        return _raise
244

    
245
    @classmethod
246
    def flavor_id(this, foo):
247
        def _raise(self, *args, **kwargs):
248
            flavor_id = kwargs.get('flavor_id', None)
249
            try:
250
                flavor_id = int(flavor_id)
251
                return foo(self, *args, **kwargs)
252
            except ValueError as ve:
253
                msg = 'Invalid flavor id %s ' % flavor_id,
254
                details = 'Flavor id must be a positive integer',
255
                raiseCLIError(ve, msg, details=details, importance=1)
256
            except ClientError as ce:
257
                if flavor_id and ce.status == 404 and (
258
                    'flavor' in ('%s' % ce).lower()
259
                ):
260
                        msg = 'No flavor with id %s found' % flavor_id,
261
                        raiseCLIError(ce, msg, details=this.about_flavor_id)
262
                raise
263
        return _raise
264

    
265
    @classmethod
266
    def server_id(this, foo):
267
        def _raise(self, *args, **kwargs):
268
            server_id = kwargs.get('server_id', None)
269
            try:
270
                server_id = int(server_id)
271
                return foo(self, *args, **kwargs)
272
            except ValueError as ve:
273
                msg = 'Invalid server(VM) id %s' % server_id,
274
                details = ['id must be a positive integer'],
275
                raiseCLIError(ve, msg, details=details, importance=1)
276
            except ClientError as ce:
277
                err_msg = ('%s' % ce).lower()
278
                if (
279
                    ce.status == 404 and 'server' in err_msg
280
                ) or (
281
                    ce.status == 400 and 'not found' in err_msg
282
                ):
283
                    msg = 'server(VM) with id %s not found' % server_id,
284
                    raiseCLIError(ce, msg, details=[
285
                        '* to get existing VM ids: /server list',
286
                        '* to get VM details: /server info <VM id>'])
287
                raise
288
        return _raise
289

    
290
    @classmethod
291
    def firewall(this, foo):
292
        def _raise(self, *args, **kwargs):
293
            profile = kwargs.get('profile', None)
294
            try:
295
                return foo(self, *args, **kwargs)
296
            except ClientError as ce:
297
                if ce.status == 400 and profile and (
298
                    'firewall' in ('%s' % ce).lower()
299
                ):
300
                    msg = '%s is an invalid firewall profile term' % profile
301
                    raiseCLIError(ce, msg, details=[
302
                        'Try one of the following:',
303
                        '* DISABLED: Shutdown firewall',
304
                        '* ENABLED: Firewall in normal mode',
305
                        '* PROTECTED: Firewall in secure mode'])
306
                raise
307
        return _raise
308

    
309
    @classmethod
310
    def nic_id(this, foo):
311
        def _raise(self, *args, **kwargs):
312
            try:
313
                return foo(self, *args, **kwargs)
314
            except ClientError as ce:
315
                nic_id = kwargs.get('nic_id', None)
316
                if nic_id and ce.status == 404 and (
317
                    'network interface' in ('%s' % ce).lower()
318
                ):
319
                    server_id = kwargs.get('server_id', '<no server>')
320
                    err_msg = 'No nic %s on server(VM) with id %s' % (
321
                        nic_id,
322
                        server_id)
323
                    raiseCLIError(ce, err_msg, details=[
324
                        '* check server(VM) with id %s: /server info %s' % (
325
                            server_id,
326
                            server_id),
327
                        '* list nics for server(VM) with id %s:' % server_id,
328
                        '      /server addr %s' % server_id])
329
                raise
330
        return _raise
331

    
332
    @classmethod
333
    def nic_format(this, foo):
334
        def _raise(self, *args, **kwargs):
335
            try:
336
                return foo(self, *args, **kwargs)
337
            except IndexError as ie:
338
                nic_id = kwargs.get('nic_id', None)
339
                msg = 'Invalid format for network interface (nic) %s' % nic_id
340
                raiseCLIError(ie, msg, importance=1, details=[
341
                    'nid_id format: nic-<server id>-<nic id>',
342
                    '* get nics of a network: /network info <net id>',
343
                    '    (listed the "attachments" section)'])
344
        return _raise
345

    
346
    @classmethod
347
    def metadata(this, foo):
348
        def _raise(self, *args, **kwargs):
349
            key = kwargs.get('key', None)
350
            try:
351
                foo(self, *args, **kwargs)
352
            except ClientError as ce:
353
                if key and ce.status == 404 and (
354
                    'metadata' in ('%s' % ce).lower()
355
                ):
356
                        raiseCLIError(ce, 'No VM metadata with key %s' % key)
357
                raise
358
        return _raise
359

    
360

    
361
class plankton(object):
362

    
363
    about_image_id = [
364
        'How to pick a suitable image:',
365
        '* get a list of image ids: /image list',
366
        '* details of image: /flavor info <image id>']
367

    
368
    @classmethod
369
    def connection(this, foo):
370
        return generic._connection(foo, 'image.url')
371

    
372
    @classmethod
373
    def id(this, foo):
374
        def _raise(self, *args, **kwargs):
375
            image_id = kwargs.get('image_id', None)
376
            try:
377
                foo(self, *args, **kwargs)
378
            except ClientError as ce:
379
                if image_id and (
380
                    ce.status == 404
381
                    or (
382
                        ce.status == 400
383
                        and 'image not found' in ('%s' % ce).lower())
384
                    or ce.status == 411
385
                ):
386
                        msg = 'No image with id %s found' % image_id
387
                        raiseCLIError(ce, msg, details=this.about_image_id)
388
                raise
389
        return _raise
390

    
391
    @classmethod
392
    def metadata(this, foo):
393
        def _raise(self, *args, **kwargs):
394
            key = kwargs.get('key', None)
395
            try:
396
                foo(self, *args, **kwargs)
397
            except ClientError as ce:
398
                ce_msg = ('%s' % ce).lower()
399
                if ce.status == 404 or (
400
                        ce.status == 400 and 'metadata' in ce_msg):
401
                    msg = 'No properties with key %s in this image' % key
402
                    raiseCLIError(ce, msg)
403
                raise
404
        return _raise
405

    
406

    
407
class pithos(object):
408
    container_howto = [
409
        'To specify a container:',
410
        '  1. Set store.container variable (permanent)',
411
        '     /config set store.container <container>',
412
        '  2. --container=<container> (temporary, overrides 1)',
413
        '  3. Use the container:path format (temporary, overrides all)',
414
        'For a list of containers: /store list']
415

    
416
    @classmethod
417
    def connection(this, foo):
418
        return generic._connection(foo, 'store.url')
419

    
420
    @classmethod
421
    def quota(this, foo):
422
        def _raise(self, *args, **kwargs):
423
            try:
424
                return foo(self, *args, **kwargs)
425
            except ClientError as ce:
426
                if ce.status == 413:
427
                    raiseCLIError(ce, 'User quota exceeded', details=[
428
                        '* get quotas:',
429
                        '  * upper total limit:      /store quota',
430
                        '  * container limit:  /store quota <container>',
431
                        '* set a higher quota (if permitted):',
432
                        '    /store setquota <quota>[unit] <container>'
433
                        '    as long as <container quota> <= <total quota>'])
434
                raise
435
        return _raise
436

    
437
    @classmethod
438
    def container(this, foo):
439
        def _raise(self, *args, **kwargs):
440
            dst_cont = kwargs.get('dst_cont', None)
441
            try:
442
                return foo(self, *args, **kwargs)
443
            except ClientError as ce:
444
                if ce.status == 404 and 'container' in ('%s' % ce).lower():
445
                        cont = ('%s or %s' % (
446
                            self.container,
447
                            dst_cont)) if dst_cont else self.container
448
                        msg = 'Is container %s in current account?' % (cont),
449
                        raiseCLIError(ce, msg, details=this.container_howto)
450
                raise
451
        return _raise
452

    
453
    @classmethod
454
    def local_path(this, foo):
455
        def _raise(self, *args, **kwargs):
456
            local_path = kwargs.get('local_path', '<None>')
457
            try:
458
                return foo(self, *args, **kwargs)
459
            except IOError as ioe:
460
                msg = 'Failed to access file %s' % local_path,
461
                raiseCLIError(ioe, msg, importance=2)
462
        return _raise
463

    
464
    @classmethod
465
    def object_path(this, foo):
466
        def _raise(self, *args, **kwargs):
467
            try:
468
                return foo(self, *args, **kwargs)
469
            except ClientError as ce:
470
                err_msg = ('%s' % ce).lower()
471
                if (
472
                    ce.status == 404 or ce.status == 500
473
                ) and 'object' in err_msg and 'not' in err_msg:
474
                    msg = 'No object %s in container %s' % (
475
                        self.path,
476
                        self.container)
477
                    raiseCLIError(ce, msg, details=this.container_howto)
478
                raise
479
        return _raise
480

    
481
    @classmethod
482
    def object_size(this, foo):
483
        def _raise(self, *args, **kwargs):
484
            size = kwargs.get('size', None)
485
            start = kwargs.get('start', 0)
486
            end = kwargs.get('end', 0)
487
            if size:
488
                try:
489
                    size = int(size)
490
                except ValueError as ve:
491
                    msg = 'Invalid file size %s ' % size
492
                    details = ['size must be a positive integer']
493
                    raiseCLIError(ve, msg, details=details, importance=1)
494
            else:
495
                try:
496
                    start = int(start)
497
                except ValueError as e:
498
                    msg = 'Invalid start value %s in range' % start,
499
                    details = ['size must be a positive integer'],
500
                    raiseCLIError(e, msg, details=details, importance=1)
501
                try:
502
                    end = int(end)
503
                except ValueError as e:
504
                    msg = 'Invalid end value %s in range' % end
505
                    details = ['size must be a positive integer']
506
                    raiseCLIError(e, msg, details=details, importance=1)
507
                if start > end:
508
                    raiseCLIError(
509
                        'Invalid range %s-%s' % (start, end),
510
                        details=['size must be a positive integer'],
511
                        importance=1)
512
                size = end - start
513
            try:
514
                return foo(self, *args, **kwargs)
515
            except ClientError as ce:
516
                err_msg = ('%s' % ce).lower()
517
                expected = 'object length is smaller than range length'
518
                if size and (
519
                    ce.status == 416 or (
520
                        ce.status == 400 and expected in err_msg)):
521
                    raiseCLIError(ce, 'Remote object %s:%s <= %s %s' % (
522
                        self.container, self.path, format_size(size),
523
                        ('(%sB)' % size) if size >= 1024 else ''))
524
                raise
525
        return _raise