Skip to content

Commit

Permalink
Making Chef::Resource consume the new Chef::RunContext object, as wel…
Browse files Browse the repository at this point in the history
…l as reworking how delayed and immediate notifications are tracked
  • Loading branch information
Chris Walters, Tim Hinderliter authored and danielsdeleo committed Jun 3, 2010
1 parent b34ceac commit 8403e87
Showing 1 changed file with 32 additions and 39 deletions.
71 changes: 32 additions & 39 deletions chef/lib/chef/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,18 @@ class Resource
include Chef::Mixin::Language
include Chef::Mixin::ConvertToClassName

attr_accessor :actions, :params, :provider, :updated, :allowed_actions, :collection, :cookbook_name, :recipe_name, :enclosing_provider
attr_reader :resource_name, :source_line, :node, :not_if_args, :only_if_args
attr_accessor :params, :provider, :updated, :allowed_actions, :collection, :cookbook_name, :recipe_name, :enclosing_provider
attr_reader :resource_name, :source_line, :not_if_args, :only_if_args

# Each notify entry is a resource/action pair, modeled as an
# OpenStruct with a .resource and .action member
attr_reader :notifies_immediate, :notifies_delayed

def initialize(name, collection=nil, node=nil)
def initialize(name, run_context)
@name = name
if collection
@collection = collection
else
@collection = Chef::ResourceCollection.new()
end
@node = node ? node : Chef::Node.new
@run_context = run_context
@noop = nil
@before = nil
@actions = Hash.new
@params = Hash.new
@provider = nil
@allowed_actions = [ :nothing ]
Expand All @@ -57,12 +55,18 @@ def initialize(name, collection=nil, node=nil)
@not_if_args = {}
@only_if = nil
@only_if_args = {}
sline = caller(4).shift
@notifies_immediate = Hash.new
@notifies_delayed = Hash.new
sline = caller(4).shiftw
if sline
@source_line = sline.gsub!(/^(.+):(.+):.+$/, '\1 line \2')
@source_line = ::File.expand_path(@source_line) if @source_line
end
end

def node
run_context.node
end

# If an unknown method is invoked, determine whether the enclosing Provider's
# lexical scope can fulfill the request. E.g. This happens when the Resource's
Expand All @@ -77,7 +81,7 @@ def method_missing(method_symbol, *args, &block)

def load_prior_resource
begin
prior_resource = @collection.lookup(self.to_s)
prior_resource = run_context.resource_collection.lookup(self.to_s)
Chef::Log.debug("Setting #{self.to_s} to the state of the prior #{self.to_s}")
prior_resource.instance_variables.each do |iv|
unless iv.to_sym == :@source_line || iv.to_sym == :@action
Expand Down Expand Up @@ -216,7 +220,7 @@ def to_s
def to_json(*a)
instance_vars = Hash.new
self.instance_variables.each do |iv|
unless iv == "@collection" || iv == "@node"
unless iv == "@run_context"
instance_vars[iv] = self.instance_variable_get(iv)
end
end
Expand Down Expand Up @@ -260,7 +264,7 @@ def not_if(arg=nil, args = {}, &blk)
end

def run_action(action)
provider = Chef::Platform.provider_for_node(@node, self)
provider = Chef::Platform.provider_for_node(node, self)
provider.load_current_resource
provider.send("action_#{action}")
end
Expand Down Expand Up @@ -302,8 +306,8 @@ def build_from_file(cookbook_name, filename)
# default initialize method that ensures that when initialize is finally
# wrapped (see below), super is called in the event that the resource
# definer does not implement initialize
def initialize(name, collection=nil, node=nil)
super(name, collection, node)
def initialize(name, run_context)
super(name, run_context)
end

@actions_to_create = []
Expand All @@ -328,10 +332,9 @@ def actions_to_create
old_init = instance_method(:initialize)

define_method(:initialize) do |name, *optional_args|
collection = optional_args.shift
node = optional_args.shift
args_run_context = optional_args.shift
@resource_name = rname.to_sym
old_init.bind(self).call(name, collection, node)
old_init.bind(self).call(name, args_run_context)
allowed_actions.push(self.class.actions_to_create).flatten!
end
end
Expand Down Expand Up @@ -372,30 +375,20 @@ def lookup_provider_constant(name)
end
end

def check_timing(timing)
unless timing == :delayed || timing == :immediate || timing == :immediately
raise ArgumentError, "Timing must be :delayed or :immediate(ly), you said #{timing}"
end
if timing == :immediately
timing = :immediate
end
timing
end

def notifies_helper(action, resources, timing=:delayed)
timing = check_timing(timing)
rarray = resources.kind_of?(Array) ? resources : [ resources ]
rarray.each do |resource|
action_sym = action.to_sym
if @actions.has_key?(action_sym)
@actions[action_sym][timing] << resource
timing = timing.to_sym
raise ArgumentError, "invalid timing: #{timing}; must be one of: :delayed, :immediate, :immediately" unless (timing == :delayed || timing == :immediate || timing == :immediately)

resource_array = [resources].flatten
resource_array.each do |resource|
new_notify = OpenStruct.new(:resource => resource, :action => action)
if timing == :delayed
notifies_delayed << new_notify
else
@actions[action_sym] = Hash.new
@actions[action_sym][:delayed] = Array.new
@actions[action_sym][:immediate] = Array.new
@actions[action_sym][timing] << resource
notifies_immediate << new_notify
end
end

true
end
end
Expand Down

0 comments on commit 8403e87

Please sign in to comment.