[Kimchi-devel] [PATCH v2 3/4] Github #329: server, root and utils changes

Daniel Barboza danielhb at linux.vnet.ibm.com
Tue Apr 8 20:50:29 UTC 2014


From: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>

In server.py adjustments were need to consider that the cherrypy
instance that will run as frontend will not have a model instance
and logging.

utils.py: the code that verifies and validates the commmand line
arguments to launch kimchid was moved here to avoid code repetition,
now that the function is called in more than one place.

root.py: do not load API.json schema for the frontend instance.

Signed-off-by: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
---
 src/kimchi/root.py   |   6 ++--
 src/kimchi/server.py | 100 ++++++++++++++++++++++++++++++---------------------
 src/kimchi/utils.py  |  42 +++++++++++++++++++++-
 3 files changed, 104 insertions(+), 44 deletions(-)

diff --git a/src/kimchi/root.py b/src/kimchi/root.py
index 9bae34a..4ca28b9 100644
--- a/src/kimchi/root.py
+++ b/src/kimchi/root.py
@@ -92,8 +92,10 @@ class KimchiRoot(Root):
         self.default_page = 'kimchi-ui.html'
         for ident, node in sub_nodes.items():
             setattr(self, ident, node(model))
-        self.api_schema = json.load(open(os.path.join(paths.src_dir,
-                                                      'API.json')))
+        # backend only
+        if model is not None:
+            self.api_schema = json.load(open(os.path.join(paths.src_dir,
+                                                          'API.json')))
         self.paths = paths
         self.domain = 'kimchi'
         self.messages = messages
diff --git a/src/kimchi/server.py b/src/kimchi/server.py
index 0d02868..c9512fe 100644
--- a/src/kimchi/server.py
+++ b/src/kimchi/server.py
@@ -56,69 +56,87 @@ def set_no_cache():
         hList = h
 
 
+def ignore_cookie():
+    cherrypy.response.cookie = cherrypy.request.cookie
+
+
 class Server(object):
     def __init__(self, options):
-        make_dirs = [
-            os.path.dirname(os.path.abspath(options.access_log)),
-            os.path.dirname(os.path.abspath(options.error_log)),
-            os.path.dirname(os.path.abspath(config.get_object_store())),
-            os.path.abspath(config.get_screenshot_path()),
-            os.path.abspath(config.get_debugreports_path()),
-            os.path.abspath(config.get_distros_store())
-        ]
-        for directory in make_dirs:
-            if not os.path.isdir(directory):
-                os.makedirs(directory)
+        if options.backend:
+            make_dirs = [
+                os.path.dirname(os.path.abspath(options.access_log)),
+                os.path.dirname(os.path.abspath(options.error_log)),
+                os.path.dirname(os.path.abspath(config.get_object_store())),
+                os.path.abspath(config.get_screenshot_path()),
+                os.path.abspath(config.get_debugreports_path()),
+                os.path.abspath(config.get_distros_store())
+            ]
+            for directory in make_dirs:
+                if not os.path.isdir(directory):
+                    os.makedirs(directory)
 
         self.configObj = KimchiConfig()
         cherrypy.tools.nocache = cherrypy.Tool('on_end_resource', set_no_cache)
         cherrypy.tools.kimchiauth = cherrypy.Tool('before_handler',
                                                   auth.kimchiauth)
+
+        cherrypy.tools.ignorecookie = cherrypy.Tool('before_finalize',
+                                                    ignore_cookie)
         cherrypy.server.socket_host = options.host
         cherrypy.server.socket_port = options.port
 
-        # SSL Server
-        try:
-            if options.ssl_port and options.ssl_port > 0:
-                self._init_ssl(options)
-        except AttributeError:
-            pass
+        # SSL Server operations - backend instance only
+        if options.backend:
+            try:
+                if options.ssl_port and options.ssl_port > 0:
+                    self._init_ssl(options)
+            except AttributeError:
+                pass
+            cherrypy.log.access_file = options.access_log
+            cherrypy.log.error_file = options.error_log
+
+            logLevel = LOGGING_LEVEL.get(options.log_level, logging.DEBUG)
 
-        cherrypy.log.screen = True
-        cherrypy.log.access_file = options.access_log
-        cherrypy.log.error_file = options.error_log
+            # Create handler to rotate access log file
+            h = logging.handlers.RotatingFileHandler(options.access_log, 'a',
+                                                     10000000, 1000)
+            h.setLevel(logLevel)
+            h.setFormatter(cherrypy._cplogging.logfmt)
 
