diff --git a/Makefile.am b/Makefile.am index 7507f8514d056bd02fa5a29ddb279282bc51a07d..f788eea74c87320523d395e18285170da8b0615d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -92,7 +92,6 @@ hypervisor_PYTHON = \ rapi_PYTHON = \ lib/rapi/__init__.py \ - lib/rapi/RESTHTTPServer.py \ lib/rapi/httperror.py \ lib/rapi/baserlib.py \ lib/rapi/connector.py \ diff --git a/daemons/ganeti-rapi b/daemons/ganeti-rapi index fa1d41f3e57b2e5d0186b196a9cbccd6b89907f2..29280f82ed927ccc49ce55516f39aed240eb4a66 100755 --- a/daemons/ganeti-rapi +++ b/daemons/ganeti-rapi @@ -26,13 +26,44 @@ import optparse import sys import os -# we need to import rpc early in order to get our custom reactor, -# instead of the default twisted one; without this, things breaks in a -# not-nice-to-debug way -from ganeti import rpc from ganeti import constants +from ganeti import errors +from ganeti import http +from ganeti import rpc from ganeti import utils -from ganeti.rapi import RESTHTTPServer +from ganeti.rapi import connector + + +class RESTRequestHandler(http.HTTPRequestHandler): + """REST Request Handler Class. + + """ + def setup(self): + super(RESTRequestHandler, self).setup() + self._resmap = connector.Mapper() + + def HandleRequest(self): + """ Handels a request. + + """ + (HandlerClass, items, args) = self._resmap.getController(self.path) + handler = HandlerClass(self, items, args) + + command = self.command.upper() + try: + fn = getattr(handler, command) + except AttributeError, err: + raise http.HTTPBadRequest() + + try: + result = fn() + + except errors.OpPrereqError, err: + # TODO: "Not found" is not always the correct error. Ganeti's core must + # differentiate between different error types. + raise http.HTTPNotFound(message=str(err)) + + return result def ParseOptions(): @@ -85,9 +116,23 @@ def main(): """ options, args = ParseOptions() + if options.fork: utils.Daemonize(logfile=constants.LOG_RAPISERVER) - RESTHTTPServer.start(options) + + log_fd = open(constants.LOG_RAPIACCESS, 'a') + try: + apache_log = http.ApacheLogfile(log_fd) + httpd = http.HTTPServer(("", options.port), RESTRequestHandler, + httplog=apache_log) + try: + httpd.serve_forever() + finally: + httpd.server_close() + + finally: + log_fd.close() + sys.exit(0) diff --git a/lib/rapi/RESTHTTPServer.py b/lib/rapi/RESTHTTPServer.py deleted file mode 100644 index 0dd6556b9ad61ae68fce43dca4f7a6fdb2fff7f1..0000000000000000000000000000000000000000 --- a/lib/rapi/RESTHTTPServer.py +++ /dev/null @@ -1,74 +0,0 @@ -# -# - -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -# 02110-1301, USA. - -"""RESTfull HTTPS Server module. - -""" - -from ganeti import constants -from ganeti import http -from ganeti import errors -from ganeti import rpc -from ganeti.rapi import connector - - -class RESTRequestHandler(http.HTTPRequestHandler): - """REST Request Handler Class. - - """ - def setup(self): - super(RESTRequestHandler, self).setup() - self._resmap = connector.Mapper() - - def HandleRequest(self): - """ Handels a request. - - """ - (HandlerClass, items, args) = self._resmap.getController(self.path) - handler = HandlerClass(self, items, args) - - command = self.command.upper() - try: - fn = getattr(handler, command) - except AttributeError, err: - raise http.HTTPBadRequest() - - try: - result = fn() - - except errors.OpPrereqError, err: - # TODO: "Not found" is not always the correct error. Ganeti's core must - # differentiate between different error types. - raise http.HTTPNotFound(message=str(err)) - - return result - - -def start(options): - log_fd = open(constants.LOG_RAPIACCESS, 'a') - try: - apache_log = http.ApacheLogfile(log_fd) - httpd = http.HTTPServer(("", options.port), RESTRequestHandler, - httplog=apache_log) - try: - httpd.serve_forever() - finally: - httpd.server_close() - - finally: - log_fd.close()