summaryrefslogtreecommitdiff
path: root/chef/bin
diff options
context:
space:
mode:
authorAJ Christensen <aj@junglist.gen.nz>2009-05-07 04:21:14 +1200
committerAJ Christensen <aj@junglist.gen.nz>2009-05-09 21:24:59 +1200
commit6d361fa77271138b9333a9791dead00e187cf592 (patch)
tree078b49a2ee081708a63d2d92b39dec2cae4be056 /chef/bin
parent3df8a596568919749dbf4f3fe433f0bd8fd3876d (diff)
downloadchef-6d361fa77271138b9333a9791dead00e187cf592.tar.gz
CHEF-151: new chef-client binary, application::client class, tests
Diffstat (limited to 'chef/bin')
-rwxr-xr-xchef/bin/chef-client127
1 files changed, 4 insertions, 123 deletions
diff --git a/chef/bin/chef-client b/chef/bin/chef-client
index 4ed9a69cd3..8018ad0353 100755
--- a/chef/bin/chef-client
+++ b/chef/bin/chef-client
@@ -1,8 +1,8 @@
#!/usr/bin/env ruby
#
-# ./chef-client-new - Build a meal with chef
+# ./chef-client - Run the chef client
#
-# Author:: Adam Jacob (<adam@opscode.com>)
+# Author:: AJ Christensen (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
@@ -20,126 +20,7 @@
$: << File.join(File.dirname(__FILE__), "..", "lib")
-require 'optparse'
require 'rubygems'
-require 'chef'
-require 'chef/client'
-require 'chef/daemon'
-require 'json'
+require 'chef/application/client'
-config = {
- :config_file => "/etc/chef/client.rb"
-}
-opts = OptionParser.new do |opts|
- opts.banner = "Usage: #{$0} (options)"
- client_option_hash = {
- :config_file=> { :short=>"-c CONFIG", :long=>"--config CONFIG", :description=>"The Chef Config file to use", :proc=>nil },
- :user=> { :short=>"-u USER", :long=>"--user USER", :description=>"User to change uid to before daemonizing", :proc=>nil },
- :group=> { :short=>"-g GROUP", :long=>"--group GROUP", :description=>"Group to change gid to before daemonizing", :proc=>nil },
- :daemonize=> { :short=>"-d", :long=>"--daemonize", :description=>"Daemonize the process", :proc=> lambda { |p| true} },
- :interval=> { :short=>"-i SECONDS", :long=>"--interval SECONDS", :description=>"Run chef-client periodically, in seconds", :proc=>nil },
- :json_attribs=>{ :short=>"-j JSON_ATTRIBS", :long=>"--json-attributes JSON_ATTRIBS", :description=>"Load attributes from a JSON file/URL", :proc=>nil },
- :node_name=> { :short=>"-N NODE_NAME", :long=>"--node-name NODE_NAME", :description=>"The node name for this client", :proc=>nil },
- :splay=> { :short=>"-s SECONDS", :long=>"--splay SECONDS", :description=>"The splay time for running at intervals, in seconds", :proc=>nil },
- :log_level=> { :short=>"-l LEVEL", :long=>"--loglevel LEVEL", :description=>"Set the log level (debug, info, warn, error, fatal)", :proc=>lambda { |p| p.to_sym} },
- :log_location=>{ :short=>"-L LOGLOCATION", :long=>"--logfile LOGLOCATION", :description=>"Set the log file location, defaults to STDOUT - recommended for daemonizing", :proc=>nil },
- :validation_token=>{ :short=>"-t TOKEN", :long=>"--token TOKEN", :description=>"Set the openid validation token", :proc=>nil },
- }
-
- client_option_hash.each do |opt_key, opt_val|
- opts.on(opt_val[:short],opt_val[:long],opt_val[:description]) do |c|
- config[opt_key] = (opt_val[:proc] && opt_val[:proc].call(c)) || c
- end
- end
-
- opts.on_tail("-h", "--help", "Show this message") do
- puts opts
- exit 0
- end
-end
-opts.parse!(ARGV)
-
-trap("INT") { Chef::Application.fatal!("SIGINT received, stopping", 2) }
-trap("HUP") {
- Chef::Log.info("SIGHUP received, reloading configuration")
- Chef::Config.from_file(config[:config_file])
- Chef::Config.configure { |c| c.merge!(config) }
-}
-
-unless File.exists?(config[:config_file]) and File.readable?(config[:config_file])
- Chef::Application.fatal!("I cannot find or read the config file: #{config[:config_file]}", 1)
-end
-
-if config[:json_attribs]
- require 'net/http'
- require 'open-uri'
-
- json_io = nil
- begin
- json_io = open(config[:json_attribs])
- rescue SocketError => error
- Chef::Application.fatal!("I cannot connect to #{config[:json_attribs]}", 2)
- rescue Errno::ENOENT => error
- Chef::Application.fatal!("I cannot find #{config[:json_attribs]}", 2)
- rescue Errno::EACCES => error
- Chef::Application.fatal!("Permissions are incorrect on #{config[:json_attribs]}. Please chmod a+r #{config[:json_attribs]}", 2)
- rescue Exception => error
- Chef::Application.fatal!("Got an unexpected error reading #{config[:json_attribs]}: #{error.message}", 2)
- end
-
- json_contents = json_io.read
-
- begin
- config[:json_attribs] = JSON.parse(json_contents)
- rescue JSON::ParserError => error
- Chef::Application.fatal!("Could not parse the provided JSON file (#{config[:json_attribs]})!: " + error.message, 2)
- exit 2
- end
-end
-
-Chef::Config.from_file(config[:config_file])
-Chef::Config.configure { |c| c.merge!(config) }
-
-Chef::Daemon.change_privilege
-
-Chef::Log.init(Chef::Config[:log_location])
-Chef::Log.level(Chef::Config[:log_level])
-Chef::Config[:delay] = 0
-
-if Chef::Config[:daemonize]
- # We want to set the interval to half an hour, if one is not set.
- Chef::Config[:interval] ||= 1800
- Chef::Daemon.daemonize("chef-client")
-end
-
-if Chef::Config[:interval]
- Chef::Config[:delay] = Chef::Config[:interval].to_i + (Chef::Config[:splay] ? rand(Chef::Config[:splay]) : 0)
-end
-
-loop do
- begin
- c = Chef::Client.new
- c.json_attribs = Chef::Config[:json_attribs]
- c.validation_token = Chef::Config[:validation_token]
- c.node_name = Chef::Config[:node_name]
- c.run
- if Chef::Config[:interval]
- Chef::Log.debug("Sleeping for #{Chef::Config[:delay]} seconds")
- sleep Chef::Config[:delay]
- else
- exit 0
- end
- rescue SystemExit => e
- raise
- rescue Exception => e
- if Chef::Config[:interval]
- Chef::Log.error("#{e.class}")
- Chef::Log.fatal("#{e}\n#{e.backtrace.join("\n")}")
- Chef::Log.fatal("Sleeping for #{Chef::Config[:delay]} seconds before trying again")
- sleep Chef::Config[:delay]
- retry
- else
- raise
- end
- end
-end
+Chef::Application::Client.new.run \ No newline at end of file