diff options
Diffstat (limited to 'lib/puppet/daemon.rb')
-rwxr-xr-x | lib/puppet/daemon.rb | 198 |
1 files changed, 99 insertions, 99 deletions
diff --git a/lib/puppet/daemon.rb b/lib/puppet/daemon.rb index b0c2b56ea..aa4a12bfa 100755 --- a/lib/puppet/daemon.rb +++ b/lib/puppet/daemon.rb @@ -6,125 +6,125 @@ require 'puppet/application' # A module that handles operations common to all daemons. This is included # into the Server and Client base classes. class Puppet::Daemon - attr_accessor :agent, :server, :argv + attr_accessor :agent, :server, :argv - def daemonname - Puppet[:name] - end + def daemonname + Puppet[:name] + end - # Put the daemon into the background. - def daemonize - if pid = fork - Process.detach(pid) - exit(0) - end - - create_pidfile - - # Get rid of console logging - Puppet::Util::Log.close(:console) - - Process.setsid - Dir.chdir("/") - begin - $stdin.reopen "/dev/null" - $stdout.reopen "/dev/null", "a" - $stderr.reopen $stdout - Puppet::Util::Log.reopen - rescue => detail - Puppet.err "Could not start #{Puppet[:name]}: #{detail}" - Puppet::Util::secure_open("/tmp/daemonout", "w") { |f| - f.puts "Could not start #{Puppet[:name]}: #{detail}" - } - exit(12) - end + # Put the daemon into the background. + def daemonize + if pid = fork + Process.detach(pid) + exit(0) end - # Create a pidfile for our daemon, so we can be stopped and others - # don't try to start. - def create_pidfile - Puppet::Util.sync(Puppet[:name]).synchronize(Sync::EX) do - raise "Could not create PID file: #{pidfile}" unless Puppet::Util::Pidlock.new(pidfile).lock - end + create_pidfile + + # Get rid of console logging + Puppet::Util::Log.close(:console) + + Process.setsid + Dir.chdir("/") + begin + $stdin.reopen "/dev/null" + $stdout.reopen "/dev/null", "a" + $stderr.reopen $stdout + Puppet::Util::Log.reopen + rescue => detail + Puppet.err "Could not start #{Puppet[:name]}: #{detail}" + Puppet::Util::secure_open("/tmp/daemonout", "w") { |f| + f.puts "Could not start #{Puppet[:name]}: #{detail}" + } + exit(12) end + end - # Provide the path to our pidfile. - def pidfile - Puppet[:pidfile] + # Create a pidfile for our daemon, so we can be stopped and others + # don't try to start. + def create_pidfile + Puppet::Util.sync(Puppet[:name]).synchronize(Sync::EX) do + raise "Could not create PID file: #{pidfile}" unless Puppet::Util::Pidlock.new(pidfile).lock end - - def reexec - raise Puppet::DevError, "Cannot reexec unless ARGV arguments are set" unless argv - command = $0 + " " + argv.join(" ") - Puppet.notice "Restarting with '#{command}'" - stop(:exit => false) - exec(command) + end + + # Provide the path to our pidfile. + def pidfile + Puppet[:pidfile] + end + + def reexec + raise Puppet::DevError, "Cannot reexec unless ARGV arguments are set" unless argv + command = $0 + " " + argv.join(" ") + Puppet.notice "Restarting with '#{command}'" + stop(:exit => false) + exec(command) + end + + def reload + return unless agent + if agent.running? + Puppet.notice "Not triggering already-running agent" + return end - def reload - return unless agent - if agent.running? - Puppet.notice "Not triggering already-running agent" - return - end + agent.run + end - agent.run + # Remove the pid file for our daemon. + def remove_pidfile + Puppet::Util.sync(Puppet[:name]).synchronize(Sync::EX) do + locker = Puppet::Util::Pidlock.new(pidfile) + locker.unlock or Puppet.err "Could not remove PID file #{pidfile}" if locker.locked? end - - # Remove the pid file for our daemon. - def remove_pidfile - Puppet::Util.sync(Puppet[:name]).synchronize(Sync::EX) do - locker = Puppet::Util::Pidlock.new(pidfile) - locker.unlock or Puppet.err "Could not remove PID file #{pidfile}" if locker.locked? - end + end + + def restart + Puppet::Application.restart! + reexec unless agent and agent.running? + end + + def reopen_logs + Puppet::Util::Log.reopen + end + + # Trap a couple of the main signals. This should probably be handled + # in a way that anyone else can register callbacks for traps, but, eh. + def set_signal_traps + signals = {:INT => :stop, :TERM => :stop } + # extended signals not supported under windows + signals.update({:HUP => :restart, :USR1 => :reload, :USR2 => :reopen_logs }) unless Puppet.features.microsoft_windows? + signals.each do |signal, method| + trap(signal) do + Puppet.notice "Caught #{signal}; calling #{method}" + send(method) + end end + end - def restart - Puppet::Application.restart! - reexec unless agent and agent.running? - end + # Stop everything + def stop(args = {:exit => true}) + Puppet::Application.stop! - def reopen_logs - Puppet::Util::Log.reopen - end + server.stop if server - # Trap a couple of the main signals. This should probably be handled - # in a way that anyone else can register callbacks for traps, but, eh. - def set_signal_traps - signals = {:INT => :stop, :TERM => :stop } - # extended signals not supported under windows - signals.update({:HUP => :restart, :USR1 => :reload, :USR2 => :reopen_logs }) unless Puppet.features.microsoft_windows? - signals.each do |signal, method| - trap(signal) do - Puppet.notice "Caught #{signal}; calling #{method}" - send(method) - end - end - end + remove_pidfile - # Stop everything - def stop(args = {:exit => true}) - Puppet::Application.stop! + Puppet::Util::Log.close_all - server.stop if server + exit if args[:exit] + end - remove_pidfile + def start + set_signal_traps - Puppet::Util::Log.close_all + create_pidfile - exit if args[:exit] - end - - def start - set_signal_traps - - create_pidfile + raise Puppet::DevError, "Daemons must have an agent, server, or both" unless agent or server + agent.start if agent + server.start if server - raise Puppet::DevError, "Daemons must have an agent, server, or both" unless agent or server - agent.start if agent - server.start if server - - EventLoop.current.run - end + EventLoop.current.run + end end |