root / lib / rapi / RESTHTTPServer.py @ e2ae9123
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 |
|
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() |