概要
AWSのUbuntu22.04のインスタンス起動時にマルチユーザ用のJupyterLab環境を自動で構築するためのスクリプト。たぶん他の環境でも適用可能と思う。
今後、時間があればAnsibleに対応する。
スクリプトの概要
-
- venvをインストール
-
- venv環境に必要パッケージのインストール (pip インストール)
-
- jupyterlabをマルチユーザとして利用するためのjupyterHubの設定
-
- jupyterHubをサービスとして立ち上げるためのsystemd設定
-
- test1, test2 … test5 というユーザをLINUX上に作成。初期パスワードは myp@ssw0rd
これらのユーザのみJupyterHubを通じて、JupyterLabに接続可能
接続方法の概要
-
- 接続はssh tunnelの形成後、手元のブラウザに http://localhost:28000 を入力
- JupyterHubの画面がでたらLINUXのユーザとパスワードを入力してログイン
構築スクリプト
#!/bin/bash
cat << EOF
##################################################
#
# STEP1) USERDATA: venv installation
#
##################################################
EOF
echo "# ---- STEP 1-1) change needrestart.conf ----"
sed -i s/"\#\$nrconf{restart} = 'i'"/"\$nrconf{restart} = 'a'"/g /etc/needrestart/needrestart.conf | grep \'a\'
cat /etc/needrestart/needrestart.conf | grep -v "^#" | grep \$nrconf\{restart\}
echo "# ---- STEP 1-2) pip installation ----"
apt update
apt install python3-pip -y
apt install python3-venv -y
echo "# ---- STEP 1-3) latest node installation ----"
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
cat /etc/apt/sources.list.d/nodesource.list
apt show nodejs
sudo apt-get install -y nodejs
node --version
npm --version
echo "# ---- STEP 1-4) configurable-http-proxy Installation ----"
npm install -g configurable-http-proxy
cat << EOF
##################################################
#
# STEP2) USERDATA: JupyterLab Installation
#
##################################################
EOF
INSTALL_DIR="/opt/jupyter"
CONFIG="${INSTALL_DIR}/etc"
VENV="${INSTALL_DIR}/venv"
echo "# ---- STEP 2-1) start venv ----"
mkdir -p ${CONFIG}
python3 -m venv ${VENV}
ls -l ${INSTALL_DIR}
source ${VENV}/bin/activate
which python
echo "# ---- STEP 2-2) Jupyter Installation ----"
pip install jupyterlab
pip install jupyterhub
pip install numpy
pip install pandas
pip install psycopg
pip install matplotlib
pip install japanize_matplotlib
pip install seaborn
pip install psycopg2-binary
pip install scikit-learn
which jupyter
which jupyterhub
cat << EOF
##################################################
#
# STEP3) USERDATA: JupyterHub Settings
#
##################################################
EOF
jupyterhub --generate-config -f ${CONFIG}/jupyterhub_config.py
mv ${CONFIG}/jupyterhub_config.py ${CONFIG}/jupyterhub_config.py.org
users=("test1" "test2" "test3" "test4" "test5")
cat << EOF > ${CONFIG}/jupyterhub_config.py
c.Spawner.default_url = '/lab'
c.Authenticator.whitelist = {'${users[0]}', '${users[1]}', '${users[2]}', '${users[3]}', '${users[4]}'}
c.Authenticator.admin_users = {'root'}
c.SystemdSpawner.default_shell = '/bin/bash'
c.Spawner.notebook_dir = '~/'
c.Spawner.cmd = ['${VENV}/bin/jupyterhub-singleuser']
EOF
ls -l ${CONFIG}
cat ${CONFIG}/jupyterhub_config.py
cat << EOF
##################################################
#
# STEP4) USERDATA: systemd settings for jupyter
#
##################################################
EOF
echo "# ---- STEP 4-1) create jupyter.service ----"
cat <<EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyterlab for GateDB
After=syslog.target network.target
[Service]
User=root
StandardOutput=file:/var/log/jupyter.log
ExecStartPre=/bin/bash -c "source ${VENV}/bin/activate"
ExecStart=${VENV}/bin/jupyterhub -f ${CONFIG}/jupyterhub_config.py
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
cat /etc/systemd/system/jupyter.service
echo "# ---- STEP 4-2) start jupyterhub daemon ----"
systemctl daemon-reload
systemctl enable jupyter
systemctl start jupyter
systemctl status jupyter
cat << EOF
##################################################
#
# STEP5) USERDATA: create users
#
##################################################
EOF
for username in ${users[@]}; do
adduser --disabled-password --gecos "" "${username}"
echo "${username}:myp@ssw0rd" | chpasswd
id ${username}
done
接続方法
ssh tunnelの形成
PRIVATE_KEY="<private key>"
USERNAME="<USER NAME>"
HOSTNAME="<HOSTNAME>"
ssh -i ${PRIVATE_KEY} ${USERNAME}@${HOSTNAME} -L 28000:localhost:8000
Jupyter環境のオープン
手元のマシンに以下を入力する。
http://localhost:28000