class Puppet::Network::Server

Attributes

address[R]
port[R]

Public Class Methods

new(address, port, handlers = nil) click to toggle source
# 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

Public Instance Methods

close_streams() click to toggle source
# File lib/puppet/network/server.rb, line 25
def close_streams()
  Puppet::Daemon.close_streams()
end
create_pidfile() click to toggle source

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
daemonize() click to toggle source

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
listen() click to toggle source
# 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
listening?() click to toggle source
# File lib/puppet/network/server.rb, line 85
def listening?
  @listening
end
pidfile() click to toggle source

Provide the path to our pidfile.

# File lib/puppet/network/server.rb, line 45
def pidfile
  Puppet[:pidfile]
end
register(*indirections) click to toggle source

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_pidfile() click to toggle source

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
start() click to toggle source
# File lib/puppet/network/server.rb, line 101
def start
  create_pidfile
  close_streams if Puppet[:daemonize]
  listen
end
stop() click to toggle source
# File lib/puppet/network/server.rb, line 107
def stop
  unlisten
  remove_pidfile
end
unlisten() click to toggle source
# 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(*indirections) click to toggle source

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