Commit 1cc2146c authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Merge branch 'develop' into feature-network-api

parents e8ba3e9f f261965c
......@@ -452,7 +452,8 @@ class ArgumentParseManager(object):
def __init__(
self, exe,
arguments=None, required=None, syntax=None, description=None):
arguments=None, required=None, syntax=None, description=None,
check_required=True):
"""
:param exe: (str) the basic command (e.g. 'kamaki')
......@@ -473,13 +474,15 @@ class ArgumentParseManager(object):
:param syntax: (str) The basic syntax of the arguments. Default:
exe <cmd_group> [<cmd_subbroup> ...] <cmd>
:param description: (str) The description of the commands or ''
:param check_required: (bool) Set to False inorder not to check for
required argument values while parsing
"""
self.parser = ArgumentParser(
add_help=False, formatter_class=RawDescriptionHelpFormatter)
self._exe = exe
self.syntax = syntax or (
'%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe)
self.required = required
self.required, self.check_required = required, check_required
self.parser.description = description or ''
if arguments:
self.arguments = arguments
......@@ -613,7 +616,7 @@ class ArgumentParseManager(object):
self.update_parser()
def _parse_required_arguments(self, required, parsed_args):
if not required:
if not (self.check_required and required):
return True
if isinstance(required, tuple):
for item in required:
......
......@@ -175,10 +175,9 @@ class Shell(Cmd):
tmp_args.pop('verbose', None)
tmp_args.pop('silent', None)
tmp_args.pop('config', None)
print 'YOLO >>>\n\t', '\n\t'.join([
('%s %s %s' % k, v, v.value) for k, v in args.items()])
help_parser = ArgumentParseManager(
cmd_name, tmp_args, required, syntax=syntax, description=descr)
cmd_name, tmp_args, required,
syntax=syntax, description=descr, check_required=False)
return help_parser.print_help
def _register_command(self, cmd_path):
......
......@@ -252,7 +252,7 @@ class OutputFormatArgument(ValueArgument):
@property
def value(self):
return self._value
return getattr(self, '_value', None)
@value.setter
def value(self, newvalue):
......
......@@ -60,14 +60,6 @@ about_authentication = '\nUser Authentication:\
\n* to set authentication token: /config set cloud.<cloud>.token <token>'
class _network_wait(_service_wait):
def _wait(self, net_id, current_status, timeout=60):
super(_network_wait, self)._wait(
'Network', net_id, self.client.wait_network, current_status,
timeout=timeout)
class _port_wait(_service_wait):
def _wait(self, port_id, current_status, timeout=60):
......@@ -76,14 +68,6 @@ class _port_wait(_service_wait):
timeout=timeout)
class _port_wait(_service_wait):
def _wait(self, net_id, current_status, timeout=60):
super(_network_wait, self)._wait(
'Network', net_id, self.client.wait_network, current_status,
timeout=timeout)
class _init_network(_command_init):
@errors.generic.all
@addLogSettings
......@@ -190,7 +174,7 @@ class NetworkTypeArgument(ValueArgument):
@command(network_cmds)
class network_create(_init_network, _optional_json, _network_wait):
class network_create(_init_network, _optional_json):
"""Create a new network"""
arguments = dict(
......@@ -199,8 +183,7 @@ class network_create(_init_network, _optional_json, _network_wait):
'Make network shared (special privileges required)', '--shared'),
network_type=NetworkTypeArgument(
'Valid network types: %s' % (', '.join(NetworkTypeArgument.types)),
'--type'),
wait=FlagArgument('Wait network to build', ('-w', '--wait')),
'--type')
)
required = ('network_type', )
......@@ -210,8 +193,6 @@ class network_create(_init_network, _optional_json, _network_wait):
def _run(self, network_type):
net = self.client.create_network(
network_type, name=self['name'], shared=self['shared'])
if self['wait']:
self._wait(net['id'], net['status'])
self._print(net, self.print_dict)
def main(self):
......@@ -254,33 +235,6 @@ class network_modify(_init_network, _optional_json):
self._run(network_id=network_id)
@command(network_cmds)
class network_wait(_init_network, _network_wait):
"""Wait for network to finish [PENDING, ACTIVE, DELETED]"""
arguments = dict(
timeout=IntArgument(
'Wait limit in seconds (default: 60)', '--timeout', default=60)
)
@errors.generic.all
@errors.cyclades.connection
@errors.cyclades.network_id
def _run(self, network_id, current_status):
net = self.client.get_network_details(network_id)
if net['status'].lower() == current_status.lower():
self._wait(network_id, current_status, timeout=self['timeout'])
else:
self.error(
'Network %s: Cannot wait for status %s, '
'status is already %s' % (
network_id, current_status, net['status']))
def main(self, network_id, current_status='PENDING'):
super(self.__class__, self)._run()
self._run(network_id=network_id, current_status=current_status)
@command(subnet_cmds)
class subnet_list(_init_network, _optional_json, _name_filter, _id_filter):
"""List subnets
......@@ -453,13 +407,19 @@ class port_info(_init_network, _optional_json):
@command(port_cmds)
class port_delete(_init_network, _optional_output_cmd):
class port_delete(_init_network, _optional_output_cmd, _port_wait):
"""Delete a port (== disconnect server from network)"""
arguments = dict(
wait=FlagArgument('Wait port to be established', ('-w', '--wait'))
)
@errors.generic.all
@errors.cyclades.connection
def _run(self, port_id):
r = self.client.delete_port(port_id)
if self['wait']:
self._wait(r['id'], r['status'])
self._optional_output(r)
def main(self, port_id):
......@@ -532,7 +492,7 @@ class port_create(_init_network, _optional_json, _port_wait):
@command(port_cmds)
class port_wait(_init_network, _port_wait):
"""Wait for port to finish [PENDING, ACTIVE, DELETED]"""
"""Wait for port to finish [ACTIVE, DOWN, BUILD, ERROR]"""
arguments = dict(
timeout=IntArgument(
......@@ -551,7 +511,7 @@ class port_wait(_init_network, _port_wait):
'status is already %s' % (
port_id, current_status, port['status']))
def main(self, port_id, current_status='PENDING'):
def main(self, port_id, current_status='BUILD'):
super(self.__class__, self)._run()
self._run(port_id=port_id, current_status=current_status)
......
......@@ -497,13 +497,13 @@ class Client(Logged):
class Waiter(object):
def _wait(
self, item_id, current_status, get_status,
delay=1, max_wait=100, wait_cb=None):
"""Wait for item while its status is current_status
self, item_id, wait_status, get_status,
delay=1, max_wait=100, wait_cb=None, wait_for_status=False):
"""Wait while the item is still in wait_status or to reach it
:param server_id: integer (str or int)
:param current_status: (str)
:param wait_status: (str)
:param get_status: (method(self, item_id)) if called, returns
(status, progress %) If no way to tell progress, return None
......@@ -513,6 +513,8 @@ class Waiter(object):
:param wait_cb: (method(total steps)) returns a generator for
reporting progress or timeouts i.e., for a progress bar
:param wait_for_status: (bool) wait FOR (True) or wait WHILE (False)
:returns: (str) the new mode if successful, (bool) False if timed out
"""
status, progress = get_status(self, item_id)
......@@ -521,16 +523,17 @@ class Waiter(object):
wait_gen = wait_cb(max_wait // delay)
wait_gen.next()
if status != current_status:
if wait_cb:
try:
wait_gen.next()
except Exception:
pass
if wait_for_status ^ (status != wait_status):
# if wait_cb:
# try:
# wait_gen.next()
# except Exception:
# pass
return status
old_wait = total_wait = 0
while status == current_status and total_wait <= max_wait:
while (wait_for_status ^ (status == wait_status)) and (
total_wait <= max_wait):
if wait_cb:
try:
for i in range(total_wait - old_wait):
......@@ -549,4 +552,18 @@ class Waiter(object):
wait_gen.next()
except:
pass
return status if status != current_status else False
return status if (wait_for_status ^ (status != wait_status)) else False
def wait_for(
self, item_id, target_status, get_status,
delay=1, max_wait=100, wait_cb=None):
self._wait(
item_id, target_status, get_status, delay, max_wait, wait_cb,
wait_for_status=True)
def wait_while(
self, item_id, target_status, get_status,
delay=1, max_wait=100, wait_cb=None):
self._wait(
item_id, target_status, get_status, delay, max_wait, wait_cb,
wait_for_status=False)
......@@ -365,31 +365,9 @@ class NetworkClient(NetworkRestClient, Waiter):
# Wait methods
def wait_network(
self, net_id,
current_status='PENDING', delay=1, max_wait=100, wait_cb=None):
def get_status(self, net_id):
r = self.get_network_details(net_id)
return r['status'], None
return self._wait(
net_id, current_status, get_status, delay, max_wait, wait_cb)
def wait_subnet(
self, subnet_id,
current_status='PENDING', delay=1, max_wait=100, wait_cb=None):
def get_status(self, subnet_id):
r = self.get_subnet_details(subnet_id)
return r['status'], None
return self._wait(
subnet_id, current_status, get_status, delay, max_wait, wait_cb)
def wait_port(
self, port_id,
current_status='PENDING', delay=1, max_wait=100, wait_cb=None):
current_status='BUILD', delay=1, max_wait=100, wait_cb=None):
def get_status(self, net_id):
r = self.get_port_details(port_id)
......@@ -397,15 +375,3 @@ class NetworkClient(NetworkRestClient, Waiter):
return self._wait(
port_id, current_status, get_status, delay, max_wait, wait_cb)
def wait_floatingip(
self, floatingip_id,
current_status='PENDING', delay=1, max_wait=100, wait_cb=None):
def get_status(self, floatingip_id):
r = self.get_network_details(floatingip_id)
return r['status'], None
return self._wait(
floatingip_id,
current_status, get_status, delay, max_wait, wait_cb)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment