mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-12 04:23:01 +00:00
add Proxy.fetch_routes
and DRY up the api requests
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
# Copyright (c) Jupyter Development Team.
|
# Copyright (c) Jupyter Development Team.
|
||||||
# Distributed under the terms of the Modified BSD License.
|
# Distributed under the terms of the Modified BSD License.
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
import errno
|
import errno
|
||||||
import json
|
import json
|
||||||
import socket
|
import socket
|
||||||
@@ -16,6 +17,7 @@ from sqlalchemy.types import TypeDecorator, VARCHAR
|
|||||||
from sqlalchemy import (
|
from sqlalchemy import (
|
||||||
inspect,
|
inspect,
|
||||||
Column, Integer, String, ForeignKey, Unicode, Binary, Boolean,
|
Column, Integer, String, ForeignKey, Unicode, Binary, Boolean,
|
||||||
|
DateTime,
|
||||||
)
|
)
|
||||||
from sqlalchemy.ext.declarative import declarative_base, declared_attr
|
from sqlalchemy.ext.declarative import declarative_base, declared_attr
|
||||||
from sqlalchemy.orm import sessionmaker, relationship, backref
|
from sqlalchemy.orm import sessionmaker, relationship, backref
|
||||||
@@ -134,43 +136,47 @@ class Proxy(Base):
|
|||||||
else:
|
else:
|
||||||
return "<%s [unconfigured]>" % self.__class__.__name__
|
return "<%s [unconfigured]>" % self.__class__.__name__
|
||||||
|
|
||||||
|
def api_request(self, path, method='GET', body=None, client=None):
|
||||||
|
"""Make an authenticated API request of the proxy"""
|
||||||
|
client = client or AsyncHTTPClient()
|
||||||
|
url = url_path_join(self.api_server.url, path)
|
||||||
|
|
||||||
|
if isinstance(body, dict):
|
||||||
|
body = json.dumps(body)
|
||||||
|
self.log.debug("Fetching %s %s", method, url)
|
||||||
|
req = HTTPRequest(url,
|
||||||
|
method=method,
|
||||||
|
headers={'Authorization': 'token {}'.format(self.auth_token)},
|
||||||
|
body=body,
|
||||||
|
)
|
||||||
|
|
||||||
|
return client.fetch(req)
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def add_user(self, user, client=None):
|
def add_user(self, user, client=None):
|
||||||
"""Add a user's server to the proxy table."""
|
"""Add a user's server to the proxy table."""
|
||||||
self.log.info("Adding user %s to proxy %s => %s",
|
self.log.info("Adding user %s to proxy %s => %s",
|
||||||
user.name, user.server.base_url, user.server.host,
|
user.name, user.server.base_url, user.server.host,
|
||||||
)
|
)
|
||||||
client = client or AsyncHTTPClient()
|
|
||||||
|
|
||||||
req = HTTPRequest(url_path_join(
|
yield self.api_request(user.server.base_url,
|
||||||
self.api_server.url,
|
method='POST',
|
||||||
user.server.base_url,
|
body=dict(
|
||||||
),
|
|
||||||
method="POST",
|
|
||||||
headers={'Authorization': 'token {}'.format(self.auth_token)},
|
|
||||||
body=json.dumps(dict(
|
|
||||||
target=user.server.host,
|
target=user.server.host,
|
||||||
user=user.name,
|
user=user.name,
|
||||||
)),
|
),
|
||||||
|
client=client,
|
||||||
)
|
)
|
||||||
|
|
||||||
res = yield client.fetch(req)
|
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def delete_user(self, user, client=None):
|
def delete_user(self, user, client=None):
|
||||||
"""Remove a user's server to the proxy table."""
|
"""Remove a user's server to the proxy table."""
|
||||||
self.log.info("Removing user %s from proxy", user.name)
|
self.log.info("Removing user %s from proxy", user.name)
|
||||||
client = client or AsyncHTTPClient()
|
yield self.api_request(user.server.base_url,
|
||||||
req = HTTPRequest(url_path_join(
|
method='DELETE',
|
||||||
self.api_server.url,
|
client=client,
|
||||||
user.server.base_url,
|
|
||||||
),
|
|
||||||
method="DELETE",
|
|
||||||
headers={'Authorization': 'token {}'.format(self.auth_token)},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
res = yield client.fetch(req)
|
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def add_all_users(self):
|
def add_all_users(self):
|
||||||
"""Update the proxy table from the database.
|
"""Update the proxy table from the database.
|
||||||
@@ -186,6 +192,12 @@ class Proxy(Base):
|
|||||||
for f in futures:
|
for f in futures:
|
||||||
yield f
|
yield f
|
||||||
|
|
||||||
|
@gen.coroutine
|
||||||
|
def fetch_routes(self, client=None):
|
||||||
|
"""Fetch the proxy's routes"""
|
||||||
|
resp = yield self.api_request('/', client=client)
|
||||||
|
raise gen.Return(json.loads(resp.body.decode('utf8', 'replace')))
|
||||||
|
|
||||||
|
|
||||||
class Hub(Base):
|
class Hub(Base):
|
||||||
"""Bring it all together at the hub.
|
"""Bring it all together at the hub.
|
||||||
@@ -235,6 +247,7 @@ class User(Base):
|
|||||||
_server_id = Column(Integer, ForeignKey('servers.id'))
|
_server_id = Column(Integer, ForeignKey('servers.id'))
|
||||||
server = relationship(Server, primaryjoin=_server_id == Server.id)
|
server = relationship(Server, primaryjoin=_server_id == Server.id)
|
||||||
admin = Column(Boolean, default=False)
|
admin = Column(Boolean, default=False)
|
||||||
|
last_activity = Column(DateTime, default=datetime.utcnow)
|
||||||
|
|
||||||
api_tokens = relationship("APIToken", backref="user")
|
api_tokens = relationship("APIToken", backref="user")
|
||||||
cookie_tokens = relationship("CookieToken", backref="user")
|
cookie_tokens = relationship("CookieToken", backref="user")
|
||||||
@@ -277,6 +290,7 @@ class User(Base):
|
|||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def spawn(self, spawner_class, base_url='/', hub=None, config=None):
|
def spawn(self, spawner_class, base_url='/', hub=None, config=None):
|
||||||
|
"""Start the user's spawner"""
|
||||||
db = inspect(self).session
|
db = inspect(self).session
|
||||||
if hub is None:
|
if hub is None:
|
||||||
hub = db.query(Hub).first()
|
hub = db.query(Hub).first()
|
||||||
@@ -302,6 +316,7 @@ class User(Base):
|
|||||||
|
|
||||||
# store state
|
# store state
|
||||||
self.state = spawner.get_state()
|
self.state = spawner.get_state()
|
||||||
|
self.last_activity = datetime.utcnow()
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
yield self.server.wait_up()
|
yield self.server.wait_up()
|
||||||
@@ -309,6 +324,7 @@ class User(Base):
|
|||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
"""Stop the user's spawner"""
|
||||||
if self.spawner is None:
|
if self.spawner is None:
|
||||||
return
|
return
|
||||||
status = yield self.spawner.poll()
|
status = yield self.spawner.poll()
|
||||||
|
Reference in New Issue
Block a user