file.manage_file module needs sensible defaults, and args need documenting
file.managed_file used to work fine from the API in hydrogen with the following: 'arg': [self.statefile, '', '{}', '', '{}', 'root', 'root', '0644', 'base', '', 'contents="' + ' '.join(states) + '"\n']
Now with Helium it fails with unrevealing errors even in debug mode (string index must be integer, not str.), with no way to determine which argument it no longer likes.
- These arguments need to be better documented. What is 'ret'???
- Most of these arguments should have sensible default values.
Calling the module should be as easy as
arg: ['/full/path/to/file', 'root', 'root', '0644', 'conents="some string or [some, list]"] or arg ['/full/path/to/file', 'root', 'root', '0644', source='some_url']
all other args should have sensible defaults. As is, the module is difficult to use at best, requiring a great deal of trial and error to get it right, and apparently suffered undocumented changes from hydrogen->helium such that those painstakingly discovered usable values (most of which are empty strings or empy dicts) no longer work!
Please give the file.manage_file some love! :-)
file.manage_file could definitely use some more documentation love. Thanks for the report. We'll get this fixed up.
I think this module began life as a helper for the file.managed state. Now that it's gaining traction elsewhere we should make it more friendly and easier to use via the CLI or the API either by setting default values for most of the arguments or by making a friendlier function that wraps this one.
It's really handy in some cases to be able to create a file directly with the API. My specific use case is to massively parallelize salt state testing via salt itself, by creating a file in /tmp on dozens of hosts with a list of states to render, and populating a script there that is then run from the api on all hosts in parallel with client=local_async, then gathering the results as they come it. Being able to create those two files, and then run one as a script with the other as an argument, is really cool ... but difficult with the current interface (and not really doable now that it has changed in some way, so I do a much less elegant workaround using a run.cmd command that echos to a file).
So I guess this is a long-winded upvote for my own bug report, saying I would use file.manage_file or a wrapper (file.manage ?) that would allow easy use of templates or content definitions, like is common via a state but done in batch mode from the API instead.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.
I really think this issue should be reopened. file.manage_file has got to be one of the more popular execution modules but is still a mess to work with. Why does it take a fixed number arguments? The documented arguments in the example aren't even correct at the time of writing in the latest stable release (although I see a PR has made it into master recently to address that). It's not surprising it's going to break this way whenever some new functionality needs to be added, but it doesn't have to be this way.
Here's an example of using this execution module to deploy a file from S3 (which looks to be the only way of doing this via an execution module unless I've overlooked something):
sudo salt-call file.manage_file /root/.ssh/salt/id_rsa.pub '' '{}' \
"${s3_key_dir}/id_rsa.pub" \
'{hash_type: "md5", "hsum": ''f0266bf9903751348a207ba385f130a1}' \
root root '0600' '' base ''
What do those '', '{}' and base arguments even mean? It's ugly and not intuitive. It could instead be changed to work like this:
sudo salt-call file.manage_file /root/.ssh/salt/id_rsa.pub \
source="${s3_key_dir}/id_rsa.pub" \
hash_type=md5 hash_checksum=f0266bf9903751348a207ba385f130a1 \
user=root group=root mode=0600
It looks to me like it should be possible to convert the existing positional arguments into supporting the above keyword arguments without breaking backwards compatibility.
It's not even clear to me why hash_type and hash_checksum are mandatory when we're pulling in from a trusted secure source (a private S3 bucket in this case). I think these were probably initially implemented just for use in pulling 3rd party content over http and https, and when the execution module was updated to support S3 sources the new use cases and exiting logic were never properly reevaluated.
I even take issue with the function name! Why do we call it file.manage_file instead of file.manage? It's already obvious it's for managing a file, so maybe we can rename it and set manage_file = file for backwards compatibility? This same issue applies to a number of other execution modules floating around. It seems redundant and I'd love to see this cleaned up.