Revision 3cd62121 daemons/ganeti-rapi

b/daemons/ganeti-rapi
26 26
import sys
27 27
import os
28 28

  
29
# we need to import rpc early in order to get our custom reactor,
30
# instead of the default twisted one; without this, things breaks in a
31
# not-nice-to-debug way
32
from ganeti import rpc
33 29
from ganeti import constants
30
from ganeti import errors
31
from ganeti import http
32
from ganeti import rpc
34 33
from ganeti import utils
35
from ganeti.rapi import RESTHTTPServer
34
from ganeti.rapi import connector
35

  
36

  
37
class RESTRequestHandler(http.HTTPRequestHandler):
38
  """REST Request Handler Class.
39

  
40
  """
41
  def setup(self):
42
    super(RESTRequestHandler, self).setup()
43
    self._resmap = connector.Mapper()
44

  
45
  def HandleRequest(self):
46
    """ Handels a request.
47

  
48
    """
49
    (HandlerClass, items, args) = self._resmap.getController(self.path)
50
    handler = HandlerClass(self, items, args)
51

  
52
    command = self.command.upper()
53
    try:
54
      fn = getattr(handler, command)
55
    except AttributeError, err:
56
      raise http.HTTPBadRequest()
57

  
58
    try:
59
      result = fn()
60

  
61
    except errors.OpPrereqError, err:
62
      # TODO: "Not found" is not always the correct error. Ganeti's core must
63
      # differentiate between different error types.
64
      raise http.HTTPNotFound(message=str(err))
65

  
66
    return result
36 67

  
37 68

  
38 69
def ParseOptions():
......
85 116

  
86 117
  """
87 118
  options, args = ParseOptions()
119

  
88 120
  if options.fork:
89 121
    utils.Daemonize(logfile=constants.LOG_RAPISERVER)
90
  RESTHTTPServer.start(options)
122

  
123
  log_fd = open(constants.LOG_RAPIACCESS, 'a')
124
  try:
125
    apache_log = http.ApacheLogfile(log_fd)
126
    httpd = http.HTTPServer(("", options.port), RESTRequestHandler,
127
                            httplog=apache_log)
128
    try:
129
      httpd.serve_forever()
130
    finally:
131
      httpd.server_close()
132

  
133
  finally:
134
    log_fd.close()
135

  
91 136
  sys.exit(0)
92 137

  
93 138

  

Also available in: Unified diff