make user.running a method

accept server names
This commit is contained in:
Min RK
2017-07-18 14:48:29 +02:00
parent d6565076f5
commit 4b5aad41b1
9 changed files with 28 additions and 30 deletions

View File

@@ -93,7 +93,7 @@ class APIHandler(BaseHandler):
'name': user.name, 'name': user.name,
'admin': user.admin, 'admin': user.admin,
'groups': [ g.name for g in user.groups ], 'groups': [ g.name for g in user.groups ],
'server': user.url if user.running else None, 'server': user.url if user.running('') else None,
'pending': None, 'pending': None,
'last_activity': user.last_activity.isoformat(), 'last_activity': user.last_activity.isoformat(),
} }
@@ -105,7 +105,7 @@ class APIHandler(BaseHandler):
# TODO: named servers # TODO: named servers
servers = model['servers'] = {} servers = model['servers'] = {}
for name, spawner in user.spawners.items(): for name, spawner in user.spawners.items():
if False and user.running(name): if user.running(name):
servers[name] = s = {'name': name} servers[name] = s = {'name': name}
if spawner._spawn_pending: if spawner._spawn_pending:
s['pending'] = 'spawn' s['pending'] = 'spawn'

View File

@@ -148,7 +148,7 @@ class UserAPIHandler(APIHandler):
raise web.HTTPError(400, "Cannot delete yourself!") raise web.HTTPError(400, "Cannot delete yourself!")
if user.spawner._stop_pending: if user.spawner._stop_pending:
raise web.HTTPError(400, "%s's server is in the process of stopping, please wait." % name) raise web.HTTPError(400, "%s's server is in the process of stopping, please wait." % name)
if user.running: if user.running(''):
yield self.stop_single_user(user) yield self.stop_single_user(user)
if user.spawner._stop_pending: if user.spawner._stop_pending:
raise web.HTTPError(400, "%s's server is in the process of stopping, please wait." % name) raise web.HTTPError(400, "%s's server is in the process of stopping, please wait." % name)
@@ -185,7 +185,7 @@ class UserServerAPIHandler(APIHandler):
@admin_or_self @admin_or_self
def post(self, name): def post(self, name):
user = self.find_user(name) user = self.find_user(name)
if user.running: if user.running(''):
# include notify, so that a server that died is noticed immediately # include notify, so that a server that died is noticed immediately
state = yield user.spawner.poll_and_notify() state = yield user.spawner.poll_and_notify()
if state is None: if state is None:
@@ -203,7 +203,7 @@ class UserServerAPIHandler(APIHandler):
if user.spawner._stop_pending: if user.spawner._stop_pending:
self.set_status(202) self.set_status(202)
return return
if not user.running: if not user.running(''):
raise web.HTTPError(400, "%s's server is not running" % name) raise web.HTTPError(400, "%s's server is not running" % name)
# include notify, so that a server that died is noticed immediately # include notify, so that a server that died is noticed immediately
status = yield user.spawner.poll_and_notify() status = yield user.spawner.poll_and_notify()
@@ -240,8 +240,8 @@ class UserNamedServerAPIHandler(APIHandler):
if spawner._stop_pending: if spawner._stop_pending:
self.set_status(202) self.set_status(202)
return return
#if not user.running: if not user.running(name):
# raise web.HTTPError(400, "%s's server is not running" % name) raise web.HTTPError(400, "%s's server is not running" % name)
# include notify, so that a server that died is noticed immediately # include notify, so that a server that died is noticed immediately
status = yield spawner.poll_and_notify() status = yield spawner.poll_and_notify()
if status is not None: if status is not None:
@@ -268,8 +268,6 @@ class UserAdminAccessAPIHandler(APIHandler):
user = self.find_user(name) user = self.find_user(name)
if user is None: if user is None:
raise web.HTTPError(404) raise web.HTTPError(404)
if not user.running:
raise web.HTTPError(400, "%s's server is not running" % name)
default_handlers = [ default_handlers = [

View File

@@ -54,7 +54,7 @@ class LoginHandler(BaseHandler):
user = self.get_current_user() user = self.get_current_user()
if user: if user:
if not next_url: if not next_url:
if user.running: if user.running(''):
next_url = user.url next_url = user.url
else: else:
next_url = self.hub.base_url next_url = self.hub.base_url

View File

@@ -45,7 +45,7 @@ class RootHandler(BaseHandler):
return return
user = self.get_current_user() user = self.get_current_user()
if user: if user:
if user.running: if user.running(''):
url = user.url url = user.url
self.log.debug("User is running: %s", url) self.log.debug("User is running: %s", url)
self.set_login_cookie(user) # set cookie self.set_login_cookie(user) # set cookie
@@ -64,7 +64,7 @@ class HomeHandler(BaseHandler):
@gen.coroutine @gen.coroutine
def get(self): def get(self):
user = self.get_current_user() user = self.get_current_user()
if user.running: if user.running(''):
# trigger poll_and_notify event in case of a server that died # trigger poll_and_notify event in case of a server that died
yield user.spawner.poll_and_notify() yield user.spawner.poll_and_notify()
html = self.render_template('home.html', html = self.render_template('home.html',
@@ -94,7 +94,7 @@ class SpawnHandler(BaseHandler):
def get(self): def get(self):
"""GET renders form for spawning with user-specified options""" """GET renders form for spawning with user-specified options"""
user = self.get_current_user() user = self.get_current_user()
if user.running: if not self.allow_named_servers and user.running(''):
url = user.url url = user.url
self.log.debug("User is running: %s", url) self.log.debug("User is running: %s", url)
self.redirect(url) self.redirect(url)
@@ -110,7 +110,7 @@ class SpawnHandler(BaseHandler):
def post(self): def post(self):
"""POST spawns with user-specified options""" """POST spawns with user-specified options"""
user = self.get_current_user() user = self.get_current_user()
if user.running: if not self.allow_named_servers and user.running(''):
url = user.url url = user.url
self.log.warning("User is already running: %s", url) self.log.warning("User is already running: %s", url)
self.redirect(url) self.redirect(url)

View File

@@ -31,7 +31,6 @@ def db():
user = orm.User( user = orm.User(
name=getuser(), name=getuser(),
) )
user.servers.append(orm.Server())
_db.add(user) _db.add(user)
_db.commit() _db.commit()
return _db return _db

View File

@@ -161,7 +161,7 @@ def test_spawn_fails(db, io_loop):
with pytest.raises(RuntimeError) as exc: with pytest.raises(RuntimeError) as exc:
io_loop.run_sync(user.spawn) io_loop.run_sync(user.spawn)
assert user.server is None assert user.server is None
assert not user.running assert not user.running('')
def test_groups(db): def test_groups(db):

View File

@@ -18,7 +18,7 @@ def test_singleuser_auth(app, io_loop):
# login, start the server # login, start the server
cookies = app.login_user('nandy') cookies = app.login_user('nandy')
user = app.users['nandy'] user = app.users['nandy']
if not user.running: if not user.running(''):
io_loop.run_sync(user.spawn) io_loop.run_sync(user.spawn)
url = public_url(app, user) url = public_url(app, user)
@@ -52,7 +52,7 @@ def test_disable_user_config(app, io_loop):
cookies = app.login_user('nandy') cookies = app.login_user('nandy')
user = app.users['nandy'] user = app.users['nandy']
# stop spawner, if running: # stop spawner, if running:
if user.running: if user.running(''):
print("stopping") print("stopping")
io_loop.run_sync(user.stop) io_loop.run_sync(user.stop)
# start with new config: # start with new config:

View File

@@ -149,11 +149,12 @@ class User(HasTraits):
self.spawners = _SpawnerDict(self._new_spawner) self.spawners = _SpawnerDict(self._new_spawner)
# load existing named spawners # load existing named spawners
for name in self.orm_spawners: for name in self.orm_spawners:
self.log.debug("Loading spawner %s:%s", self.name, name)
self.spawners[name] = self._new_spawner(name) self.spawners[name] = self._new_spawner(name)
def _new_spawner(self, name): def _new_spawner(self, name):
"""Create a new spawner""" """Create a new spawner"""
self.log.debug("Creating Spawner for %s:%s", self.name, name)
orm_spawner = self.orm_spawners.get(name) orm_spawner = self.orm_spawners.get(name)
if orm_spawner is None: if orm_spawner is None:
orm_spawner = orm.Spawner(user=self.orm_user, name=name) orm_spawner = orm.Spawner(user=self.orm_user, name=name)
@@ -202,11 +203,12 @@ class User(HasTraits):
def __repr__(self): def __repr__(self):
return repr(self.orm_user) return repr(self.orm_user)
@property def running(self, name):
def running(self):
"""property for whether a user has a running server""" """property for whether a user has a running server"""
spawner = self.spawner if name not in self.spawners:
return False
spawner = self.spawners[name]
if spawner._spawn_pending or spawner._stop_pending: if spawner._spawn_pending or spawner._stop_pending:
return False # server is not running if spawn or stop is still pending return False # server is not running if spawn or stop is still pending
if spawner.server is None: if spawner.server is None:
@@ -222,12 +224,11 @@ class User(HasTraits):
"""My name, escaped for use in URLs, cookies, etc.""" """My name, escaped for use in URLs, cookies, etc."""
return quote(self.name, safe='@') return quote(self.name, safe='@')
@property def proxy_spec(self, name=''):
def proxy_spec(self):
if self.settings.get('subdomain_host'): if self.settings.get('subdomain_host'):
return self.domain + self.base_url return url_path_join(self.domain, self.base_url, name)
else: else:
return self.base_url return url_path_join(self.base_url, name)
@property @property
def domain(self): def domain(self):

View File

@@ -44,12 +44,12 @@
<td class="admin-col col-sm-2">{% if u.admin %}admin{% endif %}</td> <td class="admin-col col-sm-2">{% if u.admin %}admin{% endif %}</td>
<td class="time-col col-sm-3">{{u.last_activity.isoformat() + 'Z'}}</td> <td class="time-col col-sm-3">{{u.last_activity.isoformat() + 'Z'}}</td>
<td class="server-col col-sm-2 text-center"> <td class="server-col col-sm-2 text-center">
<span class="stop-server btn btn-xs btn-danger {% if not u.running %}hidden{% endif %}">stop server</span> <span class="stop-server btn btn-xs btn-danger {% if not u.running('') %}hidden{% endif %}">stop server</span>
<span class="start-server btn btn-xs btn-success {% if u.running %}hidden{% endif %}">start server</span> <span class="start-server btn btn-xs btn-success {% if u.running('') %}hidden{% endif %}">start server</span>
</td> </td>
<td class="server-col col-sm-1 text-center"> <td class="server-col col-sm-1 text-center">
{% if admin_access %} {% if admin_access %}
<span class="access-server btn btn-xs btn-success {% if not u.running %}hidden{% endif %}">access server</span> <span class="access-server btn btn-xs btn-success {% if not u.running('') %}hidden{% endif %}">access server</span>
{% endif %} {% endif %}
</td> </td>
<td class="edit-col col-sm-1 text-center"> <td class="edit-col col-sm-1 text-center">