class Chef::Application::Client

Constants

IMMEDIATE_RUN_SIGNAL
RECONFIGURE_SIGNAL
SELF_PIPE

Mimic self_pipe sleep from Unicorn to capture signals safely

Attributes

chef_client_json[R]

Public Instance Methods

configure_logging() click to toggle source
Calls superclass method Chef::Application#configure_logging
# File lib/chef/application/client.rb, line 402
def configure_logging
  super
  Mixlib::Authentication::Log.use_log_devices( Chef::Log )
  Ohai::Log.use_log_devices( Chef::Log )
end
load_config_file() click to toggle source
Calls superclass method Chef::Application#load_config_file
# File lib/chef/application/client.rb, line 386
def load_config_file
  if !config.has_key?(:config_file) && !config[:disable_config]
    if config[:local_mode]
      config[:config_file] = Chef::WorkstationConfigLoader.new(nil, Chef::Log).config_location
    else
      config[:config_file] = Chef::Config.platform_specific_path("/etc/chef/client.rb")
    end
  end

  # Load the client.rb configuration
  super

  # Load all config files in client.d
  load_dot_d(Chef::Config[:client_d_dir]) if Chef::Config[:client_d_dir]
end
reconfigure() click to toggle source

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

Calls superclass method Chef::Application#reconfigure
# File lib/chef/application/client.rb, line 312
def reconfigure
  super

  raise Chef::Exceptions::PIDFileLockfileMatch if Chef::Util::PathHelper.paths_eql? (Chef::Config[:pid_file] || "" ), (Chef::Config[:lockfile] || "")

  set_specific_recipes

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

  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.has_key?(:chef_repo_path) && Chef::Config.chef_repo_path.nil?
    Chef::Config.delete(:chef_repo_path)
    Chef::Log.warn "chef_repo_path was set in a config file but was empty. Assuming #{Chef::Config.chef_repo_path}"
  end

  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

  if Chef::Config[:recipe_url]
    if !Chef::Config.local_mode
      Chef::Application.fatal!("chef-client recipe-url can be used only in local-mode")
    else
      if Chef::Config[:delete_entire_chef_repo]
        Chef::Log.trace "Cleanup path #{Chef::Config.chef_repo_path} before extract recipes into it"
        FileUtils.rm_rf(recipes_path, :secure => true)
      end
      Chef::Log.trace "Creating path #{Chef::Config.chef_repo_path} to extract recipes into"
      FileUtils.mkdir_p(Chef::Config.chef_repo_path)
      tarball_path = File.join(Chef::Config.chef_repo_path, "recipes.tgz")
      fetch_recipe_tarball(Chef::Config[:recipe_url], tarball_path)
      Mixlib::Archive.new(tarball_path).extract(Chef::Config.chef_repo_path, perms: false, ignore: /^\.$/)
      config_path = File.join(Chef::Config.chef_repo_path, ".chef/config.rb")
      Chef::Config.from_string(IO.read(config_path), config_path) if File.file?(config_path)
    end
  end

  Chef::Config.chef_zero.host = config[:chef_zero_host] if config[:chef_zero_host]
  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

  # supervisor processes are enabled by default for interval-running processes but not for one-shot runs
  if Chef::Config[:client_fork].nil?
    Chef::Config[:client_fork] = !!Chef::Config[:interval]
  end

  if !Chef::Config[:client_fork] && Chef::Config[:interval] && !Chef::Platform.windows?
    Chef::Application.fatal!(unforked_interval_error_message)
  end

  if Chef::Config[:json_attribs]
    config_fetcher = Chef::ConfigFetcher.new(Chef::Config[:json_attribs])
    @chef_client_json = config_fetcher.fetch_json
  end

  if mode = config[:audit_mode] || Chef::Config[:audit_mode]
    expected_modes = [:enabled, :disabled, :audit_only]
    unless expected_modes.include?(mode)
      Chef::Application.fatal!(unrecognized_audit_mode(mode))
    end
  end
end
run_application() click to toggle source

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

# File lib/chef/application/client.rb, line 432
def run_application
  if Chef::Config[:version]
    puts "Chef version: #{::Chef::VERSION}"
  end

  if !Chef::Config[:client_fork] || Chef::Config[:once]
    begin
      # run immediately without interval sleep, or splay
      run_chef_client(Chef::Config[:specific_recipes])
    rescue SystemExit
      raise
    rescue Exception => e
      Chef::Application.fatal!("#{e.class}: #{e.message}", e)
    end
  else
    interval_run_chef_client
  end
end
setup_application() click to toggle source
# File lib/chef/application/client.rb, line 408
def setup_application
  Chef::Daemon.change_privilege
