Class: Chef::PolicyBuilder::Dynamic

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/chef/policy_builder/dynamic.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_name, ohai_data, json_attribs, override_runlist, events) ⇒ Dynamic

Returns a new instance of Dynamic.



43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



41
42
43
# File 'lib/chef/policy_builder/dynamic.rb', line 41

def events
  @events
end

#json_attribsObject (readonly)

Returns the value of attribute json_attribs.



39
40
41
# File 'lib/chef/policy_builder/dynamic.rb', line 39

def json_attribs
  @json_attribs
end

#nodeObject (readonly)

Returns the value of attribute node.



36
37
38
# File 'lib/chef/policy_builder/dynamic.rb', line 36

def node
  @node
end

#node_nameObject (readonly)

Returns the value of attribute node_name.



37
38
39
# File 'lib/chef/policy_builder/dynamic.rb', line 37

def node_name
  @node_name
end

#ohai_dataObject (readonly)

Returns the value of attribute ohai_data.



38
39
40
# File 'lib/chef/policy_builder/dynamic.rb', line 38

def ohai_data
  @ohai_data
end

#override_runlistObject (readonly)

Returns the value of attribute override_runlist.



40
41
42
# File 'lib/chef/policy_builder/dynamic.rb', line 40

def override_runlist
  @override_runlist
end

Instance Method Details

#build_nodeChef::Node

Applies external attributes (e.g., from JSON file, environment, policyfile, etc.) and determines the correct expanded run list for the run.

Returns:



100
# File 'lib/chef/policy_builder/dynamic.rb', line 100

def_delegator :implementation, :build_node

#configObject



162
163
164
# File 'lib/chef/policy_builder/dynamic.rb', line 162

def config
  Chef::Config
end

#expanded_run_list#recipes, #roles

Resolves the run list to a form containing only recipes and sets the ‘roles` and `recipes` automatic attributes on the node.

Returns:

  • (#recipes, #roles)

    A RunListExpansion or duck-type.



116
# File 'lib/chef/policy_builder/dynamic.rb', line 116

def_delegator :implementation, :expand_run_list

#implementationPolicyBuilder::Policyfile, PolicyBuilder::ExpandNodeObject

Returns the selected implementation, or raises if not set. The implementation is set when #load_node is called.



142
143
144
# 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_nodeChef::Node

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.

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



151
152
153
154
155
156
157
158
159
160
# 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

#setup_run_contextChef::RunContext

Synchronizes cookbooks and initializes the run context object for the run.

Returns:



108
# File 'lib/chef/policy_builder/dynamic.rb', line 108

def_delegator :implementation, :setup_run_context

#sync_cookbooksHash{String => Chef::CookbookManifest}

Synchronizes cookbooks. In a normal chef-client run, this is handled by #setup_run_context, but may be called directly in some circumstances.

Returns:



125
# File 'lib/chef/policy_builder/dynamic.rb', line 125

def_delegator :implementation, :sync_cookbooks

#temporary_policy?true, false

Indicates whether the policy is temporary, which means an override_runlist was provided. Chef::Client uses this to decide whether to do the final node save at the end of the run or not.

Returns:

  • (true, false)


134
# File 'lib/chef/policy_builder/dynamic.rb', line 134

def_delegator :implementation, :temporary_policy?