Commit 1df6506c authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Use new HTTP server classes in ganeti-noded

Reviewed-by: iustinp
parent a43f68dc
......@@ -27,8 +27,6 @@
import os
import sys
import traceback
import BaseHTTPServer
import simplejson
import errno
import logging
......@@ -41,78 +39,39 @@ from ganeti import constants
from ganeti import objects
from ganeti import errors
from ganeti import ssconf
from ganeti import http
from ganeti import utils
_EXIT_GANETI_NODED = False
class ServerObject(BaseHTTPServer.BaseHTTPRequestHandler):
class NodeDaemonRequestHandler(http.HTTPRequestHandler):
"""The server implementation.
This class holds all methods exposed over the RPC interface.
"""
def do_PUT(self):
"""Handle a post request.
def HandleRequest(self):
"""Handle a request.
"""
global _EXIT_GANETI_NODED
if self.command.upper() != "PUT":
raise http.HTTPBadRequest()
path = self.path
if path.startswith("/"):
path = path[1:]
logging.debug("ServerObject: received call '%s'", path)
mname = "perspective_%s" % path
if not hasattr(self, mname):
self.send_error(404)
return False
method = getattr(self, mname)
try:
body_length = int(self.headers.get('Content-Length', '0'))
except ValueError:
self.send_error(400, 'No Content-Length header or invalid format')
return False
method = getattr(self, "perspective_%s" % path, None)
if method is None:
raise httperror.HTTPNotFound()
try:
body = self.rfile.read(body_length)
except socket.error, err:
logger.Error("Socket error while reading: %s" % str(err))
return
try:
params = simplejson.loads(body)
logging.debug("ServerObject: method parameters: %s", params)
result = method(params)
payload = simplejson.dumps(result)
return method(self.post_data)
except errors.QuitGanetiException, err:
global _EXIT_GANETI_NODED
_EXIT_GANETI_NODED = True
if isinstance(err.args, tuple) and len(err.args) == 2:
if err.args[0]:
self.send_error(500, "Error: %s" % str(err[1]))
else:
payload = simplejson.dumps(err.args[1])
else:
self.log_message('QuitGanetiException Usage Error')
self.send_error(500, "Error: %s" % str(err))
except Exception, err:
self.send_error(500, "Error: %s" % str(err))
return False
self.send_response(200)
self.send_header('Content-Length', str(len(payload)))
self.end_headers()
self.wfile.write(payload)
return True
def log_message(self, format, *args):
"""Log a request to the log.
This is the same as the parent, we just log somewhere else.
"""
msg = ("%s - - [%s] %s" %
(self.address_string(),
self.log_date_time_string(),
format % args))
logging.debug(msg)
# the new block devices --------------------------
......@@ -573,6 +532,11 @@ class ServerObject(BaseHTTPServer.BaseHTTPRequestHandler):
new_file_storage_dir)
class NodeDaemonHttpServer(http.HTTPServer):
def __init__(self, server_address):
http.HTTPServer.__init__(self, server_address, NodeDaemonRequestHandler)
def ParseOptions():
"""Parse the command line options.
......@@ -639,7 +603,7 @@ def main():
global _EXIT_GANETI_NODED
httpd = BaseHTTPServer.HTTPServer(('', port), ServerObject)
httpd = NodeDaemonHttpServer(('', port))
while (not _EXIT_GANETI_NODED):
httpd.handle_request()
......
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