-        logLevel = LOGGING_LEVEL.get(options.log_level, logging.DEBUG)
-        dev_env = options.environment != 'production'
+            # Add access log file to cherrypy configuration
+            cherrypy.log.access_log.addHandler(h)
 
-        # Create handler to rotate access log file
-        h = logging.handlers.RotatingFileHandler(options.access_log, 'a',
-                                                 10000000, 1000)
-        h.setLevel(logLevel)
-        h.setFormatter(cherrypy._cplogging.logfmt)
+            # Create handler to rotate error log file
+            h = logging.handlers.RotatingFileHandler(options.error_log, 'a',
+                                                     10000000, 1000)
+            h.setLevel(logLevel)
+            h.setFormatter(cherrypy._cplogging.logfmt)
 
-        # Add access log file to cherrypy configuration
-        cherrypy.log.access_log.addHandler(h)
+            # Add rotating log file to cherrypy configuration
+            cherrypy.log.error_log.addHandler(h)
 
-        # Create handler to rotate error log file
-        h = logging.handlers.RotatingFileHandler(options.error_log, 'a',
-                                                 10000000, 1000)
-        h.setLevel(logLevel)
-        h.setFormatter(cherrypy._cplogging.logfmt)
+        else:
+            cherrypy.log.access_file = None
+            cherrypy.log.error_file = None
 
-        # Add rotating log file to cherrypy configuration
-        cherrypy.log.error_log.addHandler(h)
+        cherrypy.log.screen = options.log_screen
 
         # Handling running mode
+        dev_env = options.environment != 'production'
         if not dev_env:
             cherrypy.config.update({'environment': 'production'})
 
-        if hasattr(options, 'model'):
-            model_instance = options.model
-        elif options.test:
-            model_instance = mockmodel.get_mock_environment()
+        # model_instance won't be created in the frontend
+        # instance
+        if options.backend:
+            if hasattr(options, 'model'):
+                model_instance = options.model
+            elif options.test:
+                model_instance = mockmodel.get_mock_environment()
+            else:
+                model_instance = model.Model()
         else:
-            model_instance = model.Model()
+            model_instance = None
 
         if isinstance(model_instance, model.Model):
             vnc_ws_proxy = vnc.new_ws_proxy()
diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
index a30dcfe..3b2223c 100644
--- a/src/kimchi/utils.py
+++ b/src/kimchi/utils.py
@@ -20,6 +20,7 @@
 
 import cherrypy
 import grp
+import optparse
 import os
 import psutil
 import pwd
@@ -28,12 +29,13 @@ import subprocess
 import traceback
 import urllib2
 from multiprocessing import Process, Queue
+from optparse import OptionParser
 from threading import Timer
 
 from cherrypy.lib.reprconf import Parser
 
 from kimchi.asynctask import AsyncTask
-from kimchi.config import paths, PluginPaths
+from kimchi.config import config, paths, PluginPaths
 from kimchi.exception import InvalidParameter, TimeoutExpired
 
 
@@ -264,3 +266,41 @@ def probe_file_permission_as_user(file, user):
     p.start()
     p.join()
     return queue.get()
+
+
+def parse_command_line_options():
+    ACCESS_LOG = "kimchi-access.log"
+    ERROR_LOG = "kimchi-error.log"
+
+    host = config.get("server", "host")
+    port = config.get("server", "port")
+    ssl_port = config.get("server", "ssl_port")
+    runningEnv = config.get('server', 'environment')
+    logDir = config.get("logging", "log_dir")
+    logLevel = config.get("logging", "log_level")
+
+    parser = OptionParser()
+    parser.add_option('--host', type="string", default=host,
+                      help="Hostname to listen on")
+    parser.add_option('--port', type="int", default=port,
+                      help="Port to listen on")
+    parser.add_option('--ssl-port', type="int", default=ssl_port,
+                      help="Enable a SSL server on the given port")
+    parser.add_option('--log-level', default=logLevel,
+                      help="Logging level")
+    parser.add_option('--access-log',
+                      default=os.path.join(logDir, ACCESS_LOG),
+                      help="Access log file")
+    parser.add_option('--error-log',
+                      default=os.path.join(logDir, ERROR_LOG),
+                      help="Error log file")
+    parser.add_option('--environment', default=runningEnv,
+                      help="Running environment of kimchi server")
+    parser.add_option('--test', action='store_true',
+                      help="Run server in mock model")
+    parser.add_option('--backend', action='store_true',
+                      help=optparse.SUPPRESS_HELP)
+    parser.add_option('--log_screen', action='store_true',
+                      help=optparse.SUPPRESS_HELP)
+
+    return parser.parse_args()
-- 
1.8.3.1




More information about the Kimchi-devel mailing list