Class: Chef::PolicyBuilder::Dynamic
- Inherits:
-
Object
- Object
- Chef::PolicyBuilder::Dynamic
- 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
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#json_attribs ⇒ Object
readonly
Returns the value of attribute json_attribs.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#node_name ⇒ Object
readonly
Returns the value of attribute node_name.
-
#ohai_data ⇒ Object
readonly
Returns the value of attribute ohai_data.
-
#override_runlist ⇒ Object
readonly
Returns the value of attribute override_runlist.
Instance Method Summary collapse
-
#build_node ⇒ Chef::Node
Applies external attributes (e.g., from JSON file, environment, policyfile, etc.) and determines the correct expanded run list for the run.
- #config ⇒ Object
-
#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.
-
#implementation ⇒ PolicyBuilder::Policyfile, PolicyBuilder::ExpandNodeObject
Returns the selected implementation, or raises if not set.
-
#initialize(node_name, ohai_data, json_attribs, override_runlist, events) ⇒ Dynamic
constructor
A new instance of Dynamic.
-
#load_node ⇒ Chef::Node
Loads the node state from the server, then picks the correct implementation class based on the node and json_attribs.
-
#select_implementation(node) ⇒ Object
private
Sets the implementation based on the content of the node, node JSON (i.e., the ‘-j JSON_FILE` data), and config.
-
#setup_run_context ⇒ Chef::RunContext
Synchronizes cookbooks and initializes the run context object for the run.
-
#sync_cookbooks ⇒ Hash{String => Chef::CookbookManifest}
Synchronizes cookbooks.
-
#temporary_policy? ⇒ true, false
Indicates whether the policy is temporary, which means an override_runlist was provided.
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
#events ⇒ Object (readonly)
Returns the value of attribute events.
41 42 43 |
# File 'lib/chef/policy_builder/dynamic.rb', line 41 def events @events end |
#json_attribs ⇒ Object (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 |
#node ⇒ Object (readonly)
Returns the value of attribute node.
36 37 38 |
# File 'lib/chef/policy_builder/dynamic.rb', line 36 def node @node end |
#node_name ⇒ Object (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_data ⇒ Object (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_runlist ⇒ Object (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_node ⇒ Chef::Node
Applies external attributes (e.g., from JSON file, environment, policyfile, etc.) and determines the correct expanded run list for the run.
99 |
# File 'lib/chef/policy_builder/dynamic.rb', line 99 def_delegator :implementation, :build_node |
#config ⇒ Object
161 162 163 |
# File 'lib/chef/policy_builder/dynamic.rb', line 161 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.
115 |
# File 'lib/chef/policy_builder/dynamic.rb', line 115 def_delegator :implementation, :expand_run_list |
#implementation ⇒ PolicyBuilder::Policyfile, PolicyBuilder::ExpandNodeObject
Returns the selected implementation, or raises if not set. The implementation is set when #load_node is called.
141 142 143 |
# 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 |
#load_node ⇒ Chef::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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/chef/policy_builder/dynamic.rb', line 64 def load_node events.node_load_start(node_name, config) Chef::Log.debug("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 |
#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.
150 151 152 153 154 155 156 157 158 159 |
# 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 |
#setup_run_context ⇒ Chef::RunContext
Synchronizes cookbooks and initializes the run context object for the run.
107 |
# File 'lib/chef/policy_builder/dynamic.rb', line 107 def_delegator :implementation, :setup_run_context |
#sync_cookbooks ⇒ Hash{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.
124 |
# File 'lib/chef/policy_builder/dynamic.rb', line 124 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.
133 |
# File 'lib/chef/policy_builder/dynamic.rb', line 133 def_delegator :implementation, :temporary_policy? |