ansible/test/integration/targets/module_utils_respawn/library/respawnme.py
Matt Davis 4c5ce5a1a9
module compat for py3.8+ controller (#73423)
* module compat for py3.8+ controller

* replaced internal usages of selinux bindings with internal ctypes binding (allows basic selinux operations from any Python interpreter), plus tests

* added new respawn_module API to allow modules to import Python packages that are only available under a well-known interpreter, plus tests

* added respawn logic to modules that need Python libs from a specific system interpreter (apt, apt_repository, dnf, yum)

minimize internal HAVE_SELINUX usage

spurious junk

pep8

* pylint fixes

* add RHEL8 Python 3.8 testing

* more pylint

* import sanity

* unit tests

* changelog update

* fix a bunch of stuff

* tweak changelog

* fix setup_rpm_repo on EL8

* misc sanity/test fixes

* misc feedback tweaks

* fix import fallback in test module

* fix selinux MU test

* fix dnf tests to avoid python-dependent test packages

* add trailing LFs to aliases

* fix yum tests to avoid test package with Python deps

* hack create_repo for EL6 to create noarch package
2021-02-10 21:32:59 -08:00

44 lines
1.7 KiB
Python

#!/usr/bin/python
# Copyright: (c) 2021, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import sys
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.respawn import respawn_module, has_respawned
def main():
mod = AnsibleModule(argument_spec=dict(
mode=dict(required=True, choices=['multi_respawn', 'no_respawn', 'respawn'])
))
# just return info about what interpreter we're currently running under
if mod.params['mode'] == 'no_respawn':
mod.exit_json(interpreter_path=sys.executable)
elif mod.params['mode'] == 'respawn':
if not has_respawned():
new_interpreter = os.path.join(mod.tmpdir, 'anotherpython')
os.symlink(sys.executable, new_interpreter)
respawn_module(interpreter_path=new_interpreter)
# respawn should always exit internally- if we continue executing here, it's a bug
raise Exception('FAIL, should never reach this line')
else:
# return the current interpreter, as well as a signal that we created a different one
mod.exit_json(created_interpreter=sys.executable, interpreter_path=sys.executable)
elif mod.params['mode'] == 'multi_respawn':
# blindly respawn with the current interpreter, the second time should bomb
respawn_module(sys.executable)
# shouldn't be any way for us to fall through, but just in case, that's also a bug
raise Exception('FAIL, should never reach this code')
if __name__ == '__main__':
main()