Class: Chef::Application::Client

Inherits:
Chef::Application show all
Defined in:
lib/chef/application/client.rb

Constant Summary collapse

SELF_PIPE =

Mimic self_pipe sleep from Unicorn to capture signals safely

[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Chef::Application

#auto_log_level?, #configure_chef, #configure_stdout_logger, debug_stacktrace, exit!, fatal!, #resolve_log_level, #run, #run_chef_client, #using_output_formatter?, #want_additional_logger?

Constructor Details

#initializeClient

Returns a new instance of Client.



210
211
212
# File 'lib/chef/application/client.rb', line 210

def initialize
  super
end

Instance Attribute Details

#chef_client_jsonObject (readonly)

Returns the value of attribute chef_client_json.



208
209
210
# File 'lib/chef/application/client.rb', line 208

def chef_client_json
  @chef_client_json
end

Instance Method Details

#configure_loggingObject



261
262
263
264
265
# File 'lib/chef/application/client.rb', line 261

def configure_logging
  super
  Mixlib::Authentication::Log.use_log_devices( Chef::Log )
  Ohai::Log.use_log_devices( Chef::Log )
end

#reconfigureObject

Reconfigure the chef client Re-open the JSON attributes and load them into the node



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
# File 'lib/chef/application/client.rb', line 216

def reconfigure
  super

  Chef::Config[:chef_server_url] = config[:chef_server_url] if config.has_key? :chef_server_url
  unless Chef::Config[:exception_handlers].any? {|h| Chef::Handler::ErrorReport === h}
    Chef::Config[:exception_handlers] << Chef::Handler::ErrorReport.new
  end

  if Chef::Config[:daemonize]
    Chef::Config[:interval] ||= 1800
  end

  if Chef::Config[:once]
    Chef::Config[:interval] = nil
    Chef::Config[:splay] = nil
  end

  if Chef::Config[:json_attribs]
    begin
      json_io = case Chef::Config[:json_attribs]
                when /^(http|https):\/\//
                  @rest = Chef::REST.new(Chef::Config[:json_attribs], nil, nil)
                  @rest.get_rest(Chef::Config[:json_attribs], true).open
                else
                  open(Chef::Config[:json_attribs])
                end
    rescue SocketError => error
      Chef::Application.fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2)
    rescue Errno::ENOENT => error
      Chef::Application.fatal!("I cannot find #{Chef::Config[:json_attribs]}", 2)
    rescue Errno::EACCES => error
      Chef::Application.fatal!("Permissions are incorrect on #{Chef::Config[:json_attribs]}. Please chmod a+r #{Chef::Config[:json_attribs]}", 2)
    rescue Exception => error
      Chef::Application.fatal!("Got an unexpected error reading #{Chef::Config[:json_attribs]}: #{error.message}", 2)
    end

    begin
      @chef_client_json = Chef::JSONCompat.from_json(json_io.read)
      json_io.close unless json_io.closed?
    rescue JSON::ParserError => error
      Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
    end
  end
end

#run_applicationObject

Run the chef client, optionally daemonizing or looping at intervals.



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
328
329
330
331
332
# File 'lib/chef/application/client.rb', line 272

def run_application
  unless Chef::Platform.windows?
    SELF_PIPE.replace IO.pipe

    trap("USR1") do
      Chef::Log.info("SIGUSR1 received, waking up")
      SELF_PIPE[1].putc('.') # wakeup master process from select
    end
  end

  if Chef::Config[:version]
    puts "Chef version: #{::Chef::VERSION}"
  end

  if Chef::Config[:daemonize]
    Chef::Daemon.daemonize("chef-client")
  end

  loop do
    begin
      if Chef::Config[:splay]
        splay = rand Chef::Config[:splay]
        Chef::Log.debug("Splay sleep #{splay} seconds")
        sleep splay
      end
      run_chef_client
      if Chef::Config[:interval]
        Chef::Log.debug("Sleeping for #{Chef::Config[:interval]} seconds")
        unless SELF_PIPE.empty?
          client_sleep Chef::Config[:interval]
        else
          # Windows
          sleep Chef::Config[:interval]
        end
      else
        Chef::Application.exit! "Exiting", 0
      end
    rescue Chef::Application::Wakeup => e
      Chef::Log.debug("Received Wakeup signal.  Starting run.")
      next
    rescue SystemExit => e
      raise
    rescue Exception => e
      if Chef::Config[:interval]
        Chef::Log.error("#{e.class}: #{e}")
        Chef::Application.debug_stacktrace(e)
        Chef::Log.error("Sleeping for #{Chef::Config[:interval]} seconds before trying again")
        unless SELF_PIPE.empty?
          client_sleep Chef::Config[:interval]
        else
          # Windows
          sleep Chef::Config[:interval]
        end
        retry
      else
        Chef::Application.debug_stacktrace(e)
        Chef::Application.fatal!("#{e.class}: #{e.message}", 1)
      end
    end
  end
end

#setup_applicationObject



267
268
269
# File 'lib/chef/application/client.rb', line 267

def setup_application
  Chef::Daemon.change_privilege
end