From cf25437019abb688861ebb89d9b7354011bb1790 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 1 Apr 2018 15:58:44 +0100 Subject: [PATCH] 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. --- ansible_mitogen/helpers.py | 16 ++++++++++++++++ ansible_mitogen/mixins.py | 20 +++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ansible_mitogen/helpers.py b/ansible_mitogen/helpers.py index 1b686d7b..50dd3cec 100644 --- a/ansible_mitogen/helpers.py +++ b/ansible_mitogen/helpers.py @@ -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 diff --git a/ansible_mitogen/mixins.py b/ansible_mitogen/mixins.py index 70f63ec6..151358f9 100644 --- a/ansible_mitogen/mixins.py +++ b/ansible_mitogen/mixins.py @@ -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): """