Skip to content

Commit 0b7fd3f

Browse files
committed
issue #591: ansible: restore CWD prior to AnsibleModule initialization.
1 parent 92ac716 commit 0b7fd3f

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

ansible_mitogen/runner.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,22 @@ def _setup_environ(self):
344344
env.update(self.env)
345345
self._env = TemporaryEnvironment(env)
346346

347+
def _revert_cwd(self):
348+
"""
349+
#591: make a best-effort attempt to return to :attr:`good_temp_dir`.
350+
"""
351+
try:
352+
os.chdir(self.good_temp_dir)
353+
except OSError:
354+
LOG.debug('%r: could not restore CWD to %r',
355+
self, self.good_temp_dir)
356+
347357
def revert(self):
348358
"""
349359
Revert any changes made to the process after running a module. The base
350360
implementation simply restores the original environment.
351361
"""
362+
self._revert_cwd()
352363
self._env.revert()
353364
self.revert_temp_dir()
354365

docs/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,29 @@ Enhancements
3030
<https://docs.ansible.com/ansible/latest/plugins/become.html>`_
3131
functionality, which will be addressed in a future release.
3232

33+
3334
Fixes
3435
^^^^^
3536

3637
* `#590 <https://github.com/dw/mitogen/issues/590>`_: the importer can handle
3738
modules that replace themselves in :mod:`sys.modules` during import.
3839

40+
* `#591 <https://github.com/dw/mitogen/issues/591>`_: the target's current
41+
working directory is restored to a known-existent directory between tasks to
42+
ensure :func:`os.getcwd` will not fail when called, in the same way that
43+
:class:`AnsibleModule` restores it during initialization. However this
44+
restore happens before the module ever executes, ensuring any code that calls
45+
:func:`os.getcwd` prior to :class:`AnsibleModule` initialization, such as the
46+
Ansible 2.7 ``pip`` module, cannot fail due to the behavior of a prior task.
47+
3948

4049
Thanks!
4150
~~~~~~~
4251

4352
Mitogen would not be possible without the support of users. A huge thanks for
4453
bug reports, testing, features and fixes in this release contributed by
54+
`Anton Markelov <https://github.com/strangeman>`_,
55+
`Nigel Metheringham <https://github.com/nigelm>`_,
4556
`Orion Poplawski <https://github.com/opoplawski>`_, and
4657
`Ulrich Schreiner <https://github.com/ulrichSchreiner>`_.
4758

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/python
2+
# #591: call os.getcwd() before AnsibleModule ever gets a chance to fix up the
3+
# process environment.
4+
5+
import os
6+
7+
try:
8+
import json
9+
except ImportError:
10+
import simplejson as json
11+
12+
print(json.dumps({
13+
'cwd': os.getcwd()
14+
}))

tests/ansible/regression/all.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
- include: issue_177__copy_module_failing.yml
1010
- include: issue_332_ansiblemoduleerror_first_occurrence.yml
1111
- include: issue_590__sys_modules_crap.yml
12+
- include: issue_591__setuptools_cwd_crash.yml
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# #591: process CWD is not reset before start of module execution. This is
2+
# usually fine, except for modules importing setuptools early, which attempts
3+
# to call getcwd() before AnsibleModule has had a chance to clean up the
4+
# process environment.
5+
6+
- hosts: test-targets
7+
tasks:
8+
- meta: end_play
9+
when: not is_mitogen
10+
11+
- custom_python_run_script:
12+
script: |
13+
import os
14+
os.chdir(module.tmpdir)
15+
16+
# Will crash if process has a nonexistent CWD.
17+
- custom_python_os_getcwd:
18+
script: |
19+
import os
20+
self._connection.get_chain().call(os.getcwd)

0 commit comments

Comments
 (0)