From fb849c342db9313bd22eade507c45c76761ff278 Mon Sep 17 00:00:00 2001 From: Maxwell G Date: Sat, 13 May 2023 17:22:42 +0000 Subject: [PATCH 1/2] respawn copr module when dnf is missing Fixes: https://bugzilla.redhat.com/2203513 --- changelogs/fragments/6522-copr-respawn.yaml | 4 +++ plugins/modules/copr.py | 25 +++++++++++++++++++ tests/integration/targets/copr/tasks/main.yml | 6 ----- 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/6522-copr-respawn.yaml diff --git a/changelogs/fragments/6522-copr-respawn.yaml b/changelogs/fragments/6522-copr-respawn.yaml new file mode 100644 index 00000000000..8d5922e4c92 --- /dev/null +++ b/changelogs/fragments/6522-copr-respawn.yaml @@ -0,0 +1,4 @@ +--- +minor_changes: + - "copr - respawn module to use the system python interpreter when the ``dnf`` python module is not available in ``ansible_python_interpreter`` + (https://github.com/ansible-collections/community.general/pull/6522)." diff --git a/plugins/modules/copr.py b/plugins/modules/copr.py index 965c2a935d5..45de8bda7b0 100644 --- a/plugins/modules/copr.py +++ b/plugins/modules/copr.py @@ -103,6 +103,29 @@ from ansible.module_utils.basic import AnsibleModule # pylint: disable=import-error from ansible.module_utils.urls import open_url # pylint: disable=import-error +HAS_RESPAWN_UTIL = False +if not HAS_DNF_PACKAGES: + try: + from ansible.module_utils.common import respawn + except ImportError: + pass + else: + HAS_RESPAWN_UTIL = True + + +def _respawn_dnf(): + if respawn.has_respawned(): + return + system_interpreters = ( + "/usr/libexec/platform-python", + "/usr/bin/python3", + "/usr/bin/python2", + "/usr/bin/python", + ) + interpreter = respawn.probe_interpreters_for_module(system_interpreters, "dnf") + if interpreter: + respawn.respawn_module(interpreter) + class CoprModule(object): """The class represents a copr module. @@ -460,6 +483,8 @@ def run_module(): params = module.params if not HAS_DNF_PACKAGES: + if HAS_RESPAWN_UTIL: + _respawn_dnf() module.fail_json(msg=missing_required_lib("dnf"), exception=DNF_IMP_ERR) CoprModule.ansible_module = module diff --git a/tests/integration/targets/copr/tasks/main.yml b/tests/integration/targets/copr/tasks/main.yml index 0e465172407..0d66378112c 100644 --- a/tests/integration/targets/copr/tasks/main.yml +++ b/tests/integration/targets/copr/tasks/main.yml @@ -10,12 +10,6 @@ ansible_distribution == 'Fedora' or (ansible_os_family == 'RedHat' and ansible_distribution != 'Fedora' and ansible_distribution_major_version | int >= 8) - # The copr module imports dnf which is only available for the system Python - # interpreter. - - > - not (ansible_distribution == 'CentOS' and - ansible_distribution_major_version | int == 8 and not - ansible_python_version.startswith('3.6')) block: - debug: var=copr_chroot - name: enable copr project From 934389f9fbafceae5d3f271413b2618a2ab2c263 Mon Sep 17 00:00:00 2001 From: Maxwell G Date: Sat, 13 May 2023 19:11:08 +0000 Subject: [PATCH 2/2] don't guard common.respawn module_util import --- plugins/modules/copr.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/plugins/modules/copr.py b/plugins/modules/copr.py index 45de8bda7b0..1311de849da 100644 --- a/plugins/modules/copr.py +++ b/plugins/modules/copr.py @@ -97,21 +97,13 @@ DNF_IMP_ERR = traceback.format_exc() HAS_DNF_PACKAGES = False +from ansible.module_utils.common import respawn from ansible.module_utils.six.moves.urllib.error import HTTPError from ansible.module_utils.basic import missing_required_lib from ansible.module_utils import distro # pylint: disable=import-error from ansible.module_utils.basic import AnsibleModule # pylint: disable=import-error from ansible.module_utils.urls import open_url # pylint: disable=import-error -HAS_RESPAWN_UTIL = False -if not HAS_DNF_PACKAGES: - try: - from ansible.module_utils.common import respawn - except ImportError: - pass - else: - HAS_RESPAWN_UTIL = True - def _respawn_dnf(): if respawn.has_respawned(): @@ -483,8 +475,7 @@ def run_module(): params = module.params if not HAS_DNF_PACKAGES: - if HAS_RESPAWN_UTIL: - _respawn_dnf() + _respawn_dnf() module.fail_json(msg=missing_required_lib("dnf"), exception=DNF_IMP_ERR) CoprModule.ansible_module = module