From 3b515eb41c99efe1ee9d0f5b000b8ece89b8ded1 Mon Sep 17 00:00:00 2001 From: Kit Yan Choi Date: Fri, 15 Jul 2016 13:32:58 +0100 Subject: [PATCH 1/3] url -> urlpath where possible and appropriate --- remoteappmanager/application.py | 8 ++++---- remoteappmanager/handlers/base_handler.py | 2 ++ remoteappmanager/handlers/home_handler.py | 12 ++++++------ remoteappmanager/utils.py | 4 +++- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/remoteappmanager/application.py b/remoteappmanager/application.py index b5131dbaf..e151412a4 100644 --- a/remoteappmanager/application.py +++ b/remoteappmanager/application.py @@ -151,11 +151,11 @@ def start(self): def _get_handlers(self): """Returns the registered handlers""" - base_url = self.command_line_config.base_url + base_urlpath = self.command_line_config.base_url return [ - (base_url, HomeHandler), - (base_url.rstrip('/'), - web.RedirectHandler, {"url": base_url}), + (base_urlpath, HomeHandler), + (base_urlpath.rstrip('/'), + web.RedirectHandler, {"url": base_urlpath}), ] def _jinja_init(self, settings): diff --git a/remoteappmanager/handlers/base_handler.py b/remoteappmanager/handlers/base_handler.py index d24cf3975..f141601ac 100644 --- a/remoteappmanager/handlers/base_handler.py +++ b/remoteappmanager/handlers/base_handler.py @@ -32,7 +32,9 @@ def render(self, template_name, **kwargs): """ args = dict( user=self.current_user, + # command_line_config.base_url is often a url path base_url=self.application.command_line_config.base_url, + # hub_prefix is often a url path '/hub' logout_url=urljoin( self.application.command_line_config.hub_prefix, "logout") diff --git a/remoteappmanager/handlers/home_handler.py b/remoteappmanager/handlers/home_handler.py index 9ba81f695..5effbfe50 100644 --- a/remoteappmanager/handlers/home_handler.py +++ b/remoteappmanager/handlers/home_handler.py @@ -113,11 +113,11 @@ def _actionhandler_start(self, options): # The server is up and running. Now contact the proxy and add # the container url to it. - url = yield self.application.reverse_proxy.add_container(container) + urlpath = yield self.application.reverse_proxy.add_container(container) # Redirect the user - self.log.info('Redirecting to {}'.format(url)) - self.redirect(url) + self.log.info('Redirecting to {}'.format(urlpath)) + self.redirect(urlpath) @gen.coroutine def _actionhandler_view(self, options): @@ -133,10 +133,10 @@ def _actionhandler_view(self, options): yield self._wait_for_container_ready(container) # in case the reverse proxy is not already set up - url = yield self.application.reverse_proxy.add_container(container) + urlpath = yield self.application.reverse_proxy.add_container(container) - self.log.info('Redirecting to {}'.format(url)) - self.redirect(url) + self.log.info('Redirecting to {}'.format(urlpath)) + self.redirect(urlpath) @gen.coroutine def _actionhandler_stop(self, options): diff --git a/remoteappmanager/utils.py b/remoteappmanager/utils.py index a6caf8803..0f8ba8a6f 100644 --- a/remoteappmanager/utils.py +++ b/remoteappmanager/utils.py @@ -1,7 +1,9 @@ def url_path_join(*pieces): - """Join components of url into a relative url + """Join components of url into a relative url path Use to prevent double slash when joining subpath. This will leave the initial and final / in place + + Assume pieces do not contain protocol (e.g. http://) """ stripped = [s.strip('/') for s in pieces] result = '/'.join(s for s in stripped if s) From a32bd7b0792329fe809559e9e02627d1affd9c06 Mon Sep 17 00:00:00 2001 From: Kit Yan Choi Date: Fri, 15 Jul 2016 13:47:50 +0100 Subject: [PATCH 2/3] modified command-line option in the spawner --- remoteappmanager/application.py | 6 +++--- remoteappmanager/command_line_config.py | 6 +++--- remoteappmanager/handlers/base_handler.py | 4 +--- remoteappmanager/handlers/home_handler.py | 4 ++-- remoteappmanager/spawner.py | 8 ++++++++ tests/handlers/test_home_handler.py | 2 +- tests/test_command_line_config.py | 2 +- 7 files changed, 19 insertions(+), 13 deletions(-) diff --git a/remoteappmanager/application.py b/remoteappmanager/application.py index e151412a4..89475ba23 100644 --- a/remoteappmanager/application.py +++ b/remoteappmanager/application.py @@ -51,7 +51,7 @@ def __init__(self, settings.update(as_dict(command_line_config)) settings.update(as_dict(file_config)) settings["static_url_prefix"] = ( - self._command_line_config.base_url + "static/") + self._command_line_config.base_urlpath + "static/") self._jinja_init(settings) @@ -98,7 +98,7 @@ def _reverse_proxy_default(self): return ReverseProxy( endpoint_url=self.command_line_config.proxy_api_url, auth_token=auth_token, - base_urlpath=self.command_line_config.base_url + base_urlpath=self.command_line_config.base_urlpath ) @default("hub") @@ -151,7 +151,7 @@ def start(self): def _get_handlers(self): """Returns the registered handlers""" - base_urlpath = self.command_line_config.base_url + base_urlpath = self.command_line_config.base_urlpath return [ (base_urlpath, HomeHandler), (base_urlpath.rstrip('/'), diff --git a/remoteappmanager/command_line_config.py b/remoteappmanager/command_line_config.py index 9f827d39b..ddd1cb679 100644 --- a/remoteappmanager/command_line_config.py +++ b/remoteappmanager/command_line_config.py @@ -22,7 +22,7 @@ class CommandLineConfig(HasTraits): # This is the url path that the user sees and which leads to this server. # typically, it's /user/username - base_url = Unicode(help="The base url where the server resides") + base_urlpath = Unicode(help="The base url where the server resides") # This is the host of the hub. It's always empty (jupyterhub decision) hub_host = Unicode(help="The url of the jupyterhub server") @@ -64,5 +64,5 @@ def parse_config(self): set_traits_from_dict(self, options) - # Normalize the base_url to end with a slash - self.base_url = with_end_slash(self.base_url) + # Normalize the base_urlpath to end with a slash + self.base_urlpath = with_end_slash(self.base_urlpath) diff --git a/remoteappmanager/handlers/base_handler.py b/remoteappmanager/handlers/base_handler.py index f141601ac..46dd44853 100644 --- a/remoteappmanager/handlers/base_handler.py +++ b/remoteappmanager/handlers/base_handler.py @@ -32,9 +32,7 @@ def render(self, template_name, **kwargs): """ args = dict( user=self.current_user, - # command_line_config.base_url is often a url path - base_url=self.application.command_line_config.base_url, - # hub_prefix is often a url path '/hub' + base_url=self.application.command_line_config.base_urlpath, logout_url=urljoin( self.application.command_line_config.hub_prefix, "logout") diff --git a/remoteappmanager/handlers/home_handler.py b/remoteappmanager/handlers/home_handler.py index 5effbfe50..074f107b5 100644 --- a/remoteappmanager/handlers/home_handler.py +++ b/remoteappmanager/handlers/home_handler.py @@ -161,7 +161,7 @@ def _actionhandler_stop(self, options): # We don't have fancy stuff at the moment to change the button, so # we just reload the page. - self.redirect(self.application.command_line_config.base_url) + self.redirect(self.application.command_line_config.base_urlpath) # private @@ -326,7 +326,7 @@ def _wait_for_container_ready(self, container): server_url = "http://{}:{}{}/".format( container.ip, container.port, - url_path_join(self.application.command_line_config.base_url, + url_path_join(self.application.command_line_config.base_urlpath, container.urlpath)) yield _wait_for_http_server_2xx( diff --git a/remoteappmanager/spawner.py b/remoteappmanager/spawner.py index 32ea5c691..a226fa96c 100644 --- a/remoteappmanager/spawner.py +++ b/remoteappmanager/spawner.py @@ -33,6 +33,10 @@ def __init__(self, **kwargs): def get_args(self): args = super().get_args() + + for iarg, arg in enumerate(args): + args[iarg] = arg.replace('--base-url=', '--base-urlpath=') + args.append("--proxy-api-url={}".format(self.proxy.api_server.url)) args.append("--config-file={}".format(self.config_file_path)) return args @@ -103,6 +107,10 @@ def clear_state(self): def get_args(self): args = super().get_args() + + for iarg, arg in enumerate(args): + args[iarg] = arg.replace('--base-url=', '--base-urlpath=') + args.append("--proxy-api-url={}".format(self.proxy.api_server.url)) args.append("--config-file={}".format(self.config_file_path)) return args diff --git a/tests/handlers/test_home_handler.py b/tests/handlers/test_home_handler.py index c532f9e04..8f9b7dced 100644 --- a/tests/handlers/test_home_handler.py +++ b/tests/handlers/test_home_handler.py @@ -59,7 +59,7 @@ def get_app(self): 'pending': None, 'name': command_line_config.user, 'admin': False, - 'server': command_line_config.base_url}) + 'server': command_line_config.base_urlpath}) app.db = mock.Mock(spec=ABCAccounting) app.container_manager = mock.Mock(spec=ContainerManager) app.container_manager.start_container = mock_coro_factory(Container()) diff --git a/tests/test_command_line_config.py b/tests/test_command_line_config.py index c2347359c..88fbe43ee 100644 --- a/tests/test_command_line_config.py +++ b/tests/test_command_line_config.py @@ -18,7 +18,7 @@ def setUp(self): def test_initialization(self): for key, value in arguments.items(): - if key == "base-url": + if key == "base-urlpath": value = with_end_slash(value) self.assertEqual(getattr(self.config, key.replace("-", "_")), From 12d4cbad7eece711b83198711b2fc3f884c5f643 Mon Sep 17 00:00:00 2001 From: Kit Yan Choi Date: Fri, 15 Jul 2016 13:56:05 +0100 Subject: [PATCH 3/3] fix tests --- tests/spawner/test_spawner.py | 1 + tests/utils.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/spawner/test_spawner.py b/tests/spawner/test_spawner.py index ca7de5b4e..e080456b8 100644 --- a/tests/spawner/test_spawner.py +++ b/tests/spawner/test_spawner.py @@ -83,6 +83,7 @@ def test_args(self): args = self.spawner.get_args() self.assertIn("--proxy-api-url=http://127.0.0.1:12345/foo/bar/", args) self.assertIn("--config-file={}".format(path), args) + self.assertIn("--base-urlpath=/", args) def test_cmd(self): self.assertEqual(self.spawner.cmd, ['remoteappmanager']) diff --git a/tests/utils.py b/tests/utils.py index 201c7d741..bbb371993 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -251,7 +251,7 @@ def containers_dict(): "user": "username", "port": 57022, "cookie-name": "jupyter-hub-token-username", - "base-url": "/user/username/", + "base-urlpath": "/user/username/", "hub-host": "", "hub-prefix": "/hub/", "hub-api-url": "http://172.17.5.167:8081/hub/api",