From 8821dbaa13dfd7d2bb79df5364b0033b7d6c6e5d Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Mon, 25 Mar 2024 11:25:35 -0700 Subject: [PATCH] Make ansible package check more robust The JSON parser will fail if given the empty string, so make sure that if we don't get any output back, we don't try to parse it. Additionally, if no extra packages are required, then don't bother running the command in the first place. Finally, include the missing package list in the error log message rather than in a separate debug log entry, for easier correlation. Change-Id: I0c39c74fdf05611439b35cd72b8ab70b836f4c1a --- zuul/lib/ansible.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/zuul/lib/ansible.py b/zuul/lib/ansible.py index b8ec6bbb6a..cbfbe02e29 100644 --- a/zuul/lib/ansible.py +++ b/zuul/lib/ansible.py @@ -236,6 +236,8 @@ class AnsibleManager: result = False try: extra_packages = self._getAnsible(version).extra_packages + if not extra_packages: + return True # Formerly this used pkg_resources which has been deprecated. If # there is a better way to accomplish this task please change @@ -248,19 +250,21 @@ class AnsibleManager: command += extra_packages ret = subprocess.run(command, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) - to_be_installed = json.loads(ret.stdout)["install"] + stderr=subprocess.PIPE) + if ret.stdout: + to_be_installed = json.loads(ret.stdout)["install"] + else: + to_be_installed = None # We check manually so that we can log the missing packages # properly. We also need to check the the JSON output to determine # if any changes were necessary. if to_be_installed: - self.log.error( - 'Ansible version %s installation is missing packages' % - version) missing = ["%s %s" % (x["metadata"]["name"], x["metadata"]["version"]) for x in to_be_installed] - self.log.debug("These packages are missing: %s", missing) + self.log.error( + 'Ansible version %s installation is missing packages %s', + version, missing) else: result = True except Exception: