瀏覽代碼

HUE-563. Hue management commands should be backwards-compatible.

Aditya Acharya 14 年之前
父節點
當前提交
2a515f6ae2

+ 1 - 1
desktop/conf.dist/hue.ini

@@ -42,7 +42,7 @@ http_500_debug_mode=0
 ## server_user=hue
 ## server_group=hue
 
-# If set to false, runserver will not actually start the web server.
+# If set to false, runcpserver will not actually start the web server.
 # Used if Apache is being used as a WSGI container.
 ## enable_server=yes
 

+ 1 - 1
desktop/core/setup.py

@@ -27,7 +27,7 @@ setup(
 
       entry_points = { 'console_scripts': [ 'supervisor = desktop.supervisor:main',
                                             'hue = desktop.manage_entry:entry', ],
-                       'desktop.supervisor.specs': [ 'runserver = desktop:SUPERVISOR_SPEC',
+                       'desktop.supervisor.specs': [ 'runcpserver = desktop:SUPERVISOR_SPEC',
                                                      'kt_renewer = desktop.kt_renewer:SPEC',
                                                      ]
                        },

+ 1 - 1
desktop/core/src/desktop/__init__.py

@@ -17,4 +17,4 @@
 from desktop.supervisor import DjangoCommandSupervisee
 
 SUPERVISOR_SPEC = DjangoCommandSupervisee(
-  "runserver", drop_root=False)
+  "runcpserver", drop_root=False)

+ 1 - 1
desktop/core/src/desktop/conf.py

@@ -50,7 +50,7 @@ SSL_PRIVATE_KEY = Config(
   default=None)
 ENABLE_SERVER = Config(
   key="enable_server",
-  help="If set to false, runserver will not actually start the web server.  Used if Apache is being used as a WSGI container.",
+  help="If set to false, runcpserver will not actually start the web server.  Used if Apache is being used as a WSGI container.",
   type=coerce_bool,
   default=True)
 CHERRYPY_SERVER_THREADS = Config(

+ 146 - 0
desktop/core/src/desktop/management/commands/runcherrypyserver.py

@@ -0,0 +1,146 @@
+#!/usr/bin/env python
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# a thirdparty project
+
+import sys, os, logging
+from django.core.management.base import BaseCommand
+
+from desktop import conf
+
+
+CPSERVER_HELP = r"""
+  Run Hue using the CherryPy WSGI server.
+"""
+
+CPSERVER_OPTIONS = {
+  'host': conf.HTTP_HOST.get(),
+  'port': conf.HTTP_PORT.get(),
+  'server_name': 'localhost',
+  'threads': conf.CHERRYPY_SERVER_THREADS.get(),
+  'daemonize': False, # supervisor does this for us
+  'workdir': None,
+  'pidfile': None,
+  'server_user': conf.SERVER_USER.get(),
+  'server_group': conf.SERVER_GROUP.get(),
+  'ssl_certificate': conf.SSL_CERTIFICATE.get(),
+  'ssl_private_key': conf.SSL_PRIVATE_KEY.get()
+}
+
+
+class Command(BaseCommand):
+    help = "CherryPy Server for Desktop."
+    args = ""
+
+    def handle(self, *args, **options):
+        from django.conf import settings
+        from django.utils import translation
+
+        if not conf.ENABLE_SERVER.get():
+          logging.info("Hue is configured to not start its own web server.")
+          sys.exit(0)
+
+        # Activate the current language, because it won't get activated later.
+        try:
+            translation.activate(settings.LANGUAGE_CODE)
+        except AttributeError:
+            pass
+        runcpserver(args)
+        
+    def usage(self, subcommand):
+        return CPSERVER_HELP
+
+def change_uid_gid(uid, gid=None):
+    """Try to change UID and GID to the provided values.
+    UID and GID are given as names like 'nobody' not integer.
+
+    Src: http://mail.mems-exchange.org/durusmail/quixote-users/4940/1/
+    """
+    if not os.geteuid() == 0:
+        # Do not try to change the gid/uid if not root.
+        return
+    (uid, gid) = get_uid_gid(uid, gid)
+    os.setgid(gid)
+    os.setuid(uid)
+
+def get_uid_gid(uid, gid=None):
+    """Try to change UID and GID to the provided values.
+    UID and GID are given as names like 'nobody' not integer.
+
+    Src: http://mail.mems-exchange.org/durusmail/quixote-users/4940/1/
+    """
+    import pwd, grp
+    uid, default_grp = pwd.getpwnam(uid)[2:4]
+    if gid is None:
+        gid = default_grp
+    else:
+        try:
+            gid = grp.getgrnam(gid)[2]            
+        except KeyError:
+            gid = default_grp
+    return (uid, gid)
+
+def drop_privileges_if_necessary(options):
+  if os.geteuid() == 0 and options['server_user'] and options['server_group']:
+    #ensure the that the daemon runs as specified user
+    change_uid_gid(options['server_user'], options['server_group'])
+
+def start_server(options):
+    """
+    Start CherryPy server
+    """
+    from desktop.lib.wsgiserver import CherryPyWSGIServer as Server
+    from django.core.handlers.wsgi import WSGIHandler
+    # Translogger wraps a WSGI app with Apache-style combined logging.
+    server = Server(
+        (options['host'], int(options['port'])),
+        WSGIHandler(),
+        int(options['threads']), 
+        options['server_name']
+    )
+    if options['ssl_certificate'] and options['ssl_private_key']:
+        server.ssl_certificate = options['ssl_certificate']
+        server.ssl_private_key = options['ssl_private_key']  
+    try:
+        server.bind_server()
+        drop_privileges_if_necessary(options)
+        server.listen_and_loop()
+    except KeyboardInterrupt:
+        server.stop()
+
+
+def runcpserver(argset=[], **kwargs):
+    # Get the options
+    options = CPSERVER_OPTIONS.copy()
+    options.update(kwargs)
+    for x in argset:
+        if "=" in x:
+            k, v = x.split('=', 1)
+        else:
+            k, v = x, True
+        options[k.lower()] = v
+    
+    if "help" in options:
+        print CPSERVER_HELP
+        return
+
+    # Start the webserver
+    print 'starting server with options %s' % options
+    start_server(options)
+
+
+if __name__ == '__main__':
+    runcpserver(sys.argv[1:])

+ 22 - 117
desktop/core/src/desktop/management/commands/runcpserver.py

@@ -14,133 +14,38 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-# a thirdparty project
 
-import sys, os, logging
+import logging
 from django.core.management.base import BaseCommand
-
 from desktop import conf
+from desktop import supervisor
+import os
+import sys
 
-
-CPSERVER_HELP = r"""
-  Run Hue using the CherryPy WSGI server.
+SERVER_HELP = r"""
+  Run Hue using either the CherryPy server or the Spawning server, based on
+  the current configuration.
 """
 
-CPSERVER_OPTIONS = {
-  'host': conf.HTTP_HOST.get(),
-  'port': conf.HTTP_PORT.get(),
-  'server_name': 'localhost',
-  'threads': conf.CHERRYPY_SERVER_THREADS.get(),
-  'daemonize': False, # supervisor does this for us
-  'workdir': None,
-  'pidfile': None,
-  'server_user': conf.SERVER_USER.get(),
-  'server_group': conf.SERVER_GROUP.get(),
-  'ssl_certificate': conf.SSL_CERTIFICATE.get(),
-  'ssl_private_key': conf.SSL_PRIVATE_KEY.get()
-}
-
+LOG = logging.getLogger(__name__)
 
 class Command(BaseCommand):
-    help = "CherryPy Server for Desktop."
-    args = ""
-
-    def handle(self, *args, **options):
-        from django.conf import settings
-        from django.utils import translation
-
-        if not conf.ENABLE_SERVER.get():
-          logging.info("Hue is configured to not start its own web server.")
-          sys.exit(0)
-
-        # Activate the current language, because it won't get activated later.
-        try:
-            translation.activate(settings.LANGUAGE_CODE)
-        except AttributeError:
-            pass
-        runcpserver(args)
-        
-    def usage(self, subcommand):
-        return CPSERVER_HELP
-
-def change_uid_gid(uid, gid=None):
-    """Try to change UID and GID to the provided values.
-    UID and GID are given as names like 'nobody' not integer.
-
-    Src: http://mail.mems-exchange.org/durusmail/quixote-users/4940/1/
-    """
-    if not os.geteuid() == 0:
-        # Do not try to change the gid/uid if not root.
-        return
-    (uid, gid) = get_uid_gid(uid, gid)
-    os.setgid(gid)
-    os.setuid(uid)
-
-def get_uid_gid(uid, gid=None):
-    """Try to change UID and GID to the provided values.
-    UID and GID are given as names like 'nobody' not integer.
-
-    Src: http://mail.mems-exchange.org/durusmail/quixote-users/4940/1/
-    """
-    import pwd, grp
-    uid, default_grp = pwd.getpwnam(uid)[2:4]
-    if gid is None:
-        gid = default_grp
-    else:
-        try:
-            gid = grp.getgrnam(gid)[2]            
-        except KeyError:
-            gid = default_grp
-    return (uid, gid)
-
-def drop_privileges_if_necessary(options):
-  if os.geteuid() == 0 and options['server_user'] and options['server_group']:
-    #ensure the that the daemon runs as specified user
-    change_uid_gid(options['server_user'], options['server_group'])
-
-def start_server(options):
-    """
-    Start CherryPy server
-    """
-    from desktop.lib.wsgiserver import CherryPyWSGIServer as Server
-    from django.core.handlers.wsgi import WSGIHandler
-    # Translogger wraps a WSGI app with Apache-style combined logging.
-    server = Server(
-        (options['host'], int(options['port'])),
-        WSGIHandler(),
-        int(options['threads']), 
-        options['server_name']
-    )
-    if options['ssl_certificate'] and options['ssl_private_key']:
-        server.ssl_certificate = options['ssl_certificate']
-        server.ssl_private_key = options['ssl_private_key']  
-    try:
-        server.bind_server()
-        drop_privileges_if_necessary(options)
-        server.listen_and_loop()
-    except KeyboardInterrupt:
-        server.stop()
-
+  help = "Web server for Hue."
 
-def runcpserver(argset=[], **kwargs):
-    # Get the options
-    options = CPSERVER_OPTIONS.copy()
-    options.update(kwargs)
-    for x in argset:
-        if "=" in x:
-            k, v = x.split('=', 1)
-        else:
-            k, v = x, True
-        options[k.lower()] = v
-    
-    if "help" in options:
-        print CPSERVER_HELP
-        return
+  def handle(self, *args, **options):
+    runserver()
 
-    # Start the webserver
-    print 'starting server with options %s' % options
-    start_server(options)
+  def usage(self, subcommand):
+    return SERVER_HELP
 
+def runserver():
+  script_name = "runspawningserver"
+  if conf.USE_CHERRYPY_SERVER.get():
+    script_name = "runcherrypyserver"
+  cmdv = supervisor.DjangoCommandSupervisee(script_name).cmdv
+  os.execv(cmdv[0], cmdv)
+  LOG.error("Failed to exec '%s' with argument '%s'" % (cmdv[0], cmdv[1],))
+  sys.exit(-1)
 
 if __name__ == '__main__':
-    runcpserver(sys.argv[1:])
+  runserver()

+ 0 - 51
desktop/core/src/desktop/management/commands/runserver.py

@@ -1,51 +0,0 @@
-#!/usr/bin/env python
-# Licensed to Cloudera, Inc. under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  Cloudera, Inc. licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-import logging
-from django.core.management.base import BaseCommand
-from desktop import conf
-from desktop import supervisor
-import os
-import sys
-
-SERVER_HELP = r"""
-  Run Hue using either the CherryPy server or the Spawning server, based on
-  the current configuration.
-"""
-
-LOG = logging.getLogger(__name__)
-
-class Command(BaseCommand):
-  help = "Web server for Hue."
-
-  def handle(self, *args, **options):
-    runserver()
-
-  def usage(self, subcommand):
-    return SERVER_HELP
-
-def runserver():
-  script_name = "runspawningserver"
-  if conf.USE_CHERRYPY_SERVER.get():
-    script_name = "runcpserver"
-  cmdv = supervisor.DjangoCommandSupervisee(script_name).cmdv
-  os.execv(cmdv[0], cmdv)
-  LOG.error("Failed to exec '%s' with argument '%s'" % (cmdv[0], cmdv[1],))
-  sys.exit(-1)
-
-if __name__ == '__main__':
-  runserver()