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, destroy_server_connectivity, exit!, fatal!, #resolve_log_level, #run, #run_chef_client, setup_server_connectivity, #using_output_formatter?, #want_additional_logger?

Constructor Details

#initializeClient

Returns a new instance of Client.



223
224
225
226
# File 'lib/chef/application/client.rb', line 223

def initialize
  super
  @exit_gracefully = false
end

Instance Attribute Details

#chef_client_jsonObject (readonly)

Returns the value of attribute chef_client_json.



221
222
223
# File 'lib/chef/application/client.rb', line 221

def chef_client_json
  @chef_client_json
end

Instance Method Details

#configure_loggingObject



269
270
271
272
273
# File 'lib/chef/application/client.rb', line 269

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

#load_config_fileObject



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/chef/application/client.rb', line 256

def load_config_file
  Chef::Config.config_file_jail = config[:config_file_jail] if config[:config_file_jail]
  if !config.has_key?(:config_file)
    if config[:local_mode]
      require 'chef/knife'
      config[:config_file] = Chef::Knife.locate_config_file
    else
      config[:config_file] = Chef::Config.platform_specific_path("/etc/chef/client.rb")
    end
  end
  super
end

#reconfigureObject

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



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

def reconfigure
  super

  Chef::Config[:chef_server_url] = config[:chef_server_url] if config.has_key? :chef_server_url

  Chef::Config.local_mode = config[:local_mode] if config.has_key?(:local_mode)
  if Chef::Config.local_mode && !Chef::Config.has_key?(:cookbook_path) && !Chef::Config.has_key?(:chef_repo_path)
    Chef::Config.chef_repo_path = Chef::Config.find_chef_repo_path(Dir.pwd)
  end
  Chef::Config.chef_zero.port = config[:chef_zero_port] if config[:chef_zero_port]

  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]
    config_fetcher = Chef::ConfigFetcher.new(Chef::Config[:json_attribs])
    @chef_client_json = config_fetcher.fetch_json
  end
end

#run_applicationObject

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



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
333
334
335
336
337
338
339
340
341
342
# File 'lib/chef/application/client.rb', line 280

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

    trap("TERM") do
      Chef::Log.info("SIGTERM received, exiting gracefully")
      @exit_gracefully = true
      SELF_PIPE[1].putc('.')
    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
      Chef::Application.exit!("Exiting", 0) if @exit_gracefully
      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 SystemExit => e
      raise
    rescue Exception => e
      if Chef::Config[:interval]
        Chef::Log.error("#{e.class}: #{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.fatal!("#{e.class}: #{e.message}", 1)
      end
    end
  end
end

#setup_applicationObject



275
276
277
# File 'lib/chef/application/client.rb', line 275

def setup_application
  Chef::Daemon.change_privilege
end