Implement wait for node primary/secondary in CLI

Use a different wait method of querying the node status every
half-second during the transition, in order to wait on the transition to
complete if desired.

Closes #72
This commit is contained in:
Joshua Boniface 2020-02-19 14:33:31 -05:00
parent d2a5fe59c0
commit 39ce704969
2 changed files with 45 additions and 4 deletions

View File

@ -78,8 +78,7 @@ def call_api(config, operation, request_uri, params=None, data=None, files=None)
data=data data=data
) )
except Exception as e: except Exception as e:
click.echo('Failed to connect to the API: {}'.format(e)) return False, 'Failed to connect to the API: {}'.format(e)
exit(1)
# Display debug output # Display debug output
if config['debug']: if config['debug']:

View File

@ -348,7 +348,11 @@ def cli_node():
@click.argument( @click.argument(
'node' 'node'
) )
def node_secondary(node): @click.option(
'-w', '--wait', 'wait', is_flag=True, default=False,
help='Wait for transition to complete before returning.'
)
def node_secondary(node, wait):
""" """
Take NODE out of primary router mode. Take NODE out of primary router mode.
""" """
@ -361,6 +365,23 @@ def node_secondary(node):
click.echo() click.echo()
retcode, retmsg = pvc_node.node_coordinator_state(config, node, 'secondary') retcode, retmsg = pvc_node.node_coordinator_state(config, node, 'secondary')
if not retcode:
cleanup(retcode, retmsg)
else:
click.echo(retmsg)
if wait:
click.echo("Waiting for state transition... ", nl=False)
# Every half-second, check if the API is reachable and the node is in secondary state
while True:
try:
_retcode, _retmsg = pvc_node.node_info(config, node)
if _retmsg['coordinator_state'] == 'secondary':
retmsg = "done."
break
else:
time.sleep(0.5)
except:
time.sleep(0.5)
cleanup(retcode, retmsg) cleanup(retcode, retmsg)
############################################################################### ###############################################################################
@ -370,7 +391,11 @@ def node_secondary(node):
@click.argument( @click.argument(
'node' 'node'
) )
def node_primary(node): @click.option(
'-w', '--wait', 'wait', is_flag=True, default=False,
help='Wait for transition to complete before returning.'
)
def node_primary(node, wait):
""" """
Put NODE into primary router mode. Put NODE into primary router mode.
""" """
@ -383,6 +408,23 @@ def node_primary(node):
click.echo() click.echo()
retcode, retmsg = pvc_node.node_coordinator_state(config, node, 'primary') retcode, retmsg = pvc_node.node_coordinator_state(config, node, 'primary')
if not retcode:
cleanup(retcode, retmsg)
else:
click.echo(retmsg)
if wait:
click.echo("Waiting for state transition... ", nl=False)
# Every half-second, check if the API is reachable and the node is in secondary state
while True:
try:
_retcode, _retmsg = pvc_node.node_info(config, node)
if _retmsg['coordinator_state'] == 'primary':
retmsg = "done."
break
else:
time.sleep(0.5)
except:
time.sleep(0.5)
cleanup(retcode, retmsg) cleanup(retcode, retmsg)
############################################################################### ###############################################################################