From 3e7cd20d4388b9f19db6da89a7c0a56e018a8482 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Fri, 22 Aug 2008 18:29:49 -0700 Subject: [PATCH] New "-k" command-line parameter to shutdown an existing local Luminotes server (if allowed by the configuration). --- config/Desktop.py | 1 + controller/Root.py | 10 ++++++++++ controller/test/Test_root.py | 22 ++++++++++++++++++++++ luminotes.py | 10 +++++++++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/config/Desktop.py b/config/Desktop.py index 1d20d98..2bc4ba6 100644 --- a/config/Desktop.py +++ b/config/Desktop.py @@ -12,6 +12,7 @@ settings = { "luminotes.launch_browser": True, "luminotes.db_host": None, # use local SQLite database "luminotes.auto_login_username": "desktopuser", + "luminotes.allow_shutdown_command": True, # used to stop the process during uninstall "luminotes.rate_plans": [ { "name": "desktop", diff --git a/controller/Root.py b/controller/Root.py index 76c0bc4..d2eee47 100644 --- a/controller/Root.py +++ b/controller/Root.py @@ -367,6 +367,16 @@ class Root( object ): response = u"pong", ) + @expose( view = Json ) + def shutdown( self ): + # this is typically only allowed in the desktop configuration + if self.__settings[ u"global" ].get( u"luminotes.allow_shutdown_command" ) is not True: + return dict() + + cherrypy.server.stop() + + return dict() + def _cp_on_http_error( self, status, message ): """ CherryPy HTTP error handler, used to display page not found and generic error pages. diff --git a/controller/test/Test_root.py b/controller/test/Test_root.py index 9ba15be..4f6fa0b 100644 --- a/controller/test/Test_root.py +++ b/controller/test/Test_root.py @@ -482,6 +482,28 @@ class Test_root( Test_controller ): assert result.get( "response" ) == u"pong" + def test_shutdown( self ): + self.settings[ u"global" ][ u"luminotes.allow_shutdown_command" ] = True + + assert cherrypy.server._is_ready() is True + result = self.http_get( "/shutdown" ) + + assert cherrypy.server._is_ready() is False + + def test_shutdown_disallowed_explicitly( self ): + self.settings[ u"global" ][ u"luminotes.allow_shutdown_command" ] = False + + assert cherrypy.server._is_ready() is True + result = self.http_get( "/shutdown" ) + + assert cherrypy.server._is_ready() is True + + def test_shutdown_disallowed_implicitly( self ): + assert cherrypy.server._is_ready() is True + result = self.http_get( "/shutdown" ) + + assert cherrypy.server._is_ready() is True + def test_404( self ): result = self.http_get( "/four_oh_four" ) diff --git a/luminotes.py b/luminotes.py index f91c88e..efe8e80 100755 --- a/luminotes.py +++ b/luminotes.py @@ -47,11 +47,19 @@ def main( args ): launch_browser = cherrypy.config.configMap[ u"global" ].get( u"luminotes.launch_browser" ) - # check to see if the server is already running socket.setdefaulttimeout( INITIAL_SOCKET_TIMEOUT_SECONDS ) server_url = u"http://localhost:%d/" % cherrypy.config.configMap[ u"global" ].get( u"server.socket_port" ) server_present = True + # if requested, attempt to shutdown an existing server and exit + if args and "-k" in args: + try: + urllib.urlopen( "%sshutdown" % server_url ) + except urllib.URLError: + pass + sys.exit( 0 ) + + # check to see if the server is already running try: urllib.urlopen( "%sping" % server_url ) except urllib.URLError: