diff options
author | AJ Christensen <aj@opscode.com> | 2009-07-29 17:29:33 +1200 |
---|---|---|
committer | AJ Christensen <aj@opscode.com> | 2009-07-29 17:29:33 +1200 |
commit | 9b077b5d46fedfbb44c60f2837f119036d2ac141 (patch) | |
tree | f0c1f904516db884d8e652bfaf2954232c4add09 /chef/lib/chef/application/solo.rb | |
parent | c56064f02840b49ed5e2bfcc4828a290e7f33a3f (diff) | |
download | chef-9b077b5d46fedfbb44c60f2837f119036d2ac141.tar.gz |
CHEF-420: Decouple splay from interval
Port behaviour to Solo aswell, sync up daemonization, privilege
dropping and looping.
Diffstat (limited to 'chef/lib/chef/application/solo.rb')
-rw-r--r-- | chef/lib/chef/application/solo.rb | 88 |
1 files changed, 85 insertions, 3 deletions
diff --git a/chef/lib/chef/application/solo.rb b/chef/lib/chef/application/solo.rb index c431f973ac..ec436487b7 100644 --- a/chef/lib/chef/application/solo.rb +++ b/chef/lib/chef/application/solo.rb @@ -18,6 +18,7 @@ require 'chef/application' require 'chef/client' require 'chef/config' +require 'chef/daemon' require 'chef/log' require 'net/http' require 'open-uri' @@ -51,7 +52,49 @@ class Chef::Application::Solo < Chef::Application :boolean => true, :show_options => true, :exit => 0 - + + option :user, + :short => "-u USER", + :long => "--user USER", + :description => "User to set privilege to", + :proc => nil + + option :group, + :short => "-g GROUP", + :long => "--group GROUP", + :description => "Group to set privilege to", + :proc => nil + + option :daemonize, + :short => "-d", + :long => "--daemonize", + :description => "Daemonize the process", + :proc => lambda { |p| true } + + option :interval, + :short => "-i SECONDS", + :long => "--interval SECONDS", + :description => "Run chef-client periodically, in seconds", + :proc => lambda { |s| s.to_i } + + option :json_attribs, + :short => "-j JSON_ATTRIBS", + :long => "--json-attributes JSON_ATTRIBS", + :description => "Load attributes from a JSON file or URL", + :proc => nil + + option :node_name, + :short => "-N NODE_NAME", + :long => "--node-name NODE_NAME", + :description => "The node name for this client", + :proc => nil + + option :splay, + :short => "-s SECONDS", + :long => "--splay SECONDS", + :description => "The splay time for running at intervals, in seconds", + :proc => lambda { |s| s.to_i } + option :json_attribs, :short => "-j JSON_ATTRIBS", :long => "--json-attributes JSON_ATTRIBS", @@ -73,7 +116,11 @@ class Chef::Application::Solo < Chef::Application def reconfigure super - Chef::Config[:solo] = true + Chef::Config.solo = true + + if Chef::Config[:daemonize] + Chef::Config[:interval] ||= 1800 + end if Chef::Config[:json_attribs] begin @@ -113,12 +160,47 @@ class Chef::Application::Solo < Chef::Application end def setup_application + Chef::Daemon.change_privilege + @chef_solo = Chef::Client.new @chef_solo.json_attribs = @chef_solo_json @chef_solo.node_name = Chef::Config[:node_name] end def run_application - @chef_solo.run_solo + 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 + + @chef_solo.run_solo + + if Chef::Config[:interval] + Chef::Log.debug("Sleeping for #{Chef::Config[:interval]} seconds") + sleep Chef::Config[:interval] + else + Chef::Application.exit! "Exiting", 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[:interval]} seconds before trying again") + sleep Chef::Config[:interval] + retry + else + raise + end + end + end end end |