add the option to rename the extracted files in the archive state module. This is done using an option of tar (--transform)
authorPaul Tonelli <paul.tonelli@logilab.fr>
Tue, 26 Mar 2013 09:24:07 +0100
changeset 99 bc524028cdc2
parent 98 032c0fd12892
child 100 7a6ea6e5474c
add the option to rename the extracted files in the archive state module. This is done using an option of tar (--transform)
_states/archive.py
--- a/_states/archive.py	Tue Mar 26 09:18:44 2013 +0100
+++ b/_states/archive.py	Tue Mar 26 09:24:07 2013 +0100
@@ -13,7 +13,7 @@
 log = logging.getLogger(__name__)
 
 def extracted(name, source, archive_format, tar_options=None, source_hash=None,
-              if_missing=None):
+              if_missing=None, rename_regexp=None):
     '''
     State that make sure an archive is extracted in a directory.
     The downloaded archive is erased if succesfully extracted.
@@ -29,6 +29,7 @@
         - source_hash: md5=499ae16dcae71eeb7c3a30c75ea7a1a6
         - archive_format: tar
         - tar_options: z
+        - rename_regexp: s/.*\.img/image.img/
         - if_missing: /opt/graylog2-server-0.9.6p1/
 
     name
@@ -49,6 +50,9 @@
         Only used for tar format, it need to be the tar argument specific to
         this archive, such as 'j' for bzip2, 'z' for gzip, '' for uncompressed
         tar, 'J' for LZMA.
+
+    rename_regexp
+        Only used for tar, can be used to rename extracted files using sed
     '''
     ret = {'name': name, 'result': None, 'changes': {}, 'comment': ''}
     valid_archives = ('tar', 'rar', 'zip')
@@ -111,8 +115,9 @@
     else:
         # this is needed until merging PR 2651
         log.debug("Untar %s in %s", filename, name)
-        results = __salt__['cmd.run_all']('tar -xv{0}f {1}'.format(tar_options,
-                                                                   filename),
+        results = __salt__['cmd.run_all']('tar -xv{0}f {1} --transform \'{2}\''.format(tar_options,
+                                                                   filename,
+                                                                   rename_regexp),
                                           cwd=name)
         if results['retcode'] != 0:
             return results