Repository navigation
Expand file tree
/
Copy pathclient.rb
More file actions
327 lines (295 loc) · 10.5 KB
/
Copy pathclient.rb
File metadata and controls
327 lines (295 loc) · 10.5 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, 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 'chef/config'
require 'chef/mixin/params_validate'
require 'chef/mixin/generate_url'
require 'chef/mixin/checksum'
require 'chef/log'
require 'chef/rest'
require 'chef/platform'
require 'chef/node'
require 'chef/role'
require 'chef/file_cache'
require 'chef/compile'
require 'chef/runner'
require 'ohai'
class Chef
class Client
include Chef::Mixin::GenerateURL
include Chef::Mixin::Checksum
attr_accessor :node, :registration, :safe_name, :json_attribs, :validation_token, :node_name, :ohai
# Creates a new Chef::Client.
def initialize()
@node = nil
@safe_name = nil
@validation_token = nil
@registration = nil
@json_attribs = nil
@node_name = nil
@node_exists = true
Mixlib::Authentication::Log.logger = Ohai::Log.logger = Chef::Log.logger
@ohai = Ohai::System.new
@ohai_has_run = false
if File.exists?(Chef::Config[:client_key])
@rest = Chef::REST.new(Chef::Config[:chef_server_url])
else
@rest = Chef::REST.new(Chef::Config[:chef_server_url], nil, nil)
end
end
# Do a full run for this Chef::Client. Calls:
#
# * build_node - Get the last known state, merge with local changes
# * register - Make sure we have an openid
# * authenticate - Authenticate with our openid
# * sync_definitions - Populate the local cache with all the definitions
# * sync_recipes - Populate the local cache with all the recipes
# * do_attribute_files - Populate the local cache with all attributes, and execute them
# * save_node - Store the new node configuration
# * converge - Bring this system up to date, based on the local cache
# * save_node - Store the node again, in case convergence altered future state
#
# === Returns
# true:: Always returns true.
def run
start_time = Time.now
Chef::Log.info("Starting Chef Run")
determine_node_name
register
build_node(@node_name)
save_node
sync_cookbooks
save_node
converge
save_node
end_time = Time.now
Chef::Log.info("Chef Run complete in #{end_time - start_time} seconds")
true
end
# Similar to Chef::Client#run, but instead of talking to the Chef server,
# simply runs in a standalone ("solo") mode.
#
# Someday, we'll have chef_chewbacca.
#
# === Returns
# true:: Always returns true.
def run_solo
start_time = Time.now
Chef::Log.info("Starting Chef Solo Run")
determine_node_name
build_node(@node_name, true)
converge(true)
end_time = Time.now
Chef::Log.info("Chef Run complete in #{end_time - start_time} seconds")
true
end
def run_ohai
if @ohai.keys
@ohai.refresh_plugins
else
@ohai.all_plugins
end
end
def determine_node_name
run_ohai
unless @safe_name && @node_name
if Chef::Config[:node_name]
@node_name = Chef::Config[:node_name]
else
@node_name ||= @ohai[:fqdn] ? @ohai[:fqdn] : @ohai[:hostname]
Chef::Config[:node_name] = @node_name
end
@safe_name = @node_name.gsub(/\./, '_')
end
@node_name
end
# Builds a new node object for this client. Starts with querying for the FQDN of the current
# host (unless it is supplied), then merges in the facts from Ohai.
#
# === Parameters
# node_name<String>:: The name of the node to build - defaults to nil
#
# === Returns
# node<Chef::Node>:: Returns the created node object, also stored in @node
def build_node(node_name=nil, solo=false)
node_name ||= determine_node_name
raise RuntimeError, "Unable to determine node name from ohai" unless node_name
Chef::Log.debug("Building node object for #{@safe_name}")
unless solo
begin
@node = @rest.get_rest("nodes/#{@safe_name}")
rescue Net::HTTPServerException => e
unless e.message =~ /^404/
raise e
end
end
end
unless @node
@node_exists = false
@node ||= Chef::Node.new
@node.name(node_name)
end
if @json_attribs
Chef::Log.debug("Adding JSON Attributes")
@json_attribs.each do |key, value|
if key == "recipes" || key == "run_list"
value.each do |recipe|
unless @node.recipes.detect { |r| r == recipe }
Chef::Log.debug("Adding recipe #{recipe}")
@node.recipes << recipe
end
end
else
Chef::Log.debug("JSON Attribute: #{key} - #{value.inspect}")
@node[key] = value
end
end
end
ohai.each do |field, value|
Chef::Log.debug("Ohai Attribute: #{field} - #{value.inspect}")
@node[field] = value
end
platform, version = Chef::Platform.find_platform_and_version(@node)
Chef::Log.debug("Platform is #{platform} version #{version}")
@node[:platform] = platform
@node[:platform_version] = version
@node[:tags] = Array.new unless @node.attribute?(:tags)
@node
end
#
# === Returns
# true:: Always returns true
def register
if File.exists?(Chef::Config[:validation_key])
@vr = Chef::REST.new(Chef::Config[:client_url], Chef::Config[:validation_client_name], Chef::Config[:validation_key])
@vr.register(@node_name, Chef::Config[:client_key])
else
Chef::Log.debug("Validation key #{Chef::Config[:validation_key]} is not present - skipping registration")
end
# We now have the client key, and should use it from now on.
@rest = Chef::REST.new(Chef::Config[:chef_server_url])
true
end
# Update the file caches for a given cache segment. Takes a segment name
# and a hash that matches one of the cookbooks/_attribute_files style
# remote file listings.
#
# === Parameters
# segment<String>:: The cache segment to update
# remote_list<Hash>:: A cookbooks/_attribute_files style remote file listing
def update_file_cache(cookbook_name, parts)
Chef::Log.debug("Synchronizing cookbook #{cookbook_name}")
file_canonical = Hash.new
[ "recipes", "attributes", "definitions", "libraries" ].each do |segment|
remote_list = parts[segement]
# segement = cookbook segment
# remote_list = list of file hashes
#
# We need the list of known good attribute files, so we can delete any that are
# just laying about.
remote_list.each do |rf|
cache_file = File.join("cookbooks", cookbook_name, segment, rf['name'])
file_canonical[cache_file] = true
# For back-compat between older clients and new chef servers
rf['checksum'] ||= nil
current_checksum = nil
if Chef::FileCache.has_key?(cache_file)
current_checksum = checksum(Chef::FileCache.load(cache_file, false))
end
rf_url = generate_cookbook_url(
rf['name'],
cookbook_name,
segment,
@node,
current_checksum ? { 'checksum' => current_checksum } : nil
)
if current_checksum != rf['checksum']
changed = true
begin
raw_file = @rest.get_rest(rf_url, true)
rescue Net::HTTPRetriableError => e
if e.response.kind_of?(Net::HTTPNotModified)
changed = false
Chef::Log.debug("Cache file #{cache_file} is unchanged")
else
raise e
end
end
if changed
Chef::Log.info("Storing updated #{cache_file} in the cache.")
Chef::FileCache.move_to(raw_file.path, cache_file)
end
end
end
Chef::FileCache.list.each do |cache_file|
if cache_file =~ /^cookbooks\/(recipes|attributes|definitions|libraries)\//
unless file_canonical[cache_file]
Chef::Log.info("Removing #{cache_file} from the cache; it is no longer on the server.")
Chef::FileCache.delete(cache_file)
end
end
end
end
end
# Synchronizes all the cookbooks from the chef-server.
#
# === Returns
# true:: Always returns true
def sync_cookbooks
Chef::Log.debug("Synchronizing cookbooks")
cookbook_hash = @rest.get_rest("nodes/#{@safe_name}/cookbooks")
Chef::Log.debug("Cookbooks to load: #{cookbook_hash.inspect}")
cookbook_hash.each do |cookbook_name, parts|
update_file_cache(cookbook_name, parts)
end
end
# Updates the current node configuration on the server.
#
# === Returns
# true:: Always returns true
def save_node
Chef::Log.debug("Saving the current state of node #{@safe_name}")
if @node_exists
@node = @rest.put_rest("nodes/#{@safe_name}", @node)
else
result = @rest.post_rest("nodes", @node)
@node = @rest.get_rest(result['uri'])
@node_exists = true
end
true
end
# Compiles the full list of recipes for the server, and passes it to an instance of
# Chef::Runner.converge.
#
# === Returns
# true:: Always returns true
def converge(solo=false)
Chef::Log.debug("Compiling recipes for node #{@safe_name}")
unless solo
Chef::Config[:cookbook_path] = File.join(Chef::Config[:file_cache_path], "cookbooks")
end
compile = Chef::Compile.new(@node)
compile.load_libraries
compile.load_attributes
compile.load_definitions
compile.load_recipes
Chef::Log.debug("Converging node #{@safe_name}")
cr = Chef::Runner.new(@node, compile.collection)
cr.converge
true
end
end
end