Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / RESTHTTPServer.py @ a0638838

History | View | Annotate | Download (2 kB)

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
from ganeti.rapi import httperror
29

    
30

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

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

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

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

    
52
    try:
53
      result = fn()
54

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

    
62

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

    
74
  finally:
75
    log_fd.close()