From 3cd62121bf36f1f6d4918de28892e89e194910e1 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Mon, 28 Jul 2008 10:17:13 +0000
Subject: [PATCH] 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
---
 Makefile.am                |  1 -
 daemons/ganeti-rapi        | 57 +++++++++++++++++++++++++----
 lib/rapi/RESTHTTPServer.py | 74 --------------------------------------
 3 files changed, 51 insertions(+), 81 deletions(-)
 delete mode 100644 lib/rapi/RESTHTTPServer.py

diff --git a/Makefile.am b/Makefile.am
index 7507f8514..f788eea74 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 fa1d41f3e..29280f82e 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 0dd6556b9..000000000
--- 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()
-- 
GitLab