Skip to content

Commit b9c5c84

Browse files
committed
WIP
1 parent 98ae5e2 commit b9c5c84

10 files changed

Lines changed: 59 additions & 327 deletions

File tree

.github/workflows/tests.yml

Lines changed: 3 additions & 256 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
name: Tests
44

5-
# env:
6-
# ANSIBLE_VERBOSITY: 3
7-
# MITOGEN_LOG_LEVEL: DEBUG
5+
env:
6+
ANSIBLE_VERBOSITY: 3
7+
MITOGEN_LOG_LEVEL: DEBUG
88

99
on:
1010
pull_request:
@@ -15,251 +15,6 @@ on:
1515
# https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners/about-github-hosted-runners
1616
# https://github.com/actions/runner-images/blob/main/README.md#software-and-image-support
1717
jobs:
18-
u2204:
19-
name: u2204 ${{ matrix.tox_env }}
20-
# https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2204-Readme.md
21-
runs-on: ubuntu-22.04
22-
timeout-minutes: 25
23-
24-
strategy:
25-
fail-fast: false
26-
matrix:
27-
include:
28-
- name: Ans_27_210
29-
tox_env: py27-mode_ansible-ansible2.10
30-
- name: Ans_27_4
31-
tox_env: py27-mode_ansible-ansible4
32-
33-
- name: Ans_36_210
34-
python_version: '3.6'
35-
tox_env: py36-mode_ansible-ansible2.10
36-
- name: Ans_36_4
37-
python_version: '3.6'
38-
tox_env: py36-mode_ansible-ansible4
39-
40-
- name: Mito_27
41-
tox_env: py27-mode_mitogen
42-
- name: Mito_36
43-
python_version: '3.6'
44-
tox_env: py36-mode_mitogen
45-
46-
steps:
47-
- uses: actions/checkout@v4
48-
- uses: docker/login-action@v3
49-
with:
50-
registry: ghcr.io
51-
username: ${{ github.actor }}
52-
password: ${{ secrets.GITHUB_TOKEN }}
53-
- name: Install build deps
54-
run: |
55-
set -o errexit -o nounset -o pipefail
56-
57-
PYTHON=$(python -c 'import re; print(re.sub(r"^py([23])([0-9]{1,2}).*", r"python\1.\2", "${{ matrix.tox_env }}"))')
58-
59-
if [[ -z $PYTHON ]]; then
60-
echo 1>&2 "Python interpreter could not be determined"
61-
exit 1
62-
fi
63-
64-
sudo apt-get update
65-
66-
if [[ $PYTHON == "python2.7" ]]; then
67-
sudo apt install -y python2-dev sshpass virtualenv
68-
elif [[ $PYTHON == "python3.6" ]]; then
69-
sudo apt install -y gcc-10 make libbz2-dev liblzma-dev libreadline-dev libsqlite3-dev libssl-dev sshpass virtualenv zlib1g-dev
70-
curl --fail --silent --show-error --location https://pyenv.run | bash
71-
CC=gcc-10 ~/.pyenv/bin/pyenv install --force 3.6
72-
else
73-
echo 1>&2 "Python interpreter $PYTHON not available"
74-
exit 1
75-
fi
76-
- name: Show Python versions
77-
run: |
78-
set -o errexit -o nounset -o pipefail
79-
80-
# macOS builders lack a realpath command
81-
type python && python -c"import os.path;print(os.path.realpath('$(type -p python)'))" && python --version
82-
type python2 && python2 -c"import os.path;print(os.path.realpath('$(type -p python2)'))" && python2 --version
83-
type python3 && python3 -c"import os.path;print(os.path.realpath('$(type -p python3)'))" && python3 --version
84-
echo
85-
86-
if [ -e /usr/bin/python ]; then
87-
echo "/usr/bin/python: sys.executable: $(/usr/bin/python -c 'import sys; print(sys.executable)')"
88-
fi
89-
90-
if [ -e /usr/bin/python2 ]; then
91-
echo "/usr/bin/python2: sys.executable: $(/usr/bin/python2 -c 'import sys; print(sys.executable)')"
92-
fi
93-
94-
if [ -e /usr/bin/python2.7 ]; then
95-
echo "/usr/bin/python2.7: sys.executable: $(/usr/bin/python2.7 -c 'import sys; print(sys.executable)')"
96-
fi
97-
- name: Install tooling
98-
run: |
99-
set -o errexit -o nounset -o pipefail
100-
101-
# Tox environment name (e.g. py312-mode_mitogen) -> Python executable name (e.g. python3.12)
102-
PYTHON=$(python -c 'import re; print(re.sub(r"^py([23])([0-9]{1,2}).*", r"python\1.\2", "${{ matrix.tox_env }}"))')
103-
104-
if [[ -z $PYTHON ]]; then
105-
echo 1>&2 "Python interpreter could not be determined"
106-
exit 1
107-
fi
108-
109-
if [[ $PYTHON == "python2.7" ]]; then
110-
curl "https://bootstrap.pypa.io/pip/2.7/get-pip.py" --output "get-pip.py"
111-
"$PYTHON" get-pip.py --user --no-python-version-warning
112-
# Avoid Python 2.x pip masking system pip
113-
rm -f ~/.local/bin/{easy_install,pip,wheel}
114-
elif [[ $PYTHON == "python3.6" ]]; then
115-
PYTHON="$HOME/.pyenv/versions/3.6.15/bin/python3.6"
116-
fi
117-
118-
"$PYTHON" -m pip install -r "tests/requirements-tox.txt"
119-
- name: Run tests
120-
env:
121-
GITHUB_ACTOR: ${{ github.actor }}
122-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
123-
run: |
124-
set -o errexit -o nounset -o pipefail
125-
126-
# Tox environment name (e.g. py312-mode_mitogen) -> Python executable name (e.g. python3.12)
127-
PYTHON=$(python -c 'import re; print(re.sub(r"^py([23])([0-9]{1,2}).*", r"python\1.\2", "${{ matrix.tox_env }}"))')
128-
129-
if [[ -z $PYTHON ]]; then
130-
echo 1>&2 "Python interpreter could not be determined"
131-
exit 1
132-
fi
133-
134-
if [[ $PYTHON == "python3.6" ]]; then
135-
PYTHON="$HOME/.pyenv/versions/3.6.15/bin/python3.6"
136-
fi
137-
138-
"$PYTHON" -m tox -e "${{ matrix.tox_env }}"
139-
140-
u2404:
141-
name: u2404 ${{ matrix.tox_env }}
142-
# https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2404-Readme.md
143-
runs-on: ubuntu-24.04
144-
timeout-minutes: 25
145-
146-
strategy:
147-
fail-fast: false
148-
matrix:
149-
include:
150-
- name: Ans_311_210
151-
python_version: '3.11'
152-
tox_env: py311-mode_ansible-ansible2.10
153-
- name: Ans_311_3
154-
python_version: '3.11'
155-
tox_env: py311-mode_ansible-ansible3
156-
- name: Ans_311_4
157-
python_version: '3.11'
158-
tox_env: py311-mode_ansible-ansible4
159-
- name: Ans_311_5
160-
python_version: '3.11'
161-
tox_env: py311-mode_ansible-ansible5
162-
- name: Ans_313_6
163-
python_version: '3.13'
164-
tox_env: py313-mode_ansible-ansible6
165-
- name: Ans_313_7
166-
python_version: '3.13'
167-
tox_env: py313-mode_ansible-ansible7
168-
- name: Ans_313_8
169-
python_version: '3.13'
170-
tox_env: py313-mode_ansible-ansible8
171-
- name: Ans_313_9
172-
python_version: '3.13'
173-
tox_env: py313-mode_ansible-ansible9
174-
- name: Ans_313_10
175-
python_version: '3.13'
176-
tox_env: py313-mode_ansible-ansible10
177-
- name: Ans_313_11
178-
python_version: '3.13'
179-
tox_env: py313-mode_ansible-ansible11
180-
- name: Ans_313_12
181-
python_version: '3.13'
182-
tox_env: py313-mode_ansible-ansible12
183-
184-
- name: Van_313_11
185-
python_version: '3.13'
186-
tox_env: py313-mode_ansible-ansible11-strategy_linear
187-
- name: Van_313_12
188-
python_version: '3.13'
189-
tox_env: py313-mode_ansible-ansible12-strategy_linear
190-
191-
- name: Mito_313
192-
python_version: '3.13'
193-
tox_env: py313-mode_mitogen
194-
195-
steps:
196-
- uses: actions/checkout@v4
197-
- uses: actions/setup-python@v5
198-
with:
199-
python-version: ${{ matrix.python_version }}
200-
if: ${{ matrix.python_version }}
201-
- uses: docker/login-action@v3
202-
with:
203-
registry: ghcr.io
204-
username: ${{ github.actor }}
205-
password: ${{ secrets.GITHUB_TOKEN }}
206-
- name: Install build deps
207-
run: |
208-
set -o errexit -o nounset -o pipefail
209-
210-
sudo apt-get update
211-
sudo apt-get install -y sshpass virtualenv
212-
- name: Show Python versions
213-
run: |
214-
set -o errexit -o nounset -o pipefail
215-
216-
# macOS builders lack a realpath command
217-
type python && python -c"import os.path;print(os.path.realpath('$(type -p python)'))" && python --version
218-
type python2 && python2 -c"import os.path;print(os.path.realpath('$(type -p python2)'))" && python2 --version
219-
type python3 && python3 -c"import os.path;print(os.path.realpath('$(type -p python3)'))" && python3 --version
220-
echo
221-
222-
if [ -e /usr/bin/python ]; then
223-
echo "/usr/bin/python: sys.executable: $(/usr/bin/python -c 'import sys; print(sys.executable)')"
224-
fi
225-
226-
if [ -e /usr/bin/python2 ]; then
227-
echo "/usr/bin/python2: sys.executable: $(/usr/bin/python2 -c 'import sys; print(sys.executable)')"
228-
fi
229-
230-
if [ -e /usr/bin/python2.7 ]; then
231-
echo "/usr/bin/python2.7: sys.executable: $(/usr/bin/python2.7 -c 'import sys; print(sys.executable)')"
232-
fi
233-
- name: Install tooling
234-
run: |
235-
set -o errexit -o nounset -o pipefail
236-
237-
# Tox environment name (e.g. py312-mode_mitogen) -> Python executable name (e.g. python3.12)
238-
PYTHON=$(python -c 'import re; print(re.sub(r"^py([23])([0-9]{1,2}).*", r"python\1.\2", "${{ matrix.tox_env }}"))')
239-
240-
if [[ -z $PYTHON ]]; then
241-
echo 1>&2 "Python interpreter could not be determined"
242-
exit 1
243-
fi
244-
245-
"$PYTHON" -m pip install -r "tests/requirements-tox.txt"
246-
- name: Run tests
247-
env:
248-
GITHUB_ACTOR: ${{ github.actor }}
249-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
250-
run: |
251-
set -o errexit -o nounset -o pipefail
252-
253-
# Tox environment name (e.g. py312-mode_mitogen) -> Python executable name (e.g. python3.12)
254-
PYTHON=$(python -c 'import re; print(re.sub(r"^py([23])([0-9]{1,2}).*", r"python\1.\2", "${{ matrix.tox_env }}"))')
255-
256-
if [[ -z $PYTHON ]]; then
257-
echo 1>&2 "Python interpreter could not be determined"
258-
exit 1
259-
fi
260-
261-
"$PYTHON" -m tox -e "${{ matrix.tox_env }}"
262-
26318
macos:
26419
name: macos ${{ matrix.tox_env }}
26520
# https://github.com/actions/runner-images/blob/main/images/macos/macos-13-Readme.md
@@ -279,12 +34,6 @@ jobs:
27934
- name: Van_313_11
28035
tox_env: py313-mode_localhost-ansible11-strategy_linear
28136

282-
- name: Loc_313_12
283-
tox_env: py313-mode_localhost-ansible12
284-
285-
- name: Van_313_12
286-
tox_env: py313-mode_localhost-ansible12-strategy_linear
287-
28837
steps:
28938
- uses: actions/checkout@v4
29039
- uses: actions/setup-python@v5
@@ -351,8 +100,6 @@ jobs:
351100
check:
352101
if: always()
353102
needs:
354-
- u2204
355-
- u2404
356103
- macos
357104
runs-on: ubuntu-latest
358105
steps:

ansible_mitogen/mixins.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -470,30 +470,27 @@ def _low_level_execute_command(self, cmd, sudoable=True, in_data=None,
470470
# chicken-and-egg issue, mitogen needs a python to run low_level_execute_command
471471
# which is required by Ansible's discover_interpreter function
472472
if self._mitogen_discovering_interpreter:
473-
possible_pythons = [
474-
'/usr/bin/python',
475-
'python3',
476-
'python3.7',
477-
'python3.6',
478-
'python3.5',
479-
'python2.7',
480-
'python2.6',
481-
'/usr/libexec/platform-python',
482-
'/usr/bin/python3',
483-
'python'
484-
]
473+
if self._connection.transport in {'ssh'}:
474+
possible_pythons = [
475+
'$(for p in python3 python2 python; do command -v "$p" 2>/dev/null && break; done;)',
476+
]
477+
else:
478+
possible_pythons = ['python3' 'python2', 'python']
485479
else:
486480
# not used, just adding a filler value
487481
possible_pythons = ['python']
488482

489483
for possible_python in possible_pythons:
490484
try:
485+
LOG.debug('_low_level_execute_command(): trying possible_python=%r', possible_python)
491486
self._mitogen_interpreter_candidate = possible_python
492487
rc, stdout, stderr = self._connection.exec_command(
493488
cmd, in_data, sudoable, mitogen_chdir=chdir,
494489
)
490+
LOG.debug('_low_level_execute_command(): got rc=%d, stdout=%r, stderr=%r', rc, stdout, stderr)
495491
# TODO: what exception is thrown?
496-
except:
492+
except BaseException as exc:
493+
LOG.debug('%r._low_level_execute_command for possible_python=%r: %s, %r', self, possible_python, type(exc), exc)
497494
# we've reached the last python attempted and failed
498495
if possible_python == possible_pythons[-1]:
499496
raise

ansible_mitogen/transport_config.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,19 @@
7373
from ansible.module_utils.six import with_metaclass
7474
from ansible.module_utils.parsing.convert_bool import boolean
7575

76+
import ansible_mitogen.utils
7677
import mitogen.core
7778

7879

7980
LOG = logging.getLogger(__name__)
8081

82+
if ansible_mitogen.utils.ansible_version[:2] >= (2, 19):
83+
_FALLBACK_INTERPRETER = ansible.executor.interpreter_discovery._FALLBACK_INTERPRETER
84+
elif ansible_mitogen.utils.ansible_version[:2] >= (2, 17):
85+
_FALLBACK_INTERPRETER = u'/usr/bin/python3'
86+
else:
87+
_FALLBACK_INTERPRETER = u'/usr/bin/python'
88+
8189

8290
def run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_python):
8391
"""
@@ -107,7 +115,9 @@ def run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_pyth
107115
# blow away the discovered_interpreter_config cache and rediscover
108116
del task_vars['ansible_facts'][discovered_interpreter_config]
109117

110-
if discovered_interpreter_config not in task_vars['ansible_facts']:
118+
try:
119+
s = task_vars['ansible_facts'][discovered_interpreter_config]
120+
except KeyError:
111121
action._mitogen_discovering_interpreter = True
112122
# fake pipelining so discover_interpreter can be happy
113123
action._connection.has_pipelining = True
@@ -121,8 +131,6 @@ def run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_pyth
121131
# cache discovered interpreter
122132
task_vars['ansible_facts'][discovered_interpreter_config] = s
123133
action._connection.has_pipelining = False
124-
else:
125-
s = task_vars['ansible_facts'][discovered_interpreter_config]
126134

127135
# propagate discovered interpreter as fact
128136
action._discovered_interpreter_key = discovered_interpreter_config
@@ -144,9 +152,9 @@ def parse_python_path(s, task_vars, action, rediscover_python):
144152
s = 'auto'
145153

146154
s = run_interpreter_discovery_if_necessary(s, task_vars, action, rediscover_python)
147-
# if unable to determine python_path, fallback to '/usr/bin/python'
148155
if not s:
149-
s = '/usr/bin/python'
156+
s = _FALLBACK_INTERPRETER
157+
# raise ValueError("Interpreter discovery failed, got: %r", s)
150158

151159
return ansible.utils.shlex.shlex_split(s)
152160

@@ -715,9 +723,6 @@ def port(self):
715723

716724
def python_path(self, rediscover_python=False):
717725
s = self._host_vars.get('ansible_python_interpreter')
718-
# #511, #536: executor/module_common.py::_get_shebang() hard-wires
719-
# "/usr/bin/python" as the default interpreter path if no other
720-
# interpreter is specified.
721726
return parse_python_path(
722727
s,
723728
task_vars=self._task_vars,

0 commit comments

Comments
 (0)