Revision c3d42104
b/kamaki/cli/argument/__init__.py | ||
---|---|---|
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 | 34 |
from kamaki.cli.config import Config |
35 |
from kamaki.cli.errors import CLISyntaxError, raiseCLIError |
|
35 |
from kamaki.cli.errors import ( |
|
36 |
CLISyntaxError, raiseCLIError, CLIInvalidArgument) |
|
36 | 37 |
from kamaki.cli.utils import split_input, to_bytes |
37 | 38 |
|
38 | 39 |
from datetime import datetime as dtm |
... | ... | |
375 | 376 |
raiseCLIError(e, 'KeyValueArgument Syntax Error') |
376 | 377 |
|
377 | 378 |
|
379 |
class StatusArgument(ValueArgument): |
|
380 |
"""Initialize with valid_states=['list', 'of', 'states'] |
|
381 |
First state is the default""" |
|
382 |
|
|
383 |
def __init__(self, *args, **kwargs): |
|
384 |
self.valid_states = kwargs.pop('valid_states', ['BUILD', ]) |
|
385 |
super(StatusArgument, self).__init__(*args, **kwargs) |
|
386 |
|
|
387 |
@property |
|
388 |
def value(self): |
|
389 |
return getattr(self, '_value', None) |
|
390 |
|
|
391 |
@value.setter |
|
392 |
def value(self, new_status): |
|
393 |
if new_status: |
|
394 |
new_status = new_status.upper() |
|
395 |
if new_status not in self.valid_states: |
|
396 |
raise CLIInvalidArgument( |
|
397 |
'Invalid argument %s' % new_status, details=[ |
|
398 |
'Usage: ' |
|
399 |
'%s=[%s]' % (self.lvalue, '|'.join(self.valid_states))]) |
|
400 |
self._value = new_status |
|
401 |
|
|
402 |
|
|
378 | 403 |
class ProgressBarArgument(FlagArgument): |
379 | 404 |
"""Manage a progress bar""" |
380 | 405 |
|
b/kamaki/cli/commands/cyclades.py | ||
---|---|---|
44 | 44 |
from kamaki.clients.cyclades import CycladesClient |
45 | 45 |
from kamaki.cli.argument import ( |
46 | 46 |
FlagArgument, ValueArgument, KeyValueArgument, RepeatableArgument, |
47 |
ProgressBarArgument, DateArgument, IntArgument) |
|
47 |
ProgressBarArgument, DateArgument, IntArgument, StatusArgument)
|
|
48 | 48 |
from kamaki.cli.commands import _command_init, errors, addLogSettings |
49 | 49 |
from kamaki.cli.commands import ( |
50 | 50 |
_optional_output_cmd, _optional_json, _name_filter, _id_filter) |
... | ... | |
69 | 69 |
' [mode=]MODE: permission in octal (e.g., 0777)', |
70 | 70 |
'e.g., -p /tmp/my.file,owner=root,mode=0777'] |
71 | 71 |
|
72 |
server_states = ('BUILD', 'ACTIVE', 'STOPPED', 'REBOOT') |
|
73 |
|
|
72 | 74 |
|
73 | 75 |
class _service_wait(object): |
74 | 76 |
|
... | ... | |
277 | 279 |
@errors.cyclades.connection |
278 | 280 |
@errors.cyclades.server_id |
279 | 281 |
def _run(self, server_id): |
280 |
vm = self.client.get_server_nics(server_id) |
|
281 | 282 |
if self['nics']: |
282 |
self._print(vm.get('attachments', [])) |
|
283 |
self._print( |
|
284 |
self.client.get_server_nics(server_id), self.print_dict) |
|
283 | 285 |
elif self['network_id']: |
284 | 286 |
self._print( |
285 | 287 |
self.client.get_server_network_nics( |
... | ... | |
290 | 292 |
elif self['diagnostics']: |
291 | 293 |
self._print(self.client.get_server_diagnostics(server_id)) |
292 | 294 |
else: |
295 |
vm = self.client.get_server_details(server_id) |
|
293 | 296 |
uuids = self._uuids2usernames([vm['user_id'], vm['tenant_id']]) |
294 | 297 |
vm['user_id'] += ' (%s)' % uuids[vm['user_id']] |
295 | 298 |
vm['tenant_id'] += ' (%s)' % uuids[vm['tenant_id']] |
... | ... | |
297 | 300 |
|
298 | 301 |
def main(self, server_id): |
299 | 302 |
super(self.__class__, self)._run() |
300 |
choose_one = ('nics', 'vnc', 'stats')
|
|
303 |
choose_one = ('nics', 'stats', 'diagnostics')
|
|
301 | 304 |
count = len([a for a in choose_one if self[a]]) |
302 | 305 |
if count > 1: |
303 | 306 |
raise CLIInvalidArgument('Invalid argument compination', details=[ |
... | ... | |
775 | 778 |
|
776 | 779 |
@command(server_cmds) |
777 | 780 |
class server_wait(_init_cyclades, _server_wait): |
778 |
"""Wait for server to finish (BUILD, STOPPED, REBOOT, ACTIVE)"""
|
|
781 |
"""Wait for server to change its status (default: BUILD)"""
|
|
779 | 782 |
|
780 | 783 |
arguments = dict( |
781 | 784 |
timeout=IntArgument( |
782 |
'Wait limit in seconds (default: 60)', '--timeout', default=60) |
|
785 |
'Wait limit in seconds (default: 60)', '--timeout', default=60), |
|
786 |
server_status=StatusArgument( |
|
787 |
'Status to wait for (%s, default: %s)' % ( |
|
788 |
', '.join(server_states), server_states[0]), |
|
789 |
'--status', |
|
790 |
valid_states=server_states) |
|
783 | 791 |
) |
784 | 792 |
|
785 | 793 |
@errors.generic.all |
... | ... | |
795 | 803 |
'status is already %s' % ( |
796 | 804 |
server_id, current_status, r['status'])) |
797 | 805 |
|
798 |
def main(self, server_id, current_status='BUILD'):
|
|
806 |
def main(self, server_id): |
|
799 | 807 |
super(self.__class__, self)._run() |
800 |
self._run(server_id=server_id, current_status=current_status)
|
|
808 |
self._run(server_id=server_id, current_status=self['server_status'])
|
|
801 | 809 |
|
802 | 810 |
|
803 | 811 |
@command(flavor_cmds) |
b/kamaki/cli/commands/network.py | ||
---|---|---|
40 | 40 |
CLIBaseUrlError, CLIInvalidArgument, raiseCLIError) |
41 | 41 |
from kamaki.clients.cyclades import CycladesNetworkClient, ClientError |
42 | 42 |
from kamaki.cli.argument import ( |
43 |
FlagArgument, ValueArgument, RepeatableArgument, IntArgument) |
|
43 |
FlagArgument, ValueArgument, RepeatableArgument, IntArgument, |
|
44 |
StatusArgument) |
|
44 | 45 |
from kamaki.cli.commands import _command_init, errors, addLogSettings |
45 | 46 |
from kamaki.cli.commands import ( |
46 | 47 |
_optional_output_cmd, _optional_json, _name_filter, _id_filter) |
... | ... | |
59 | 60 |
\n to set authentication token: \ |
60 | 61 |
[kamaki] config set cloud.<CLOUD>.token <TOKEN>' |
61 | 62 |
|
63 |
port_states = ('BUILD', 'ACTIVE', 'DOWN', 'ERROR') |
|
64 |
|
|
62 | 65 |
|
63 | 66 |
class _port_wait(_service_wait): |
64 | 67 |
|
... | ... | |
484 | 487 |
self._run(port_id=port_id) |
485 | 488 |
|
486 | 489 |
|
487 |
class PortStatusArgument(ValueArgument): |
|
488 |
|
|
489 |
valid = ('BUILD', 'ACTIVE', 'DOWN', 'ERROR') |
|
490 |
|
|
491 |
@property |
|
492 |
def value(self): |
|
493 |
return getattr(self, '_value', None) |
|
494 |
|
|
495 |
@value.setter |
|
496 |
def value(self, new_status): |
|
497 |
if new_status: |
|
498 |
new_status = new_status.upper() |
|
499 |
if new_status in self.valid: |
|
500 |
raise CLIInvalidArgument( |
|
501 |
'Invalid argument %s' % new_status, details=[ |
|
502 |
'Status valid values: %s'] % ', '.join(self.valid)) |
|
503 |
self._value = new_status |
|
504 |
|
|
505 |
|
|
506 | 490 |
class _port_create(_init_network, _optional_json, _port_wait): |
507 | 491 |
|
508 | 492 |
def connect(self, network_id, device_id): |
... | ... | |
557 | 541 |
|
558 | 542 |
@command(port_cmds) |
559 | 543 |
class port_wait(_init_network, _port_wait): |
560 |
"""Wait for port to finish [ACTIVE, DOWN, BUILD, ERROR]"""
|
|
544 |
"""Wait for port to finish (default: BUILD)"""
|
|
561 | 545 |
|
562 | 546 |
arguments = dict( |
563 |
current_status=PortStatusArgument( |
|
564 |
'Wait while in this status', '--status'), |
|
547 |
port_status=StatusArgument( |
|
548 |
'Wait while in this status (%s, default: %s)' % ( |
|
549 |
', '.join(port_states), port_states[0]), |
|
550 |
'--status', |
|
551 |
valid_states=port_states), |
|
565 | 552 |
timeout=IntArgument( |
566 | 553 |
'Wait limit in seconds (default: 60)', '--timeout', default=60) |
567 | 554 |
) |
568 | 555 |
|
569 | 556 |
@errors.generic.all |
570 | 557 |
@errors.cyclades.connection |
571 |
def _run(self, port_id, current_status):
|
|
558 |
def _run(self, port_id, port_status):
|
|
572 | 559 |
port = self.client.get_port_details(port_id) |
573 |
if port['status'].lower() == current_status.lower():
|
|
574 |
self._wait(port_id, current_status, timeout=self['timeout'])
|
|
560 |
if port['status'].lower() == port_status.lower():
|
|
561 |
self._wait(port_id, port_status, timeout=self['timeout'])
|
|
575 | 562 |
else: |
576 | 563 |
self.error( |
577 | 564 |
'Port %s: Cannot wait for status %s, ' |
578 | 565 |
'status is already %s' % ( |
579 |
port_id, current_status, port['status']))
|
|
566 |
port_id, port_status, port['status']))
|
|
580 | 567 |
|
581 | 568 |
def main(self, port_id): |
582 | 569 |
super(self.__class__, self)._run() |
583 |
current_status = self['current_status'] or self.arguments[ |
|
584 |
'current_status'].valid[0] |
|
585 |
self._run(port_id=port_id, current_status=current_status) |
|
570 |
port_status = self['port_status'] or port_states[0] |
|
571 |
self._run(port_id=port_id, port_status=port_status) |
|
586 | 572 |
|
587 | 573 |
|
588 | 574 |
@command(ip_cmds) |
Also available in: Unified diff