Merge pull request #117 from minrk/cli-token

allow creating new API tokens on the command line
This commit is contained in:
Min RK
2015-01-07 17:15:45 -08:00
6 changed files with 148 additions and 7 deletions

View File

@@ -79,6 +79,44 @@ flags = {
SECRET_BYTES = 2048 # the number of bytes to use when generating new secrets
class NewToken(Application):
"""Generate and print a new API token"""
name = 'jupyterhub-token'
description = """Generate and return new API token for a user.
Usage:
jupyterhub token [username]
"""
examples = """
$> jupyterhub token kaylee
ab01cd23ef45
"""
name = Unicode(getuser())
def parse_command_line(self, argv=None):
super().parse_command_line(argv=argv)
if not self.extra_args:
return
if len(self.extra_args) > 1:
print("Must specify exactly one username", file=sys.stderr)
self.exit(1)
self.name = self.extra_args[0]
def start(self):
hub = JupyterHub(parent=self)
hub.init_db()
hub.init_users()
user = orm.User.find(hub.db, self.name)
if user is None:
print("No such user: %s" % self.name)
self.exit(1)
token = user.new_api_token()
print(token)
class JupyterHub(Application):
"""An Application for starting a Multi-User Jupyter Notebook server."""
name = 'jupyterhub'
@@ -104,6 +142,10 @@ class JupyterHub(Application):
aliases = Dict(aliases)
flags = Dict(flags)
subcommands = {
'token': (NewToken, "Generate an API token for a user")
}
classes = List([
Spawner,
LocalProcessSpawner,
@@ -125,7 +167,7 @@ class JupyterHub(Application):
Useful for daemonizing jupyterhub.
"""
)
last_activity_interval = Integer(600, config=True,
last_activity_interval = Integer(300, config=True,
help="Interval (in seconds) at which to update last-activity timestamps."
)
proxy_check_interval = Integer(30, config=True,
@@ -695,7 +737,7 @@ class JupyterHub(Application):
@catch_config_error
def initialize(self, *args, **kwargs):
super().initialize(*args, **kwargs)
if self.generate_config:
if self.generate_config or self.subapp:
return
self.load_config_file(self.config_file)
self.init_logging()
@@ -800,6 +842,9 @@ class JupyterHub(Application):
def start(self):
"""Start the whole thing"""
if self.subapp:
return self.subapp.start()
if self.generate_config:
self.write_config_file()
return