summaryrefslogtreecommitdiff
path: root/chef/lib/chef/client.rb
blob: fd7e263ce0379db7b3eda876f43a313c40dc0e79 (plain)
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
#
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# 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 File.join(File.dirname(__FILE__), "mixin", "params_validate")
require File.join(File.dirname(__FILE__), "mixin", "generate_url")
require File.join(File.dirname(__FILE__), "mixin", "checksum")

require 'rubygems'
require 'facter'

class Chef
  class Client
    
    include Chef::Mixin::GenerateURL
    include Chef::Mixin::Checksum
    
    attr_accessor :node, :registration, :safe_name
    
    # Creates a new Chef::Client.
    def initialize()
      @node = nil
      @safe_name = nil
      @registration = nil
      @rest = Chef::REST.new(Chef::Config[:registration_url])
    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
      build_node
      register
      authenticate
      sync_definitions
      sync_recipes
      do_attribute_files
      save_node
      converge
      save_node
      true
    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 Facter.
    #
    # === Parameters
    # node_name<String>:: The name of the node to build - defaults to nil
    #
    # === Returns
    # node:: Returns the created node object, also stored in @node
    def build_node(node_name=nil)
      node_name ||= Facter["fqdn"].value ? Facter["fqdn"].value : Facter["hostname"].value
      @safe_name = node_name.gsub(/\./, '_')
      Chef::Log.debug("Building node object for #{@safe_name}")
      begin
        @node = @rest.get_rest("nodes/#{@safe_name}")
      rescue Net::HTTPServerException => e
        unless e.message =~ /^404/
          raise e
        end
      end
      unless @node
        @node ||= Chef::Node.new
        @node.name(node_name)
      end
      Facter.each do |field, value|
        @node[field] = value
      end
      @node
    end
    
    # If this node has been registered before, this method will fetch the current registration
    # data.
    #
    # If it has not, we register it by calling create_registration.
    #
    # === Returns
    # true:: Always returns true
    def register
      Chef::Log.debug("Registering #{@safe_name} for an openid") 
      @registration = nil
      begin
        @registration = @rest.get_rest("registrations/#{@safe_name}")
      rescue Net::HTTPServerException => e
        unless e.message =~ /^404/
          raise e
        end
      end
      
      if @registration
        reg = Chef::FileStore.load("registration", @safe_name)
        @secret = reg["secret"]
      else
        create_registration
      end
      true
    end
    
    # Generates a random secret, stores it in the Chef::Filestore with the "registration" key,
    # and posts our nodes registration information to the server.
    #
    # === Returns
    # true:: Always returns true
    def create_registration
      @secret = random_password(500)
      Chef::FileStore.store("registration", @safe_name, { "secret" => @secret })
      @rest.post_rest("registrations", { :id => @safe_name, :password => @secret })
      true
    end
    
    # Authenticates the node via OpenID.
    #
    # === Returns
    # true:: Always returns true
    def authenticate
      Chef::Log.debug("Authenticating #{@safe_name} via openid") 
      response = @rest.post_rest('openid/consumer/start', { 
        "openid_identifier" => "#{Chef::Config[:openid_url]}/openid/server/node/#{@safe_name}",
        "submit" => "Verify"
      })
      @rest.post_rest(
        "#{Chef::Config[:openid_url]}#{response["action"]}",
        { "password" => @secret }
      )
    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(segment, remote_list)  
      # We need the list of known good attribute files, so we can delete any that are
      # just laying about.
      file_canonical = Hash.new
      
      remote_list.each do |rf|
        cache_file = File.join("cookbooks", rf['cookbook'], segment, rf['name'])
        file_canonical[cache_file] = true
      
        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'], 
          rf['cookbook'], 
          segment, 
          @node, 
          current_checksum ? { 'checksum' => current_checksum } : nil
        )
        Chef::Log.debug(rf_url)
      
        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
      
      Chef::FileCache.list.each do |cache_file|
        if cache_file.match("cookbooks/.+?/#{segment}")
          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
    
    # Gets all the attribute files included in all the cookbooks available on the server,
    # and executes them.
    #
    # === Returns
    # true:: Always returns true
    def do_attribute_files
      Chef::Log.debug("Synchronizing attributes")
      update_file_cache("attributes", @rest.get_rest('cookbooks/_attribute_files'))
      Chef::FileCache.list.each do |cache_file|
        if cache_file.match("cookbooks/.+?/attributes")
          Chef::Log.debug("Executing #{cache_file}")
          @node.from_file(Chef::FileCache.load(cache_file, false))
        end
      end
      true
    end
    
    def sync_definitions
      Chef::Log.debug("Synchronizing definitions") 
      update_file_cache("definitions", @rest.get_rest('cookbooks/_definition_files'))
    end
    
    def sync_recipes
      Chef::Log.debug("Synchronizing attributes") 
      update_file_cache("recipes", @rest.get_rest('cookbooks/_recipe_files'))
    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}")
      @node = @rest.put_rest("nodes/#{@safe_name}", @node)
      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
      Chef::Log.debug("Compiling recipes for node #{@safe_name}")
      Chef::Config[:cookbook_path] = File.join(Chef::Config[:file_cache_path], "cookbooks")
      compile = Chef::Compile.new()
      compile.node = @node
      compile.load_definitions
      compile.load_recipes

      Chef::Log.debug("Executing recipes for node #{@safe_name}")
      cr = Chef::Runner.new(@node, compile.collection)
      cr.converge
      true
    end
    
    protected
      # Generates a random password of "len" length.
      def random_password(len)
        chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
        newpass = ""
        1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
        newpass
      end

  end
end