Skip to content

Commit

Permalink
api info file written
Browse files Browse the repository at this point in the history
  • Loading branch information
dwsutherland committed Nov 22, 2022
1 parent c204b88 commit 6765e1e
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cylc/uiserver/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
"""

from concurrent.futures import ProcessPoolExecutor
from contextlib import suppress
import getpass
import json
import os
from pathlib import Path, PurePath
import sys
from typing import List
Expand Down Expand Up @@ -105,6 +108,9 @@
from cylc.uiserver.workflows_mgr import WorkflowsManager


API_INFO_FILE = f'{USER_CONF_ROOT / "api_info.json"}'


class PathType(TraitType):
"""A pathlib traitlet type which allows string and undefined values."""

Expand Down Expand Up @@ -400,6 +406,10 @@ def initialize_settings(self):
for key, value in self.config['CylcUIServer'].items()
)
)
# Make API token available to server's user.
# Do it here to avoid overwriting via server start attempt,
# when server already running.
self.write_api_info()
# start the async scan task running (do this on server start not init)
ioloop.IOLoop.current().add_callback(
self.workflows_mgr.run
Expand Down Expand Up @@ -505,6 +515,22 @@ def set_auth(self):
def initialize_templates(self):
"""Change the jinja templating environment."""

def write_api_info(self):
api_info = self.serverapp.server_info()
api_token = os.environ.get("JUPYTERHUB_API_TOKEN")
api_url = os.environ.get("JUPYTERHUB_SERVICE_URL")
# Could be none, if server not launched by hub.
if api_token:
api_info['token'] = api_token
if api_url:
api_info['url'] = api_url
Path(API_INFO_FILE).parent.mkdir(parents=True, exist_ok=True)
with suppress(FileNotFoundError):
os.unlink(API_INFO_FILE)
fd = os.open(API_INFO_FILE, os.O_CREAT | os.O_WRONLY, mode=0o600)
os.write(fd, json.dumps(api_info).encode("utf-8"))
os.close(fd)

@classmethod
def launch_instance(cls, argv=None, **kwargs):
if argv is None:
Expand All @@ -514,6 +540,9 @@ def launch_instance(cls, argv=None, **kwargs):
super().launch_instance(argv=argv, **kwargs)

async def stop_extension(self):
# Remove API token if hub spawned
with suppress(FileNotFoundError):
os.unlink(API_INFO_FILE)
# stop the async scan task
await self.workflows_mgr.stop()
for sub in self.data_store_mgr.w_subs.values():
Expand Down

0 comments on commit 6765e1e

Please sign in to comment.