-
Notifications
You must be signed in to change notification settings - Fork 81
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 resource current value loading to actions, and converge helper #127
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0eef775
Add load and converge proposal
jkeiser 31fe261
Update load-and-converge.md
jkeiser 1b2d45e
Explain unspecified attribute behavior better
jkeiser fd03380
Add is_set?
jkeiser b5e198e
Remove compile-time read from proposal, rename load -> load_actual_value
jkeiser 71d2a02
Rename load_actual_value to load_current_value
jkeiser adc622f
Rename "converge" to "converge_if_changed"
jkeiser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
--- | ||
RFC: unassigned | ||
Author: John Keiser <john@johnkeiser.com> | ||
Status: Draft | ||
Type: Standards Track | ||
--- | ||
|
||
# Easy Resource Load And Converge | ||
|
||
With the introduction of `action` on resources, it becomes useful to have a | ||
blessed way to get the actual value of the resource. This proposal adds `load` and `converge` to help with this purpose, enabling: | ||
|
||
- Low-ceremony load methods (as easy to write as we can make it) | ||
- A super easy converge model that automatically compares current vs. desired | ||
values and prints green text | ||
|
||
## Motivation | ||
|
||
As a Chef resource writer, | ||
I want to be able to read the current value of my resource at converge time, | ||
so that it is easy to tell the difference between current and desired value. | ||
|
||
As a Chef resource writer, | ||
I want a converge model that compares current and desired values for me, | ||
So that the easiest converge to write is the most correct one. | ||
|
||
## Specification | ||
|
||
### `load_actual_value`: in-place resource load | ||
|
||
When using `action`, one needs a way to load the *actual* system value of the resource, so that it can be compared to the desired value and a decision made as to whether to change anything. | ||
|
||
When the resource writer defines `load_actual_value` on the resource class, it can be called to load the real system value into the resource. Before any action runs, this will be used by `load_current_resource` to load the resource. `action` will do some important work before calling the new method: | ||
|
||
1. Create a new instance of the resource with the same name. | ||
2. Copy all non-desired-state values from the desired resource into the new instance. | ||
3. Call `load_actual_value` on the new instance. | ||
|
||
#### Non-existence | ||
|
||
To appropriately handle actual value loading, the user needs a way to specify that the actual value legitimately does not exist (rather than simply not filling in the object and getting `nil`s in it). If `load_actual_value` raises `Chef::Exceptions::ActualValueDoesNotExist`, the new resource will be discarded and `current_resource` becomes `nil`. The `actual_value_does_not_exist!` method can be called to raise this. | ||
|
||
NOTE: The alternative was to have users return `false` if the resource does not exist; but I didn't want users to be forced into the ceremony of a trailing `true` line. | ||
|
||
```ruby | ||
load_actual_value do | ||
# Check for existence before doing anything else. | ||
actual_value_does_not_exist! if !File.exist?(path) | ||
|
||
# Set "mode" on the resource. | ||
mode File.stat(path).mode | ||
end | ||
``` | ||
|
||
The block will also be passed the original (desired) resource as a parameter, in case it is needed. | ||
|
||
#### Inheritance | ||
|
||
`super` in `load_actual_value!` will call the superclass's `load_actual_value!` method. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is actually possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh! I'll fix that one. I was using def load_actual_value! at first. |
||
|
||
#### Handling Multi-Key Resources | ||
|
||
The new resource is created with all properties copied over *except* desired state properties (properties in `ResourceClass.state_properties`). This means `name`, and properties with `identity: true` or `desired_state: false` are copied over. Normal `property` and `attribute` are not. | ||
|
||
```ruby | ||
class DataBagItem < Chef::Resource | ||
# Copied | ||
attribute :item_name, name_attribute: true | ||
attribute :data_bag_name, identity: true | ||
attribute :recursively_delete, desired_state: false | ||
# Not copied: | ||
attribute :data | ||
def load_actual_value! | ||
data Chef::DataBagItem.new(data_bag_name, item_name).data | ||
end | ||
end | ||
``` | ||
|
||
### `converge`: automatic test-and-set | ||
|
||
The new `converge do ... end` syntax is added to actions, which enables a *lot* of help for resource writers to make safe, effective resources. It performs several key tasks common to nearly every resource (which are often not done correctly): | ||
|
||
- Goes through all attributes on the resource and checks whether the desired | ||
value is different from the current value. | ||
- If any attributes are different, prints appropriate green text. | ||
- Honors why-run (and does not call the `converge` block if why-run is enabled). | ||
|
||
```ruby | ||
class File < Chef::Resource | ||
property :path, name_attribute: true | ||
property :content | ||
|
||
load_actual_value do | ||
actual_value_does_not_exist! unless File.exist?(path) | ||
content IO.read(path) | ||
end | ||
|
||
action :create do | ||
converge do | ||
IO.write(path, content) | ||
end | ||
end | ||
end | ||
``` | ||
|
||
#### Desired value = actual value | ||
|
||
> The easiest way to write a resource must be the most correct one. | ||
|
||
There is a subtle pitfall when updating a resource, where the user has set *some* values, but not all. One can easily end up writing a resource which will overwrite perfectly good system properties with their defaults, which can cause instability. If the user does not specify a property, it is generally preferable to preserve its existing value rather than overwrite it. | ||
|
||
To prevent this, referencing the bare property in an `action` will now yield the *actual* value if load_actual_value succeeded, and the *default* value if we are creating a new resource (if `load_actual_value` raised `ActualValueDoesNotExist`). | ||
|
||
```ruby | ||
class File < Chef::Resource | ||
property :path, name_attribute: true | ||
property :mode, default: 0666 | ||
property :content | ||
|
||
load_actual_value do | ||
actual_value_does_not_exist! unless File.exist?(path) | ||
mode File.stat(path).mode | ||
content IO.read(path) | ||
end | ||
|
||
action :create do | ||
converge do | ||
File.chmod(mode, path) | ||
IO.write(path, content) | ||
end | ||
end | ||
end | ||
|
||
file '/x.txt' do | ||
# Before the change, the above code would have modified `mode` to be `0666`. | ||
# After, it leaves `mode` alone. | ||
content 'Hello World' | ||
end | ||
``` | ||
|
||
There will be times when the old behavior of overwriting with defaults is desired. The resource writer can still find out whether `mode` was set with `property_is_set?(:mode)`, and can still access the default value with `new_resource.mode` if it is not set. | ||
|
||
There are no backwards-compatibility issues with this because it only applies to `action`, which has not been released yet. | ||
|
||
#### Compound Resource Convergence | ||
|
||
Some resources perform several different (possibly expensive) operations depending on what is set. `converge :attribute1, :attribute2, ... do` allows the user to target different groups of changes based on exactly which attributes have changed: | ||
|
||
```ruby | ||
class File < Chef::Resource | ||
property :path, name_attribute: true | ||
property :mode | ||
property :content | ||
|
||
load_actual_value do | ||
actual_value_does_not_exist! unless File.exist?(path) | ||
mode File.stat(path).mode | ||
content IO.read(path) | ||
end | ||
|
||
action :create do | ||
converge :mode do | ||
File.chmod(mode, path) | ||
end | ||
converge :content do | ||
IO.write(path, content) | ||
end | ||
end | ||
end | ||
``` | ||
|
||
## Copyright | ||
|
||
This work is in the public domain. In jurisdictions that do not allow for this, this work is available under CC0. To the extent possible under law, the person who associated CC0 with this work has waived all copyright and related or neighboring rights to this work. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems kind of unfortunate, but I agree I don't know of a better option. As an API it seems really silly though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another possibility is
actual_value_exists? { File.exist?(path) }
. Not as much a fan of that, but it can make sense.