Debian Codegraph @

ansible_collections/community/general/plugins/modules/riak.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2013, James Martin <[email protected]>, Drew Kerrigan <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: riak
11 short_description: This module handles some common Riak operations
12 description:
13 - This module can be used to join nodes to a cluster, check the status of the cluster.
14 author:
15 - "James Martin (@jsmartin)"
16 - "Drew Kerrigan (@drewkerrigan)"
17 extends_documentation_fragment:
18 - community.general._attributes
19 attributes:
20 check_mode:
21 support: none
22 diff_mode:
23 support: none
24 options:
25 command:
26 description:
27 - The command you would like to perform against the cluster.
28 choices: ['ping', 'kv_test', 'join', 'plan', 'commit']
29 type: str
30 config_dir:
31 description:
32 - The path to the riak configuration directory.
33 default: /etc/riak
34 type: path
35 http_conn:
36 description:
37 - The IP address and port that is listening for Riak HTTP queries.
38 default: 127.0.0.1:8098
39 type: str
40 target_node:
41 description:
42 - The target node for certain operations (join, ping).
43 default: [email protected]
44 type: str
45 wait_for_handoffs:
46 description:
47 - Number of seconds to wait for handoffs to complete.
48 type: int
49 default: 0
50 wait_for_ring:
51 description:
52 - Number of seconds to wait for all nodes to agree on the ring.
53 type: int
54 default: 0
55 wait_for_service:
56 description:
57 - Waits for a riak service to come online before continuing.
58 choices: ['kv']
59 type: str
60 validate_certs:
61 description:
62 - If V(false), SSL certificates are not validated. This should only be used on personally controlled sites using self-signed
63 certificates.
64 type: bool
65 default: true
66 """
67
68 EXAMPLES = r"""
69 - name: "Join's a Riak node to another node"
70 community.general.riak:
71 command: join
72 target_node: [email protected]
73
74 - name: Wait for handoffs to finish. Use with async and poll.
75 community.general.riak:
76 wait_for_handoffs: true
77
78 - name: Wait for riak_kv service to startup
79 community.general.riak:
80 wait_for_service: kv
81 """
82
83 import json
84 import time
85
86 from ansible.module_utils.basic import AnsibleModule
87 from ansible.module_utils.urls import fetch_url
88
89
90 def ring_check(module, riak_admin_bin):
91 cmd = riak_admin_bin + ["ringready"]
92 rc, out, err = module.run_command(cmd)
93 return rc == 0 and "TRUE All nodes agree on the ring" in out
94
95
96 def main():
97 module = AnsibleModule(
98 argument_spec=dict(
99 command=dict(choices=["ping", "kv_test", "join", "plan", "commit"]),
100 config_dir=dict(default="/etc/riak", type="path"),
101 http_conn=dict(default="127.0.0.1:8098"),
102 target_node=dict(default="[email protected]"),
103 wait_for_handoffs=dict(default=0, type="int"),
104 wait_for_ring=dict(default=0, type="int"),
105 wait_for_service=dict(choices=["kv"]),
106 validate_certs=dict(default=True, type="bool"),
107 )
108 )
109 module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"}
110
111 command = module.params.get("command")
112 http_conn = module.params.get("http_conn")
113 target_node = module.params.get("target_node")
114 wait_for_handoffs = module.params.get("wait_for_handoffs")
115 wait_for_ring = module.params.get("wait_for_ring")
116 wait_for_service = module.params.get("wait_for_service")
117
118 # make sure riak commands are on the path
119 riak_bin = module.get_bin_path("riak")
120 riak_admin_bin = module.get_bin_path("riak-admin")
121 riak_admin_bin = [riak_admin_bin] if riak_admin_bin is not None else [riak_bin, "admin"]
122
123 timeout = time.time() + 120
124 while True:
125 if time.time() > timeout:
126 module.fail_json(msg="Timeout, could not fetch Riak stats.")
127 (response, info) = fetch_url(module, f"http://{http_conn}/stats", force=True, timeout=5)
128 if info["status"] == 200:
129 stats_raw = response.read()
130 break
131 time.sleep(5)
132
133 # here we attempt to load those stats,
134 try:
135 stats = json.loads(stats_raw)
136 except Exception:
137 module.fail_json(msg="Could not parse Riak stats.")
138
139 node_name = stats["nodename"]
140 nodes = stats["ring_members"]
141 ring_size = stats["ring_creation_size"]
142 rc, out, err = module.run_command([riak_bin, "version"])
143 version = out.strip()
144
145 result = dict(node_name=node_name, nodes=nodes, ring_size=ring_size, version=version)
146
147 if command == "ping":
148 cmd = [riak_bin, "ping", target_node]
149 rc, out, err = module.run_command(cmd)
150 if rc == 0:
151 result["ping"] = out
152 else:
153 module.fail_json(msg=out)
154
155 elif command == "kv_test":
156 cmd = riak_admin_bin + ["test"]
157 rc, out, err = module.run_command(cmd)
158 if rc == 0:
159 result["kv_test"] = out
160 else:
161 module.fail_json(msg=out)
162
163 elif command == "join":
164 if nodes.count(node_name) == 1 and len(nodes) > 1:
165 result["join"] = "Node is already in cluster or staged to be in cluster."
166 else:
167 cmd = riak_admin_bin + ["cluster", "join", target_node]
168 rc, out, err = module.run_command(cmd)
169 if rc == 0:
170 result["join"] = out
171 result["changed"] = True
172 else:
173 module.fail_json(msg=out)
174
175 elif command == "plan":
176 cmd = riak_admin_bin + ["cluster", "plan"]
177 rc, out, err = module.run_command(cmd)
178 if rc == 0:
179 result["plan"] = out
180 if "Staged Changes" in out:
181 result["changed"] = True
182 else:
183 module.fail_json(msg=out)
184
185 elif command == "commit":
186 cmd = riak_admin_bin + ["cluster", "commit"]
187 rc, out, err = module.run_command(cmd)
188 if rc == 0:
189 result["commit"] = out
190 result["changed"] = True
191 else:
192 module.fail_json(msg=out)
193
194 # this could take a while, recommend to run in async mode
195 if wait_for_handoffs:
196 timeout = time.time() + wait_for_handoffs
197 while True:
198 cmd = riak_admin_bin + ["transfers"]
199 rc, out, err = module.run_command(cmd)
200 if "No transfers active" in out:
201 result["handoffs"] = "No transfers active."
202 break
203 time.sleep(10)
204 if time.time() > timeout:
205 module.fail_json(msg="Timeout waiting for handoffs.")
206
207 if wait_for_service:
208 cmd = riak_admin_bin + ["wait_for_service", f"riak_{wait_for_service}", node_name]
209 rc, out, err = module.run_command(cmd)
210 result["service"] = out
211
212 if wait_for_ring:
213 timeout = time.time() + wait_for_ring
214 while True:
215 if ring_check(module, riak_admin_bin):
216 break
217 time.sleep(10)
218 if time.time() > timeout:
219 module.fail_json(msg="Timeout waiting for nodes to agree on ring.")
220
221 result["ring_ready"] = ring_check(module, riak_admin_bin)
222
223 module.exit_json(**result)
224
225
226 if __name__ == "__main__":
227 main()