root / kamaki / cli / commands / errors.py @ de73876b
History | View | Annotate | Download (20.6 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 |
if ce.status == 401: |
69 |
raiseCLIError(ce, 'Authorization failed', details=[
|
70 |
'Make sure a valid token is provided:',
|
71 |
' to check if token is valid: /astakos authenticate',
|
72 |
' to set token: /config set [.server.]token <token>',
|
73 |
' to get current token: /config get [server.]token'])
|
74 |
elif ce.status in range(-12, 200) + [302, 401, 403, 500]: |
75 |
raiseCLIError(ce, importance=3, details=[
|
76 |
'Check if service is up or set to url %s' % base_url,
|
77 |
' to get url: /config get %s' % base_url,
|
78 |
' to set url: /config set %s <URL>' % base_url])
|
79 |
elif ce.status == 404\ |
80 |
and 'kamakihttpresponse' in ('%s' % ce).lower(): |
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 = ['Maximum number of networks reached',
|
218 |
'* to get a list of networks: /network list',
|
219 |
'* to delete a network: /network delete <net id>']
|
220 |
raiseCLIError(ce, msg, details=details) |
221 |
raise
|
222 |
return _raise
|
223 |
|
224 |
@classmethod
|
225 |
def network_in_use(this, foo): |
226 |
def _raise(self, *args, **kwargs): |
227 |
network_id = kwargs.get('network_id', None) |
228 |
try:
|
229 |
return foo(self, *args, **kwargs) |
230 |
except ClientError as ce: |
231 |
if network_id and ce.status == 400: |
232 |
msg = 'Network with id %s does not exist' % network_id,
|
233 |
raiseCLIError(ce, msg, details=self.about_network_id)
|
234 |
elif network_id or ce.status == 421: |
235 |
msg = 'Network with id %s is in use' % network_id,
|
236 |
raiseCLIError(ce, msg, details=[ |
237 |
'Disconnect all nics/VMs of this network first',
|
238 |
'* to get nics: /network info %s' % network_id,
|
239 |
'. (under "attachments" section)',
|
240 |
'* to disconnect: /network disconnect <nic id>'])
|
241 |
raise
|
242 |
return _raise
|
243 |
|
244 |
@classmethod
|
245 |
def flavor_id(this, foo): |
246 |
def _raise(self, *args, **kwargs): |
247 |
flavor_id = kwargs.get('flavor_id', None) |
248 |
try:
|
249 |
flavor_id = int(flavor_id)
|
250 |
return foo(self, *args, **kwargs) |
251 |
except ValueError as ve: |
252 |
msg = 'Invalid flavor id %s ' % flavor_id,
|
253 |
details = 'Flavor id must be a positive integer',
|
254 |
raiseCLIError(ve, msg, details=details, importance=1)
|
255 |
except ClientError as ce: |
256 |
if flavor_id and ce.status == 404 and ( |
257 |
'flavor' in ('%s' % ce).lower() |
258 |
): |
259 |
msg = 'No flavor with id %s found' % flavor_id,
|
260 |
raiseCLIError(ce, msg, details=this.about_flavor_id) |
261 |
raise
|
262 |
return _raise
|
263 |
|
264 |
@classmethod
|
265 |
def server_id(this, foo): |
266 |
def _raise(self, *args, **kwargs): |
267 |
server_id = kwargs.get('server_id', None) |
268 |
try:
|
269 |
server_id = int(server_id)
|
270 |
return foo(self, *args, **kwargs) |
271 |
except ValueError as ve: |
272 |
msg = 'Invalid server(VM) id %s' % server_id,
|
273 |
details = ['id must be a positive integer'],
|
274 |
raiseCLIError(ve, msg, details=details, importance=1)
|
275 |
except ClientError as ce: |
276 |
err_msg = ('%s' % ce).lower()
|
277 |
if (
|
278 |
ce.status == 404 and 'server' in err_msg |
279 |
) or (
|
280 |
ce.status == 400 and 'not found' in err_msg |
281 |
): |
282 |
msg = 'server(VM) with id %s not found' % server_id,
|
283 |
raiseCLIError(ce, msg, details=[ |
284 |
'* to get existing VM ids: /server list',
|
285 |
'* to get VM details: /server info <VM id>'])
|
286 |
raise
|
287 |
return _raise
|
288 |
|
289 |
@classmethod
|
290 |
def firewall(this, foo): |
291 |
def _raise(self, *args, **kwargs): |
292 |
profile = kwargs.get('profile', None) |
293 |
try:
|
294 |
return foo(self, *args, **kwargs) |
295 |
except ClientError as ce: |
296 |
if ce.status == 400 and profile and ( |
297 |
'firewall' in ('%s' % ce).lower() |
298 |
): |
299 |
msg = '%s is an invalid firewall profile term' % profile
|
300 |
raiseCLIError(ce, msg, details=[ |
301 |
'Try one of the following:',
|
302 |
'* DISABLED: Shutdown firewall',
|
303 |
'* ENABLED: Firewall in normal mode',
|
304 |
'* PROTECTED: Firewall in secure mode'])
|
305 |
raise
|
306 |
return _raise
|
307 |
|
308 |
@classmethod
|
309 |
def nic_id(this, foo): |
310 |
def _raise(self, *args, **kwargs): |
311 |
try:
|
312 |
return foo(self, *args, **kwargs) |
313 |
except ClientError as ce: |
314 |
nic_id = kwargs.get('nic_id', None) |
315 |
if nic_id and ce.status == 404 and ( |
316 |
'network interface' in ('%s' % ce).lower() |
317 |
): |
318 |
server_id = kwargs.get('server_id', '<no server>') |
319 |
err_msg = 'No nic %s on server(VM) with id %s' % (
|
320 |
nic_id, |
321 |
server_id) |
322 |
raiseCLIError(ce, err_msg, details=[ |
323 |
'* check server(VM) with id %s: /server info %s' % (
|
324 |
server_id, |
325 |
server_id), |
326 |
'* list nics for server(VM) with id %s:' % server_id,
|
327 |
' /server addr %s' % server_id])
|
328 |
raise
|
329 |
return _raise
|
330 |
|
331 |
@classmethod
|
332 |
def nic_format(this, foo): |
333 |
def _raise(self, *args, **kwargs): |
334 |
try:
|
335 |
return foo(self, *args, **kwargs) |
336 |
except IndexError as ie: |
337 |
nic_id = kwargs.get('nic_id', None) |
338 |
msg = 'Invalid format for network interface (nic) %s' % nic_id
|
339 |
raiseCLIError(ie, msg, importance=1, details=[
|
340 |
'nid_id format: nic-<server id>-<nic id>',
|
341 |
'* get nics of a network: /network info <net id>',
|
342 |
' (listed the "attachments" section)'])
|
343 |
return _raise
|
344 |
|
345 |
@classmethod
|
346 |
def metadata(this, foo): |
347 |
def _raise(self, *args, **kwargs): |
348 |
key = kwargs.get('key', None) |
349 |
try:
|
350 |
foo(self, *args, **kwargs)
|
351 |
except ClientError as ce: |
352 |
if key and ce.status == 404 and ( |
353 |
'metadata' in ('%s' % ce).lower() |
354 |
): |
355 |
raiseCLIError(ce, 'No VM metadata with key %s' % key)
|
356 |
raise
|
357 |
return _raise
|
358 |
|
359 |
|
360 |
class plankton(object): |
361 |
|
362 |
about_image_id = [ |
363 |
'How to pick a suitable image:',
|
364 |
'* get a list of image ids: /image list',
|
365 |
'* details of image: /flavor info <image id>']
|
366 |
|
367 |
@classmethod
|
368 |
def connection(this, foo): |
369 |
return generic._connection(foo, 'image.url') |
370 |
|
371 |
@classmethod
|
372 |
def id(this, foo): |
373 |
def _raise(self, *args, **kwargs): |
374 |
image_id = kwargs.get('image_id', None) |
375 |
try:
|
376 |
foo(self, *args, **kwargs)
|
377 |
except ClientError as ce: |
378 |
if image_id and ( |
379 |
ce.status == 404
|
380 |
or (
|
381 |
ce.status == 400
|
382 |
and 'image not found' in ('%s' % ce).lower()) |
383 |
or ce.status == 411 |
384 |
): |
385 |
msg = 'No image with id %s found' % image_id
|
386 |
raiseCLIError(ce, msg, details=this.about_image_id) |
387 |
raise
|
388 |
return _raise
|
389 |
|
390 |
@classmethod
|
391 |
def metadata(this, foo): |
392 |
def _raise(self, *args, **kwargs): |
393 |
key = kwargs.get('key', None) |
394 |
try:
|
395 |
foo(self, *args, **kwargs)
|
396 |
except ClientError as ce: |
397 |
if ce.status == 404 or ( |
398 |
(ce.status == 400 and 'metadata' in ('%s' % ce).lower())): |
399 |
msg = 'No properties with key %s in this image' % key
|
400 |
raiseCLIError(ce, msg) |
401 |
raise
|
402 |
return _raise
|
403 |
|
404 |
|
405 |
class pithos(object): |
406 |
container_howto = [ |
407 |
"" 'To specify a container:', |
408 |
' 1. Set store.container variable (permanent)',
|
409 |
' /config set store.container <container>',
|
410 |
' 2. --container=<container> (temporary, overrides 1)',
|
411 |
' 3. Use the container:path format (temporary, overrides all)',
|
412 |
'For a list of containers: /store list']
|
413 |
|
414 |
@classmethod
|
415 |
def connection(this, foo): |
416 |
return generic._connection(foo, 'store.url') |
417 |
|
418 |
@classmethod
|
419 |
def quota(this, foo): |
420 |
def _raise(self, *args, **kwargs): |
421 |
try:
|
422 |
return foo(self, *args, **kwargs) |
423 |
except ClientError as ce: |
424 |
if ce.status == 413: |
425 |
raiseCLIError(ce, 'User quota exceeded', details=[
|
426 |
'* get quotas:',
|
427 |
' * upper total limit: /store quota',
|
428 |
' * container limit: /store quota <container>',
|
429 |
'* set a higher quota (if permitted):',
|
430 |
' /store setquota <quota>[unit] <container>'
|
431 |
' as long as <container quota> <= <total quota>'])
|
432 |
raise
|
433 |
return _raise
|
434 |
|
435 |
@classmethod
|
436 |
def container(this, foo): |
437 |
def _raise(self, *args, **kwargs): |
438 |
dst_cont = kwargs.get('dst_cont', None) |
439 |
try:
|
440 |
return foo(self, *args, **kwargs) |
441 |
except ClientError as ce: |
442 |
if ce.status == 404 and 'container' in ('%s' % ce).lower(): |
443 |
cont = ('%s or %s' % (
|
444 |
self.container,
|
445 |
dst_cont)) if dst_cont else self.container |
446 |
msg = 'Is container %s in current account?' % (cont),
|
447 |
raiseCLIError(ce, msg, details=this.container_howto) |
448 |
raise
|
449 |
return _raise
|
450 |
|
451 |
@classmethod
|
452 |
def local_path(this, foo): |
453 |
def _raise(self, *args, **kwargs): |
454 |
local_path = kwargs.get('local_path', '<None>') |
455 |
try:
|
456 |
return foo(self, *args, **kwargs) |
457 |
except IOError as ioe: |
458 |
msg = 'Failed to access file %s' % local_path,
|
459 |
raiseCLIError(ioe, msg, importance=2)
|
460 |
return _raise
|
461 |
|
462 |
@classmethod
|
463 |
def object_path(this, foo): |
464 |
def _raise(self, *args, **kwargs): |
465 |
try:
|
466 |
return foo(self, *args, **kwargs) |
467 |
except ClientError as ce: |
468 |
err_msg = ('%s' % ce).lower()
|
469 |
if (
|
470 |
ce.status == 404 or ce.status == 500 |
471 |
) and 'object' in err_msg and 'not' in err_msg: |
472 |
msg = 'No object %s in container %s' % (
|
473 |
self.path,
|
474 |
self.container)
|
475 |
raiseCLIError(ce, msg, details=this.container_howto) |
476 |
raise
|
477 |
return _raise
|
478 |
|
479 |
@classmethod
|
480 |
def object_size(this, foo): |
481 |
def _raise(self, *args, **kwargs): |
482 |
size = kwargs.get('size', None) |
483 |
start = kwargs.get('start', 0) |
484 |
end = kwargs.get('end', 0) |
485 |
if size:
|
486 |
try:
|
487 |
size = int(size)
|
488 |
except ValueError as ve: |
489 |
msg = 'Invalid file size %s ' % size
|
490 |
details = ['size must be a positive integer']
|
491 |
raiseCLIError(ve, msg, details=details, importance=1)
|
492 |
else:
|
493 |
try:
|
494 |
start = int(start)
|
495 |
except ValueError as e: |
496 |
msg = 'Invalid start value %s in range' % start,
|
497 |
details = ['size must be a positive integer'],
|
498 |
raiseCLIError(e, msg, details=details, importance=1)
|
499 |
try:
|
500 |
end = int(end)
|
501 |
except ValueError as e: |
502 |
msg = 'Invalid end value %s in range' % end
|
503 |
details = ['size must be a positive integer']
|
504 |
raiseCLIError(e, msg, details=details, importance=1)
|
505 |
if start > end:
|
506 |
raiseCLIError( |
507 |
'Invalid range %s-%s' % (start, end),
|
508 |
details=['size must be a positive integer'],
|
509 |
importance=1)
|
510 |
size = end - start |
511 |
try:
|
512 |
return foo(self, *args, **kwargs) |
513 |
except ClientError as ce: |
514 |
err_msg = ('%s' % ce).lower()
|
515 |
if size and (ce.status == 416 or |
516 |
(ce.status == 400 and |
517 |
'object length is smaller than range length' in err_msg)): |
518 |
raiseCLIError(ce, 'Remote object %s:%s <= %s %s' % (
|
519 |
self.container,
|
520 |
self.path,
|
521 |
format_size(size), |
522 |
('(%sB)' % size) if size >= 1024 else '')) |
523 |
raise
|
524 |
return _raise
|