Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam-D-Lewis committed Dec 17, 2024
1 parent aee7385 commit db648c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
3 changes: 3 additions & 0 deletions jhub_apps/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class JAppsConfig(SingletonConfigurable):
startup_apps = List(
trait=Any, # TODO: Change this, use Instance() maybe or define a new type - https://traitlets.readthedocs.io/en/stable/defining_traits.html

description="only add a server if it is not already created or edit an existing one to match the config, won't delete any servers",

# This class should be a ServerCreation class + user + thumbnail
default_value=[{
'display_name': 'Adam\'s App',
Expand All @@ -84,6 +86,7 @@ class JAppsConfig(SingletonConfigurable):
{'users': ['alice', 'john', 'admin'],
'groups': ['alpha', 'beta']},
'servername': 'my-server',
'username': 'alice',
}],
help="List of apps to start on JHub Apps Launcher startup",
).tag(config=True)
13 changes: 9 additions & 4 deletions jhub_apps/hub_client/hub_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,22 @@ def get_user(self, user=None):
return user

@requires_user_token
def get_server(self, username, servername):
def get_server(self, username, servername=None):
users = self.get_users()
filter_given_user = [user for user in users if user["name"] == username]
if not filter_given_user:
logger.info(f"No user with username: {username} found.")
return
else:
given_user = filter_given_user[0]
for name, server in given_user["servers"].items():
if name == servername:
return server

if servername:
for name, server in given_user["servers"].items():
if name == servername:
return server
else:
# return all user servers
return given_user["servers"]

def normalize_server_name(self, servername):
# Convert text to lowercase
Expand Down
40 changes: 30 additions & 10 deletions jhub_apps/service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
from pathlib import Path
import pprint
from itertools import groupby
from operator import itemgetter

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
Expand All @@ -16,6 +18,9 @@
from jhub_apps.service.routes import router
from jhub_apps.service.utils import get_jupyterhub_config
from jhub_apps.version import get_version
import structlog

logger = structlog.get_logger(__name__)

### When managed by Jupyterhub, the actual endpoints
### will be served out prefixed by /services/:name.
Expand All @@ -28,22 +33,37 @@
async def lifespan(app: FastAPI):
config = get_jupyterhub_config()
user_options_list = config['JAppsConfig']['startup_apps']
instantiate_startup_apps(user_options_list, username='alice')
# group user options by username

grouped_user_options_list = groupby(user_options_list, itemgetter('username'))
for username, user_options_list in grouped_user_options_list:
instantiate_startup_apps(user_options_list, username=username)

yield

def instantiate_startup_apps(server_creation_list: list[dict[str, Any]], username: str):
# instantiate custom apps
hub_client = HubClient(username=username)

existing_servers = hub_client.get_server(username=username)

for server_creation_dict in server_creation_list:
print(f"Instantiating app with user_options: {pprint.pformat(server_creation_dict)}") # TODO: Remove
user_options = UserOptions(**server_creation_dict)
hub_client = HubClient(username=username)
hub_client.create_server(
username=username,
servername=server_creation_dict['servername'],
# servername=None, # throws an error
user_options=user_options,
)
servername = server_creation_dict['servername']
if server_creation_dict['servername'] in existing_servers:
# update the server
logger.info(f"{'='*50}Updating server: {server_creation_dict['servername']}")
hub_client.edit_server(username, servername, user_options)
else:
# create the server
logger.info(f"{'='*50}Instantiating app with user_options: {pprint.pformat(server_creation_dict)}") # TODO: Remove
# user_options = UserOptions(**server_creation_dict)

hub_client.create_server(
username=username,
servername=servername,
user_options=user_options,
)
logger.info('Done instantiating apps')

app = FastAPI(
title="JApps Service",
Expand Down

0 comments on commit db648c6

Please sign in to comment.