class Chef::PolicyBuilder::Dynamic
PolicyBuilder that selects either a Policyfile or non-Policyfile implementation based on the content of the node object.
Attributes
Public Class Methods
# File lib/chef/policy_builder/dynamic.rb, line 43 def initialize(node_name, ohai_data, json_attribs, override_runlist, events) @implementation = nil @node_name = node_name @ohai_data = ohai_data @json_attribs = json_attribs @override_runlist = override_runlist @events = events @node = nil end
Public Instance Methods
# File lib/chef/policy_builder/dynamic.rb, line 161 def config Chef::Config end
Returns the selected implementation, or raises if not set. The implementation is set when load_node is called.
@return [PolicyBuilder::Policyfile, PolicyBuilder::ExpandNodeObject]
# File lib/chef/policy_builder/dynamic.rb, line 141 def implementation @implementation || raise(Exceptions::InvalidPolicybuilderCall, "#load_node must be called before other policy builder methods") end
Loads the node state from the server, then picks the correct implementation class based on the node and json_attribs.
Calls finish_load_node on the implementation object to complete the loading process. All subsequent lifecycle calls are delegated.
@return [Chef::Node] the loaded node.
# File lib/chef/policy_builder/dynamic.rb, line 64 def load_node events.node_load_start(node_name, config) Chef::Log.trace("Building node object for #{node_name}") @node = if Chef::Config[:solo_legacy_mode] Chef::Node.build(node_name) else Chef::Node.find_or_create(node_name) end select_implementation(node) implementation.finish_load_node(node) node rescue Exception => e events.node_load_failed(node_name, e, config) raise end
@api private
Sets the implementation based on the content of the node, node JSON (i.e., the `-j JSON_FILE` data), and config. This is only public for testing purposes; production code should call load_node instead.
# File lib/chef/policy_builder/dynamic.rb, line 150 def select_implementation(node) if policyfile_set_in_config? || policyfile_attribs_in_node_json? || node_has_policyfile_attrs?(node) || policyfile_compat_mode_config? @implementation = Policyfile.new(node_name, ohai_data, json_attribs, override_runlist, events) else @implementation = ExpandNodeObject.new(node_name, ohai_data, json_attribs, override_runlist, events) end end
Private Instance Methods
# File lib/chef/policy_builder/dynamic.rb, line 167 def node_has_policyfile_attrs?(node) node.policy_name || node.policy_group end
# File lib/chef/policy_builder/dynamic.rb, line 171 def policyfile_attribs_in_node_json? json_attribs.key?("policy_name") || json_attribs.key?("policy_group") end
# File lib/chef/policy_builder/dynamic.rb, line 179 def policyfile_compat_mode_config? config[:deployment_group] && !config[:policy_document_native_api] end
# File lib/chef/policy_builder/dynamic.rb, line 175 def policyfile_set_in_config? config[:use_policyfile] || config[:policy_name] || config[:policy_group] end