Skip to content

Commit 2b4102a

Browse files
authored
Merge pull request #267 from mahendrapaipuri/upgrade_jh_4
Upgrade singleuser.py to JupyterHub 4
2 parents d75a5db + 050ae92 commit 2b4102a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

batchspawner/singleuser.py

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

67
import requests
78
from jupyterhub.services.auth import HubAuth
@@ -32,8 +33,20 @@ def main(argv=None):
3233
**kwargs,
3334
)
3435

36+
# Read the env var JUPYTERHUB_SERVICE_URL and replace port in the URL
37+
# with free port that we found here
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}")
47+
3548
cmd_path = which(sys.argv[1])
36-
sys.argv = sys.argv[1:] + [f"--port={port}"]
49+
sys.argv = sys.argv[1:]
3750
run_path(cmd_path, run_name="__main__")
3851

3952

0 commit comments

Comments
 (0)