summaryrefslogtreecommitdiff
path: root/lib/chef/resource.rb
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2019-06-17 21:07:08 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2019-08-12 11:29:16 -0700
commit35a63ddc192e50c45f6e94a3b270ed0e75c93668 (patch)
treefed93952978c3b75febae5d90f80e1f0cd5fcc8b /lib/chef/resource.rb
parent84da5f7a45d7ccba250d160626e6da8762a7f222 (diff)
downloadchef-35a63ddc192e50c45f6e94a3b270ed0e75c93668.tar.gz
Add unified_mode switch for resources
This is inspired by "use_inline_resources". Setting `unified_mode false` in a resource would be the existing behavior with separate compile/converge phases. Setting `unified_mode true` in a resource will eliminate the converge phase. Reverse notifications and delayed notifications will still fire. The resource action will behave like all resources are executing at compile time. As a aside, notifications have never worked for resources firing at compile time. This implementation gets that behavior correct so that notifications will work. Of course forward immediate notifications to resources not yet declared will not be possible. Setting `resource_unified_mode_default true` in `Chef::Config` would turn off the split compile/converge mode for every custom resource. NOTE: This does not affect recipe mode at all. Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib/chef/resource.rb')
-rw-r--r--lib/chef/resource.rb48
1 files changed, 28 insertions, 20 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 7f0895d6c9..a413e3e8d4 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -182,7 +182,7 @@ class Chef
{ action: { kind_of: Symbol, equal_to: allowed_actions } }
)
# the resource effectively sends a delayed notification to itself
- run_context.add_delayed_action(Notification.new(self, action, self))
+ run_context.add_delayed_action(Notification.new(self, action, self, run_context.unified_mode))
end
end
@@ -453,7 +453,6 @@ class Chef
#
attr_reader :elapsed_time
- #
# @return [Boolean] If the resource was executed by the runner
#
attr_accessor :executed_by_runner
@@ -985,6 +984,16 @@ class Chef
resource_name automatic_name
end
+ # If the resource's action should run in separated compile/converge mode.
+ #
+ # @param flag [Boolean] value to set unified_mode to
+ # @return [Boolean] unified_mode value
+ def self.unified_mode(flag = nil)
+ @unified_mode = Chef::Config[:resource_unified_mode_default] if @unified_mode.nil?
+ @unified_mode = flag unless flag.nil?
+ !!@unified_mode
+ end
+
#
# The list of allowed actions for the resource.
#
@@ -1038,7 +1047,6 @@ class Chef
default_action action_name
end
- #
# Define an action on this resource.
#
# The action is defined as a *recipe* block that will be compiled and then
@@ -1076,7 +1084,6 @@ class Chef
default_action action if Array(default_action) == [:nothing]
end
- #
# Define a method to load up this resource's properties with the current
# actual values.
#
@@ -1087,7 +1094,6 @@ class Chef
define_method(:load_current_value!, &load_block)
end
- #
# Call this in `load_current_value` to indicate that the value does not
# exist and that `current_resource` should therefore be `nil`.
#
@@ -1097,7 +1103,6 @@ class Chef
raise Chef::Exceptions::CurrentValueDoesNotExist
end
- #
# Get the current actual value of this resource.
#
# This does not cache--a new value will be returned each time.
@@ -1154,7 +1159,6 @@ class Chef
end
end
- #
# Ensure the action class actually gets created. This is called
# when the user does `action :x do ... end`.
#
@@ -1211,22 +1215,27 @@ class Chef
# @return [Chef::RunContext] The run context for this Resource. This is
# where the context for the current Chef run is stored, including the node
# and the resource collection.
+ #
attr_accessor :run_context
# @return [Mixlib::Log::Child] The logger for this resources. This is a child
# of the run context's logger, if one exists.
+ #
attr_reader :logger
# @return [String] The cookbook this resource was declared in.
+ #
attr_accessor :cookbook_name
# @return [String] The recipe this resource was declared in.
+ #
attr_accessor :recipe_name
# @return [Chef::Provider] The provider this resource was declared in (if
# it was declared in an LWRP). When you call methods that do not exist
# on this Resource, Chef will try to call the method on the provider
# as well before giving up.
+ #
attr_accessor :enclosing_provider
# @return [String] The source line where this resource was declared.
@@ -1234,6 +1243,7 @@ class Chef
# of these formats:
# /some/path/to/file.rb:80:in `wombat_tears'
# C:/some/path/to/file.rb:80 in 1`wombat_tears'
+ #
attr_accessor :source_line
# @return [String] The actual name that was used to create this resource.
@@ -1242,37 +1252,40 @@ class Chef
# user will expect to see the thing they wrote, not the type that was
# returned. May be `nil`, in which case callers should read #resource_name.
# See #declared_key.
+ #
attr_accessor :declared_type
- #
# Iterates over all immediate and delayed notifications, calling
# resolve_resource_reference on each in turn, causing them to
# resolve lazy/forward references.
- def resolve_notification_references
+ #
+ def resolve_notification_references(always_raise = false)
run_context.before_notifications(self).each do |n|
- n.resolve_resource_reference(run_context.resource_collection)
+ n.resolve_resource_reference(run_context.resource_collection, true)
end
+
run_context.immediate_notifications(self).each do |n|
- n.resolve_resource_reference(run_context.resource_collection)
+ n.resolve_resource_reference(run_context.resource_collection, always_raise)
end
+
run_context.delayed_notifications(self).each do |n|
- n.resolve_resource_reference(run_context.resource_collection)
+ n.resolve_resource_reference(run_context.resource_collection, always_raise)
end
end
# Helper for #notifies
def notifies_before(action, resource_spec)
- run_context.notifies_before(Notification.new(resource_spec, action, self))
+ run_context.notifies_before(Notification.new(resource_spec, action, self, run_context.unified_mode))
end
# Helper for #notifies
def notifies_immediately(action, resource_spec)
- run_context.notifies_immediately(Notification.new(resource_spec, action, self))
+ run_context.notifies_immediately(Notification.new(resource_spec, action, self, run_context.unified_mode))
end
# Helper for #notifies
def notifies_delayed(action, resource_spec)
- run_context.notifies_delayed(Notification.new(resource_spec, action, self))
+ run_context.notifies_delayed(Notification.new(resource_spec, action, self, run_context.unified_mode))
end
class << self
@@ -1321,7 +1334,6 @@ class Chef
end
end
- #
# This API can be used for backcompat to do:
#
# chef_version_for_provides "< 14.0" if defined?(:chef_version_for_provides)
@@ -1343,7 +1355,6 @@ class Chef
@chef_version_for_provides = constraint
end
- #
# Mark this resource as providing particular DSL.
#
# Resources have an automatic DSL based on their resource_name, equivalent to
@@ -1472,7 +1483,6 @@ class Chef
@default_description
end
- #
# The cookbook in which this Resource was defined (if any).
#
# @return Chef::CookbookVersion The cookbook in which this Resource was defined.
@@ -1498,7 +1508,6 @@ class Chef
provider
end
- #
# Preface an exception message with generic Resource information.
#
# @param e [StandardError] An exception with `e.message`
@@ -1554,7 +1563,6 @@ class Chef
klass
end
- #
# Returns the class with the given resource_name.
#
# NOTE: Chef::Resource.resource_matching_short_name(:package) returns