From fc55025199aec61f12f86426ce92c52901d5ddf8 Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri <44365948+mahendrapaipuri@users.noreply.github.com> Date: Sat, 13 May 2023 17:34:50 +0200 Subject: [PATCH 1/4] Pass port arg via env var instead of CLI jupyterhub-singleuser is an extension in JH 4 and it does not expect any CLI args. We need to pass the free port that we found in batchspawner singleuser script to jupyterhub-singleuser via `JUPYTERHUB_SERVICE_URL` env variable. --- batchspawner/singleuser.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/batchspawner/singleuser.py b/batchspawner/singleuser.py index a56d67db..cd6e7eae 100644 --- a/batchspawner/singleuser.py +++ b/batchspawner/singleuser.py @@ -1,6 +1,7 @@ import os import sys +from urllib.parse import urlparse from runpy import run_path from shutil import which @@ -34,8 +35,15 @@ def main(argv=None): **kwargs, ) + # 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}" + cmd_path = which(sys.argv[1]) - sys.argv = sys.argv[1:] + ["--port={}".format(port)] + sys.argv = sys.argv[1:] run_path(cmd_path, run_name="__main__") From c30b593b6b1be48c30756f204c424a55fb84f65c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 13 May 2023 15:42:25 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- batchspawner/singleuser.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/batchspawner/singleuser.py b/batchspawner/singleuser.py index cd6e7eae..c2d12d40 100644 --- a/batchspawner/singleuser.py +++ b/batchspawner/singleuser.py @@ -40,7 +40,9 @@ def main(argv=None): 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}" + os.environ[ + "JUPYTERHUB_SERVICE_URL" + ] = f"{url.scheme}://{url.hostname}:{port}{url.path}" cmd_path = which(sys.argv[1]) sys.argv = sys.argv[1:] From 3f2ad162b09e9b09fa7edee3692a4980f376ca37 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Mar 2024 10:14:34 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- batchspawner/singleuser.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/batchspawner/singleuser.py b/batchspawner/singleuser.py index f24f4688..4c6d4836 100644 --- a/batchspawner/singleuser.py +++ b/batchspawner/singleuser.py @@ -1,6 +1,5 @@ import os import sys - from runpy import run_path from shutil import which from urllib.parse import urlparse @@ -39,9 +38,9 @@ def main(argv=None): 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}" + os.environ["JUPYTERHUB_SERVICE_URL"] = ( + f"{url.scheme}://{url.hostname}:{port}{url.path}" + ) cmd_path = which(sys.argv[1]) sys.argv = sys.argv[1:] From 050ae92d4af11795cd28016daa806cf84adbd5ae Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 8 Mar 2024 12:14:47 +0100 Subject: [PATCH 4/4] 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 4c6d4836..3f1b1ecf 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:]