4 # Copyright (C) 2006, 2007 Google Inc.
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.
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.
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
21 """ Ganeti Remote API master script.
31 from ganeti import constants
32 from ganeti import errors
33 from ganeti import http
34 from ganeti import daemon
35 from ganeti import ssconf
36 from ganeti import utils
37 from ganeti.rapi import connector
39 import ganeti.http.server
42 class RemoteApiHttpServer(http.server.HttpServer):
43 """REST Request Handler Class.
46 def __init__(self, *args, **kwargs):
47 http.server.HttpServer.__init__(self, *args, **kwargs)
48 self._resmap = connector.Mapper()
50 def HandleRequest(self, req):
54 (HandlerClass, items, args) = self._resmap.getController(req.request_path)
55 handler = HandlerClass(items, args, req)
57 method = req.request_method.upper()
59 fn = getattr(handler, method)
60 except AttributeError, err:
61 raise http.HttpBadRequest()
65 sn = handler.getSerialNumber()
67 req.response_headers[http.HTTP_ETAG] = str(sn)
69 logging.exception("Error while handling the %s request", method)
76 """Parse the command line options.
78 @return: (options, args) as from OptionParser.parse_args()
81 parser = optparse.OptionParser(description="Ganeti Remote API",
82 usage="%prog [-d] [-p port]",
83 version="%%prog (ganeti) %s" %
84 constants.RAPI_VERSION)
85 parser.add_option("-d", "--debug", dest="debug",
86 help="Enable some debug messages",
87 default=False, action="store_true")
88 parser.add_option("-p", "--port", dest="port",
89 help="Port to run API (%s default)." %
91 default=constants.RAPI_PORT, type="int")
92 parser.add_option("-S", "--https", dest="ssl",
93 help="Secure HTTP protocol with SSL",
94 default=False, action="store_true")
95 parser.add_option("-K", "--ssl-key", dest="ssl_key",
97 default=None, type="string")
98 parser.add_option("-C", "--ssl-cert", dest="ssl_cert",
99 help="SSL certificate",
100 default=None, type="string")
101 parser.add_option("-f", "--foreground", dest="fork",
102 help="Don't detach from the current terminal",
103 default=True, action="store_false")
105 options, args = parser.parse_args()
108 print >> sys.stderr, "Usage: %s [-d] [-p port]" % sys.argv[0]
111 if options.ssl and not (options.ssl_cert and options.ssl_key):
112 print >> sys.stderr, ("For secure mode please provide "
113 "--ssl-key and --ssl-cert arguments")
123 options, args = ParseOptions()
125 ssconf.CheckMaster(options.debug)
128 utils.Daemonize(logfile=constants.LOG_RAPISERVER)
130 utils.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug,
131 stderr_logging=not options.fork)
133 utils.WritePidFile(constants.RAPI_PID)
135 mainloop = daemon.Mainloop()
136 server = RemoteApiHttpServer(mainloop, "", options.port)
143 utils.RemovePidFile(constants.RAPI_PID)
146 if __name__ == '__main__':