Implement user,group,mode,selinux settings for unarchive.
This is a partial fix for #234. Still have to figure out how to make change reporting work as we can no longer rely on tar's --compare option
This commit is contained in:
parent
44354e31e4
commit
f9a6ec95b0
1 changed files with 62 additions and 29 deletions
|
@ -76,7 +76,10 @@ EXAMPLES = '''
|
|||
'''
|
||||
|
||||
import os
|
||||
from zipfile import ZipFile
|
||||
|
||||
class UnarchiveError(Exception):
|
||||
pass
|
||||
|
||||
# class to handle .zip files
|
||||
class ZipFile(object):
|
||||
|
@ -86,6 +89,20 @@ class ZipFile(object):
|
|||
self.dest = dest
|
||||
self.module = module
|
||||
self.cmd_path = self.module.get_bin_path('unzip')
|
||||
self._files_in_archive = []
|
||||
|
||||
@property
|
||||
def files_in_archive(self, force_refresh=False):
|
||||
if self._files_in_archive and not force_refresh:
|
||||
return self._files_in_archive
|
||||
|
||||
archive = ZipFile(self.src)
|
||||
try:
|
||||
self._files_in_archive = archive.namelist()
|
||||
except:
|
||||
raise UnarchiveError('Unable to list files in the archive')
|
||||
|
||||
return self._files_in_archive
|
||||
|
||||
def is_unarchived(self):
|
||||
return dict(unarchived=False)
|
||||
|
@ -114,6 +131,22 @@ class TgzFile(object):
|
|||
self.module = module
|
||||
self.cmd_path = self.module.get_bin_path('tar')
|
||||
self.zipflag = 'z'
|
||||
self._files_in_archive = []
|
||||
|
||||
@property
|
||||
def files_in_archive(self, force_refresh=False):
|
||||
if self._files_in_archive and not force_refresh:
|
||||
return self._files_in_archive
|
||||
|
||||
cmd = '%s -t%sf "%s"' % (self.cmd_path, self.zipflag, self.src)
|
||||
rc, out, err = self.module.run_command(cmd)
|
||||
if rc != 0:
|
||||
raise UnarchiveError('Unable to list files in the archive')
|
||||
|
||||
for filename in out.splitlines():
|
||||
if filename:
|
||||
self._files_in_archive.append(filename)
|
||||
return self._files_in_archive
|
||||
|
||||
def is_unarchived(self):
|
||||
cmd = '%s -v -C "%s" --diff -%sf "%s"' % (self.cmd_path, self.dest, self.zipflag, self.src)
|
||||
|
@ -129,41 +162,35 @@ class TgzFile(object):
|
|||
def can_handle_archive(self):
|
||||
if not self.cmd_path:
|
||||
return False
|
||||
cmd = '%s -t%sf "%s"' % (self.cmd_path, self.zipflag, self.src)
|
||||
rc, out, err = self.module.run_command(cmd)
|
||||
if rc == 0:
|
||||
if len(out.splitlines(True)) > 0:
|
||||
|
||||
try:
|
||||
if self.files_in_archive:
|
||||
return True
|
||||
except UnarchiveError:
|
||||
pass
|
||||
# Errors and no files in archive assume that we weren't able to
|
||||
# properly unarchive it
|
||||
return False
|
||||
|
||||
|
||||
# class to handle tar files that aren't compressed
|
||||
class TarFile(TgzFile):
|
||||
def __init__(self, src, dest, module):
|
||||
self.src = src
|
||||
self.dest = dest
|
||||
self.module = module
|
||||
self.cmd_path = self.module.get_bin_path('tar')
|
||||
super(TarFile, self).__init__(src, dest, module)
|
||||
self.zipflag = ''
|
||||
|
||||
|
||||
# class to handle bzip2 compressed tar files
|
||||
class TarBzip(TgzFile):
|
||||
def __init__(self, src, dest, module):
|
||||
self.src = src
|
||||
self.dest = dest
|
||||
self.module = module
|
||||
self.cmd_path = self.module.get_bin_path('tar')
|
||||
super(TarFile, self).__init__(src, dest, module)
|
||||
self.zipflag = 'j'
|
||||
|
||||
|
||||
# class to handle xz compressed tar files
|
||||
class TarXz(TgzFile):
|
||||
def __init__(self, src, dest, module):
|
||||
self.src = src
|
||||
self.dest = dest
|
||||
self.module = module
|
||||
self.cmd_path = self.module.get_bin_path('tar')
|
||||
super(TarFile, self).__init__(src, dest, module)
|
||||
self.zipflag = 'J'
|
||||
|
||||
|
||||
|
@ -193,6 +220,7 @@ def main():
|
|||
src = os.path.expanduser(module.params['src'])
|
||||
dest = os.path.expanduser(module.params['dest'])
|
||||
copy = module.params['copy']
|
||||
file_args = module.load_file_common_arguments(module.params)
|
||||
|
||||
# did tar file arrive?
|
||||
if not os.path.exists(src):
|
||||
|
@ -217,8 +245,7 @@ def main():
|
|||
res_args['check_results'] = handler.is_unarchived()
|
||||
if res_args['check_results']['unarchived']:
|
||||
res_args['changed'] = False
|
||||
module.exit_json(**res_args)
|
||||
|
||||
else:
|
||||
# do the unpack
|
||||
try:
|
||||
res_args['extract_results'] = handler.unarchive()
|
||||
|
@ -226,11 +253,17 @@ def main():
|
|||
module.fail_json(msg="failed to unpack %s to %s" % (src, dest), **res_args)
|
||||
except IOError:
|
||||
module.fail_json(msg="failed to unpack %s to %s" % (src, dest))
|
||||
|
||||
else:
|
||||
res_args['changed'] = True
|
||||
|
||||
# do we need to change perms?
|
||||
for filename in handler.files_in_archive:
|
||||
file_args['path'] = os.path.join(dest, filename)
|
||||
res_args['changed'] = module.set_fs_attributes_if_different(file_args, res_args['changed'])
|
||||
|
||||
module.exit_json(**res_args)
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in a new issue