From 39ce7049698c8eab4b2e7f1a2961669f93855e91 Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Wed, 19 Feb 2020 14:33:31 -0500 Subject: [PATCH] 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 --- client-cli/cli_lib/common.py | 3 +-- client-cli/pvc.py | 46 ++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/client-cli/cli_lib/common.py b/client-cli/cli_lib/common.py index 06685ba4..5bf019ff 100644 --- a/client-cli/cli_lib/common.py +++ b/client-cli/cli_lib/common.py @@ -78,8 +78,7 @@ def call_api(config, operation, request_uri, params=None, data=None, files=None) data=data ) except Exception as e: - click.echo('Failed to connect to the API: {}'.format(e)) - exit(1) + return False, 'Failed to connect to the API: {}'.format(e) # Display debug output if config['debug']: diff --git a/client-cli/pvc.py b/client-cli/pvc.py index 39f068e6..a740861b 100755 --- a/client-cli/pvc.py +++ b/client-cli/pvc.py @@ -348,7 +348,11 @@ def cli_node(): @click.argument( '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. """ @@ -361,6 +365,23 @@ def node_secondary(node): click.echo() 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) ############################################################################### @@ -370,7 +391,11 @@ def node_secondary(node): @click.argument( '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. """ @@ -383,6 +408,23 @@ def node_primary(node): click.echo() 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) ###############################################################################