Skip to content

Commit 050ae92

Browse files
committed
update handling of $JUPYTERHUB_SERVICE_URL when defined
JupyterHub 2.0 uses $JUPYTERHUB_SERVICE_URL instead of `--port` CLI argument
1 parent 3f2ad16 commit 050ae92

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

batchspawner/singleuser.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
from runpy import run_path
44
from shutil import which
5-
from urllib.parse import urlparse
5+
from urllib.parse import urlparse, urlunparse
66

77
import requests
88
from jupyterhub.services.auth import HubAuth
@@ -35,12 +35,15 @@ def main(argv=None):
3535

3636
# Read the env var JUPYTERHUB_SERVICE_URL and replace port in the URL
3737
# with free port that we found here
38-
url = urlparse(os.environ.get("JUPYTERHUB_SERVICE_URL", ""))
39-
# Updated URL. We are effectively passing the port arg via env var
40-
if url.hostname:
41-
os.environ["JUPYTERHUB_SERVICE_URL"] = (
42-
f"{url.scheme}://{url.hostname}:{port}{url.path}"
43-
)
38+
# JUPYTERHUB_SERVICE_URL is added in JupyterHub 2.0
39+
service_url_env = os.environ.get("JUPYTERHUB_SERVICE_URL", "")
40+
if service_url_env:
41+
url = urlparse(os.environ["JUPYTERHUB_SERVICE_URL"])
42+
url = url._replace(netloc=f"{url.hostname}:{port}")
43+
os.environ["JUPYTERHUB_SERVICE_URL"] = urlunparse(url)
44+
else:
45+
# JupyterHub < 2.0 specifies port on the command-line
46+
sys.argv.append(f"--port={port}")
4447

4548
cmd_path = which(sys.argv[1])
4649
sys.argv = sys.argv[1:]

0 commit comments

Comments
 (0)