only_if / not_if command guard with lazy behavior
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 8.2k
- Forks
- 2.5k
- Avg merge
- 4d 5h
- Merged PRs (30d)
- 23
Description
Describe the Enhancement:
When using only_if / not_if with a String, i.e. passing in a command instead of Ruby code, it does not appear easily possible to lazily evaluate node attributes.
For example, the following code will evaluate the value of node['some-attribute'] at compile-time:
file "foo" do
content "bar"
only_if "some-command #{node['some-attribute']}"
end
It'd be nice to have something like the following, but I realize that implementing this literally may be confusing since only_if already lazily evaluates Ruby code (see also https://github.com/chef/chef/issues/10243).
file "foo" do
content "bar"
only_if lazy { "some-command #{node['some-attribute']}" }
end
Describe the Need:
I don't know exactly how common this need is, but it seems like there's an opportunity for syntactic sugar here that would make writing these kinds of things more pleasant.
Current Alternative
It is possible for a user to define a custom resource, or do something like only_if { shell_out("some-command #{node['some-attribute']}").status.success? }.
Can We Help You Implement This?:
Sure, I think the relevant code is here: https://github.com/chef/chef/blob/4ce99c419028c169aeb3e68ec954b79f20e654c5/lib/chef/resource/conditional.rb#L56-L77
The tough part would be deciding what the syntax should be, because lazy might not be a good fit since it could be confusing. I imagine one way might be to have the user pass an Array, and have the guard_interpreter first de-lazy any array elements that are lazy, for example:
file "foo" do
content "bar"
only_if ['some-command', lazy { node['some-attribute'] }]
end
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with lib/chef/resource/conditional.rb around lines 56–77 and review issue #10243 for the existing lazy Ruby behavior. Decide and document a non-confusing syntax for lazily evaluating command-string guards, then add coverage showing node attributes are evaluated when the guard runs rather than at compile time.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- devops
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100