Class: Puppet::Network::Server
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#protocols ⇒ Object
readonly
Returns the value of attribute protocols.
-
#server_type ⇒ Object
readonly
Returns the value of attribute server_type.
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.
- #http_server_class ⇒ Object
-
#initialize(args = {}) ⇒ Server
constructor
A new instance of Server.
- #listen ⇒ Object
- #listening? ⇒ Boolean
-
#pidfile ⇒ Object
Provide the path to our pidfile.
-
#register(*indirections) ⇒ Object
Register handlers for REST networking, based on the Indirector.
-
#register_xmlrpc(*namespaces) ⇒ Object
Register xmlrpc handlers for backward compatibility.
-
#remove_pidfile ⇒ Object
Remove the pid file for our daemon.
- #start ⇒ Object
- #stop ⇒ Object
- #unlisten ⇒ Object
-
#unregister(*indirections) ⇒ Object
Unregister Indirector handlers.
-
#unregister_xmlrpc(*namespaces) ⇒ Object
Unregister xmlrpc handlers.
Constructor Details
#initialize(args = {}) ⇒ Server
Returns a new instance of Server.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/puppet/network/server.rb', line 53 def initialize(args = {}) valid_args = [:handlers, :xmlrpc_handlers, :port] bad_args = args.keys.find_all { |p| ! valid_args.include?(p) }.collect { |p| p.to_s }.join(",") raise ArgumentError, "Invalid argument(s) #{bad_args}" unless bad_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}]") @port = args[:port] || Puppet[:masterport] || raise(ArgumentError, "Must specify :port or configure Puppet :masterport") @address = determine_bind_address @protocols = [ :rest, :xmlrpc ] @listening = false @routes = {} @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 |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
5 6 7 |
# File 'lib/puppet/network/server.rb', line 5 def address @address end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
5 6 7 |
# File 'lib/puppet/network/server.rb', line 5 def port @port end |
#protocols ⇒ Object (readonly)
Returns the value of attribute protocols.
5 6 7 |
# File 'lib/puppet/network/server.rb', line 5 def protocols @protocols end |
#server_type ⇒ Object (readonly)
Returns the value of attribute server_type.
5 6 7 |
# File 'lib/puppet/network/server.rb', line 5 def server_type @server_type 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.
34 35 36 37 38 |
# File 'lib/puppet/network/server.rb', line 34 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.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/puppet/network/server.rb', line 8 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 Puppet::Util.secure_open("/tmp/daemonout", "w") { |f| f.puts "Could not start #{Puppet[:name]}: #{detail}" } raise "Could not start #{Puppet[:name]}: #{detail}" end end |
#http_server_class ⇒ Object
136 137 138 |
# File 'lib/puppet/network/server.rb', line 136 def http_server_class http_server_class_by_type(@server_type) end |
#listen ⇒ Object
124 125 126 127 128 |
# File 'lib/puppet/network/server.rb', line 124 def listen raise "Cannot listen -- already listening." if listening? @listening = true http_server.listen(:address => address, :port => port, :handlers => @routes.keys, :xmlrpc_handlers => @xmlrpc_routes.keys, :protocols => protocols) end |
#listening? ⇒ Boolean
120 121 122 |
# File 'lib/puppet/network/server.rb', line 120 def listening? @listening end |
#pidfile ⇒ Object
Provide the path to our pidfile.
49 50 51 |
# File 'lib/puppet/network/server.rb', line 49 def pidfile Puppet[:pidfile] end |
#register(*indirections) ⇒ Object
Register handlers for REST networking, based on the Indirector.
75 76 77 78 79 80 81 |
# File 'lib/puppet/network/server.rb', line 75 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 |
#register_xmlrpc(*namespaces) ⇒ Object
Register xmlrpc handlers for backward compatibility.
98 99 100 101 102 103 104 |
# File 'lib/puppet/network/server.rb', line 98 def register_xmlrpc(*namespaces) raise ArgumentError, "XMLRPC namespaces are required." if namespaces.empty? namespaces.flatten.each do |name| Puppet::Network::Handler.handler(name) || raise(ArgumentError, "Cannot locate XMLRPC handler for namespace '#{name}'.") @xmlrpc_routes[name.to_sym] = true end end |
#remove_pidfile ⇒ Object
Remove the pid file for our daemon.
41 42 43 44 45 46 |
# File 'lib/puppet/network/server.rb', line 41 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 |
#start ⇒ Object
140 141 142 143 |
# File 'lib/puppet/network/server.rb', line 140 def start create_pidfile listen end |
#stop ⇒ Object
145 146 147 148 |
# File 'lib/puppet/network/server.rb', line 145 def stop unlisten remove_pidfile end |
#unlisten ⇒ Object
130 131 132 133 134 |
# File 'lib/puppet/network/server.rb', line 130 def unlisten raise "Cannot unlisten -- not currently listening." unless listening? http_server.unlisten @listening = false end |
#unregister(*indirections) ⇒ Object
Unregister Indirector handlers.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/puppet/network/server.rb', line 84 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 |
#unregister_xmlrpc(*namespaces) ⇒ Object
Unregister xmlrpc handlers.
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/puppet/network/server.rb', line 107 def unregister_xmlrpc(*namespaces) raise "Cannot unregister xmlrpc handlers while server is listening." if listening? namespaces = @xmlrpc_routes.keys if namespaces.empty? namespaces.flatten.each do |i| raise(ArgumentError, "XMLRPC handler '#{i}' is unknown.") unless @xmlrpc_routes[i.to_sym] end namespaces.flatten.each do |i| @xmlrpc_routes.delete(i.to_sym) end end |