Class: Puppet::Network::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/network/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Server

Returns a new instance of Server.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vendor/puppet/network/server.rb', line 52

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

#addressObject (readonly)

Returns the value of attribute address.



5
6
7
# File 'lib/vendor/puppet/network/server.rb', line 5

def address
  @address
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/vendor/puppet/network/server.rb', line 5

def port
  @port
end

#protocolsObject (readonly)

Returns the value of attribute protocols.



5
6
7
# File 'lib/vendor/puppet/network/server.rb', line 5

def protocols
  @protocols
end

#server_typeObject (readonly)

Returns the value of attribute server_type.



5
6
7
# File 'lib/vendor/puppet/network/server.rb', line 5

def server_type
  @server_type
end

Instance Method Details

#create_pidfileObject

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/vendor/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

#daemonizeObject

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/vendor/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.replace_file("/tmp/daemonout", 0644) { |f|
      f.puts "Could not start #{Puppet[:name]}: #{detail}"
    }
    raise "Could not start #{Puppet[:name]}: #{detail}"
  end
end

#http_server_classObject



135
136
137
# File 'lib/vendor/puppet/network/server.rb', line 135

def http_server_class
  http_server_class_by_type(@server_type)
end

#listenObject



123
124
125
126
127
# File 'lib/vendor/puppet/network/server.rb', line 123

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

Returns:

  • (Boolean)


119
120
121
# File 'lib/vendor/puppet/network/server.rb', line 119

def listening?
  @listening
end

#pidfileObject

Provide the path to our pidfile.



48
49
50
# File 'lib/vendor/puppet/network/server.rb', line 48

def pidfile
  Puppet[:pidfile]
end

#register(*indirections) ⇒ Object

Register handlers for REST networking, based on the Indirector.

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
# File 'lib/vendor/puppet/network/server.rb', line 74

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.

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
# File 'lib/vendor/puppet/network/server.rb', line 97

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_pidfileObject

Remove the pid file for our daemon.



41
42
43
44
45
# File 'lib/vendor/puppet/network/server.rb', line 41

def remove_pidfile
  Puppet::Util.synchronize_on(Puppet[:name],Sync::EX) do
    Puppet::Util::Pidlock.new(pidfile).unlock
  end
end

#startObject



139
140
141
142
# File 'lib/vendor/puppet/network/server.rb', line 139

def start
  create_pidfile
  listen
end

#stopObject



144
145
146
147
# File 'lib/vendor/puppet/network/server.rb', line 144

def stop
  unlisten
  remove_pidfile
end

#unlistenObject



129
130
131
132
133
# File 'lib/vendor/puppet/network/server.rb', line 129

def unlisten
  raise "Cannot unlisten -- not currently listening." unless listening?
  http_server.unlisten
  @listening = false
end

#unregister(*indirections) ⇒ Object

Unregister Indirector handlers.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vendor/puppet/network/server.rb', line 83

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.



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vendor/puppet/network/server.rb', line 106

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