Last active
December 10, 2015 23:08
-
-
Save coderanger/4506662 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Chef | |
class SubResourceCollection < ResourceCollection | |
def initialize(parent) | |
@parent = parent | |
super() | |
end | |
def lookup(resource) | |
super | |
rescue Chef::Exceptions::ResourceNotFound | |
@parent.lookup(resource) | |
end | |
end | |
class Provider | |
class << self | |
def notifying_action(key, &block) | |
action key do | |
# So that we can refer to these within the sub-run-context | |
# block. | |
cached_new_resource = new_resource | |
cached_current_resource = current_resource | |
# Setup a sub-run-context. | |
sub_run_context = @run_context.dup | |
sub_run_context.resource_collection = | |
Chef::SubResourceCollection.new(@run_context.resource_collection) | |
# Declare sub-resources within the sub-run-context. Since they | |
# are declared here, they do not pollute the parent run-context. | |
begin | |
original_run_context, @run_context = | |
@run_context, sub_run_context | |
instance_eval(&block) | |
ensure | |
@run_context = original_run_context | |
end | |
# Converge the sub-run-context inside the provider action. | |
# Make sure to mark the resource as updated-by-last-action if | |
# any sub-run-context resources were updated (any actual | |
# actions taken against the system) during the | |
# sub-run-context convergence. | |
begin | |
Chef::Runner.new(sub_run_context).converge | |
ensure | |
if sub_run_context.resource_collection.any?(&:updated?) | |
new_resource.updated_by_last_action(true) | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment