Ensure action plugins remove tmp dirs created (#15501)

fixes #14917
This commit is contained in:
Brian Coca 2016-04-20 13:39:12 -04:00
parent 67e6bd18e4
commit a5d79a39d5
8 changed files with 29 additions and 18 deletions

View file

@ -64,6 +64,7 @@ class ActionBase(with_metaclass(ABCMeta, object)):
# Backwards compat: self._display isn't really needed, just import the global display and use that. # Backwards compat: self._display isn't really needed, just import the global display and use that.
self._display = display self._display = display
self._cleanup_remote_tmp = False
self._supports_check_mode = True self._supports_check_mode = True
@abstractmethod @abstractmethod
@ -254,7 +255,7 @@ class ActionBase(with_metaclass(ABCMeta, object)):
def _remove_tmp_path(self, tmp_path): def _remove_tmp_path(self, tmp_path):
'''Remove a temporary path we created. ''' '''Remove a temporary path we created. '''
if tmp_path and "-tmp-" in tmp_path: if tmp_path and self._cleanup_remote_tmp and not C.DEFAULT_KEEP_REMOTE_FILES and "-tmp-" in tmp_path:
cmd = self._connection._shell.remove(tmp_path, recurse=True) cmd = self._connection._shell.remove(tmp_path, recurse=True)
# If we have gotten here we have a working ssh configuration. # If we have gotten here we have a working ssh configuration.
# If ssh breaks we could leave tmp directories out on the remote system. # If ssh breaks we could leave tmp directories out on the remote system.

View file

@ -97,16 +97,14 @@ class ActionModule(ActionBase):
result['msg'] = "src and dest are required" result['msg'] = "src and dest are required"
return result return result
cleanup_remote_tmp = False
remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
if not tmp: if not tmp:
tmp = self._make_tmp_path(remote_user) tmp = self._make_tmp_path(remote_user)
cleanup_remote_tmp = True self._cleanup_remote_tmp = True
if boolean(remote_src): if boolean(remote_src):
result.update(self._execute_module(tmp=tmp, task_vars=task_vars, delete_remote_tmp=False)) result.update(self._execute_module(tmp=tmp, task_vars=task_vars, delete_remote_tmp=False))
if cleanup_remote_tmp: self._remove_tmp_path(tmp)
self._remove_tmp_path(tmp)
return result return result
elif self._task._role is not None: elif self._task._role is not None:
src = self._loader.path_dwim_relative(self._task._role._role_path, 'files', src) src = self._loader.path_dwim_relative(self._task._role._role_path, 'files', src)
@ -166,7 +164,6 @@ class ActionModule(ActionBase):
else: else:
result.update(self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False)) 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)
self._remove_tmp_path(tmp)
return result return result

View file

@ -41,6 +41,7 @@ class ActionModule(ActionBase):
remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
if not tmp: if not tmp:
tmp = self._make_tmp_path(remote_user) tmp = self._make_tmp_path(remote_user)
self._cleanup_remote_tmp=True
module_name = self._task.action module_name = self._task.action
async_module_path = self._connection._shell.join_path(tmp, 'async_wrapper') async_module_path = self._connection._shell.join_path(tmp, 'async_wrapper')
@ -91,8 +92,7 @@ class ActionModule(ActionBase):
result.update(self._low_level_execute_command(cmd=async_cmd)) result.update(self._low_level_execute_command(cmd=async_cmd))
# clean up after # clean up after
if tmp and "tmp" in tmp and not C.DEFAULT_KEEP_REMOTE_FILES: self._remove_tmp_path(tmp)
self._remove_tmp_path(tmp)
result['changed'] = True result['changed'] = True

View file

