RESTHTTPServer.py 2.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
#

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

"""RESTfull HTTPS Server module.

"""

from ganeti import constants
24
from ganeti import http
25
26
from ganeti import errors
from ganeti import rpc
27
from ganeti.rapi import connector
28
29
30
from ganeti.rapi import httperror


31
class RESTRequestHandler(http.HTTPRequestHandler):
32
33
34
35
  """REST Request Handler Class.

  """
  def setup(self):
36
    super(RESTRequestHandler, self).setup()
37
    self._resmap = connector.Mapper()
38
39
40
  
  def HandleRequest(self):
    """ Handels a request.
41
42

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

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

52
53
    try:
      result = fn()
54

55
56
57
58
59
60
    except errors.OpPrereqError, err:
      # TODO: "Not found" is not always the correct error. Ganeti's core must
      # differentiate between different error types.
      raise httperror.HTTPNotFound(message=str(err))
    
    return result
61
62
63


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

74
  finally:
75
    log_fd.close()