Skip to content

Commit

Permalink
Create binderhub build handler
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Apr 9, 2024
1 parent 9451464 commit f70878f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/environments/NewEnvironmentDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ function _NewEnvironmentDialog(props: INewEnvironmentDialogProps) {
[setFormValues]
);
const validated = useMemo(() => {
return Boolean(formValues.repo) && Boolean(formValues.ref);
}, [formValues]);
return Boolean(formValues.repo);
}, [formValues.repo]);

const [selectedProfile, setSelectedProfile] = useState<number>(0);
const [selectedProvider, setSelectedProvider] = useState<number>(0);
Expand Down Expand Up @@ -212,6 +212,7 @@ function _NewEnvironmentDialog(props: INewEnvironmentDialogProps) {
.replace('https://', '')
.replace(/\//g, '-')
.replace(/\./g, '-');
data.ref = data.ref && data.ref.length > 0 ? data.ref : 'HEAD';
data.cpu = data.cpu ?? '2';
data.memory = data.memory ?? '2';
data.username = data.username ?? '';
Expand Down Expand Up @@ -272,6 +273,7 @@ function _NewEnvironmentDialog(props: INewEnvironmentDialogProps) {
label="Reference (git commit)"
type="text"
placeholder="HEAD"
required={false}
onChange={e => updateFormValue('ref', e.target.value)}
value={formValues.ref ?? ''}
/>
Expand Down
36 changes: 27 additions & 9 deletions tljh_repo2docker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from traitlets import Dict, Int, List, Unicode, default, validate
from traitlets.config.application import Application

from tljh_repo2docker.binderhub_builder import BinderHubBuildHandler

from .builder import BuildHandler
from .dbutil import async_session_context_factory, sync_to_async_url, upgrade_if_needed
from .environments import EnvironmentsHandler
Expand Down Expand Up @@ -142,9 +144,9 @@ def _default_log_level(self):

repo_providers = List(
default_value=[
{"label": "Git", "value": "git"},
{"label": "GitHub", "value": "gh"},
{"label": "Gitlab", "value": "gl"},
{"label": "Git", "value": "git"},
],
trait=Dict,
help="""
Expand Down Expand Up @@ -228,16 +230,32 @@ def init_handlers(self) -> tp.List:
url_path_join(self.service_prefix, r"environments"),
EnvironmentsHandler,
),
(url_path_join(self.service_prefix, r"api/environments"), BuildHandler),
(
url_path_join(
self.service_prefix, r"api/environments/([^/]+)/logs"
),
LogsHandler,
),
]
)

if self.binderhub_url:
handlers.extend(
[
(
url_path_join(self.service_prefix, r"api/environments"),
BinderHubBuildHandler,
)
]
)
else:
handlers.extend(
[
(
url_path_join(
self.service_prefix, r"api/environments/([^/]+)/logs"
),
LogsHandler,
),
(
url_path_join(self.service_prefix, r"api/environments"),
BuildHandler,
),
]
)
return handlers

def init_db(self):
Expand Down
40 changes: 40 additions & 0 deletions tljh_repo2docker/binderhub_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import json

import requests
from tornado import web
from .base import BaseHandler, require_admin_role
from urllib.parse import quote
from jupyterhub.utils import url_path_join


class BinderHubBuildHandler(BaseHandler):
"""
Handle requests to build user environments using BinderHub service
"""

@web.authenticated
@require_admin_role
async def post(self):
data = self.get_json_body()
repo = data["repo"]
ref = data["ref"]
name = data["name"].lower()
memory = data["memory"]
cpu = data["cpu"]
provider = data["provider"]
print(f"Building binder for {provider}@{repo}@{ref}")
binder_url = self.settings.get("binderhub_url")
quoted_repo = quote(repo, safe="")
url = url_path_join(binder_url, "build", provider, quoted_repo, ref)

params = {"build_only": "true"}

async with self.client.stream("GET", url, params=params, timeout=None) as r:
async for line in r.aiter_lines():
print("proviDDDDDDDDDDDDDDDDder", line)
# if line.startswith("data:"):
# print(line.split(":", 1)[1])

self.set_status(200)
self.set_header("content-type", "application/json")
self.finish(json.dumps({"status": "ok"}))
2 changes: 2 additions & 0 deletions ui-tests/binderhub_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@


c.BinderHub.debug = True
c.BinderHub.auth_enabled = True
c.BinderHub.enable_api_only_mode = True
c.BinderHub.use_registry = False
c.BinderHub.builder_required = False

Expand Down
12 changes: 10 additions & 2 deletions ui-tests/jupyterhub_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

HERE = Path(__file__).parent
tljh_config = load_config()

tljh_config["services"]["cull"]["enabled"] = False
apply_config(tljh_config, c)

tljh_custom_jupyterhub_config(c)
Expand Down Expand Up @@ -48,6 +48,8 @@
],
"url": "http://localhost:8585",
"environment": binderhub_environment,
"oauth_client_id": "service-binderhub",
"oauth_no_confirm": True,
},
{
"name": "tljh_repo2docker",
Expand Down Expand Up @@ -81,7 +83,12 @@
{
"description": "Role for tljh_repo2docker service",
"name": "tljh-repo2docker-service",
"scopes": ["read:users", "read:roles:users", "admin:servers"],
"scopes": [
"read:users",
"read:roles:users",
"admin:servers",
"access:services!service=binder",
],
"services": ["tljh_repo2docker"],
},
{
Expand All @@ -95,6 +102,7 @@
"self",
# access to the env page
"access:services!service=tljh_repo2docker",
"access:services!service=binder",
],
},
]

0 comments on commit f70878f

Please sign in to comment.