Skip to content

Commit

Permalink
Merge pull request #39 from minrk/0.7
Browse files Browse the repository at this point in the history
deploy with jupyterhub 0.7
  • Loading branch information
minrk committed Nov 15, 2016
2 parents 1f43cac + d41ead3 commit dfbcc59
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
25 changes: 18 additions & 7 deletions roles/cull_idle/files/cull_idle_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@
- single-user websocket ping interval (default: 30s)
- JupyterHub.last_activity_interval (default: 5 minutes)
Generate an API token and store it in `JPY_API_TOKEN`:
You can run this as a service managed by JupyterHub with this in your config::
export JPY_API_TOKEN=`jupyterhub token`
python cull_idle_servers.py [--timeout=900] [--url=http://127.0.0.1:8081/hub]
c.JupyterHub.services = [
{
'name': 'cull-idle',
'admin': True,
'command': 'python cull_idle_servers.py --timeout=3600'.split(),
}
]
Or run it manually by generating an API token and storing it in `JUPYTERHUB_API_TOKEN`:
export JUPYTERHUB_API_TOKEN=`jupyterhub token`
python cull_idle_servers.py [--timeout=900] [--url=http://127.0.0.1:8081/hub/api]
"""

import datetime
Expand All @@ -34,7 +45,7 @@ def cull_idle(url, api_token, timeout):
auth_header = {
'Authorization': 'token %s' % api_token
}
req = HTTPRequest(url=url + '/api/users',
req = HTTPRequest(url=url + '/users',
headers=auth_header,
)
now = datetime.datetime.utcnow()
Expand All @@ -47,7 +58,7 @@ def cull_idle(url, api_token, timeout):
last_activity = parse_date(user['last_activity'])
if user['server'] and last_activity < cull_limit:
app_log.info("Culling %s (inactive since %s)", user['name'], last_activity)
req = HTTPRequest(url=url + '/api/users/%s/server' % user['name'],
req = HTTPRequest(url=url + '/users/%s/server' % user['name'],
method='DELETE',
headers=auth_header,
)
Expand All @@ -60,15 +71,15 @@ def cull_idle(url, api_token, timeout):
app_log.debug("Finished culling %s", name)

if __name__ == '__main__':
define('url', default='http://127.0.0.1:8081/hub', help="The JupyterHub API URL")
define('url', default=os.environ.get('JUPYTERHUB_API_URL'), help="The JupyterHub API URL")
define('timeout', default=600, help="The idle timeout (in seconds)")
define('cull_every', default=0, help="The interval (in seconds) for checking for idle servers to cull")

parse_command_line()
if not options.cull_every:
options.cull_every = options.timeout // 2

api_token = os.environ['JPY_API_TOKEN']
api_token = os.environ['JUPYTERHUB_API_TOKEN']

loop = IOLoop.current()
cull = lambda : cull_idle(options.url, api_token, options.timeout)
Expand Down
4 changes: 2 additions & 2 deletions roles/cull_idle/templates/cull_idle_servers.conf.j2
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# {{ ansible_managed }}

[program:cull_idle_servers]
environment=JPY_API_TOKEN='{{ cull_idle_servers_hubapi_token }}'
environment=JUPYTERHUB_API_TOKEN='{{ cull_idle_servers_hubapi_token }}'
command=/opt/conda/bin/python3 {{jupyterhub_srv_dir}}/cull_idle_servers.py --cull-every={{ cull_every }} --timeout={{ cull_timeout}}
redirect_stderr=true
stdout_logfile={{ jupyterhub_log_dir }}/cull_idle_servers.log
autostart=true
autorestart=false
stopasgroup=true
user=root
user=nobody
directory={{jupyterhub_srv_dir}}
4 changes: 2 additions & 2 deletions roles/jupyterhub/tasks/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
pip: name={{item}} state=present editable=false
become: true
with_items:
- jupyterhub==0.6
- oauthenticator==0.3
- jupyterhub==0.7.0b1
- oauthenticator==0.5
20 changes: 13 additions & 7 deletions roles/jupyterhub/templates/jupyterhub_config.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ c.Authenticator.whitelist = {
c.Authenticator.whitelist = set()
{% endif %}

c.JupyterHub.api_tokens = {}
c.JupyterHub.services = [
{% if formgrader_hubapi_token %}
c.JupyterHub.api_tokens.update({
'{{formgrader_hubapi_token}}': '{{nbgrader_owner}}'
})
{
'name': 'formgrader',
'admin': True,
'token': '{{formgrader_hubapi_token}}',
'url': 'http://127.0.0.1:{{nbgrader_port}}',
},
{% endif %}
{% if cull_idle_servers_hubapi_token %}
c.JupyterHub.api_tokens.update({
'{{cull_idle_servers_hubapi_token}}': '{{cull_idle_servers_owner}}'
})
{
'name': 'cull_idle_servers',
'admin': True,
'token': '{{cull_idle_servers_hubapi_token}}',
},
{% endif %}
]

0 comments on commit dfbcc59

Please sign in to comment.