From 52efd7438c44c127e5687200bda2174e17086f68 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Thu, 10 Mar 2016 14:06:41 -0500 Subject: [PATCH] Fixing template/assemble action plugins related to tmp dir use/cleanup --- lib/ansible/plugins/action/__init__.py | 4 ++-- lib/ansible/plugins/action/assemble.py | 18 ++++++++++++++---- lib/ansible/plugins/action/copy.py | 4 ++-- lib/ansible/plugins/action/template.py | 22 +++++++++++++++------- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 093ddd058e5..917e16dec38 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -291,7 +291,7 @@ class ActionBase(with_metaclass(ABCMeta, object)): res = self._low_level_execute_command(cmd, sudoable=sudoable) return res - def _execute_remote_stat(self, path, all_vars, follow): + def _execute_remote_stat(self, path, all_vars, follow, tmp=None): ''' Get information from remote file. ''' @@ -302,7 +302,7 @@ class ActionBase(with_metaclass(ABCMeta, object)): get_checksum=True, checksum_algo='sha1', ) - mystat = self._execute_module(module_name='stat', module_args=module_args, task_vars=all_vars) + mystat = self._execute_module(module_name='stat', module_args=module_args, task_vars=all_vars, tmp=tmp, delete_remote_tmp=(tmp is None)) if 'failed' in mystat and mystat['failed']: raise AnsibleError('Failed to get information on remote file (%s): %s' % (path, mystat['msg'])) diff --git a/lib/ansible/plugins/action/assemble.py b/lib/ansible/plugins/action/assemble.py index 3dc0bad53f5..eeb13c21ae9 100644 --- a/lib/ansible/plugins/action/assemble.py +++ b/lib/ansible/plugins/action/assemble.py @@ -97,8 +97,15 @@ class ActionModule(ActionBase): result['msg'] = "src and dest are required" return result + cleanup_remote_tmp = False + if not tmp: + tmp = self._make_tmp_path() + cleanup_remote_tmp = True + if boolean(remote_src): - result.update(self._execute_module(tmp=tmp, task_vars=task_vars)) + result.update(self._execute_module(tmp=tmp, task_vars=task_vars, delete_remote_tmp=False)) + if cleanup_remote_tmp: + self._remove_tmp_path(tmp) return result elif self._task._role is not None: src = self._loader.path_dwim_relative(self._task._role._role_path, 'files', src) @@ -119,7 +126,7 @@ class ActionModule(ActionBase): path_checksum = checksum_s(path) dest = self._remote_expand_user(dest) - dest_stat = self._execute_remote_stat(dest, all_vars=task_vars, follow=follow) + dest_stat = self._execute_remote_stat(dest, all_vars=task_vars, follow=follow, tmp=tmp) diff = {} @@ -152,11 +159,14 @@ class ActionModule(ActionBase): new_module_args.update( dict( src=xfered,)) - res = self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars, tmp=tmp) + res = self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False) if diff: res['diff'] = diff result.update(res) else: - result.update(self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, tmp=tmp)) + result.update(self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False)) + + if tmp and cleanup_remote_tmp: + self._remove_tmp_path(tmp) return result diff --git a/lib/ansible/plugins/action/copy.py b/lib/ansible/plugins/action/copy.py index a833b28b160..b8094b2bd61 100644 --- a/lib/ansible/plugins/action/copy.py +++ b/lib/ansible/plugins/action/copy.py @@ -169,7 +169,7 @@ class ActionModule(ActionBase): dest_file = self._connection._shell.join_path(dest) # Attempt to get remote file info - dest_status = self._execute_remote_stat(dest_file, all_vars=task_vars, follow=follow) + dest_status = self._execute_remote_stat(dest_file, all_vars=task_vars, follow=follow, tmp=tmp) if dest_status['exists'] and dest_status['isdir']: # The dest is a directory. @@ -182,7 +182,7 @@ class ActionModule(ActionBase): else: # Append the relative source location to the destination and get remote stats again dest_file = self._connection._shell.join_path(dest, source_rel) - dest_status = self._execute_remote_stat(dest_file, all_vars=task_vars, follow=follow) + dest_status = self._execute_remote_stat(dest_file, all_vars=task_vars, follow=follow, tmp=tmp) if dest_status['exists'] and not force: # remote_file does not exist so continue to next iteration. diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py index 22782b225a8..5ddd624bde2 100644 --- a/lib/ansible/plugins/action/template.py +++ b/lib/ansible/plugins/action/template.py @@ -33,21 +33,21 @@ class ActionModule(ActionBase): TRANSFERS_FILES = True - def get_checksum(self, dest, all_vars, try_directory=False, source=None): + def get_checksum(self, dest, all_vars, try_directory=False, source=None, tmp=None): try: - dest_stat = self._execute_remote_stat(dest, all_vars=all_vars, follow=False) + dest_stat = self._execute_remote_stat(dest, all_vars=all_vars, follow=False, tmp=tmp) if dest_stat['exists'] and dest_stat['isdir'] and try_directory and source: base = os.path.basename(source) dest = os.path.join(dest, base) - dest_stat = self._execute_remote_stat(dest, all_vars=all_vars, follow=False) + dest_stat = self._execute_remote_stat(dest, all_vars=all_vars, follow=False, tmp=tmp) except Exception as e: return dict(failed=True, msg=to_bytes(e)) return dest_stat['checksum'] - def run(self, tmp='', task_vars=None): + def run(self, tmp=None, task_vars=None): ''' handler for template operations ''' if task_vars is None: task_vars = dict() @@ -137,8 +137,13 @@ class ActionModule(ActionBase): result['msg'] = type(e).__name__ + ": " + str(e) return result + cleanup_remote_tmp = False + if not tmp: + tmp = self._make_tmp_path() + cleanup_remote_tmp = True + local_checksum = checksum_s(resultant) - remote_checksum = self.get_checksum(dest, task_vars, not directory_prepended, source=source) + remote_checksum = self.get_checksum(dest, task_vars, not directory_prepended, source=source, tmp=tmp) if isinstance(remote_checksum, dict): # Error from remote_checksum is a dict. Valid return is a str result.update(remote_checksum) @@ -170,7 +175,7 @@ class ActionModule(ActionBase): follow=True, ), ) - result.update(self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars)) + result.update(self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False)) if result.get('changed', False) and self._play_context.diff: result['diff'] = diff @@ -189,6 +194,9 @@ class ActionModule(ActionBase): follow=True, ), ) - result.update(self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars)) + result.update(self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False)) + + if tmp and cleanup_remote_tmp: + self._remove_tmp_path(tmp) return result