Skip to content

Commit

Permalink
Merge pull request #45 from astiunov/spawner-form
Browse files Browse the repository at this point in the history
User-friendly everware, lot of stuff
  • Loading branch information
anaderi committed Mar 4, 2016
2 parents 79721a5 + 4db571a commit 4c11988
Show file tree
Hide file tree
Showing 23 changed files with 665 additions and 101 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ node_modules
.DS_Store
build
dist
env.sh
# ignore config file at the top-level of the repo
# but not sub-dirs
/jupyterhub_config.py
jupyterhub_cookie_secret
jupyterhub.sqlite
jupyterhub.log
share/static/components
share/static/css/style.min.css
share/static/css/style.min.css.map
*.egg-info
MANIFEST
.coverage
htmlcov

whitelist.txt
*.swp
1 change: 1 addition & 0 deletions clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -f jupyterhub.sqlite
File renamed without changes.
3 changes: 3 additions & 0 deletions everware/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

from .spawner import *
from .authenticator import *
from .user_spawn_handler import *
from .user_wait_handler import *
from .home_handler import *
33 changes: 28 additions & 5 deletions everware/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import json
import os
import urllib
import signal

from tornado.auth import OAuth2Mixin
from tornado.escape import url_escape
Expand All @@ -21,6 +22,28 @@
from jupyterhub.utils import url_path_join

from traitlets import Unicode, Set
from traitlets.config import LoggingConfigurable


class DefaultWhitelistHandler(LoggingConfigurable):

def __init__(self, filename, config, authenticator):
super().__init__()
self.filename = filename
self.authenticator = authenticator
authenticator.whitelist = set(x.rstrip() for x in open(filename))
config.Authenticator.whitelist = authenticator.whitelist
signal.signal(signal.SIGHUP, self.reload_whitelist)


def reload_whitelist(self, signal, frame):
self.authenticator.whitelist = set(
x.rstrip() for x in open(self.filename)
)
self.log.info(
'Whitelist reloaded:\n%s',
'\n'.join(self.authenticator.whitelist)
)


class GitHubMixin(OAuth2Mixin):
Expand All @@ -39,7 +62,7 @@ class WelcomeHandler(BaseHandler):
def _render(self, login_error=None, username=None):
return self.render_template('login.html',
next=url_escape(self.get_argument('next', default='')),
repo_url=url_escape(self.get_argument('repo_url', default='')),
repourl=url_escape(self.get_argument('repourl', default='')),
username=username,
login_error=login_error,
)
Expand Down Expand Up @@ -78,11 +101,11 @@ def get(self):
redirect_uri = self.authenticator.oauth_callback_url or guess_uri
self.log.info('oauth redirect: %r', redirect_uri)

repo_url = self.get_argument('repo_url', '')
repourl = self.get_argument('repourl', '')

state = {'unique': 42}
if repo_url:
state['repo_url'] = repo_url
if repourl:
state['repourl'] = repourl

self.authorize_redirect(
redirect_uri=redirect_uri,
Expand Down Expand Up @@ -117,7 +140,7 @@ def get(self):
if username:
user = self.user_from_username(username)
self.set_login_cookie(user)
if 'repo_url' in state:
if 'repourl' in state:
self.log.debug("Redirect with %s", state)
self.redirect(self.hub.server.base_url +'/home?'+urllib.parse.urlencode(state))
else:
Expand Down
29 changes: 29 additions & 0 deletions everware/home_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from tornado import web, gen

from jupyterhub.handlers.base import BaseHandler
from IPython.html.utils import url_path_join
from tornado.httputil import url_concat


class HomeHandler(BaseHandler):
"""Render the user's home page."""

@web.authenticated
def get(self):
user = self.get_current_user()
repourl = self.get_argument('repourl', '')
if repourl:
self.log.info('Got %s in home' % repourl)
self.redirect(url_concat(
url_path_join(self.hub.server.base_url, 'spawn'), {
'repourl': repourl
}
))
return
html = self.render_template('home.html',
user=user
)
self.finish(html)



67 changes: 67 additions & 0 deletions everware/image_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from tornado import gen
from tornado.locks import Event
from tornado.ioloop import IOLoop
from itertools import count


def singleton(cls):
instances = {}

def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance


@singleton
class ImageHandler():

def __init__(self):
self._images = {}

def get_waiter(self, image_name):
if image_name not in self._images:
self._images[image_name] = ImageMutex()
return self._images[image_name]


class ImageMutex():

def __init__(self):
self._mutex = Event()
self._blocked = count()
self._building_log = []
self._exception = None

@gen.coroutine
def block(self):
value = self._blocked.__next__() # single bytecode operation
if value:
yield self._mutex.wait()
return value

def __enter__(self):
if self._exception is not None:
raise self._exception
return self

def __exit__(self, exc_type, exc_value, traceback):
self._building_log = []
if isinstance(exc_value, Exception):
self._exception = exc_type(exc_value)
self._mutex.set()

def add_to_log(self, message, level=1):
self._building_log.append({
'text': message,
'level': level
})

@property
def building_log(self):
return self._building_log

@property
def last_exception(self):
return self._exception

0 comments on commit 4c11988

Please sign in to comment.