mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-30 17:06:19 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -136,6 +136,7 @@ KAZOO_IMP_ERR = None
|
|||
try:
|
||||
from kazoo.client import KazooClient
|
||||
from kazoo.handlers.threading import KazooTimeoutError
|
||||
|
||||
KAZOO_INSTALLED = True
|
||||
except ImportError:
|
||||
KAZOO_IMP_ERR = traceback.format_exc()
|
||||
|
|
@ -148,46 +149,39 @@ from ansible.module_utils.common.text.converters import to_bytes
|
|||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
hosts=dict(required=True, type='str'),
|
||||
name=dict(required=True, type='str'),
|
||||
value=dict(type='str'),
|
||||
op=dict(choices=['get', 'wait', 'list']),
|
||||
state=dict(choices=['present', 'absent']),
|
||||
timeout=dict(default=300, type='int'),
|
||||
recursive=dict(default=False, type='bool'),
|
||||
auth_scheme=dict(default='digest', choices=['digest', 'sasl']),
|
||||
auth_credential=dict(type='str', no_log=True),
|
||||
use_tls=dict(default=False, type='bool'),
|
||||
hosts=dict(required=True, type="str"),
|
||||
name=dict(required=True, type="str"),
|
||||
value=dict(type="str"),
|
||||
op=dict(choices=["get", "wait", "list"]),
|
||||
state=dict(choices=["present", "absent"]),
|
||||
timeout=dict(default=300, type="int"),
|
||||
recursive=dict(default=False, type="bool"),
|
||||
auth_scheme=dict(default="digest", choices=["digest", "sasl"]),
|
||||
auth_credential=dict(type="str", no_log=True),
|
||||
use_tls=dict(default=False, type="bool"),
|
||||
),
|
||||
supports_check_mode=False
|
||||
supports_check_mode=False,
|
||||
)
|
||||
|
||||
if not KAZOO_INSTALLED:
|
||||
module.fail_json(msg=missing_required_lib('kazoo >= 2.1'), exception=KAZOO_IMP_ERR)
|
||||
module.fail_json(msg=missing_required_lib("kazoo >= 2.1"), exception=KAZOO_IMP_ERR)
|
||||
|
||||
check = check_params(module.params)
|
||||
if not check['success']:
|
||||
module.fail_json(msg=check['msg'])
|
||||
if not check["success"]:
|
||||
module.fail_json(msg=check["msg"])
|
||||
|
||||
zoo = KazooCommandProxy(module)
|
||||
try:
|
||||
zoo.start()
|
||||
except KazooTimeoutError:
|
||||
module.fail_json(msg='The connection to the ZooKeeper ensemble timed out.')
|
||||
module.fail_json(msg="The connection to the ZooKeeper ensemble timed out.")
|
||||
|
||||
command_dict = {
|
||||
'op': {
|
||||
'get': zoo.get,
|
||||
'list': zoo.list,
|
||||
'wait': zoo.wait
|
||||
},
|
||||
'state': {
|
||||
'present': zoo.present,
|
||||
'absent': zoo.absent
|
||||
}
|
||||
"op": {"get": zoo.get, "list": zoo.list, "wait": zoo.wait},
|
||||
"state": {"present": zoo.present, "absent": zoo.absent},
|
||||
}
|
||||
|
||||
command_type = 'op' if 'op' in module.params and module.params['op'] is not None else 'state'
|
||||
command_type = "op" if "op" in module.params and module.params["op"] is not None else "state"
|
||||
method = module.params[command_type]
|
||||
result, result_dict = command_dict[command_type][method]()
|
||||
zoo.shutdown()
|
||||
|
|
@ -199,36 +193,40 @@ def main():
|
|||
|
||||
|
||||
def check_params(params):
|
||||
if not params['state'] and not params['op']:
|
||||
return {'success': False, 'msg': 'Please define an operation (op) or a state.'}
|
||||
if not params["state"] and not params["op"]:
|
||||
return {"success": False, "msg": "Please define an operation (op) or a state."}
|
||||
|
||||
if params['state'] and params['op']:
|
||||
return {'success': False, 'msg': 'Please choose an operation (op) or a state, but not both.'}
|
||||
if params["state"] and params["op"]:
|
||||
return {"success": False, "msg": "Please choose an operation (op) or a state, but not both."}
|
||||
|
||||
return {'success': True}
|
||||
return {"success": True}
|
||||
|
||||
|
||||
class KazooCommandProxy():
|
||||
class KazooCommandProxy:
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
self.zk = KazooClient(module.params['hosts'], use_ssl=module.params['use_tls'])
|
||||
self.zk = KazooClient(module.params["hosts"], use_ssl=module.params["use_tls"])
|
||||
|
||||
def absent(self):
|
||||
return self._absent(self.module.params['name'])
|
||||
return self._absent(self.module.params["name"])
|
||||
|
||||
def exists(self, znode):
|
||||
return self.zk.exists(znode)
|
||||
|
||||
def list(self):
|
||||
children = self.zk.get_children(self.module.params['name'])
|
||||
return True, {'count': len(children), 'items': children, 'msg': 'Retrieved znodes in path.',
|
||||
'znode': self.module.params['name']}
|
||||
children = self.zk.get_children(self.module.params["name"])
|
||||
return True, {
|
||||
"count": len(children),
|
||||
"items": children,
|
||||
"msg": "Retrieved znodes in path.",
|
||||
"znode": self.module.params["name"],
|
||||
}
|
||||
|
||||
def present(self):
|
||||
return self._present(self.module.params['name'], self.module.params['value'])
|
||||
return self._present(self.module.params["name"], self.module.params["value"])
|
||||
|
||||
def get(self):
|
||||
return self._get(self.module.params['name'])
|
||||
return self._get(self.module.params["name"])
|
||||
|
||||
def shutdown(self):
|
||||
self.zk.stop()
|
||||
|
|
@ -236,32 +234,31 @@ class KazooCommandProxy():
|
|||
|
||||
def start(self):
|
||||
self.zk.start()
|
||||
if self.module.params['auth_credential']:
|
||||
self.zk.add_auth(self.module.params['auth_scheme'], self.module.params['auth_credential'])
|
||||
if self.module.params["auth_credential"]:
|
||||
self.zk.add_auth(self.module.params["auth_scheme"], self.module.params["auth_credential"])
|
||||
|
||||
def wait(self):
|
||||
return self._wait(self.module.params['name'], self.module.params['timeout'])
|
||||
return self._wait(self.module.params["name"], self.module.params["timeout"])
|
||||
|
||||
def _absent(self, znode):
|
||||
if self.exists(znode):
|
||||
self.zk.delete(znode, recursive=self.module.params['recursive'])
|
||||
return True, {'changed': True, 'msg': 'The znode was deleted.'}
|
||||
self.zk.delete(znode, recursive=self.module.params["recursive"])
|
||||
return True, {"changed": True, "msg": "The znode was deleted."}
|
||||
else:
|
||||
return True, {'changed': False, 'msg': 'The znode does not exist.'}
|
||||
return True, {"changed": False, "msg": "The znode does not exist."}
|
||||
|
||||
def _get(self, path):
|
||||
if self.exists(path):
|
||||
value, zstat = self.zk.get(path)
|
||||
stat_dict = {}
|
||||
for i in dir(zstat):
|
||||
if not i.startswith('_'):
|
||||
if not i.startswith("_"):
|
||||
attr = getattr(zstat, i)
|
||||
if isinstance(attr, (int, str)):
|
||||
stat_dict[i] = attr
|
||||
result = True, {'msg': 'The node was retrieved.', 'znode': path, 'value': value,
|
||||
'stat': stat_dict}
|
||||
result = True, {"msg": "The node was retrieved.", "znode": path, "value": value, "stat": stat_dict}
|
||||
else:
|
||||
result = False, {'msg': 'The requested node does not exist.'}
|
||||
result = False, {"msg": "The requested node does not exist."}
|
||||
|
||||
return result
|
||||
|
||||
|
|
@ -270,27 +267,32 @@ class KazooCommandProxy():
|
|||
(current_value, zstat) = self.zk.get(path)
|
||||
if value != current_value:
|
||||
self.zk.set(path, to_bytes(value))
|
||||
return True, {'changed': True, 'msg': 'Updated the znode value.', 'znode': path,
|
||||
'value': value}
|
||||
return True, {"changed": True, "msg": "Updated the znode value.", "znode": path, "value": value}
|
||||
else:
|
||||
return True, {'changed': False, 'msg': 'No changes were necessary.', 'znode': path, 'value': value}
|
||||
return True, {"changed": False, "msg": "No changes were necessary.", "znode": path, "value": value}
|
||||
else:
|
||||
self.zk.create(path, to_bytes(value), makepath=True)
|
||||
return True, {'changed': True, 'msg': 'Created a new znode.', 'znode': path, 'value': value}
|
||||
return True, {"changed": True, "msg": "Created a new znode.", "znode": path, "value": value}
|
||||
|
||||
def _wait(self, path, timeout, interval=5):
|
||||
lim = time.time() + timeout
|
||||
|
||||
while time.time() < lim:
|
||||
if self.exists(path):
|
||||
return True, {'msg': 'The node appeared before the configured timeout.',
|
||||
'znode': path, 'timeout': timeout}
|
||||
return True, {
|
||||
"msg": "The node appeared before the configured timeout.",
|
||||
"znode": path,
|
||||
"timeout": timeout,
|
||||
}
|
||||
else:
|
||||
time.sleep(interval)
|
||||
|
||||
return False, {'msg': 'The node did not appear before the operation timed out.', 'timeout': timeout,
|
||||
'znode': path}
|
||||
return False, {
|
||||
"msg": "The node did not appear before the operation timed out.",
|
||||
"timeout": timeout,
|
||||
"znode": path,
|
||||
}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue