Skip to content
Snippets Groups Projects
Commit 813c593e authored by schmittu's avatar schmittu :beer:
Browse files

added scripts for setting up euler to run jupyter notebooks

parent 4ee02273
No related branches found
No related tags found
No related merge requests found
Showing
with 510 additions and 0 deletions
printout.pdf
**/.*.sw?
# Scripts and files
- `setup_euler.py` does:
- login to euler for all course user accounts specified in
`credentials.txt` file and accepts license agreement if asked.
- sets up and checks passwordless ssh access for course accounts on euler
- `update_conda_env_on_euler.sh` sets up conda and packages for the workshop
in `/cluster/scratch/schmittu/mlw`, repeated calls will run updates, e.g. when
`../mlw_packages.yml` is modified
- `setup_course_accounts_on_euler.sh` copies current notebooks and scripts
from `to_deploy_on_euler/` to euler for every user from `credentials.txt`.
- `update_installer_at_sis_website.sh` zips abnd copies files from `to_deploy_on_course_computers`
to `https://sis.id.ethz.ch`. These can later be downloaded.
## Notes
- we use the same keypair for all course accounts. This could be improved in the future,
but would require some work.
- the private key is also hard coded in `start_jupyter_nb.sh` script!.
#! /bin/sh
#
# create_printout.sh
# Copyright (C) 2019 Uwe Schitt <uwe.schmitt@id.ethz.ch>
#
# Distributed under terms of the MIT license.
#
function create_md {
cat credentials.txt | while read -r USER PASSWORD
do
echo "### Preparation"
echo "1. Start Computers, choose *Fedora* in the boot menu shown."
echo
echo "2. To login use:"
echo
echo " - user : <code>$USER</code>"
echo
echo " - password : <code>$PASSWORD</code>"
echo
echo "3. Open a terminal"
echo
echo "4. To download startup script enter"
echo
echo ' ```bash'
echo " wget https://sis.id.ethz.ch/installer_wlw.zip"
echo '```'
echo " and press RETURN."
echo
echo "5. To extract the downloaded archive enter"
echo
echo ' ```bash'
echo " unzip installer_wlw.zip"
echo '```'
echo " and press RETURN."
echo
echo "### During the course"
echo
echo "To startup jupyter notebook on Euler enter"
echo
echo '```bash'
echo "bash start_jupyter_nb.sh"
echo '```'
echo " and press RETURN."
echo
echo "### After the course"
echo
echo "To upload notebooks to your polybox:"
echo
echo '```bash'
echo "bash upload_to_polybox.sh"
echo '```'
echo "and press RETURN."
echo
echo '<p style="page-break-after: always;" />'
done
}
OUT=printout.pdf
create_md | pandoc -f markdown -t html5 -c pandoc.css -o $OUT -
echo
ls -l $OUT
mlwl01 generatedPWD157
mlwl02 generatedPWD358
mlwl03 generatedPWD539
mlwl04 generatedPWD553
mlwl05 generatedPWD2
mlwl06 generatedPWD743
mlwl07 generatedPWD643
mlwl08 generatedPWD927
mlwl09 generatedPWD950
mlwl10 generatedPWD673
mlwl11 generatedPWD6
mlwl12 generatedPWD796
mlwl13 generatedPWD316
mlwl14 generatedPWD526
mlwl15 generatedPWD609
mlwl16 generatedPWD113
mlwl17 generatedPWD169
mlwl18 generatedPWD803
mlwl19 generatedPWD430
mlwl20 generatedPWD496
mlwl21 generatedPWD285
mlwl22 generatedPWD329
mlwl23 generatedPWD885
mlwl24 generatedPWD704
mlwl25 generatedPWD809
mlwl26 generatedPWD561
mlwl27 generatedPWD226
mlwl28 generatedPWD75
mlwl29 generatedPWD571
mlwl30 generatedPWD646
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAQEA3hDbsNOkKnAmKXuVONMyEMjFOs9I3R7CuFjlHXhAZBmygqIgN9NR
XBWVi/cuxF5dSE3btHLfmJh+lZ5o3SIN6EqnSlu1laKyPErx8RTrpTRIasB3i4b4gNYqrJ
OpNsbt8TWAAKfcMJVFHRFspzgJAAjgWtahZRLBS1Xh2ERw607Eu+rsU4SnALIQLDuhqxML
/zAAP/hUH+m8/iD8WR2yDm3cURpT/p6MiHH9UAHB55scC33cyZaXKArufEN5oGhA3MjX08
vheMqF0FcpYSfLF5Az1jSBajezaNNKjk6k7P213wrq77bnRhvAWmeZ3Yyg84X5+SwbQx5A
6/OH/jd2dwAAA9jyP7ZA8j+2QAAAAAdzc2gtcnNhAAABAQDeENuw06QqcCYpe5U40zIQyM
U6z0jdHsK4WOUdeEBkGbKC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WGl6Q1bcN7FETlBbjt/T/PRT
UfT0MlFxZHZE8lMfskM0IHiu5YwWyss7291xLuUQh9NoM0eOqANjdhOb80CdfyYauS96nL
SarwWU34YNfPor6LAAAAIHV3ZXNjaG1pdHRAaWQtc2lzLXZwbi0yOC5ldGh6LmNoAQI=
-----END OPENSSH PRIVATE KEY-----
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDeENuw06QqcCYpe5U40zIQyMU6z0jdHsK4WOUdeEBkGbKCoiA301FcFZWL9y7EXl1ITdu0ct+YmH6VnmjdIg3oSqdKW7WVorI8SvHxFOulNEhqwHeLhviA1iqsk6k2xu3xNYAAp9wwlUUdEWynOAkACOBa1qFlEsFLVeHYRHDrTsS76uxThKcAshAsO6GrEwv/MAA/+FQf6bz+IPxZHbIObdxRGlP+noyIcf1QAcHnmxwLfdzJlpcoCu58Q3mgaEDcyNfTy+F4yoXQVylhJ8sXkDPWNIFqN7No00qOTqTs/bXfCurvtudGG8BaZ5ndjKDzhfn5LBtDHkDr84f+N3Z3 uweschmitt@id-sis-vpn-28.ethz.ch
/*
* pandoc.css
* Copyright (C) 2019 Uwe Schitt
*
* Distributed under terms of the MIT license.
*/
body {
background: white;
color: black;
font-family: Verdana;
}
code {
font-size: 135%;
margin-top: 10px;
margin-bottom: 10px;
}
.maybe_break {
page-break-after: auto;
}
#! /bin/sh
#
# deploy_notebooks.sh
# Copyright (C) 2019 Uwe Schitt <uwe.schmitt@id.ethz.ch>
#
# Distributed under terms of the MIT license.
#
EULER=euler.ethz.ch
cat credentials.txt | while read -r USER PASSWORD
do
# USER=$(echo $line | cut -d" " -f 1)
echo "=========================================================================="
echo $USER
echo
# https://stackoverflow.com/questions/13800225/
ssh -n -i id_rsa $USER@$EULER "mkdir -p mlw"
scp -i id_rsa ../??_*.ipynb ../custom.html $USER@$EULER:mlw
scp -i id_rsa -r ../data ../images $USER@$EULER:mlw
scp -i id_rsa ./to_deploy_on_euler/* ./upload_to_polybox_from_euler.sh $USER@$EULER:
done
#! /usr/bin/env python
# Copyright © 2019 Uwe Schitt <uwe.schmitt@id.ethz.ch>
import pexpect
import sys
import time
import pdb
from pexpect.exceptions import TIMEOUT
credentials = [line.split() for line in open("credentials.txt")]
EULER = "euler.ethz.ch"
def open_conn(cmd):
print(cmd)
conn = pexpect.spawn(cmd, echo=True, encoding="utf-8")
conn.logfile = sys.stdout
return conn
def auth_pw(conn, password):
conn.expect("password:")
conn.sendline(password)
def accept_license_agreements(user, password):
ssh_conn = open_conn(f"ssh {user}@{EULER}")
auth_pw(ssh_conn, password)
try:
ssh_conn.expect("to exit: ", timeout=1)
ssh_conn.sendline("Yes")
time.sleep(.5)
except TIMEOUT:
pass
ssh_conn.close()
def create_ssh_folders(user, password):
ssh_conn = open_conn(f"ssh {user}@{EULER} 'mkdir -p .ssh; ls -al .ssh'")
auth_pw(ssh_conn, password)
ssh_conn.expect(pexpect.EOF)
ssh_conn.close()
def deploy_ssh_keys(user, password):
ssh_conn = open_conn(f"scp id_rsa.pub {user}@{EULER}:.ssh/authorized_keys")
auth_pw(ssh_conn, password)
ssh_conn.expect(pexpect.EOF)
ssh_conn.close()
def check_passwordless_login(user):
ssh_conn = open_conn(f"ssh -i id_rsa {user}@{EULER}")
try:
ssh_conn.expect("password: ", timeout=1)
except TIMEOUT:
return True
else:
return False
finally:
ssh_conn.close()
flags = []
for user, password in credentials:
accept_license_agreements(user, password)
create_ssh_folders(user, password)
deploy_ssh_keys(user, password)
ok = check_passwordless_login(user)
flags.append(ok)
assert all(flags)
#!/bin/bash
# Script to start a jupyter notebook from a local computer on Euler/Leonhard Open
# Samuel Fux, Dec. 2018 @ETH Zurich
# change history:
# 24.01.2019 Added option to specify cluster on which the notebook is executed
# function to print usage instructions
function print_usage {
echo -e "Usage:\t start_jupyter_nb.sh NETHZ_USERNAME\n"
echo -e "Arguments:\n"
echo -e "NETHZ_USERNAME\t\tNETHZ username for which the notebook should be started"
echo -e "./start_jupyter_nb.sh sfux\n"
}
# if number of command line arguments is different from 5 or if $1==-h or $1==--help
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
print_usage
exit
fi
cat > id_rsa <<KEYEND
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAQEA3hDbsNOkKnAmKXuVONMyEMjFOs9I3R7CuFjlHXhAZBmygqIgN9NR
XBWVi/cuxF5dSE3btHLfmJh+lZ5o3SIN6EqnSlu1laKyPErx8RTrpTRIasB3i4b4gNYqrJ
OpNsbt8TWAAKfcMJVFHRFspzgJAAjgWtahZRLBS1Xh2ERw607Eu+rsU4SnALIQLDuhqxML
/zAAP/hUH+m8/iD8WR2yDm3cURpT/p6MiHH9UAHB55scC33cyZaXKArufEN5oGhA3MjX08
vheMqF0FcpYSfLF5Az1jSBajezaNNKjk6k7P213wrq77bnRhvAWmeZ3Yyg84X5+SwbQx5A
6/OH/jd2dwAAA9jyP7ZA8j+2QAAAAAdzc2gtcnNhAAABAQDeENuw06QqcCYpe5U40zIQyM
U6z0jdHsK4WOUdeEBkGbKCoiA301FcFZWL9y7EXl1ITdu0ct+YmH6VnmjdIg3oSqdKW7WV
orI8SvHxFOulNEhqwHeLhviA1iqsk6k2xu3xNYAAp9wwlUUdEWynOAkACOBa1qFlEsFLVe
HYRHDrTsS76uxThKcAshAsO6GrEwv/MAA/+FQf6bz+IPxZHbIObdxRGlP+noyIcf1QAcHn
mxwLfdzJlpcoCu58Q3mgaEDcyNfTy+F4yoXQVylhJ8sXkDPWNIFqN7No00qOTqTs/bXfCu
rvtudGG8BaZ5ndjKDzhfn5LBtDHkDr84f+N3Z3AAAAAwEAAQAAAQEA0gYBscDJEDYXcb5n
SGI+hK9OEiv/ztoL8cxpjTCxPbssIO7zJ7ldxojHcqQqgCB7eynf9E9899aqShmTNinCG4
OXqvUHzWkdx3ZeUtOldw72urgjmDvkBf4ISEYzkRId8rbVxEj+MJjM0Awtxg1yPa0ne+Jo
rK5LUFP+kMtSPMjz3L3RsT4W+ewLeFfLkJdsDdMTLFfiLpuZw9MwACOvsy8hPRabLrVQMO
uIn1H6Je9hmN+cZZ+41pImFDa3nC3/RMKy8i2+nTgv9eKG+rXXjfadDBsVnIi0btzhB+Zb
6m6iLd+m6dj9//AXSfFZgvsowd09GO5cgoc4K+DVdbKeWQAAAIBzFVQ58PtZDRbjGl3hDY
0nPDzKdMfG5H1tJkprwVUYnKuRaTyuJHB8yc7PLYtLw64/LpldAR9yYf1h25EgiWGjjCNB
6SyrcdIOUlpGglYabQ7/UmLfzvWddgWNM10faPblrrTo8M6LWlz7FJh9fWbbN73K6plPRx
oYyTBtzTJo1wAAAIEA/ZtqiBovZHkSsyHpFaK599/pFJk+oooT1njazYBuhq49ZmrXKXwD
IJQx25fcVPcloGKVJqeGqQTR84AjOkFScicXdSHzlNLZJaO6xYiHmve3z2bKeOtEo+WFUH
1Nzqf6qEENtqGvQE4598H63fk1JQKgslxeHV6SATM7MHRUsUUAAACBAOApQTGtwlhKo/q2
PlvwetPgXrXUUWnCe8hqALwondtARrZN6eMl3LpwTAln2FWGl6Q1bcN7FETlBbjt/T/PRT
UfT0MlFxZHZE8lMfskM0IHiu5YwWyss7291xLuUQh9NoM0eOqANjdhOb80CdfyYauS96nL
SarwWU34YNfPor6LAAAAIHV3ZXNjaG1pdHRAaWQtc2lzLXZwbi0yOC5ldGh6LmNoAQI=
-----END OPENSSH PRIVATE KEY-----
KEYEND
chmod 0700 id_rsa
# Parse and check command line arguments (cluster, NETHZ username, number of cores, run time limit, memory limit per NUM_CORES)
# check on which cluster the script should run and load the proper python module
CHOSTNAME="euler.ethz.ch"
# no need to do checks on the username. If it is wrong, the SSH commands will not work
USERNAME="${1:-$USER}"
echo -e "NETHZ username: $USERNAME"
# number of cores to be used
NUM_CORES=1
# run time limit
RUN_TIME="04:00"
# memory per core
MEM_PER_CORE=2000
# check if some old files are left from a previous session and delete them
ssh -i id_rsa -T $USERNAME@$CHOSTNAME <<ENDSSH
if [ -f /cluster/home/$USERNAME/jnbinfo ]; then
rm /cluster/home/$USERNAME/jnbinfo
fi
if [ -f /cluster/home/$USERNAME/jnbip ]; then
rm /cluster/home/$USERNAME/jnbip
fi
ENDSSH
EULER_INSTALL_HONE="/cluster/scratch/schmittu/mlw/"
# run the jupyter notebook job on Euler/Leonhard Open and save ip, port and the token
# in the files jnbip and jninfo in the home directory of the user on Euler/Leonhard Open
ssh -i id_rsa $USERNAME@$CHOSTNAME "echo source /etc/bashrc > .bashrc"
# ssh -i id_rsa $USERNAME@$CHOSTNAME bsub -n $NUM_CORES -W $RUN_TIME -R "rusage[mem=$MEM_PER_CORE]" bash -l <<ENDBSUB
ssh -i id_rsa $USERNAME@$CHOSTNAME bsub -J mlw_jupyter -R light <<ENDBSUB
set -x
export XDG_RUNTIME_DIR=
IP_REMOTE="\$(hostname -i)"
echo "Remote IP:\$IP_REMOTE" >> /cluster/home/$USERNAME/jnbip
$EULER_INSTALL_HONE/conda_env/bin/jupyter notebook --notebook-dir \$HOME/mlw --no-browser --ip "\$IP_REMOTE" &> /cluster/home/$USERNAME/jnbinfo
ENDBSUB
# wait until jupyternotebook has started, poll every 10 seconds to check if $HOME/jupyternbinfo exists
# once the file exists and is not empty, the notebook has been startet and is listening
ssh -i id_rsa $USERNAME@$CHOSTNAME "while ! [ -e /cluster/home/$USERNAME/jnbinfo -a -s /cluster/home/$USERNAME/jnbinfo ]; do echo 'Waiting for jupyter notebook to start, sleep for 10 sec'; sleep 10; done"
sleep 1
# get remote ip, port and token from files stored on Euler/Leonhard Open
echo -e "Receiving ip, port and token from jupyter notebook"
remoteip=$(ssh -i id_rsa $USERNAME@$CHOSTNAME "cat /cluster/home/$USERNAME/jnbip | grep -m1 'Remote IP' | cut -d ':' -f 2")
remoteport=$(ssh -i id_rsa $USERNAME@$CHOSTNAME "cat /cluster/home/$USERNAME/jnbinfo | grep -m1 token | cut -d '/' -f 3 | cut -d ':' -f 2")
jnbtoken=$(ssh -i id_rsa $USERNAME@$CHOSTNAME "cat /cluster/home/$USERNAME/jnbinfo | grep -m1 token | cut -d '=' -f 2")
if [[ "$remoteip" == "" ]]; then
echo -e "Error: remote ip is not defined. Terminating script."
echo -e "Please login to the cluster and check with bjobs if the batch job is still running."
exit 1
fi
if [[ "$remoteport" == "" ]]; then
echo -e "Error: remote port is not defined. Terminating script."
echo -e "Please login to the cluster and check with bjobs if the batch job is still running."
exit 1
fi
if [[ "$jnbtoken" == "" ]]; then
echo -e "Error: token for the jupyter notebook is not defined. Terminating script."
echo -e "Please login to the cluster and check with bjobs if the batch job is still running."
exit 1
fi
echo -e "Remote IP address: $remoteip"
echo -e "Remote port: $remoteport"
echo -e "Jupyter token: $jnbtoken"
# get a free port on local computer
echo -e "Determining free port on local computer"
PORTN=$(python -c 'import socket; s=socket.socket(); s.bind(("",0)); print(s.getsockname()[1]); s.close()')
echo -e "Local port: $PORTN"
PATH_TO_SOCKET=/tmp/$USER.$$.ssh_socket
# setup SSH tunnel from local computer to compute node via login node
echo -e "Setting up SSH tunnel for connecting the browser to the jupyter notebook"
echo CONTROL_PATH is $PATH_TO_SOCKET
ssh -i id_rsa $USERNAME@$CHOSTNAME -M -S $PATH_TO_SOCKET -L $PORTN:$remoteip:$remoteport -N -f
# SSH tunnel is started in the background, pause 5 seconds to make sure
# it is established before starting the browser
sleep 5
# save url in variable
nburl=http://localhost:$PORTN/?token=$jnbtoken
echo -e "Starting browser and connecting it to jupyter notebook"
echo -e "Connecting to url "$nburl
if [[ "$OSTYPE" == "linux-gnu" ]]; then
xdg-open $nburl
elif [[ "$OSTYPE" == "darwin"* ]]; then
open $nburl
else
echo -e "Your operating system does not allow to start the browser automatically."
echo -e "Please open $nburl in your browser."
fi
while true; do
echo
echo -n "Enter 'Y' to shutdown notebook: ";
read INPUT;
if test X$INPUT = XY; then
break;
fi;
echo 'invalid input, try again'
done;
ssh -i id_rsa $USERNAME@$CHOSTNAME "bkill -J mlw_jupyter"
ssh -S $PATH_TO_SOCKET -O exit $CHOSTNAME
#! /bin/sh
#
# upload_to_polybox.sh
# Copyright (C) 2019 Uwe Schitt <uwe.schmitt@id.ethz.ch>
#
# Distributed under terms of the MIT license.
#
ssh -i id_rsa -t mlwl07@euler.ethz.ch bash upload_to_polybox_from_euler.sh
#! /bin/bash
#
# upload_to_polybox.sh
# Copyright (C) 2019 Uwe Schitt <uwe.schmitt@id.ethz.ch>
#
# Distributed under terms of the MIT license.
#
echo -n "your netzh acoount name : "
read USER
echo -n "your netzh account password : "
read -s PASSWORD
echo
ARCHIVE=machine_learning_workshop_notebooks.zip
zip -9 -r $ARCHIVE mlw
curl -T $ARCHIVE --user "$USER:$PASSWORD" -sw '%{http_code}' https://polybox.ethz.ch/remote.php/dav/files/$USER/
FAILED=$?
echo
if test $FAILED = "0"; then
echo upload done
else
echo upload failed
fi
#!/bin/bash
set -x
ROOT="/cluster/scratch/schmittu/mlw"
scp ../mlw_packages.yml euler:${ROOT}
MINICONDA_INSTALLER=Miniconda3-latest-Linux-x86_64.sh
ssh euler bash <<EOL
set -x
cd ${ROOT}
test -f $MINICONDA_INSTALLER || wget https://repo.anaconda.com/miniconda/$MINICONDA_INSTALLER
test -f miniconda3 || bash Miniconda3-latest-Linux-x86_64.sh -b -p ./miniconda3
if test -d conda_env; then
miniconda3/bin/conda env update -p conda_env -f mlw_packages.yml;
else
miniconda3/bin/conda env create -p conda_env -f mlw_packages.yml;
fi
EOL
#! /bin/sh
#
# copy_start_jupyter_nb_to_sis_website.sh
# Copyright (C) 2019 Uwe Schitt <uwe.schmitt@id.ethz.ch>
#
# Distributed under terms of the MIT license.
#
cd to_deploy_on_course_computers/
zip installer_mlw.zip start_jupyter_nb.sh upload_to_polybox.sh
scp installer_mlw.zip w3_scientific@scientific.ethz.ch:htdocs
rm installer_mlw.zip
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment