From 2c5c0a5e7132a57d69594769a8e1245cfe77121c Mon Sep 17 00:00:00 2001 From: Rollin Thomas Date: Mon, 11 Nov 2019 18:42:13 -0800 Subject: [PATCH 1/3] Support named servers --- batchspawner/api.py | 9 +++++++-- batchspawner/singleuser.py | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/batchspawner/api.py b/batchspawner/api.py index bbaacbcd..387a6da0 100644 --- a/batchspawner/api.py +++ b/batchspawner/api.py @@ -13,9 +13,14 @@ def post(self): # Previous jupyterhub, 0.9.4 and before. user = self.get_current_user() data = self.get_json_body() + if self.allow_named_servers: + server_name = data.pop("server_name", "") + spawner = user.spawners[server_name] + else: + spawner = user.spawner for key, value in data.items(): - if hasattr(user.spawner, key): - setattr(user.spawner, key, value) + if hasattr(spawner, key): + setattr(spawner, key, value) self.finish(json.dumps({"message": "BatchSpawner data configured"})) self.set_status(201) diff --git a/batchspawner/singleuser.py b/batchspawner/singleuser.py index afb171a2..2f0560a9 100644 --- a/batchspawner/singleuser.py +++ b/batchspawner/singleuser.py @@ -8,6 +8,7 @@ from jupyterhub.services.auth import HubAuth def main(argv=None): + server_name = os.environ.get('JUPYTERHUB_SERVER_NAME', '') port = random_port() hub_auth = HubAuth() hub_auth.client_ca = os.environ.get('JUPYTERHUB_SSL_CLIENT_CA', '') @@ -15,11 +16,11 @@ def main(argv=None): hub_auth.keyfile = os.environ.get('JUPYTERHUB_SSL_KEYFILE', '') hub_auth._api_request(method='POST', url=url_path_join(hub_auth.api_url, 'batchspawner'), - json={'port' : port}) + json={'server_name': server_name, 'port' : port}) cmd_path = which(sys.argv[1]) sys.argv = sys.argv[1:] + ['--port={}'.format(port)] run_path(cmd_path, run_name="__main__") if __name__ == "__main__": - main() \ No newline at end of file + main() From fbaa5fd1573fe48653699400c4c1a2e8a5e23517 Mon Sep 17 00:00:00 2001 From: Rollin Thomas Date: Wed, 13 Nov 2019 11:37:07 -0800 Subject: [PATCH 2/3] Match port info to spawner based on token --- batchspawner/api.py | 12 ++++++------ batchspawner/singleuser.py | 3 +-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/batchspawner/api.py b/batchspawner/api.py index 387a6da0..fee460fc 100644 --- a/batchspawner/api.py +++ b/batchspawner/api.py @@ -12,12 +12,12 @@ def post(self): else: # Previous jupyterhub, 0.9.4 and before. user = self.get_current_user() - data = self.get_json_body() - if self.allow_named_servers: - server_name = data.pop("server_name", "") - spawner = user.spawners[server_name] - else: - spawner = user.spawner + token = self.get_auth_token() + spawner = None + for s in user.spawners.values(): + if s.api_token == token: + spawner = s + break for key, value in data.items(): if hasattr(spawner, key): setattr(spawner, key, value) diff --git a/batchspawner/singleuser.py b/batchspawner/singleuser.py index 2f0560a9..e3ffab22 100644 --- a/batchspawner/singleuser.py +++ b/batchspawner/singleuser.py @@ -8,7 +8,6 @@ from jupyterhub.services.auth import HubAuth def main(argv=None): - server_name = os.environ.get('JUPYTERHUB_SERVER_NAME', '') port = random_port() hub_auth = HubAuth() hub_auth.client_ca = os.environ.get('JUPYTERHUB_SSL_CLIENT_CA', '') @@ -16,7 +15,7 @@ def main(argv=None): hub_auth.keyfile = os.environ.get('JUPYTERHUB_SSL_KEYFILE', '') hub_auth._api_request(method='POST', url=url_path_join(hub_auth.api_url, 'batchspawner'), - json={'server_name': server_name, 'port' : port}) + json={'port' : port}) cmd_path = which(sys.argv[1]) sys.argv = sys.argv[1:] + ['--port={}'.format(port)] From 9fb20f6d53623530560c48565c278ee951a41a3c Mon Sep 17 00:00:00 2001 From: Rollin Thomas Date: Wed, 13 Nov 2019 11:50:19 -0800 Subject: [PATCH 3/3] Whoops --- batchspawner/api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/batchspawner/api.py b/batchspawner/api.py index fee460fc..514dd0a5 100644 --- a/batchspawner/api.py +++ b/batchspawner/api.py @@ -18,6 +18,7 @@ def post(self): if s.api_token == token: spawner = s break + data = self.get_json_body() for key, value in data.items(): if hasattr(spawner, key): setattr(spawner, key, value)