root / daemons / ganeti-rapi @ 3cd62121
History | View | Annotate | Download (4 kB)
1 |
#!/usr/bin/python |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2006, 2007 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
""" Ganeti Remote API master script. |
22 |
""" |
23 |
|
24 |
import glob |
25 |
import optparse |
26 |
import sys |
27 |
import os |
28 |
|
29 |
from ganeti import constants |
30 |
from ganeti import errors |
31 |
from ganeti import http |
32 |
from ganeti import rpc |
33 |
from ganeti import utils |
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 |
67 |
|
68 |
|
69 |
def ParseOptions(): |
70 |
"""Parse the command line options. |
71 |
|
72 |
Returns: |
73 |
(options, args) as from OptionParser.parse_args() |
74 |
|
75 |
""" |
76 |
parser = optparse.OptionParser(description="Ganeti Remote API", |
77 |
usage="%prog [-d] [-p port]", |
78 |
version="%%prog (ganeti) %s" % |
79 |
constants.RAPI_VERSION) |
80 |
parser.add_option("-d", "--debug", dest="debug", |
81 |
help="Enable some debug messages", |
82 |
default=False, action="store_true") |
83 |
parser.add_option("-p", "--port", dest="port", |
84 |
help="Port to run API (%s default)." % |
85 |
constants.RAPI_PORT, |
86 |
default=constants.RAPI_PORT, type="int") |
87 |
parser.add_option("-S", "--https", dest="ssl", |
88 |
help="Secure HTTP protocol with SSL", |
89 |
default=False, action="store_true") |
90 |
parser.add_option("-K", "--ssl-key", dest="ssl_key", |
91 |
help="SSL key", |
92 |
default=None, type="string") |
93 |
parser.add_option("-C", "--ssl-cert", dest="ssl_cert", |
94 |
help="SSL certificate", |
95 |
default=None, type="string") |
96 |
parser.add_option("-f", "--foreground", dest="fork", |
97 |
help="Don't detach from the current terminal", |
98 |
default=True, action="store_false") |
99 |
|
100 |
options, args = parser.parse_args() |
101 |
|
102 |
if len(args) != 0: |
103 |
print >> sys.stderr, "Usage: %s [-d] [-p port]" % sys.argv[0] |
104 |
sys.exit(1) |
105 |
|
106 |
if options.ssl and not (options.ssl_cert and options.ssl_key): |
107 |
print >> sys.stderr, ("For secure mode please provide " |
108 |
"--ssl-key and --ssl-cert arguments") |
109 |
sys.exit(1) |
110 |
|
111 |
return options, args |
112 |
|
113 |
|
114 |
def main(): |
115 |
"""Main function. |
116 |
|
117 |
""" |
118 |
options, args = ParseOptions() |
119 |
|
120 |
if options.fork: |
121 |
utils.Daemonize(logfile=constants.LOG_RAPISERVER) |
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 |
|
136 |
sys.exit(0) |
137 |
|
138 |
|
139 |
if __name__ == '__main__': |
140 |
main() |