Commit 3cd62121 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Move ganeti-rapi core code to daemon

All other daemons have their main code in themselves and not in a module.
This patch does the same to ganeti-rapi by moving the code from
lib/rapi/RESTHTTPServer.py to daemons/ganeti-rapi.

Reviewed-by: iustinp
parent e2ae9123
......@@ -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 \
......
......@@ -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)
......
#
#
# 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()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment