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

Add file pruned state #62179

Merged
merged 15 commits into from
Aug 3, 2022
Merged

Conversation

nicholasmhughes
Copy link
Collaborator

@nicholasmhughes nicholasmhughes commented Jun 16, 2022

What does this PR do?

This PR adds a file.pruned state which can delete empty directories within a given root.

What issues does this PR fix or reference?

Fixes: #62178

Previous Behavior

No native state supported this workflow.

New Behavior

Empty directories can easily be pruned from a tree. Note that the file.rmdir execution module operation was also slightly changed. Errors encountered previously returned the error string. This was changed to a more uniform boolean return. This is unlikely to affect any logic as users were most likely checking for True returns on success and then using a broad else statement, because "falsey" behavior wasn't possible with True vs str returns.

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes

Please review Salt's Contributing Guide for best practices.

See GitHub's page on GPG signing for more information about signing commits with GPG.

@nicholasmhughes nicholasmhughes requested a review from a team as a code owner June 16, 2022 13:44
@nicholasmhughes nicholasmhughes requested review from dwoz and removed request for a team June 16, 2022 13:44
@Ch3LL
Copy link
Contributor

Ch3LL commented Jun 28, 2022

looks like the windows test failures are related to these changes

@OrangeDog
Copy link
Contributor

I feel like this would be better as a recursive argument to file.absent than as a whole separate state.

@nicholasmhughes
Copy link
Collaborator Author

file-absent deletes directories regardless of whether they have contents. I felt that it was too risky for users to leverage a parameter to change that behavior because the results of forgetting to include that option would be catastrophic and result in angry users that deleted something important. Much safer to segment the behavior out into a separate state.

@nicholasmhughes
Copy link
Collaborator Author

re-run pr-debian-10-amd64-py3-pytest

@nicholasmhughes
Copy link
Collaborator Author

re-run pr-ubuntu-2004-amd64-py3-tcp-pytest

@OrangeDog
Copy link
Contributor

Also, rmdir is a verb (action) not an adjective (state).

@nicholasmhughes
Copy link
Collaborator Author

open to suggestions

@nicholasmhughes
Copy link
Collaborator Author

re-run pr-centos-7-x86_64-py3-m2crypto-pytest

@nicholasmhughes nicholasmhughes added the Sulfur v3006.0 release code name and version label Jul 8, 2022
@Ch3LL
Copy link
Contributor

Ch3LL commented Jul 11, 2022

looks like there is a merge conflict. Also maybe dir_absent instead of rmdir?

Let me ask the rest of the core team what they think of the naming of the state function here.

@whytewolf
Copy link
Collaborator

personally this seems more like a pruned_tree more than absent or rmdir.

@nicholasmhughes
Copy link
Collaborator Author

re-run pr-debian-11-amd64-py3-pytest

1 similar comment
@nicholasmhughes
Copy link
Collaborator Author

re-run pr-debian-11-amd64-py3-pytest

Copy link
Contributor

@waynew waynew left a comment

Choose a reason for hiding this comment

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

Definitely love the idea behind this! @Ch3LL asked for some feedback:

  • I don't like rmdir as the state name - sometimes we can't help but to name the states as verbs instead of adjectives, but generally .removed or .absent for states, with .remove or similar action words for the execution modules. I like Thomas' pruned suggestion. It's better than dir_absent which was the only thing I could think of 😂 That also naturally leads to the module being prune and the state pruned, which I like a lot. I guess that would mean that we'd just leave rmdir as is and create a prune module, if we wanted to make that change.
  • I have some thoughts about the deleting what we can but then failing. Maybe it doesn't matter and it's OK to partially fail. We could check that at least the folders are empty before attempting to delete all of them - basically do the existing for loop but instead of
os.rmdir(subdir_path)
deleted.append(subdir_path)

do something like

if os.listdir(subdir_path):
    errors.append([subdir_path, "Directory is not empty"])
else:
    to_delete.append(subdir_path)

for dir in to_delete:
    try:
        os.rmdir(dir)
    except OSError as exc:
        ...

But... I don't know if that's super important 🤷 - anyway, just my thoughts - I don't feel super strongly about them (aside from liking adjectives for states and verbs for modules)

@nicholasmhughes
Copy link
Collaborator Author

re-run pr-ubuntu-2204-amd64-py3-pytest

@nicholasmhughes nicholasmhughes changed the title Add file rmdir state Add file pruned state Jul 13, 2022
@Ch3LL
Copy link
Contributor

Ch3LL commented Jul 14, 2022

@waynew and @whytewolf can you re-review please

@Ch3LL Ch3LL requested a review from whytewolf July 14, 2022 17:15
Copy link
Contributor

@waynew waynew left a comment

Choose a reason for hiding this comment

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

I'm good with this 👍 I did just realize that it might just be worth noting in the documentation that it will take the steamroller approach - i.e. it will prune whatever directories it can, rather than bailing out either at the first error or looking before it leaps.

I don't think it should hold up merging the PR but it might be worth a follow-up PR 🤷

@Ch3LL Ch3LL merged commit a210dbc into saltstack:master Aug 3, 2022
@nicholasmhughes nicholasmhughes deleted the add-file-rmdir-state branch August 8, 2022 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Sulfur v3006.0 release code name and version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] Add a rmdir state
5 participants