1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#
# Author:: Daniel DeLeo (<dan@chef.io>)
# Copyright:: Copyright 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'forwardable'
require 'chef/log'
require 'chef/rest'
require 'chef/run_context'
require 'chef/config'
require 'chef/node'
require 'chef/exceptions'
class Chef
module PolicyBuilder
# PolicyBuilder that selects either a Policyfile or non-Policyfile
# implementation based on the content of the node object.
class Dynamic
extend Forwardable
attr_reader :node
attr_reader :node_name
attr_reader :ohai_data
attr_reader :json_attribs
attr_reader :override_runlist
attr_reader :events
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
## PolicyBuilder API ##
# Loads the node state from the server, then picks the correct
# implementation class based on the node and json_attribs.
def load_node
events.node_load_start(node_name, config)
Chef::Log.debug("Building node object for #{node_name}")
@node =
if Chef::Config[:solo]
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
## Delegated Public API Methods ##
def_delegator :implementation, :original_runlist
def_delegator :implementation, :run_context
def_delegator :implementation, :run_list_expansion
def_delegator :implementation, :build_node
def_delegator :implementation, :setup_run_context
def_delegator :implementation, :expand_run_list
def_delegator :implementation, :sync_cookbooks
def_delegator :implementation, :temporary_policy?
## Internal Public API ##
def implementation
@implementation or raise Exceptions::InvalidPolicybuilderCall, "#load_node must be called before other policy builder methods"
end
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
def config
Chef::Config
end
private
def node_has_policyfile_attrs?(node)
node.policy_name || node.policy_group
end
def policyfile_attribs_in_node_json?
json_attribs.key?("policy_name") || json_attribs.key?("policy_group")
end
def policyfile_set_in_config?
config[:use_policyfile] || config[:policy_name] || config[:policy_group]
end
def policyfile_compat_mode_config?
config[:deployment_group] && !config[:policy_document_native_api]
end
end
end
end
|