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
40 class RemoteApiHttpServer(http.HttpServer):
41 """REST Request Handler Class.
44 def __init__(self, *args, **kwargs):
45 http.HttpServer.__init__(self, *args, **kwargs)
46 self._resmap = connector.Mapper()
48 def HandleRequest(self, req):
52 (HandlerClass, items, args) = self._resmap.getController(req.request_path)
53 handler = HandlerClass(items, args, req)
55 method = req.request_method.upper()
57 fn = getattr(handler, method)
58 except AttributeError, err:
59 raise http.HttpBadRequest()
63 sn = handler.getSerialNumber()
65 req.response_headers[http.HTTP_ETAG] = str(sn)
67 logging.exception("Error while handling the %s request", method)
74 """Parse the command line options.
77 (options, args) as from OptionParser.parse_args()
80 parser = optparse.OptionParser(description="Ganeti Remote API",
81 usage="%prog [-d] [-p port]",
82 version="%%prog (ganeti) %s" %
83 constants.RAPI_VERSION)
84 parser.add_option("-d", "--debug", dest="debug",
85 help="Enable some debug messages",
86 default=False, action="store_true")
87 parser.add_option("-p", "--port", dest="port",
88 help="Port to run API (%s default)." %
90 default=constants.RAPI_PORT, type="int")
91 parser.add_option("-S", "--https", dest="ssl",
92 help="Secure HTTP protocol with SSL",
93 default=False, action="store_true")
94 parser.add_option("-K", "--ssl-key", dest="ssl_key",
96 default=None, type="string")
97 parser.add_option("-C", "--ssl-cert", dest="ssl_cert",
98 help="SSL certificate",
99 default=None, type="string")
100 parser.add_option("-f", "--foreground", dest="fork",
101 help="Don't detach from the current terminal",
102 default=True, action="store_false")
104 options, args = parser.parse_args()
107 print >> sys.stderr, "Usage: %s [-d] [-p port]" % sys.argv[0]
110 if options.ssl and not (options.ssl_cert and options.ssl_key):
111 print >> sys.stderr, ("For secure mode please provide "
112 "--ssl-key and --ssl-cert arguments")
122 options, args = ParseOptions()
124 ssconf.CheckMaster(options.debug)
127 utils.Daemonize(logfile=constants.LOG_RAPISERVER)
129 utils.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug,
130 stderr_logging=not options.fork)
132 utils.WritePidFile(constants.RAPI_PID)
134 mainloop = daemon.Mainloop()
135 server = RemoteApiHttpServer(mainloop, "", options.port)
142 utils.RemovePidFile(constants.RAPI_PID)
145 if __name__ == '__main__':