@ -145,6 +145,7 @@ class ActionModule(ActionBase):
if not delete_remote_tmp: if not delete_remote_tmp:
if tmp is None or "-tmp-" not in tmp: if tmp is None or "-tmp-" not in tmp:
tmp = self._make_tmp_path(remote_user) tmp = self._make_tmp_path(remote_user)
self._cleanup_remote_tmp = True
# expand any user home dir specifier # expand any user home dir specifier
dest = self._remote_expand_user(dest) dest = self._remote_expand_user(dest)
@ -161,6 +162,7 @@ class ActionModule(ActionBase):
if local_checksum is None: if local_checksum is None:
result['failed'] = True result['failed'] = True
result['msg'] = "could not find src=%s" % source_full result['msg'] = "could not find src=%s" % source_full
self._remove_tmp_path(tmp)
return result return result
# This is kind of optimization - if user told us destination is # This is kind of optimization - if user told us destination is
@ -179,6 +181,7 @@ class ActionModule(ActionBase):
if content is not None: if content is not None:
# If source was defined as content remove the temporary file and fail out. # If source was defined as content remove the temporary file and fail out.
self._remove_tempfile_if_content_defined(content, content_tempfile) self._remove_tempfile_if_content_defined(content, content_tempfile)
self._remove_tmp_path(tmp)
result['failed'] = True result['failed'] = True
result['msg'] = "can not use content with a dir as dest" result['msg'] = "can not use content with a dir as dest"
return result return result
@ -200,6 +203,7 @@ class ActionModule(ActionBase):
if delete_remote_tmp: if delete_remote_tmp:
if tmp is None or "-tmp-" not in tmp: if tmp is None or "-tmp-" not in tmp:
tmp = self._make_tmp_path(remote_user) tmp = self._make_tmp_path(remote_user)
self._cleanup_remote_tmp = True
if self._play_context.diff and not raw: if self._play_context.diff and not raw:
diffs.append(self._get_diff_data(dest_file, source_full, task_vars)) diffs.append(self._get_diff_data(dest_file, source_full, task_vars))
@ -242,7 +246,7 @@ class ActionModule(ActionBase):
) )
) )
module_return = self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars, delete_remote_tmp=delete_remote_tmp) module_return = self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=delete_remote_tmp)
module_executed = True module_executed = True
else: else:
@ -267,13 +271,15 @@ class ActionModule(ActionBase):
) )
# Execute the file module. # Execute the file module.
module_return = self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, delete_remote_tmp=delete_remote_tmp) module_return = self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=delete_remote_tmp)
module_executed = True module_executed = True
if not module_return.get('checksum'): if not module_return.get('checksum'):
module_return['checksum'] = local_checksum module_return['checksum'] = local_checksum
if module_return.get('failed'): if module_return.get('failed'):
result.update(module_return) result.update(module_return)
if not delete_remote_tmp:
self._remove_tmp_path(tmp)
return result return result
if module_return.get('changed'): if module_return.get('changed'):
changed = True changed = True
@ -284,7 +290,7 @@ class ActionModule(ActionBase):
module_return['dest'] = module_return['path'] module_return['dest'] = module_return['path']
# Delete tmp path if we were recursive or if we did not execute a module. # Delete tmp path if we were recursive or if we did not execute a module.
if (not C.DEFAULT_KEEP_REMOTE_FILES and not delete_remote_tmp) or (not C.DEFAULT_KEEP_REMOTE_FILES and delete_remote_tmp and not module_executed): if not delete_remote_tmp or (delete_remote_tmp and not module_executed):
self._remove_tmp_path(tmp) self._remove_tmp_path(tmp)
if module_executed and len(source_files) == 1: if module_executed and len(source_files) == 1:

View file

@ -54,6 +54,7 @@ class ActionModule(ActionBase):
# create the remote tmp dir if needed, and put the source file there # create the remote tmp dir if needed, and put the source file there
if tmp is None or "-tmp-" not in tmp: if tmp is None or "-tmp-" not in tmp:
tmp = self._make_tmp_path(remote_user) tmp = self._make_tmp_path(remote_user)
self._cleanup_remote_tmp = True
tmp_src = self._connection._shell.join_path(tmp, os.path.basename(src)) tmp_src = self._connection._shell.join_path(tmp, os.path.basename(src))
self._transfer_file(src, tmp_src) self._transfer_file(src, tmp_src)
@ -68,4 +69,5 @@ class ActionModule(ActionBase):
) )
result.update(self._execute_module('patch', module_args=new_module_args, task_vars=task_vars)) result.update(self._execute_module('patch', module_args=new_module_args, task_vars=task_vars))
self._remove_tmp_path(tmp)
return result return result

View file

