class Chef::PolicyBuilder::Dynamic

PolicyBuilder that selects either a Policyfile or non-Policyfile implementation based on the content of the node object.

Attributes

events[R]
json_attribs[R]
node[R]
node_name[R]
ohai_data[R]
override_runlist[R]

Public Class Methods

new(node_name, ohai_data, json_attribs, override_runlist, events) click to toggle source
# 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

config() click to toggle source
# File lib/chef/policy_builder/dynamic.rb, line 162
def config
  Chef::Config
end
implementation() click to toggle source

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 142
def implementation
  @implementation || raise(Exceptions::InvalidPolicybuilderCall, "#load_node must be called before other policy builder methods")
end
load_node() click to toggle source

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
  events.node_load_success(node)
rescue Exception => e
  events.node_load_failed(node_name, e, config)
  raise
end
select_implementation(node) click to toggle source

@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 151
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

node_has_policyfile_attrs?(node) click to toggle source
# File lib/chef/policy_builder/dynamic.rb, line 168
def node_has_policyfile_attrs?(node)
  node.policy_name || node.policy_group
end
policyfile_attribs_in_node_json?() click to toggle source
# File lib/chef/policy_builder/dynamic.rb, line 172
def policyfile_attribs_in_node_json?
  json_attribs.key?("policy_name") || json_attribs.key?("policy_group")
end
policyfile_compat_mode_config?() click to toggle source
# File lib/chef/policy_builder/dynamic.rb, line 180
def policyfile_compat_mode_config?
  config[:deployment_group] && !config[:policy_document_native_api]
end
policyfile_set_in_config?() click to toggle source
# File lib/chef/policy_builder/dynamic.rb, line 176
def policyfile_set_in_config?
  config[:policy_name] || config[:policy_group]
end