end
setup_signal_handlers() click to toggle source
Calls superclass method Chef::Application#setup_signal_handlers
# File lib/chef/application/client.rb, line 412
def setup_signal_handlers
  super

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

    trap("USR1") do
      Chef::Log.info("SIGUSR1 received, will run now or after the current run")
      SELF_PIPE[1].putc(IMMEDIATE_RUN_SIGNAL) # wakeup master process from select
    end

    # Override the trap setup in the parent so we can avoid running reconfigure during a run
    trap("HUP") do
      Chef::Log.info("SIGHUP received, will reconfigure now or after the current run")
      SELF_PIPE[1].putc(RECONFIGURE_SIGNAL) # wakeup master process from select
    end
  end
end

Private Instance Methods

audit_mode_settings_explanation() click to toggle source
# File lib/chef/application/client.rb, line 524
def audit_mode_settings_explanation
  "\n* To enable audit mode after converge, use command line option `--audit-mode enabled` or set `audit_mode :enabled` in your config file." +
    "\n* To disable audit mode, use command line option `--audit-mode disabled` or set `audit_mode :disabled` in your config file." +
    "\n* To only run audit mode, use command line option `--audit-mode audit-only` or set `audit_mode :audit_only` in your config file." +
    "\nAudit mode is disabled by default."
end
fetch_recipe_tarball(url, path) click to toggle source
# File lib/chef/application/client.rb, line 535
def fetch_recipe_tarball(url, path)
  Chef::Log.trace("Download recipes tarball from #{url} to #{path}")
  File.open(path, "wb") do |f|
    open(url) do |r|
      f.write(r.read)
    end
  end
end
interval_run_chef_client() click to toggle source
# File lib/chef/application/client.rb, line 453
def interval_run_chef_client
  if Chef::Config[:daemonize]
    Chef::Daemon.daemonize("chef-client")

    # Start first daemonized run after configured number of seconds
    if Chef::Config[:daemonize].is_a?(Integer)
      sleep_then_run_chef_client(Chef::Config[:daemonize])
    end
  end

  loop do
    sleep_then_run_chef_client(time_to_sleep)
    Chef::Application.exit!("Exiting", 0) if !Chef::Config[:interval]
  end
end
interval_sleep(sec) click to toggle source

sleep and handle queued signals

# File lib/chef/application/client.rb, line 498
def interval_sleep(sec)
  unless SELF_PIPE.empty?
    # mimic sleep with a timeout on IO.select, listening for signals setup in #setup_signal_handlers
    return unless IO.select([ SELF_PIPE[0] ], nil, nil, sec)

    signal = SELF_PIPE[0].getc.chr

    return if signal == IMMEDIATE_RUN_SIGNAL # be explicit about this behavior

    # we need to sleep again after reconfigure to avoid stampeding when logrotate runs out of cron
    if signal == RECONFIGURE_SIGNAL
      reconfigure
      interval_sleep(sec)
    end
  else
    sleep(sec)
  end
end
sleep_then_run_chef_client(sleep_sec) click to toggle source
# File lib/chef/application/client.rb, line 469
def sleep_then_run_chef_client(sleep_sec)
  Chef::Log.trace("Sleeping for #{sleep_sec} seconds")

  # interval_sleep will return early if we received a signal (unless on windows)
  interval_sleep(sleep_sec)

  run_chef_client(Chef::Config[:specific_recipes])

  reconfigure
rescue SystemExit => e
  raise
rescue Exception => e
  if Chef::Config[:interval]
    Chef::Log.error("#{e.class}: #{e}")
    Chef::Log.trace("#{e.class}: #{e}\n#{e.backtrace.join("\n")}")
    retry
  end

  Chef::Application.fatal!("#{e.class}: #{e.message}", e)
end
time_to_sleep() click to toggle source
# File lib/chef/application/client.rb, line 490
def time_to_sleep
  duration = 0
  duration += rand(Chef::Config[:splay]) if Chef::Config[:splay]
  duration += Chef::Config[:interval] if Chef::Config[:interval]
  duration
end
unforked_interval_error_message() click to toggle source
# File lib/chef/application/client.rb, line 517
def unforked_interval_error_message
  "Unforked chef-client interval runs are disabled in Chef 12." +
    "\nConfiguration settings:" +
    "#{"\n  interval  = #{Chef::Config[:interval]} seconds" if Chef::Config[:interval]}" +
    "\nEnable chef-client interval runs by setting `:client_fork = true` in your config file or adding `--fork` to your command line options."
end
unrecognized_audit_mode(mode) click to toggle source
# File lib/chef/application/client.rb, line 531
def unrecognized_audit_mode(mode)
  "Unrecognized setting #{mode} for audit mode." + audit_mode_settings_explanation
end