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.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chef/policy_builder/dynamic.rb', line 45

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.



43
44
45
# File 'lib/chef/policy_builder/dynamic.rb', line 43

def events
  @events
end

#json_attribsObject (readonly)

Returns the value of attribute json_attribs.



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

def json_attribs
  @json_attribs
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#node_nameObject (readonly)

Returns the value of attribute node_name.



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

def node_name
  @node_name
end

#ohai_dataObject (readonly)

Returns the value of attribute ohai_data.



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

def ohai_data
  @ohai_data
end

#override_runlistObject (readonly)

Returns the value of attribute override_runlist.



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

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:



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

def_delegator :implementation, :build_node

#configObject



164
165
166
# File 'lib/chef/policy_builder/dynamic.rb', line 164

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.



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

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.



144
145
146
# File 'lib/chef/policy_builder/dynamic.rb', line 144

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:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef/policy_builder/dynamic.rb', line 66

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.



153
154
155
156
157
158
159
160
161
162
# File 'lib/chef/policy_builder/dynamic.rb', line 153

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:



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

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:



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

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)


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

def_delegator :implementation, :temporary_policy?