From 050ae92d4af11795cd28016daa806cf84adbd5ae Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 8 Mar 2024 12:14:47 +0100 Subject: [PATCH] update handling of $JUPYTERHUB_SERVICE_URL when defined JupyterHub 2.0 uses $JUPYTERHUB_SERVICE_URL instead of `--port` CLI argument --- batchspawner/singleuser.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/batchspawner/singleuser.py b/batchspawner/singleuser.py index 4c6d483..3f1b1ec 100644 --- a/batchspawner/singleuser.py +++ b/batchspawner/singleuser.py @@ -2,7 +2,7 @@ import sys from runpy import run_path from shutil import which -from urllib.parse import urlparse +from urllib.parse import urlparse, urlunparse import requests from jupyterhub.services.auth import HubAuth @@ -35,12 +35,15 @@ def main(argv=None): # Read the env var JUPYTERHUB_SERVICE_URL and replace port in the URL # with free port that we found here - url = urlparse(os.environ.get("JUPYTERHUB_SERVICE_URL", "")) - # Updated URL. We are effectively passing the port arg via env var - if url.hostname: - os.environ["JUPYTERHUB_SERVICE_URL"] = ( - f"{url.scheme}://{url.hostname}:{port}{url.path}" - ) + # JUPYTERHUB_SERVICE_URL is added in JupyterHub 2.0 + service_url_env = os.environ.get("JUPYTERHUB_SERVICE_URL", "") + if service_url_env: + url = urlparse(os.environ["JUPYTERHUB_SERVICE_URL"]) + url = url._replace(netloc=f"{url.hostname}:{port}") + os.environ["JUPYTERHUB_SERVICE_URL"] = urlunparse(url) + else: + # JupyterHub < 2.0 specifies port on the command-line + sys.argv.append(f"--port={port}") cmd_path = which(sys.argv[1]) sys.argv = sys.argv[1:]