Merge pull request #2790 from jsmartin/devel

Added a ring_wait option (waits for ring_ready to return true)
This commit is contained in:
Michael DeHaan 2013-04-26 15:26:44 -07:00
commit a10ed7793a

View file

@ -59,6 +59,13 @@ options:
default: null default: null
aliases: [] aliases: []
type: 'bool' type: 'bool'
wait_for_ring:
description:
- Waits for all nodes to agreee on the status of the ring
required: false
default: null
aliases: []
type: 'bool'
wait_for_service: wait_for_service:
description: description:
- Waits for a riak service to come online before continuing. - Waits for a riak service to come online before continuing.
@ -84,14 +91,6 @@ import json
import time import time
def _run(cmd):
# returns (rc, stdout, stderr) from shell command
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
return (process.returncode, stdout, stderr)
def is_number(s): def is_number(s):
try: try:
float(s) float(s)
@ -99,12 +98,18 @@ def is_number(s):
except ValueError: except ValueError:
return False return False
def ring_check():
rc, out, err = module.run_command('riak-admin ringready 2> /dev/null')
if rc == 0 and out.find('TRUE All nodes agree on the ring') != -1:
return True
else:
return False
def status_to_json(): def status_to_json():
# remove all unnecessary symbols and whitespace # remove all unnecessary symbols and whitespace
rc, out, err = _run("riak-admin status 2> /dev/null") rc, out, err = module.run_command("riak-admin status 2> /dev/null")
if rc == 0: if rc == 0:
stats = out raw_stats = out
else: else:
module.fail_json(msg="Could not properly gather stats") module.fail_json(msg="Could not properly gather stats")
@ -148,6 +153,7 @@ def main():
http_conn=dict(required=False, default='127.0.0.1:8098'), http_conn=dict(required=False, default='127.0.0.1:8098'),
target_node=dict(default='riak@127.0.0.1', required=False), target_node=dict(default='riak@127.0.0.1', required=False),
wait_for_handoffs=dict(default=False, type='bool'), wait_for_handoffs=dict(default=False, type='bool'),
wait_for_ring=dict(default=False, type='bool'),
wait_for_service=dict( wait_for_service=dict(
required=False, default=None, choices=['kv']) required=False, default=None, choices=['kv'])
) )
@ -160,6 +166,7 @@ def main():
http_conn = module.params.get('http_conn') http_conn = module.params.get('http_conn')
target_node = module.params.get('target_node') target_node = module.params.get('target_node')
wait_for_handoffs = module.params.get('wait_for_handoffs') wait_for_handoffs = module.params.get('wait_for_handoffs')
wait_for_ring = module.params.get('wait_for_ring')
wait_for_service = module.params.get('wait_for_service') wait_for_service = module.params.get('wait_for_service')
rc = 0 rc = 0
@ -168,11 +175,11 @@ def main():
#make sure riak commands are on the path #make sure riak commands are on the path
for item in ['riak', 'riak-admin']: for item in ['riak', 'riak-admin']:
rc, out, err = _run('which %s' % item) rc, out, err = module.run_command('which %s' % item)
if rc == 1: if rc == 1:
module.fail_json(msg='Could not find path to %s executable' % item) module.fail_json(msg='Could not find path to %s executable' % item)
rc, out, err = _run( rc, out, err = module.run_command(
"riak version 2> /dev/null |grep ^riak|cut -f2 -d' '|tr -d '('") "riak version 2> /dev/null |grep ^riak|cut -f2 -d' '|tr -d '('")
if rc == 0: if rc == 0:
version = out.strip() version = out.strip()
@ -203,14 +210,14 @@ def main():
'version': version} 'version': version}
if command == 'ping': if command == 'ping':
rc, out, err = _run('riak ping %s' % target_node) rc, out, err = module.run_command('riak ping %s' % target_node)
if rc == 0: if rc == 0:
result['ping'] = out result['ping'] = out
else: else:
module.fail_json(msg=out) module.fail_json(msg=out)
elif command == 'kv_test': elif command == 'kv_test':
rc, out, err = _run('riak-admin test') rc, out, err = module.run_command('riak-admin test')
if rc == 0: if rc == 0:
result['kv_test'] = out result['kv_test'] = out
else: else:
@ -220,7 +227,7 @@ def main():
if nodes.count(node_name) == 1 and len(nodes) > 1: if nodes.count(node_name) == 1 and len(nodes) > 1:
result['join'] = 'Node is already in cluster or staged to be in cluster.' result['join'] = 'Node is already in cluster or staged to be in cluster.'
else: else:
rc, out, err = _run('riak-admin cluster join %s' % target_node) rc, out, err = module.run_command('riak-admin cluster join %s' % target_node)
if rc == 0: if rc == 0:
result['join'] = out result['join'] = out
result['changed'] = True result['changed'] = True
@ -228,7 +235,7 @@ def main():
module.fail_json(msg=out) module.fail_json(msg=out)
elif command == 'plan': elif command == 'plan':
rc, out, err = _run('riak-admin cluster plan %s' % target_node) rc, out, err = module.run_command('riak-admin cluster plan %s' % target_node)
if rc == 0: if rc == 0:
result['plan'] = out result['plan'] = out
if out.find('Staged Changes') != -1: if out.find('Staged Changes') != -1:
@ -238,7 +245,7 @@ def main():
elif command == 'commit': elif command == 'commit':
rc, out, err = _run('riak-admin cluster commit %s' % target_node) rc, out, err = module.run_command('riak-admin cluster commit %s' % target_node)
if rc == 0: if rc == 0:
result['commit'] = out result['commit'] = out
changed = True changed = True
@ -253,24 +260,26 @@ def main():
# this could take a while, recommend to run in async mode # this could take a while, recommend to run in async mode
if wait_for_handoffs: if wait_for_handoffs:
while wait == 0: while wait == 0:
time.sleep(10) rc, out, err = module.run_command('riak-admin transfers 2> /dev/null')
rc, out, err = _run('riak-admin transfers 2> /dev/null')
if out.find('No transfers active') != -1: if out.find('No transfers active') != -1:
result['handoffs'] = 'No transfers active.' result['handoffs'] = 'No transfers active.'
break break
time.sleep(10)
# this could take a while, recommend to run in async mode # this could take a while, recommend to run in async mode
if wait_for_service: if wait_for_service:
rc, out, err = _run('riak-admin wait_for_service riak_%s %s' % ( rc, out, err = module.run_command('riak-admin wait_for_service riak_%s %s' % (
wait_for_service, node_name)) wait_for_service, node_name))
result['service'] = out result['service'] = out
rc, out, err = _run('riak-admin ringready 2> /dev/null') if wait_for_ring:
if rc == 0 and out.find('TRUE All nodes agree on the ring') != -1: while wait == 0:
result['ring_ready'] = True if ring_check():
else: break
result['ring_ready'] = False time.sleep(10)
result['ring_ready'] = ring_check()
module.exit_json(**result) module.exit_json(**result)