Revision 3cd62121

b/Makefile.am
92 92

  
93 93
rapi_PYTHON = \
94 94
	lib/rapi/__init__.py \
95
	lib/rapi/RESTHTTPServer.py \
96 95
	lib/rapi/httperror.py \
97 96
	lib/rapi/baserlib.py \
98 97
	lib/rapi/connector.py \
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

  
/dev/null
1
#
2
#
3

  
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
# General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
# 02110-1301, USA.
18

  
19
"""RESTfull HTTPS Server module.
20

  
21
"""
22

  
23
from ganeti import constants
24
from ganeti import http
25
from ganeti import errors
26
from ganeti import rpc
27
from ganeti.rapi import connector
28

  
29

  
30
class RESTRequestHandler(http.HTTPRequestHandler):
31
  """REST Request Handler Class.
32

  
33
  """
34
  def setup(self):
35
    super(RESTRequestHandler, self).setup()
36
    self._resmap = connector.Mapper()
37

  
38
  def HandleRequest(self):
39
    """ Handels a request.
40

  
41
    """
42
    (HandlerClass, items, args) = self._resmap.getController(self.path)
43
    handler = HandlerClass(self, items, args)
44

  
45
    command = self.command.upper()
46
    try:
47
      fn = getattr(handler, command)
48
    except AttributeError, err:
49
      raise http.HTTPBadRequest()
50

  
51
    try:
52
      result = fn()
53

  
54
    except errors.OpPrereqError, err:
55
      # TODO: "Not found" is not always the correct error. Ganeti's core must
56
      # differentiate between different error types.
57
      raise http.HTTPNotFound(message=str(err))
58

  
59
    return result
60

  
61

  
62
def start(options):
63
  log_fd = open(constants.LOG_RAPIACCESS, 'a')
64
  try:
65
    apache_log = http.ApacheLogfile(log_fd)
66
    httpd = http.HTTPServer(("", options.port), RESTRequestHandler,
67
                            httplog=apache_log)
68
    try:
69
      httpd.serve_forever()
70
    finally:
71
      httpd.server_close()
72

  
73
  finally:
74
    log_fd.close()

Also available in: Unified diff