2017-07-21 12:36:07 -05:00
|
|
|
;;; ein-jupyterhub.el --- Interface to Jupyterhub -*- mode: emacs-lisp; fill-column: 80; lexical-binding: t -*-
|
2017-04-07 08:25:37 -05:00
|
|
|
|
|
|
|
;; Copyright (C) 2016 - John Miller
|
|
|
|
|
|
|
|
;; Authors: Takafumi Arakaki <aka.tkf at gmail.com>
|
|
|
|
;; John M. Miller <millejoh at mac.com>
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;; ein-jupyterhub.el is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; ein-jupyterhub.el is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with ein-jupyter.el. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; An interface to the Jupyterhub login and management API as described in
|
|
|
|
;;; http://jupyterhub.readthedocs.io/en/latest/api/index.html
|
|
|
|
;;;
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
2017-07-21 12:36:07 -05:00
|
|
|
(require 'request-deferred)
|
2017-07-20 18:25:40 -05:00
|
|
|
(require 'deferred)
|
2017-07-21 12:36:07 -05:00
|
|
|
(require 'ein-query)
|
|
|
|
|
|
|
|
(defvar *ein:jupyterhub-servers* (make-hash-table :test #'equal))
|
|
|
|
|
|
|
|
(defstruct ein:$jh-conn
|
|
|
|
"Data representing a connection to a jupyterhub server."
|
|
|
|
url
|
|
|
|
version
|
|
|
|
token)
|
|
|
|
|
|
|
|
(defstruct ein:$jh-user
|
|
|
|
"A jupyterhub user, per https://jupyterhub.readthedocs.io/en/latest/_static/rest-api/index.html#/definitions/User."
|
|
|
|
name
|
|
|
|
admin
|
|
|
|
groups
|
|
|
|
server
|
|
|
|
pending
|
2017-07-21 17:25:35 -05:00
|
|
|
last-activity)
|
2017-07-21 12:36:07 -05:00
|
|
|
|
|
|
|
(defun ein:get-jh-conn (url)
|
|
|
|
(gethash url *ein:jupyterhub-servers*))
|
|
|
|
|
|
|
|
(defun ein:reset-jh-servers ()
|
|
|
|
(setq *ein:jupyterhub-servers* (make-hash-table :test #'equal)))
|
|
|
|
|
|
|
|
(defun ein:jupyterhub-prepare-headers (conn settings &optional securep)
|
|
|
|
"When present, add the authorizations token to the header of a
|
|
|
|
request to Jupyterhub's REST API. Also perform the usual header
|
|
|
|
preparation ala `ein:query-prepare-header', i.e. added XSRF token
|
|
|
|
and configure cookies."
|
2017-07-21 17:25:35 -05:00
|
|
|
(let ((s (ein:query-prepare-header (ein:$jh-conn-url conn) settings securep)))
|
|
|
|
(if (ein:$jh-conn-token conn)
|
|
|
|
(setq s (plist-put s :headers (append (plist-get s :headers)
|
|
|
|
(list (cons "Authorization"
|
|
|
|
(format "token %s"
|
|
|
|
(ein:$jh-conn-token conn))))))))
|
|
|
|
s))
|
2017-07-21 12:36:07 -05:00
|
|
|
|
|
|
|
(defun ein:jupyterhub-api-url (url-or-port command &rest args)
|
|
|
|
(if args
|
2017-07-21 17:25:35 -05:00
|
|
|
(apply #'ein:url url-or-port "hub/api" command args)
|
2017-07-21 12:36:07 -05:00
|
|
|
(ein:url url-or-port "hub/api" command)))
|
|
|
|
|
2017-07-21 17:25:35 -05:00
|
|
|
(defun* ein:jupyterhub-query (conn url &rest settings)
|
|
|
|
""
|
|
|
|
(apply #'request-deferred
|
|
|
|
(url-encode-url (ein:jupyterhub-api-url (ein:$jh-conn-url conn) url))
|
|
|
|
(ein:jupyterhub-prepare-headers conn settings)))
|
|
|
|
|
2017-07-21 12:36:07 -05:00
|
|
|
(defun ein:jupyterhub-connect (url-or-port user password)
|
2017-07-20 18:25:40 -05:00
|
|
|
(deferred:$
|
|
|
|
(ein:query-deferred
|
|
|
|
(ein:jupyterhub-api-url url-or-port "/")
|
|
|
|
:type "GET"
|
|
|
|
:parser #'ein:json-read)
|
|
|
|
(deferred:nextc it
|
|
|
|
(lambda (response)
|
|
|
|
(when (and response (request-response-data response))
|
2017-07-21 12:36:07 -05:00
|
|
|
(let ((conn (make-ein:$jh-conn :url (or (ein:get-response-redirect response)
|
|
|
|
url-or-port)
|
|
|
|
:version (plist-get (request-response-data response) :version))))
|
|
|
|
conn))))
|
|
|
|
(deferred:nextc it
|
|
|
|
(lambda (conn)
|
|
|
|
(unless conn
|
|
|
|
(error "Connection to Jupyterhub server at %s failed! Maybe you used the wrong URL?" url-or-port))
|
|
|
|
(ein:jupyterhub-token-request conn user password)))
|
|
|
|
(deferred:nextc it
|
|
|
|
(lambda (conn)
|
2017-07-21 17:25:35 -05:00
|
|
|
(setf (gethash (ein:$jh-conn-url conn) *ein:jupyterhub-servers*) conn)
|
|
|
|
(ein:jupyterhub-start-server conn user)))))
|
2017-07-20 18:25:40 -05:00
|
|
|
|
2017-07-21 12:36:07 -05:00
|
|
|
(defun ein:jupyterhub-token-request (conn username password)
|
|
|
|
(deferred:$
|
|
|
|
(ein:query-deferred
|
|
|
|
(ein:jupyterhub-api-url (ein:$jh-conn-url conn) "authorizations/token")
|
|
|
|
:type "POST"
|
|
|
|
:timeout ein:content-query-timeout
|
|
|
|
:parser #'ein:json-read
|
|
|
|
:data (json-encode`(("username" . ,username)
|
|
|
|
("password" . , password))))
|
|
|
|
(deferred:nextc it
|
|
|
|
(lambda (response)
|
|
|
|
(message "response-data: %s, %s"
|
|
|
|
(request-response-data response)
|
|
|
|
(cadr (request-response-data response))) ;; FIXME: Why doesn't plist-get work?
|
|
|
|
(setf (ein:$jh-conn-token conn) (cadr (request-response-data response)))
|
|
|
|
conn))))
|
2017-04-07 08:25:37 -05:00
|
|
|
|
2017-07-21 17:25:35 -05:00
|
|
|
(defun ein:jupyterhub-get-user (conn username)
|
|
|
|
(deferred:$
|
|
|
|
(ein:jupyterhub-query
|
|
|
|
conn
|
|
|
|
(ein:url "users" username)
|
2017-04-07 08:25:37 -05:00
|
|
|
:type "GET"
|
2017-07-21 17:25:35 -05:00
|
|
|
:parser #'ein:json-read)
|
|
|
|
(deferred:nextc it
|
|
|
|
(lambda (response)
|
|
|
|
(let ((data (request-response-data response)))
|
|
|
|
(message "Response: %s" data)
|
|
|
|
(make-ein:$jh-user :name (plist-get :name data)
|
|
|
|
:admin (plist-get :admin data)
|
|
|
|
:groups (plist-get :groups data)
|
|
|
|
:server (plist-get :server data)
|
|
|
|
:pending (plist-get :pending data)
|
|
|
|
:last-activity (plist-get :last_activity data)))))))
|
|
|
|
|
|
|
|
(defun ein:jupyterhub-start-server (conn username)
|
|
|
|
(deferred:$
|
|
|
|
(ein:jupyterhub-query
|
|
|
|
conn
|
|
|
|
(ein:url "users" username "server")
|
2017-04-07 08:25:37 -05:00
|
|
|
:type "POST"
|
2017-07-21 17:25:35 -05:00
|
|
|
:parser #'ein:json-read)
|
|
|
|
(deferred:nextc it
|
|
|
|
(lambda (response)
|
|
|
|
(if (eql 201 (request-response-status-code response))
|
|
|
|
(ein:jupyterhub-get-user conn username)
|
|
|
|
(message "Response status: %s" (request-response-status-code response))
|
|
|
|
)))
|
|
|
|
(deferred:nextc it
|
|
|
|
(lambda (user)
|
|
|
|
(when (ein:$jh-user-p user)
|
|
|
|
(let ((serverurl (ein:url (ein:$jh-conn-url conn) (ein:$jh-user-server user))))
|
|
|
|
(ein:notebooklist-open serverurl)))))))
|
2017-04-07 08:25:37 -05:00
|
|
|
|
2017-07-21 17:25:35 -05:00
|
|
|
(defun ein:jupyterhub-list-users (conn)
|
|
|
|
(deferred:$
|
|
|
|
(ein:jupyterhub-query
|
|
|
|
conn
|
|
|
|
"users"
|
2017-04-07 08:25:37 -05:00
|
|
|
:type "GET"
|
2017-07-21 17:25:35 -05:00
|
|
|
:parser #'ein:json-read)
|
|
|
|
(deferred:nextc it
|
|
|
|
(lambda (response)
|
|
|
|
(message "Response: %s" (request-response-data response))
|
|
|
|
response))))
|
2017-04-07 08:25:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
(provide 'ein-jupyterhub)
|