diff options
author | Brice Figureau <brice-puppet@daysofwonder.com> | 2009-02-14 17:16:20 +0100 |
---|---|---|
committer | Brice Figureau <brice-puppet@daysofwonder.com> | 2009-02-16 20:12:11 +0100 |
commit | 156fb81ca914f6d697dc2cb81788a1d9ab5a23e4 (patch) | |
tree | 40492c5e064eff8b140b2d33c70b7b86cb267f4c /lib/puppet/application | |
parent | 0c71c5cde211808ef6fd744ccbcc82b6cfc38bb5 (diff) | |
download | puppet-156fb81ca914f6d697dc2cb81788a1d9ab5a23e4.tar.gz puppet-156fb81ca914f6d697dc2cb81788a1d9ab5a23e4.tar.xz puppet-156fb81ca914f6d697dc2cb81788a1d9ab5a23e4.zip |
Move puppetd to the Application Controller paradigm
Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'lib/puppet/application')
-rw-r--r-- | lib/puppet/application/puppetd.rb | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/lib/puppet/application/puppetd.rb b/lib/puppet/application/puppetd.rb new file mode 100644 index 000000000..4c050c7a8 --- /dev/null +++ b/lib/puppet/application/puppetd.rb @@ -0,0 +1,260 @@ +require 'puppet' +require 'puppet/application' +require 'puppet/agent' +require 'puppet/daemon' +require 'puppet/configurer' +require 'puppet/network/client' + +Puppet::Application.new(:puppetd) do + + should_parse_config + + attr_accessor :explicit_waitforcert, :args, :agent, :daemon + + preinit do + # Do an initial trap, so that cancels don't get a stack trace. + trap(:INT) do + $stderr.puts "Cancelling startup" + exit(0) + end + + { + :waitforcert => 120, # Default to checking for certs every 5 minutes + :onetime => false, + :verbose => false, + :debug => false, + :centrallogs => false, + :setdest => false, + :enable => false, + :disable => false, + :client => true, + :fqdn => nil, + :serve => [] + }.each do |opt,val| + options[opt] = val + end + + @explicit_waitforcert = false + @args = {} + @daemon = Puppet::Daemon.new + @daemon.argv = ARGV.dup + end + + option("--centrallogging") + option("--disable") + option("--enable") + option("--debug","-d") + option("--fqdn FQDN","-f") + option("--test","-t") + option("--verbose","-v") + + option("--serve HANDLER", "-s") do |arg| + if Puppet::Network::Handler.handler(arg) + options[:serve] << arg.to_sym + else + raise "Could not find handler for %s" % arg + end + end + + option("--version", "-V") do |arg| + puts "%s" % Puppet.version + exit + end + + option("--no-client") do |arg| + options[:client] = false + end + + option("--onetime", "-o") do |arg| + options[:onetime] = true + options[:waitforcert] = 0 unless @explicit_waitforcert + end + + option("--logdest", "-l") do |arg| + begin + Puppet::Util::Log.newdestination(arg) + options[:setdest] = true + rescue => detail + if Puppet[:debug] + puts detail.backtrace + end + $stderr.puts detail.to_s + end + end + + option("--waitforcert WAITFORCERT", "-w") do |arg| + options[:waitforcert] = arg.to_i + @explicit_waitforcert = true + end + + option("--port PORT","-p") do |arg| + @args[:Port] = arg + end + + dispatch do + return :onetime if options[:onetime] + return :main + end + + command(:onetime) do + unless options[:client] + $stderr.puts "onetime is specified but there is no client" + exit(43) + end + + @daemon.set_signal_traps + + begin + @agent.run + rescue => detail + if Puppet[:trace] + puts detail.backtrace + end + Puppet.err detail.to_s + end + exit(0) + end + + command(:main) do + Puppet.notice "Starting Puppet client version %s" % [Puppet.version] + + @daemon.start + end + + # Enable all of the most common test options. + def setup_test + Puppet.settings.handlearg("--ignorecache") + Puppet.settings.handlearg("--no-usecacheonfailure") + Puppet.settings.handlearg("--no-splay") + Puppet.settings.handlearg("--show_diff") + Puppet.settings.handlearg("--no-daemonize") + options[:verbose] = true + options[:onetime] = true + options[:waitforcert] = 0 + end + + # Handle the logging settings. + def setup_logs + if options[:debug] or options[:verbose] + Puppet::Util::Log.newdestination(:console) + if options[:debug] + Puppet::Util::Log.level = :debug + else + Puppet::Util::Log.level = :info + end + end + + unless options[:setdest] + Puppet::Util::Log.newdestination(:syslog) + end + end + + def enable_disable_client(agent) + if options[:enable] + agent.enable + elsif options[:disable] + agent.disable + end + exit(0) + end + + def setup_listen + unless FileTest.exists?(Puppet[:authconfig]) + Puppet.err "Will not start without authorization file %s" % + Puppet[:authconfig] + exit(14) + end + + # FIXME: we should really figure out how to distribute the CRL + # to clients. In the meantime, we just disable CRL checking if + # the CRL file doesn't exist + unless File::exist?(Puppet[:cacrl]) + Puppet[:cacrl] = 'false' + end + + handlers = nil + + if options[:serve].empty? + handlers = [:Runner] + else + handlers = options[:serve] + end + + require 'puppet/network/server' + # No REST handlers yet. + server = Puppet::Network::Server.new(:handlers => [:facts], :xmlrpc_handlers => handlers, :port => Puppet[:puppetport]) + + @daemon.server = server + end + + setup do + setup_test if options[:test] + + setup_logs + + if Puppet.settings.print_configs? + exit(Puppet.settings.print_configs ? 0 : 1) + end + + # If noop is set, then also enable diffs + if Puppet[:noop] + Puppet[:show_diff] = true + end + + args[:Server] = Puppet[:server] + if options[:fqdn] + args[:FQDN] = options[:fqdn] + Puppet[:certname] = options[:fqdn] + end + + if options[:centrallogs] + logdest = args[:Server] + + if args.include?(:Port) + logdest += ":" + args[:Port] + end + Puppet::Util::Log.newdestination(logdest) + end + + # We need to specify a ca location for things to work, but + # until the REST cert transfers are working, it needs to + # be local. + Puppet::SSL::Host.ca_location = :remote + + Puppet::Transaction::Report.terminus_class = :rest + + Puppet::Resource::Catalog.terminus_class = :rest + Puppet::Resource::Catalog.cache_class = :yaml + + Puppet::Node::Facts.terminus_class = :facter + Puppet::Node::Facts.cache_class = :rest + + # We need tomake the client either way, we just don't start it + # if --no-client is set. + @agent = Puppet::Agent.new(Puppet::Configurer) + + enable_disable_client(@agent) if options[:enable] or options[:disable] + + @daemon.agent = agent + + # It'd be nice to daemonize later, but we have to daemonize before the + # waitforcert happens. + if Puppet[:daemonize] + @daemon.daemonize + end + + host = Puppet::SSL::Host.new + cert = host.wait_for_cert(options[:waitforcert]) + + @objects = [] + + # This has to go after the certs are dealt with. + if Puppet[:listen] + unless options[:onetime] + setup_listen + else + Puppet.notice "Ignoring --listen on onetime run" + end + end + end +end |