@ -42,6 +42,7 @@ class ActionModule(ActionBase):
remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
if not tmp: if not tmp:
tmp = self._make_tmp_path(remote_user) tmp = self._make_tmp_path(remote_user)
self._cleanup_remote_tmp = True
creates = self._task.args.get('creates') creates = self._task.args.get('creates')
if creates: if creates:
@ -49,6 +50,7 @@ class ActionModule(ActionBase):
# and the filename already exists. This allows idempotence # and the filename already exists. This allows idempotence
# of command executions. # of command executions.
if self._remote_file_exists(creates): if self._remote_file_exists(creates):
self._remove_tmp_path(tmp)
return dict(skipped=True, msg=("skipped, since %s exists" % creates)) return dict(skipped=True, msg=("skipped, since %s exists" % creates))
removes = self._task.args.get('removes') removes = self._task.args.get('removes')
@ -57,6 +59,7 @@ class ActionModule(ActionBase):
# and the filename does not exist. This allows idempotence # and the filename does not exist. This allows idempotence
# of command executions. # of command executions.
if not self._remote_file_exists(removes): if not self._remote_file_exists(removes):
self._remove_tmp_path(tmp)
return dict(skipped=True, msg=("skipped, since %s does not exist" % removes)) return dict(skipped=True, msg=("skipped, since %s does not exist" % removes))
# the script name is the first item in the raw params, so we split it # the script name is the first item in the raw params, so we split it
@ -87,8 +90,7 @@ class ActionModule(ActionBase):
result.update(self._low_level_execute_command(cmd=script_cmd, sudoable=True)) result.update(self._low_level_execute_command(cmd=script_cmd, sudoable=True))
# clean up after # clean up after
if tmp and "tmp" in tmp and not C.DEFAULT_KEEP_REMOTE_FILES: self._remove_tmp_path(tmp)
self._remove_tmp_path(tmp)
result['changed'] = True result['changed'] = True

View file

@ -138,11 +138,10 @@ class ActionModule(ActionBase):
result['msg'] = type(e).__name__ + ": " + str(e) result['msg'] = type(e).__name__ + ": " + str(e)
return result return result
cleanup_remote_tmp = False
remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
if not tmp: if not tmp:
tmp = self._make_tmp_path(remote_user) tmp = self._make_tmp_path(remote_user)
cleanup_remote_tmp = True self._cleanup_remote_tmp = True
local_checksum = checksum_s(resultant) local_checksum = checksum_s(resultant)
remote_checksum = self.get_checksum(dest, task_vars, not directory_prepended, source=source, tmp=tmp) remote_checksum = self.get_checksum(dest, task_vars, not directory_prepended, source=source, tmp=tmp)
@ -197,7 +196,6 @@ class ActionModule(ActionBase):
) )
result.update(self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False)) 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)
self._remove_tmp_path(tmp)
return result return result

View file

@ -48,6 +48,7 @@ class ActionModule(ActionBase):
remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
if not tmp: if not tmp:
tmp = self._make_tmp_path(remote_user) tmp = self._make_tmp_path(remote_user)
self._cleanup_remote_tmp = True
if creates: if creates:
# do not run the command if the line contains creates=filename # do not run the command if the line contains creates=filename
@ -58,6 +59,7 @@ class ActionModule(ActionBase):
if stat and stat.get('exists', False): if stat and stat.get('exists', False):
result['skipped'] = True result['skipped'] = True
result['msg'] = "skipped, since %s exists" % creates result['msg'] = "skipped, since %s exists" % creates
self._remove_tmp_path(tmp)
return result return result
dest = self._remote_expand_user(dest) # CCTODO: Fix path for Windows hosts. dest = self._remote_expand_user(dest) # CCTODO: Fix path for Windows hosts.
@ -73,10 +75,12 @@ class ActionModule(ActionBase):
if remote_checksum == '4': if remote_checksum == '4':
result['failed'] = True result['failed'] = True
result['msg'] = "python isn't present on the system. Unable to compute checksum" result['msg'] = "python isn't present on the system. Unable to compute checksum"
self._remove_tmp_path(tmp)
return result return result
elif remote_checksum != '3': elif remote_checksum != '3':
result['failed'] = True result['failed'] = True
result['msg'] = "dest '%s' must be an existing dir" % dest result['msg'] = "dest '%s' must be an existing dir" % dest
self._remove_tmp_path(tmp)
return result return result
if copy: if copy:
@ -109,4 +113,5 @@ class ActionModule(ActionBase):
# execute the unarchive module now, with the updated args # execute the unarchive module now, with the updated args
result.update(self._execute_module(module_args=new_module_args, task_vars=task_vars)) result.update(self._execute_module(module_args=new_module_args, task_vars=task_vars))
self._remove_tmp_path(tmp)
return result return result