2014-04-01 18:54:37 +00:00
|
|
|
#!/usr/bin/python
|
2013-04-08 19:41:12 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, James Martin <jmartin@basho.com>, Drew Kerrigan <dkerrigan@basho.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
2013-04-13 02:25:02 +00:00
|
|
|
module: riak
|
2013-04-08 19:41:12 +00:00
|
|
|
short_description: This module handles some common Riak operations
|
|
|
|
description:
|
|
|
|
- This module can be used to join nodes to a cluster, check
|
|
|
|
the status of the cluster.
|
|
|
|
version_added: "1.2"
|
|
|
|
options:
|
|
|
|
command:
|
|
|
|
description:
|
|
|
|
- The command you would like to perform against the cluster.
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
choices: ['ping', 'kv_test', 'join', 'plan', 'commit']
|
|
|
|
config_dir:
|
|
|
|
description:
|
|
|
|
- The path to the riak configuration directory
|
|
|
|
required: false
|
|
|
|
default: /etc/riak
|
|
|
|
aliases: []
|
|
|
|
http_conn:
|
|
|
|
description:
|
|
|
|
- The ip address and port that is listening for Riak HTTP queries
|
|
|
|
required: false
|
|
|
|
default: 127.0.0.1:8098
|
|
|
|
aliases: []
|
|
|
|
target_node:
|
|
|
|
description:
|
|
|
|
- The target node for certain operations (join, ping)
|
|
|
|
required: false
|
|
|
|
default: riak@127.0.0.1
|
|
|
|
aliases: []
|
|
|
|
wait_for_handoffs:
|
|
|
|
description:
|
2013-05-27 21:24:10 +00:00
|
|
|
- Number of seconds to wait for handoffs to complete.
|
2013-04-08 19:41:12 +00:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2013-05-27 21:24:10 +00:00
|
|
|
type: 'int'
|
2013-04-26 21:16:47 +00:00
|
|
|
wait_for_ring:
|
|
|
|
description:
|
2013-05-27 21:24:10 +00:00
|
|
|
- Number of seconds to wait for all nodes to agree on the ring.
|
2013-04-26 21:16:47 +00:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2013-05-27 21:24:10 +00:00
|
|
|
type: 'int'
|
2013-04-08 19:41:12 +00:00
|
|
|
wait_for_service:
|
|
|
|
description:
|
|
|
|
- Waits for a riak service to come online before continuing.
|
|
|
|
required: false
|
2013-07-03 21:17:42 +00:00
|
|
|
default: None
|
2013-04-08 19:41:12 +00:00
|
|
|
aliases: []
|
|
|
|
choices: ['kv']
|
2014-03-12 15:19:22 +00:00
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: ['yes', 'no']
|
|
|
|
version_added: 1.5.1
|
2013-04-08 19:41:12 +00:00
|
|
|
'''
|
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Join's a Riak node to another node
|
|
|
|
- riak: command=join target_node=riak@10.1.1.1
|
|
|
|
|
|
|
|
# Wait for handoffs to finish. Use with async and poll.
|
|
|
|
- riak: wait_for_handoffs=yes
|
|
|
|
|
|
|
|
# Wait for riak_kv service to startup
|
|
|
|
- riak: wait_for_service=kv
|
|
|
|
'''
|
2013-04-08 19:41:12 +00:00
|
|
|
|
|
|
|
import urllib2
|
|
|
|
import time
|
2013-07-03 21:17:42 +00:00
|
|
|
import socket
|
2013-09-05 20:40:36 +00:00
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
2013-04-08 19:41:12 +00:00
|
|
|
|
2013-07-03 21:17:42 +00:00
|
|
|
def ring_check(module, riak_admin_bin):
|
2014-03-12 21:38:23 +00:00
|
|
|
cmd = '%s ringready' % riak_admin_bin
|
2013-07-03 21:17:42 +00:00
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
if rc == 0 and 'TRUE All nodes agree on the ring' in out:
|
2013-04-26 21:16:47 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2013-04-08 19:41:12 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
|
2013-07-03 21:17:42 +00:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2013-04-08 19:41:12 +00:00
|
|
|
command=dict(required=False, default=None, choices=[
|
|
|
|
'ping', 'kv_test', 'join', 'plan', 'commit']),
|
|
|
|
config_dir=dict(default='/etc/riak'),
|
|
|
|
http_conn=dict(required=False, default='127.0.0.1:8098'),
|
|
|
|
target_node=dict(default='riak@127.0.0.1', required=False),
|
2013-05-27 21:24:10 +00:00
|
|
|
wait_for_handoffs=dict(default=False, type='int'),
|
|
|
|
wait_for_ring=dict(default=False, type='int'),
|
2013-04-08 19:41:12 +00:00
|
|
|
wait_for_service=dict(
|
2014-03-13 01:26:16 +00:00
|
|
|
required=False, default=None, choices=['kv']),
|
|
|
|
validate_certs = dict(default='yes', type='bool'))
|
2013-04-08 19:41:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
command = module.params.get('command')
|
|
|
|
config_dir = module.params.get('config_dir')
|
|
|
|
http_conn = module.params.get('http_conn')
|
|
|
|
target_node = module.params.get('target_node')
|
|
|
|
wait_for_handoffs = module.params.get('wait_for_handoffs')
|
2013-04-26 21:16:47 +00:00
|
|
|
wait_for_ring = module.params.get('wait_for_ring')
|
2013-04-08 19:41:12 +00:00
|
|
|
wait_for_service = module.params.get('wait_for_service')
|
2014-03-13 01:26:16 +00:00
|
|
|
validate_certs = module.params.get('validate_certs')
|
2013-04-08 19:41:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
#make sure riak commands are on the path
|
2013-07-03 21:17:42 +00:00
|
|
|
riak_bin = module.get_bin_path('riak')
|
|
|
|
riak_admin_bin = module.get_bin_path('riak-admin')
|
2013-04-08 19:41:12 +00:00
|
|
|
|
2013-05-27 21:12:13 +00:00
|
|
|
timeout = time.time() + 120
|
|
|
|
while True:
|
2013-06-01 16:29:03 +00:00
|
|
|
if time.time() > timeout:
|
2013-05-27 21:12:13 +00:00
|
|
|
module.fail_json(msg='Timeout, could not fetch Riak stats.')
|
2014-03-10 21:06:52 +00:00
|
|
|
(response, info) = fetch_url(module, 'http://%s/stats' % (http_conn), force=True, timeout=5)
|
|
|
|
if info['status'] == 200:
|
|
|
|
stats_raw = response.read()
|
2013-05-27 21:12:13 +00:00
|
|
|
break
|
2014-03-10 21:06:52 +00:00
|
|
|
time.sleep(5)
|
2013-05-27 21:12:13 +00:00
|
|
|
|
2014-03-10 21:06:52 +00:00
|
|
|
# here we attempt to load those stats,
|
2013-04-08 19:41:12 +00:00
|
|
|
try:
|
2013-05-27 21:12:13 +00:00
|
|
|
stats = json.loads(stats_raw)
|
|
|
|
except:
|
2013-07-03 21:17:42 +00:00
|
|
|
module.fail_json(msg='Could not parse Riak stats.')
|
2013-04-08 19:41:12 +00:00
|
|
|
|
|
|
|
node_name = stats['nodename']
|
|
|
|
nodes = stats['ring_members']
|
|
|
|
ring_size = stats['ring_creation_size']
|
2014-02-19 17:12:02 +00:00
|
|
|
rc, out, err = module.run_command([riak_bin, 'version'] )
|
|
|
|
version = out.strip()
|
|
|
|
|
2013-07-03 21:17:42 +00:00
|
|
|
result = dict(node_name=node_name,
|
|
|
|
nodes=nodes,
|
|
|
|
ring_size=ring_size,
|
|
|
|
version=version)
|
2013-04-08 19:41:12 +00:00
|
|
|
|
|
|
|
if command == 'ping':
|
2013-07-03 21:17:42 +00:00
|
|
|
cmd = '%s ping %s' % ( riak_bin, target_node )
|
|
|
|
rc, out, err = module.run_command(cmd)
|
2013-04-08 19:41:12 +00:00
|
|
|
if rc == 0:
|
|
|
|
result['ping'] = out
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=out)
|
|
|
|
|
|
|
|
elif command == 'kv_test':
|
2013-07-03 21:17:42 +00:00
|
|
|
cmd = '%s test' % riak_admin_bin
|
|
|
|
rc, out, err = module.run_command(cmd)
|
2013-04-08 19:41:12 +00:00
|
|
|
if rc == 0:
|
|
|
|
result['kv_test'] = out
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=out)
|
|
|
|
|
|
|
|
elif command == 'join':
|
|
|
|
if nodes.count(node_name) == 1 and len(nodes) > 1:
|
|
|
|
result['join'] = 'Node is already in cluster or staged to be in cluster.'
|
|
|
|
else:
|
2013-07-03 21:17:42 +00:00
|
|
|
cmd = '%s cluster join %s' % (riak_admin_bin, target_node)
|
|
|
|
rc, out, err = module.run_command(cmd)
|
2013-04-08 19:41:12 +00:00
|
|
|
if rc == 0:
|
|
|
|
result['join'] = out
|
|
|
|
result['changed'] = True
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=out)
|
|
|
|
|
|
|
|
elif command == 'plan':
|
2013-07-03 21:17:42 +00:00
|
|
|
cmd = '%s cluster plan' % riak_admin_bin
|
|
|
|
rc, out, err = module.run_command(cmd)
|
2013-04-08 19:41:12 +00:00
|
|
|
if rc == 0:
|
|
|
|
result['plan'] = out
|
2013-07-03 21:17:42 +00:00
|
|
|
if 'Staged Changes' in out:
|
2013-04-08 19:41:12 +00:00
|
|
|
result['changed'] = True
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=out)
|
|
|
|
|
|
|
|
elif command == 'commit':
|
2013-07-03 21:17:42 +00:00
|
|
|
cmd = '%s cluster commit' % riak_admin_bin
|
|
|
|
rc, out, err = module.run_command(cmd)
|
2013-04-08 19:41:12 +00:00
|
|
|
if rc == 0:
|
|
|
|
result['commit'] = out
|
2013-05-05 18:54:23 +00:00
|
|
|
result['changed'] = True
|
2013-04-08 19:41:12 +00:00
|
|
|
else:
|
|
|
|
module.fail_json(msg=out)
|
|
|
|
|
|
|
|
# this could take a while, recommend to run in async mode
|
|
|
|
if wait_for_handoffs:
|
2013-05-27 21:24:10 +00:00
|
|
|
timeout = time.time() + wait_for_handoffs
|
|
|
|
while True:
|
2014-03-12 21:38:23 +00:00
|
|
|
cmd = '%s transfers' % riak_admin_bin
|
2013-07-03 21:17:42 +00:00
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
if 'No transfers active' in out:
|
2013-04-08 19:41:12 +00:00
|
|
|
result['handoffs'] = 'No transfers active.'
|
|
|
|
break
|
2013-04-26 21:16:47 +00:00
|
|
|
time.sleep(10)
|
2013-06-01 16:29:03 +00:00
|
|
|
if time.time() > timeout:
|
2013-05-27 21:24:10 +00:00
|
|
|
module.fail_json(msg='Timeout waiting for handoffs.')
|
2013-04-08 19:41:12 +00:00
|
|
|
|
|
|
|
if wait_for_service:
|
2014-03-13 01:26:16 +00:00
|
|
|
cmd = [riak_admin_bin, 'wait_for_service', 'riak_%s' % wait_for_service, node_name ]
|
2013-07-03 21:17:42 +00:00
|
|
|
rc, out, err = module.run_command(cmd)
|
2013-04-08 19:41:12 +00:00
|
|
|
result['service'] = out
|
|
|
|
|
2013-04-26 21:16:47 +00:00
|
|
|
if wait_for_ring:
|
2013-05-27 21:24:10 +00:00
|
|
|
timeout = time.time() + wait_for_ring
|
|
|
|
while True:
|
2013-07-03 21:17:42 +00:00
|
|
|
if ring_check(module, riak_admin_bin):
|
2013-04-26 21:16:47 +00:00
|
|
|
break
|
|
|
|
time.sleep(10)
|
2013-06-01 16:29:03 +00:00
|
|
|
if time.time() > timeout:
|
2013-05-27 21:24:10 +00:00
|
|
|
module.fail_json(msg='Timeout waiting for nodes to agree on ring.')
|
2013-04-26 21:16:47 +00:00
|
|
|
|
2013-07-03 21:17:42 +00:00
|
|
|
result['ring_ready'] = ring_check(module, riak_admin_bin)
|
2013-04-08 19:41:12 +00:00
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2013-12-02 20:13:49 +00:00
|
|
|
# import module snippets
|
2013-12-02 20:11:23 +00:00
|
|
|
from ansible.module_utils.basic import *
|
2014-03-13 01:26:16 +00:00
|
|
|
from ansible.module_utils.urls import *
|
2013-04-08 19:41:12 +00:00
|
|
|
|
|
|
|
main()
|