# File lib/puppet/network/server.rb, line 49 def initialize(address, port, handlers = nil) @port = port @address = address @http_server = Puppet::Network::HTTP::WEBrick.new @listening = false @routes = {} self.register(handlers) if handlers # Make sure we have all of the directories we need to function. Puppet.settings.use(:main, :ssl, :application) end
# File lib/puppet/network/server.rb, line 25 def close_streams() Puppet::Daemon.close_streams() end
Create a pidfile for our daemon, so we can be stopped and others don’t try to start.
# File lib/puppet/network/server.rb, line 31 def create_pidfile Puppet::Util.synchronize_on(Puppet.run_mode.name,Sync::EX) do raise "Could not create PID file: #{pidfile}" unless Puppet::Util::Pidlock.new(pidfile).lock end end
Put the daemon into the background.
# File lib/puppet/network/server.rb, line 12 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("/") end
# File lib/puppet/network/server.rb, line 89 def listen raise "Cannot listen -- already listening." if listening? @listening = true @http_server.listen(address, port) end
# File lib/puppet/network/server.rb, line 85 def listening? @listening end
Provide the path to our pidfile.
# File lib/puppet/network/server.rb, line 45 def pidfile Puppet[:pidfile] end
Register handlers for REST networking, based on the Indirector.
# File lib/puppet/network/server.rb, line 63 def register(*indirections) raise ArgumentError, "Indirection names are required." if indirections.empty? indirections.flatten.each do |name| Puppet::Indirector::Indirection.model(name) || raise(ArgumentError, "Cannot locate indirection '#{name}'.") @routes[name.to_sym] = true end end
Remove the pid file for our daemon.
# File lib/puppet/network/server.rb, line 38 def remove_pidfile Puppet::Util.synchronize_on(Puppet.run_mode.name,Sync::EX) do Puppet::Util::Pidlock.new(pidfile).unlock end end
# File lib/puppet/network/server.rb, line 101 def start create_pidfile close_streams if Puppet[:daemonize] listen end
# File lib/puppet/network/server.rb, line 107 def stop unlisten remove_pidfile end
# File lib/puppet/network/server.rb, line 95 def unlisten raise "Cannot unlisten -- not currently listening." unless listening? @http_server.unlisten @listening = false end
Unregister Indirector handlers.
# File lib/puppet/network/server.rb, line 72 def unregister(*indirections) raise "Cannot unregister indirections while server is listening." if listening? indirections = @routes.keys if indirections.empty? indirections.flatten.each do |i| raise(ArgumentError, "Indirection [#{i}] is unknown.") unless @routes[i.to_sym] end indirections.flatten.each do |i| @routes.delete(i.to_sym) end end