Class: Puppet::Network::HTTP::WEBrick

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/network/http/webrick.rb

Instance Method Summary collapse

Constructor Details

#initializeWEBrick

Returns a new instance of WEBrick.



11
12
13
# File 'lib/puppet/network/http/webrick.rb', line 11

def initialize
  @listening = false
end

Instance Method Details

#listen(address, port) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/network/http/webrick.rb', line 15

def listen(address, port)
  arguments = {:BindAddress => address, :Port => port, :DoNotReverseLookup => true}
  arguments.merge!(setup_logger)
  arguments.merge!(setup_ssl)

  BasicSocket.do_not_reverse_lookup = true

  @server = WEBrick::HTTPServer.new(arguments)
  @server.listeners.each { |l| l.start_immediately = false }

  @server.mount('/', Puppet::Network::HTTP::WEBrickREST)

  raise "WEBrick server is already listening" if @listening
  @listening = true
  @thread = Thread.new do
    @server.start do |sock|
      timeout = 10.0
      if ! IO.select([sock],nil,nil,timeout)
        raise "Client did not send data within %.1f seconds of connecting" % timeout
      end
      sock.accept
      @server.run(sock)
    end
  end
  sleep 0.1 until @server.status == :Running
end

#listening?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/puppet/network/http/webrick.rb', line 50

def listening?
  @listening
end

#setup_loggerObject

Configure our http log file.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/puppet/network/http/webrick.rb', line 59

def setup_logger
  # Make sure the settings are all ready for us.
  Puppet.settings.use(:main, :ssl, :application)

  if Puppet.run_mode.master?
    file = Puppet[:masterhttplog]
  else
    file = Puppet[:httplog]
  end

  # open the log manually to prevent file descriptor leak
  file_io = ::File.open(file, "a+")
  file_io.sync = true
  if defined?(Fcntl::FD_CLOEXEC)
    file_io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
  end

  args = [file_io]
  args << WEBrick::Log::DEBUG if Puppet::Util::Log.level == :debug

  logger = WEBrick::Log.new(*args)
  return :Logger => logger, :AccessLog => [
    [logger, WEBrick::AccessLog::COMMON_LOG_FORMAT ],
    [logger, WEBrick::AccessLog::REFERER_LOG_FORMAT ]
  ]
end

#setup_sslObject

Add all of the ssl cert information.

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet/network/http/webrick.rb', line 87

def setup_ssl
  results = {}

  # Get the cached copy.  We know it's been generated, too.
  host = Puppet::SSL::Host.localhost

  raise Puppet::Error, "Could not retrieve certificate for #{host.name} and not running on a valid certificate authority" unless host.certificate

  results[:SSLPrivateKey] = host.key.content
  results[:SSLCertificate] = host.certificate.content
  results[:SSLStartImmediately] = true
  results[:SSLEnable] = true
  results[:SSLOptions] = OpenSSL::SSL::OP_NO_SSLv2

  raise Puppet::Error, "Could not find CA certificate" unless Puppet::SSL::Certificate.indirection.find(Puppet::SSL::CA_NAME)

  results[:SSLCACertificateFile] = ssl_configuration.ca_auth_file
  results[:SSLVerifyClient] = OpenSSL::SSL::VERIFY_PEER

  results[:SSLCertificateStore] = host.ssl_store

  results
end

#unlistenObject



42
43
44
45
46
47
48
# File 'lib/puppet/network/http/webrick.rb', line 42

def unlisten
  raise "WEBrick server is not listening" unless @listening
  @server.shutdown
  wait_for_shutdown
  @server = nil
  @listening = false
end

#wait_for_shutdownObject



54
55
56
# File 'lib/puppet/network/http/webrick.rb', line 54

def wait_for_shutdown
  @thread.join
end