issue #177: populate Shell.tempdir global on creating a tempdir.

It looks a lot like multiple calls to _make_tmp_path() will result in
multiple temporary directories on the remote machine, only the last of
which will be cleaned up.

We must be bug-for-bug compatible for now, so ignore the problem in the
meantime.
pull/193/head
David Wilson 6 years ago
parent 6eed3aa1fa
commit cf25437019

@ -34,6 +34,7 @@ import random
import re
import stat
import subprocess
import tempfile
import threading
import mitogen.core
@ -173,6 +174,21 @@ def _async_main(job_id, module, raw_params, args, env):
_result_by_job_id[job_id] = rc
def make_temp_directory:(base_dir):
"""
Handle creation of `base_dir` if it is absent, in addition to a unique
temporary directory within `base_dir`.
:returns:
Newly created temporary directory.
"""
if not os.path.exists(base_dir):
os.makedirs(base_dir, mode=int('0700', 8))
return tempfile.mkdtemp(
dir=base_dir,
prefix='ansible-mitogen-tmp-',
)
def run_module_async(module, raw_params=None, args=None):
"""
Arrange for an Ansible module to be executed in a thread of the current

@ -198,9 +198,20 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase):
with an actual call to mkdtemp().
"""
LOG.debug('_make_tmp_path(remote_user=%r)', remote_user)
path = self.call(tempfile.mkdtemp, prefix='ansible-mitogen-tmp-')
# _make_tmp_path() is basically a global stashed away as Shell.tmpdir.
# The copy action plugin violates layering and grabs this attribute
# directly.
self._connection._shell.tmpdir = self.call(
ansible_mitogen.helpers.make_temp_directory:,
base_dir=self._remote_expand_user(
# ~/.ansible
self._connection._shell.get_option('remote_tmp')
)
)
LOG.debug('Temporary directory: %r', self._connection._shell.tmpdir)
self._cleanup_remote_tmp = True
return path
return self._connection._shell.tmpdir
def _remove_tmp_path(self, tmp_path):
"""
@ -208,8 +219,11 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase):
shutil.rmtree().
"""
LOG.debug('_remove_tmp_path(%r)', tmp_path)
if tmp_path is None:
tmp_path = self._connection._shell.tmpdir
if self._should_remove_tmp_path(tmp_path):
return self.call(shutil.rmtree, tmp_path)
self.call(shutil.rmtree, tmp_path)
self._connection._shell.tmpdir = None
def _transfer_data(self, remote_path, data):
"""

Loading…
Cancel
Save