[メモ] らずぱい: JupyterHubでマルチユーザ対応



## 概要
- [らずぱいにJupyter notebook](http://qiita.com/mt08/items/edb9bcaeaa416d3e5319) のつづき
- [GitHub - jupyterhub/jupyterhub: Multi-user server for Jupyter notebooks](https://github.com/jupyterhub/jupyterhub)をインストールして、マルチユーザ対応。

- JupyterHub
    > *With JupyterHub you can create a multi-user Hub which spawns, manages, and proxies multiple instances of the single-user Jupyter notebook (IPython notebook) server.*
    > *JupyterHub provides single-user notebook servers to many users. For example, JupyterHub could serve notebooks to a class of students, a corporate workgroup, or a science research group.*
JupyterHubを使用すると、シングルユーザーJupyterノートブック（IPythonノート）サーバーの複数のインスタンスをスポーン、管理、プロクシするマルチユーザハブを作成することができます。JupyterHubは、多くのユーザーにシングル・ユーザ・ノートブック・サーバを提供します。例えば、JupyterHubは学生のクラス、企業のワークグループ、または科学研究グループに（Jupyter)notebookを提供することができます。

    だそうです. (↑グーグル翻訳改)
- SSL関係はやらない.


## 環境
- Raspberry Pi 3
- `2016-05-27-raspbian-jessie-lite.img`
- microSD 8GB
- [らずぱいにJupyter notebook](http://qiita.com/mt08/items/edb9bcaeaa416d3e5319) のつづきになるので、Jupyterインストール済みの環境

## 手順

### nodejs/npm
1. `sudo apt-get install npm nodejs-legacy`
2. `sudo npm install -g configurable-http-proxy`

### JupyterHub

1. インストール。管理ユーザ`jupyterhub`追加

    ```bash
# JupyterHubとSudoSpawnerのインストール.
sudo pip3 install jupyterhub
sudo pip install git+https://github.com/jupyter/sudospawner
#
# ユーザjupyterhub作成. /etc/shadowにアクセス権を持つファイルのグループに追加.
sudo useradd jupyterhub
sudo usermod -a -G shadow jupyterhub
sudo mkdir /etc/jupyterhub
sudo chown jupyterhub /etc/jupyterhub
cd /etc/jupyterhub
sudo -u jupyterhub jupyterhub --generate-config
```

2. `sudo -u jupyterhub vi /etc/jupyterhub/jupyterhub_config.py`<br>3箇所変更

    ```py:/etc/jupyterhub/jupyterhub_config.py
...
c.JupyterHub.confirm_no_ssl = True
...
c.JupyterHub.spawner_class = 'sudospawner.SudoSpawner'
...
c.Spawner.notebook_dir = '~/notebooks'
...
```

3. `sudo visudo`
    - `JUPYTER_USERS`に使用するユーザを入れる。

    ```txt:以下追加
...
# comma-separated whitelist of users that can spawn single-user servers
Runas_Alias JUPYTER_USERS = pi, jns

    # the command(s) the Hub can run on behalf of the above users without needing a password# the exact path may differ, depending on how sudospawner was installed
Cmnd_Alias JUPYTER_CMD = /usr/local/bin/sudospawner

    # actually give the Hub user permission to run the above command on behalf# of the above users without prompting for a password
jupyterhub ALL=(JUPYTER_USERS) NOPASSWD:JUPYTER_CMD
```

4. `sudo -u pi mkdir ~/notebooks`<br>使用するユーザのホームフォルダに <font color='red'>**notebooks** フォルダを作成</font>する. 

5. 動作確認
`cd /etc/jupyterhub;  sudo -u jupyterhub /usr/local/bin/jupyterhub`
ブラウザを立ち上げて、`http://<ラズベリーパイのIP>:8000/`へアクセス。
Linuxのログインユーザ・パスワードをいれて、Jupyter NotebookのページへすすめばOK.

    ```bash:実行例
pi@raspberrypi:~ $ cd /etc/jupyterhub;  sudo -u jupyterhub /usr/local/bin/jupyterhub
[I 2016-08-21 06:56:01.839 JupyterHub app:622] Loading cookie_secret from /etc/jupyterhub/jupyterhub_cookie_secret
[W 2016-08-21 06:56:02.009 JupyterHub app:304] 
    Generating CONFIGPROXY_AUTH_TOKEN. Restarting the Hub will require restarting the proxy.
    Set CONFIGPROXY_AUTH_TOKEN env or JupyterHub.proxy_auth_token config to avoid this message.
    
    [W 2016-08-21 06:56:02.030 JupyterHub app:757] No admin users, admin interface will be unavailable.
[W 2016-08-21 06:56:02.030 JupyterHub app:758] Add any administrative users to `c.Authenticator.admin_users` in config.
[I 2016-08-21 06:56:02.031 JupyterHub app:785] Not using whitelist. Any authenticated user will be allowed.
[W 2016-08-21 06:56:03.107 JupyterHub app:883] pi not running.
[W 2016-08-21 06:56:04.205 JupyterHub app:883] jns not running.
[I 2016-08-21 06:56:04.259 JupyterHub app:1231] Hub API listening on http://127.0.0.1:8081/hub/
[W 2016-08-21 06:56:04.276 JupyterHub app:959] Running JupyterHub without SSL. There better be SSL termination happening somewhere else...
[I 2016-08-21 06:56:04.277 JupyterHub app:968] Starting proxy @ http://*:8000/
06:56:05.087 - info: [ConfigProxy] Proxying http://*:8000 to http://127.0.0.1:8081
06:56:05.098 - info: [ConfigProxy] Proxy API at http://127.0.0.1:8001/api/routes
[I 2016-08-21 06:56:05.205 JupyterHub app:1254] JupyterHub is now running at http://127.0.0.1:8000/
...
... >>> ブラウザでアクセス
...
[I 2016-08-21 06:56:17.998 JupyterHub log:100] 302 GET / (@127.0.0.1) 9.73ms
[I 2016-08-21 06:56:18.033 JupyterHub log:100] 302 GET /hub (@127.0.0.1) 1.93ms
[I 2016-08-21 06:56:18.069 JupyterHub log:100] 302 GET /hub/ (@127.0.0.1) 3.30ms
[I 2016-08-21 06:56:18.253 JupyterHub log:100] 200 GET /hub/login (@127.0.0.1) 161.63ms
```
![JupyterHub01.png](https://qiita-image-store.s3.amazonaws.com/0/75594/f8a7ffb5-13d8-c572-14be-29173490e729.png)


### `systemd`で自動起動

1. `sudo vi  /lib/systemd/system/jupyterhub.service`

    ```txt:/lib/systemd/system/jupyterhub.service
[Unit]
Description=Jupyterhub

    [Service]
User=jupyterhub
ExecStart=/usr/local/bin/jupyterhub
WorkingDirectory=/etc/jupyterhub

    [Install]
WantedBy=multi-user.target
```
2. サービス有効・開始・状態

    ```bash
sudo systemctl enable jupyterhub
sudo systemctl start jupyterhub
sudo systemctl status jupyterhub
```

## その他
- `c.JupyterHub.confirm_no_ssl = True`で `--no-ssl` にしてますが、ググるとすぐでてきて、簡単に対応できたと思います。(一度、動作確認してますが、ローカルネットワークならいらないかと...説明が面倒...)