Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Added clean_top argument for the archive state #54013

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions salt/states/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def extracted(name,
force=False,
overwrite=False,
clean=False,
clean_top=False,
user=None,
group=None,
if_missing=None,
Expand Down Expand Up @@ -526,6 +527,9 @@ def extracted(name,

.. versionadded:: 2016.11.1

clean_top : False
Set this to ``True`` to remove a top level directory before the extration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extraction


user
The user to own each extracted file. Not available on Windows.

Expand Down Expand Up @@ -1080,6 +1084,11 @@ def extracted(name,
))
return ret

if clean and clean_top:
ret['comment'] = "You can't set both 'clean' and 'clean_top' to True."
ret['result'] = False
return ret

extraction_needed = overwrite
contents_missing = False

Expand Down Expand Up @@ -1152,6 +1161,15 @@ def extracted(name,
)
)
return ret
if __opts__['test'] and clean_top and contents is not None:
ret['result'] = None
ret['comment'] += (
' Since the \'clean_top\' option is enabled, the '
'destination top directory would be removed first '
'and than re-created and the archive would be '
'extracted'
)
return ret

# Skip notices of incorrect types if we're cleaning
if not (clean and contents is not None):
Expand Down Expand Up @@ -1220,6 +1238,26 @@ def extracted(name,
_add_explanation(ret, source_hash_trigger, contents_missing)
return ret

if clean_top and contents is not None:
errors = []
log.debug('Removing directory %s due to clean_top set to True', name)
try:
salt.utils.files.rm_rf(name.rstrip(os.sep))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename the directory, if the archive.extract is successful, then delete, otherwise roll it back

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure here.
First of all, it adds complexity + a hidden requirement for a 2x space(to keep both old and new dir at some point of time).
Second is that the current clean: True logic doesn't do it and I feel like it's more or less the same case.

ret['changes'].setdefault(
'removed', "Directory {} was removed prior to the extraction".format(name))
except OSError as exc:
if exc.errno != errno.ENOENT:
errors.append(exc.__str__())
if errors:
msg = (
'Unable to remove the directory {}. The following '
'errors were observed:\n'.format(name)
)
for error in errors:
msg += '\n- {0}'.format(error)
ret['comment'] = msg
return ret

if clean and contents is not None:
errors = []
log.debug('Cleaning archive paths from within %s', name)
Expand Down