From 32ac43596248bc5533106d59a1d7d374fd396371 Mon Sep 17 00:00:00 2001 From: Guillaume Abrioux Date: Sun, 17 Jan 2021 20:46:31 +0100 Subject: [PATCH] library: fix bug in radosgw_zone.py If for some reason `get_zonegroup()` returns a failure, we must handle and make the module exit properly instead of failing with the following python trace: ``` Traceback (most recent call last): File "./AnsiballZ_radosgw_zone.py", line 247, in _ansiballz_main() File "./AnsiballZ_radosgw_zone.py", line 234, in _ansiballz_main exitcode = debug(sys.argv[1], zipped_mod, ANSIBALLZ_PARAMS) File "./AnsiballZ_radosgw_zone.py", line 202, in debug runpy.run_module(mod_name='ansible.modules.radosgw_zone', init_globals=None, run_name='__main__', alter_sys=True) File "/usr/lib64/python3.6/runpy.py", line 205, in run_module return _run_module_code(code, init_globals, run_name, mod_spec) File "/usr/lib64/python3.6/runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/vagrant/.ansible/tmp/ansible-tmp-1610728441.41-685133-218973990589597/debug_dir/ansible/modules/radosgw_zone.py", line 467, in main() File "/home/vagrant/.ansible/tmp/ansible-tmp-1610728441.41-685133-218973990589597/debug_dir/ansible/modules/radosgw_zone.py", line 463, in main run_module() File "/home/vagrant/.ansible/tmp/ansible-tmp-1610728441.41-685133-218973990589597/debug_dir/ansible/modules/radosgw_zone.py", line 425, in run_module zonegroup = json.loads(_out) File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode(s) File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ``` Signed-off-by: Guillaume Abrioux (cherry picked from commit fedb36688dbf05521966ee22e831283f0967d17a) --- library/radosgw_zone.py | 49 ++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/library/radosgw_zone.py b/library/radosgw_zone.py index 92e3e1f14..d4b69b1c8 100644 --- a/library/radosgw_zone.py +++ b/library/radosgw_zone.py @@ -122,6 +122,16 @@ import stat # noqa E402 import time # noqa E402 +def fatal(message, module): + ''' + Report a fatal error and exit + ''' + if module: + module.fail_json(msg=message, rc=1) + else: + raise(Exception(message)) + + def container_exec(binary, container_image): ''' Build the docker CLI to run a command inside a container @@ -414,24 +424,27 @@ def run_module(): if rc == 0: zone = json.loads(out) _rc, _cmd, _out, _err = exec_commands(module, get_zonegroup(module, container_image=container_image)) - zonegroup = json.loads(_out) - if not access_key: - access_key = '' - if not secret_key: - secret_key = '' - current = { - 'endpoints': next(zone['endpoints'] for zone in zonegroup['zones'] if zone['name'] == name), - 'access_key': zone['system_key']['access_key'], - 'secret_key': zone['system_key']['secret_key'] - } - asked = { - 'endpoints': endpoints, - 'access_key': access_key, - 'secret_key': secret_key - } - if current != asked: - rc, cmd, out, err = exec_commands(module, modify_zone(module, container_image=container_image)) - changed = True + if _rc == 0: + zonegroup = json.loads(_out) + if not access_key: + access_key = '' + if not secret_key: + secret_key = '' + current = { + 'endpoints': next(zone['endpoints'] for zone in zonegroup['zones'] if zone['name'] == name), + 'access_key': zone['system_key']['access_key'], + 'secret_key': zone['system_key']['secret_key'] + } + asked = { + 'endpoints': endpoints, + 'access_key': access_key, + 'secret_key': secret_key + } + if current != asked: + rc, cmd, out, err = exec_commands(module, modify_zone(module, container_image=container_image)) + changed = True + else: + fatal(_err, module) else: rc, cmd, out, err = exec_commands(module, create_zone(module, container_image=container_image)) changed = True