Commit 0dbd4e16 authored by Sebastian Wahl's avatar Sebastian Wahl

Added first version of remote_jupyter_manager.sh

parent 9d0d93e6
#!/bin/bash
# install / startup jupyterlab environment(s)
# Sebastian Wahl 08/2018
#
# tested on ursus/taurus, science0[1,2].geomar.de, bdata2.hlrn.de, nesh-fe1.rz.uni-kiel.de
#
# TODO:
# - add update functionality
# - add option to provide an input file with the tasks to do e.g
# $0 hosts.txt
# with hosts.txt in the form of
# swahl@ursus.geomar.de install
# swahl@science01.geomar.de install
# swahl@science01.geomar.de update
# - add option for matlab kernel?
# - add more documentation to the code
# - make basepath (currently $HOME) flexible
function connect() {
hname=$1
proxyport=$2
ssh -f -D localhost:${proxyport} $hname sleep 15
page=$(ssh $hname "\${HOME}/miniconda3/bin/python \${HOME}/miniconda3/bin/jupyter notebook list" | grep http | awk '{print$1}')
if [[ "$(uname)" == "Darwin" ]]; then
chrome=/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
ls "$chrome"
if [[ -f "$chrome" ]]; then
# TODO: disable warning output
"$chrome" --proxy-server='socks5://localhost:'${proxyport} $page &
else
echo
echo " ERROR: chrome browser needs to be installed. Go to https://www.google.com/chrome/, install Chrome and try again"
echo
fi
else
if which programname >/dev/null; then
chromium-browser --proxy-server="socks5://localhost:${proxyport}" $page
else
echo
echo " ERROR: chrome browser needs to be installed. On Ubuntu try sudo apt-get install chromium-browser and try again."
echo
fi
fi
}
if [[ $# -lt 2 ]] || [[ "$1" == "-h" ]]; then
echo
echo " Usage: need two arguments:"
echo
# TODO: how can I make connections to several remote servers possible at the same time using different tabs"
echo " $0 user@host.example.com start "
echo " to start an existing jupyterlab environment on <user@host.example.com> and connect afterwards"
echo " if jupyterlab is already running on <user@host.example.com> you will be connected with the existing instance."
echo " Close the Chromium Browser before running $0 user@host.example.com start"
echo
echo " $0 user@host.example.com stop "
echo " to stop jupyterlab running on <user@host.example.com>"
echo
echo " $0 user@host.example.com install "
echo " to install a new miniconda environment. (WARNING: takes 5-10 minutes, an existing"
echo " $HOME/miniconda3 will be saved before starting the install process."
echo " It is highly recommended to set up passwordless ssh login before installation."
echo " Run ssh-copy-id user@host.example.com to setup passwordless ssh login and test with"
echo " ssh user@host.example.com afterwards."
echo
exit 1
fi
set -vx
hname=$1
task=$2
proxyport=54321
[[ $# -eq 3 ]] && proxyport=$3
if [[ "$task" == "start" ]] || [[ "$task" == "connect" ]]; then
page=$(ssh $hname "\${HOME}/miniconda3/bin/python \${HOME}/miniconda3/bin/jupyter notebook list" | grep http | awk '{print$1}')
port=$(echo $page | awk -F: '{print$3}' | awk -F/ '{print$1}')
if [[ -z "$port" ]] ; then
echo " No Jupyterlab server running on $hname. Will start a server and connect to it "
# TODO: avoid hitting Ctrl+C --> how to I start this in the backgroud adding & does not work
echo " Wait until you see the URL of the server and then hit Ctrl+C to continue"
ssh $hname "\${HOME}/miniconda3/bin/python \${HOME}/miniconda3/bin/jupyter lab --no-browser --ip 127.0.0.1 "
echo " Jupyterlab on $hname started"
echo
else
echo " Jupyterlab on $hname, port $port already running. Connecting to existing server."
fi
connect $hname $proxyport
elif [[ "$task" == "stop" ]]; then
page=$(ssh $hname "\${HOME}/miniconda3/bin/python \${HOME}/miniconda3/bin/jupyter notebook list" | grep http | awk '{print$1}')
port=$(echo $page | awk -F: '{print$3}' | awk -F/ '{print$1}')
if [[ ! -z "$port" ]] ; then
echo " Jupyterlab on $hname, port $port, will be stopped"
ssh $hname "\${HOME}/miniconda3/bin/python \${HOME}/miniconda3/bin/jupyter notebook stop $port"
else
echo " No Jupyterlab running on $hname"
fi
echo
elif [[ "$task" == "install" ]]; then
ssh $hname bash <<EOF
test -d miniconda3 && mv -v miniconda3 miniconda_$(date +%Y%m%d_%H%M%S)
curl https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -o Miniconda3.sh
bash Miniconda3.sh -b -p \${HOME}/miniconda3
rm -f Miniconda3.sh
source \${HOME}/miniconda3/bin/activate base
conda install jupyterlab nb_conda_kernels --yes
conda create -n py3_std python=3 numpy matplotlib scipy ipykernel git --yes
conda activate py3_std
conda install -c conda-forge cdo netcdf4 xarray --yes
conda install libiconv --yes
conda install -c conda-forge cartopy --yes
EOF
elif [[ "$task" == "update" ]]; then
echo
echo "$0 $hname $task not yet implemented."
echo
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment