File 0030-Extract-archive-into-existing-directory-add-overwrit.patch of Package salt.4202

From 0a9e782a5aacd66421d50b62a2a9e8675ed58077 Mon Sep 17 00:00:00 2001
From: Bo Maryniuk <bo@suse.de>
Date: Thu, 24 Nov 2016 18:07:33 +0100
Subject: [PATCH 30/38] Extract archive into existing directory, add overwrite

* Fix path source if exists
* Add overwrite option
---
 salt/states/archive.py | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/salt/states/archive.py b/salt/states/archive.py
index 7cdae76..985164e 100644
--- a/salt/states/archive.py
+++ b/salt/states/archive.py
@@ -61,7 +61,8 @@ def extracted(name,
               tar_options=None,
               source_hash=None,
               if_missing=None,
-              keep=False):
+              keep=False,
+              overwrite=False):
     '''
     .. versionadded:: 2014.1.0
 
@@ -167,6 +168,10 @@ def extracted(name,
 
     keep
         Keep the archive in the minion's cache
+
+    overwrite
+        If archive was already extracted, then setting this to True will
+        extract it all over again.
     '''
     ret = {'name': name, 'result': None, 'changes': {}, 'comment': ''}
     valid_archives = ('tar', 'rar', 'zip')
@@ -179,23 +184,27 @@ def extracted(name,
 
     # remove this whole block after formal deprecation.
     if archive_user is not None:
-        warn_until(
-          'Boron',
-          'Passing \'archive_user\' is deprecated.'
-          'Pass \'user\' instead.'
-        )
+        warn_until('Boron', "Passing 'archive_user' is deprecated. Pass 'user' instead.")
         if user is None:
             user = archive_user
 
+    # Adjust, if the destination directory already exists
+    # In this case admins usually are using general directory,
+    # like /tmp or /opt etc.
+    if __salt__['file.directory_exists'](name):
+        name = os.path.join(name, os.path.basename(source))
+        if __salt__['file.directory_exists'](name) and overwrite:
+            __salt__['file.remove'](name)
+            log.debug("Overwrite is requested. Removed '{0}'".format(name))
+
     if not name.endswith('/'):
         name += '/'
 
     if if_missing is None:
         if_missing = name
-    if (
-        __salt__['file.directory_exists'](if_missing)
-        or __salt__['file.file_exists'](if_missing)
-    ):
+
+    if __salt__['file.directory_exists'](if_missing) \
+        or __salt__['file.file_exists'](if_missing):
         ret['result'] = True
         ret['comment'] = '{0} already exists'.format(if_missing)
         return ret
-- 
2.10.2

openSUSE Build Service is sponsored by