root / daemons / ganeti-rapi @ 16a8967d
History | View | Annotate | Download (4.1 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 logging |
26 |
import optparse |
27 |
import sys |
28 |
import os |
29 |
import signal |
30 |
|
31 |
from ganeti import logger |
32 |
from ganeti import constants |
33 |
from ganeti import errors |
34 |
from ganeti import http |
35 |
from ganeti import daemon |
36 |
from ganeti import ssconf |
37 |
from ganeti import utils |
38 |
from ganeti.rapi import connector |
39 |
|
40 |
|
41 |
class RemoteApiHttpServer(http.HttpServer): |
42 |
"""REST Request Handler Class. |
43 |
|
44 |
""" |
45 |
def __init__(self, *args, **kwargs): |
46 |
http.HttpServer.__init__(self, *args, **kwargs) |
47 |
self._resmap = connector.Mapper() |
48 |
|
49 |
def HandleRequest(self, req): |
50 |
"""Handles a request. |
51 |
|
52 |
""" |
53 |
(HandlerClass, items, args) = self._resmap.getController(req.request_path) |
54 |
handler = HandlerClass(items, args, req.request_post_data) |
55 |
|
56 |
method = req.request_method.upper() |
57 |
try: |
58 |
fn = getattr(handler, method) |
59 |
except AttributeError, err: |
60 |
raise http.HTTPBadRequest() |
61 |
|
62 |
try: |
63 |
result = fn() |
64 |
except: |
65 |
logging.exception("Error while handling the %s request", method) |
66 |
raise |
67 |
|
68 |
return result |
69 |
|
70 |
|
71 |
def ParseOptions(): |
72 |
"""Parse the command line options. |
73 |
|
74 |
Returns: |
75 |
(options, args) as from OptionParser.parse_args() |
76 |
|
77 |
""" |
78 |
parser = optparse.OptionParser(description="Ganeti Remote API", |
79 |
usage="%prog [-d] [-p port]", |
80 |
version="%%prog (ganeti) %s" % |
81 |
constants.RAPI_VERSION) |
82 |
parser.add_option("-d", "--debug", dest="debug", |
83 |
help="Enable some debug messages", |
84 |
default=False, action="store_true") |
85 |
parser.add_option("-p", "--port", dest="port", |
86 |
help="Port to run API (%s default)." % |
87 |
constants.RAPI_PORT, |
88 |
default=constants.RAPI_PORT, type="int") |
89 |
parser.add_option("-S", "--https", dest="ssl", |
90 |
help="Secure HTTP protocol with SSL", |
91 |
default=False, action="store_true") |
92 |
parser.add_option("-K", "--ssl-key", dest="ssl_key", |
93 |
help="SSL key", |
94 |
default=None, type="string") |
95 |
parser.add_option("-C", "--ssl-cert", dest="ssl_cert", |
96 |
help="SSL certificate", |
97 |
default=None, type="string") |
98 |
parser.add_option("-f", "--foreground", dest="fork", |
99 |
help="Don't detach from the current terminal", |
100 |
default=True, action="store_false") |
101 |
|
102 |
options, args = parser.parse_args() |
103 |
|
104 |
if len(args) != 0: |
105 |
print >> sys.stderr, "Usage: %s [-d] [-p port]" % sys.argv[0] |
106 |
sys.exit(1) |
107 |
|
108 |
if options.ssl and not (options.ssl_cert and options.ssl_key): |
109 |
print >> sys.stderr, ("For secure mode please provide " |
110 |
"--ssl-key and --ssl-cert arguments") |
111 |
sys.exit(1) |
112 |
|
113 |
return options, args |
114 |
|
115 |
|
116 |
def main(): |
117 |
"""Main function. |
118 |
|
119 |
""" |
120 |
options, args = ParseOptions() |
121 |
|
122 |
ssconf.CheckMaster(options.debug) |
123 |
|
124 |
if options.fork: |
125 |
utils.Daemonize(logfile=constants.LOG_RAPISERVER) |
126 |
|
127 |
logger.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug, |
128 |
stderr_logging=not options.fork) |
129 |
|
130 |
utils.WritePidFile(constants.RAPI_PID) |
131 |
try: |
132 |
mainloop = daemon.Mainloop() |
133 |
server = RemoteApiHttpServer(mainloop, ("", options.port)) |
134 |
server.Start() |
135 |
try: |
136 |
mainloop.Run() |
137 |
finally: |
138 |
server.Stop() |
139 |
finally: |
140 |
utils.RemovePidFile(constants.RAPI_PID) |
141 |
|
142 |
|
143 |
if __name__ == '__main__': |
144 |
main() |