diff options
author | Luke Kanies <luke@madstop.com> | 2008-05-07 12:25:00 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-05-07 12:25:00 -0500 |
commit | 8fd68e388aa0b26d2dacc201086960385ff5c834 (patch) | |
tree | 226b42a0afeb32bf4708b4af7a4a6060a5469abe /lib | |
parent | dd4d8684fc19adcb68c681ba1c446a737498cda0 (diff) | |
download | puppet-8fd68e388aa0b26d2dacc201086960385ff5c834.tar.gz puppet-8fd68e388aa0b26d2dacc201086960385ff5c834.tar.xz puppet-8fd68e388aa0b26d2dacc201086960385ff5c834.zip |
Adding pidfile management and daemonization to the Server
class.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/network/server.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/puppet/network/server.rb b/lib/puppet/network/server.rb index 6ea2943ea..de32db02f 100644 --- a/lib/puppet/network/server.rb +++ b/lib/puppet/network/server.rb @@ -1,8 +1,59 @@ require 'puppet/network/http' +require 'puppet/util/pidlock' class Puppet::Network::Server attr_reader :server_type, :protocols, :address, :port + # Put the daemon into the background. + def daemonize + if pid = fork() + Process.detach(pid) + exit(0) + end + + # 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 + File.open("/tmp/daemonout", "w") { |f| + f.puts "Could not start %s: %s" % [Puppet[:name], detail] + } + raise "Could not start %s: %s" % [Puppet[:name], detail] + end + 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 + unless Puppet::Util::Pidlock.new(pidfile).lock + raise "Could not create PID file: %s" % [pidfile] + end + end + 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) + if locker.locked? + locker.unlock or Puppet.err "Could not remove PID file %s" % [pidfile] + end + end + end + + # Provide the path to our pidfile. + def pidfile + Puppet[:pidfile] + end + def initialize(args = {}) @server_type = Puppet[:servertype] or raise "No servertype configuration found." # e.g., WEBrick, Mongrel, etc. http_server_class || raise(ArgumentError, "Could not determine HTTP Server class for server type [#{@server_type}]") @@ -16,6 +67,9 @@ class Puppet::Network::Server @xmlrpc_routes = {} self.register(args[:handlers]) if args[:handlers] self.register_xmlrpc(args[:xmlrpc_handlers]) if args[:xmlrpc_handlers] + + # Make sure we have all of the directories we need to function. + Puppet.settings.use(:main, :ssl, Puppet[:name]) end # Register handlers for REST networking, based on the Indirector. @@ -85,11 +139,13 @@ class Puppet::Network::Server end def start + create_pidfile listen end def stop unlisten + remove_pidfile end private |