Class: Puppet::Daemon
Overview
A module that handles operations common to all daemons. This is included into the Server and Client base classes.
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#argv ⇒ Object
Returns the value of attribute argv.
-
#server ⇒ Object
Returns the value of attribute server.
Instance Method Summary collapse
-
#create_pidfile ⇒ Object
Create a pidfile for our daemon, so we can be stopped and others don’t try to start.
-
#daemonize ⇒ Object
Put the daemon into the background.
- #daemonname ⇒ Object
-
#pidfile ⇒ Object
Provide the path to our pidfile.
- #reexec ⇒ Object
- #reload ⇒ Object
-
#remove_pidfile ⇒ Object
Remove the pid file for our daemon.
- #reopen_logs ⇒ Object
- #restart ⇒ Object
-
#set_signal_traps ⇒ Object
Trap a couple of the main signals.
- #start ⇒ Object
-
#stop(args = {:exit => true}) ⇒ Object
Stop everything.
Instance Attribute Details
#agent ⇒ Object
Returns the value of attribute agent.
9 10 11 |
# File 'lib/puppet/daemon.rb', line 9 def agent @agent end |
#argv ⇒ Object
Returns the value of attribute argv.
9 10 11 |
# File 'lib/puppet/daemon.rb', line 9 def argv @argv end |
#server ⇒ Object
Returns the value of attribute server.
9 10 11 |
# File 'lib/puppet/daemon.rb', line 9 def server @server end |
Instance Method Details
#create_pidfile ⇒ Object
Create a pidfile for our daemon, so we can be stopped and others don’t try to start.
45 46 47 48 49 |
# File 'lib/puppet/daemon.rb', line 45 def create_pidfile Puppet::Util.synchronize_on(Puppet[:name],Sync::EX) do raise "Could not create PID file: #{pidfile}" unless Puppet::Util::Pidlock.new(pidfile).lock end end |
#daemonize ⇒ Object
Put the daemon into the background.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/puppet/daemon.rb', line 16 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 end |
#daemonname ⇒ Object
11 12 13 |
# File 'lib/puppet/daemon.rb', line 11 def daemonname Puppet[:name] end |
#pidfile ⇒ Object
Provide the path to our pidfile.
52 53 54 |
# File 'lib/puppet/daemon.rb', line 52 def pidfile Puppet[:pidfile] end |
#reexec ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/puppet/daemon.rb', line 56 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 |
#reload ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/puppet/daemon.rb', line 64 def reload return unless agent if agent.running? Puppet.notice "Not triggering already-running agent" return end agent.run end |
#remove_pidfile ⇒ Object
Remove the pid file for our daemon.
75 76 77 78 79 80 |
# File 'lib/puppet/daemon.rb', line 75 def remove_pidfile Puppet::Util.synchronize_on(Puppet[:name],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 |
#reopen_logs ⇒ Object
87 88 89 |
# File 'lib/puppet/daemon.rb', line 87 def reopen_logs Puppet::Util::Log.reopen end |
#restart ⇒ Object
82 83 84 85 |
# File 'lib/puppet/daemon.rb', line 82 def restart Puppet::Application.restart! reexec unless agent and agent.running? end |
#set_signal_traps ⇒ Object
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.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/puppet/daemon.rb', line 93 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| Signal.trap(signal) do Puppet.notice "Caught #{signal}; calling #{method}" send(method) end end end |
#start ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/puppet/daemon.rb', line 118 def start set_signal_traps create_pidfile raise Puppet::DevError, "Daemons must have an agent, server, or both" unless agent or server server.start if server agent.start if agent EventLoop.current.run end |