Fixing template/assemble action plugins related to tmp dir use/cleanup

This commit is contained in:
James Cammarata 2016-03-10 14:06:41 -05:00
parent b7813fd6fd
commit 52efd7438c
4 changed files with 33 additions and 15 deletions

View file

@ -291,7 +291,7 @@ class ActionBase(with_metaclass(ABCMeta, object)):
res = self._low_level_execute_command(cmd, sudoable=sudoable) res = self._low_level_execute_command(cmd, sudoable=sudoable)
return res 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. Get information from remote file.
''' '''
@ -302,7 +302,7 @@ class ActionBase(with_metaclass(ABCMeta, object)):
get_checksum=True, get_checksum=True,
checksum_algo='sha1', 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']: if 'failed' in mystat and mystat['failed']:
raise AnsibleError('Failed to get information on remote file (%s): %s' % (path, mystat['msg'])) raise AnsibleError('Failed to get information on remote file (%s): %s' % (path, mystat['msg']))

View file

@ -97,8 +97,15 @@ 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
if not tmp:
tmp = self._make_tmp_path()
cleanup_remote_tmp = True
if boolean(remote_src): 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 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)
@ -119,7 +126,7 @@ class ActionModule(ActionBase):
path_checksum = checksum_s(path) path_checksum = checksum_s(path)
dest = self._remote_expand_user(dest) 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 = {} diff = {}
@ -152,11 +159,14 @@ class ActionModule(ActionBase):
new_module_args.update( dict( src=xfered,)) 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: if diff:
res['diff'] = diff res['diff'] = diff
result.update(res) result.update(res)
else: 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 return result

View file

@ -169,7 +169,7 @@ class ActionModule(ActionBase):
dest_file = self._connection._shell.join_path(dest) dest_file = self._connection._shell.join_path(dest)
# Attempt to get remote file info # 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']: if dest_status['exists'] and dest_status['isdir']:
# The dest is a directory. # The dest is a directory.
@ -182,7 +182,7 @@ class ActionModule(ActionBase):
else: else:
# Append the relative source location to the destination and get remote stats again # Append the relative source location to the destination and get remote stats again
dest_file = self._connection._shell.join_path(dest, source_rel) 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: if dest_status['exists'] and not force:
# remote_file does not exist so continue to next iteration. # remote_file does not exist so continue to next iteration.

View file

@ -33,21 +33,21 @@ class ActionModule(ActionBase):
TRANSFERS_FILES = True 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: 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: if dest_stat['exists'] and dest_stat['isdir'] and try_directory and source:
base = os.path.basename(source) base = os.path.basename(source)
dest = os.path.join(dest, base) 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: except Exception as e:
return dict(failed=True, msg=to_bytes(e)) return dict(failed=True, msg=to_bytes(e))
return dest_stat['checksum'] return dest_stat['checksum']
def run(self, tmp='', task_vars=None): def run(self, tmp=None, task_vars=None):
''' handler for template operations ''' ''' handler for template operations '''
if task_vars is None: if task_vars is None:
task_vars = dict() task_vars = dict()
@ -137,8 +137,13 @@ 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
if not tmp:
tmp = self._make_tmp_path()
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) remote_checksum = self.get_checksum(dest, task_vars, not directory_prepended, source=source, tmp=tmp)
if isinstance(remote_checksum, dict): if isinstance(remote_checksum, dict):
# Error from remote_checksum is a dict. Valid return is a str # Error from remote_checksum is a dict. Valid return is a str
result.update(remote_checksum) result.update(remote_checksum)
@ -170,7 +175,7 @@ class ActionModule(ActionBase):
follow=True, 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: if result.get('changed', False) and self._play_context.diff:
result['diff'] = diff result['diff'] = diff
@ -189,6 +194,9 @@ class ActionModule(ActionBase):
follow=True, 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 return result