Skip to content

Instantly share code, notes, and snippets.

@gatagat
Created November 28, 2018 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatagat/b2a512b6aff17b9c72ca1e7a28206b8b to your computer and use it in GitHub Desktop.
Save gatagat/b2a512b6aff17b9c72ca1e7a28206b8b to your computer and use it in GitHub Desktop.
Jupyterhub on II2
#!/bin/bash
MODULES=(
ipython/.5.3.0-foss-2017a-python-2.7.13 \
ipywidgets/7.2.1-foss-2017a-python-2.7.13 \
matplotlib/2.2.2-foss-2017a-python-2.7.13 \
pillow/5.1.0-foss-2017a-python-2.7.13 \
av/6.0.0-foss-2017a-python-2.7.13 \
scipy/1.0.1-foss-2017a-python-2.7.13 \
scikit-learn/0.19.1-foss-2017a-python-2.7.13 \
scikit-image/0.14.0-foss-2017a-python-2.7.13 \
tifffile/0.12.1-foss-2017a-python-2.7.13 \
h5py/2.7.1-foss-2017a-python-2.7.13 \
pyyaml/3.12-foss-2017a-python-2.7.13 \
lap/0.3.0-foss-2017a-python-2.7.13 \
psutil/5.4.1-foss-2017a-python-2.7.13 \
keras/2.1.4-foss-2017a-python-2.7.13 \
tensorflow/1.5.0-foss-2017a-python-2.7.13 \
opencv/3.4.1-foss-2017a-python-2.7.13 \
numba/0.37.0-foss-2017a-python-2.7.13 \
gcccore/6.3.0 \
)
KERNEL_BASE_DIR=~/.local/share/jupyter/kernels/
mkdir -p $KERNEL_BASE_DIR 2>/dev/null || true
TIMESTAMP=$(date +%Y%m%d%H%M%S)
KERNEL_NAME=analysis-$TIMESTAMP
shift
command pushd $KERNEL_BASE_DIR >/dev/null
if ! mkdir "$KERNEL_NAME" 2>/dev/null; then
echo 2>&2 "Error: Kernel with name \"$KERNEL_NAME\" already exists in $KERNEL_BASE_DIR"
exit 1
fi
cd "$KERNEL_NAME"
cat > "setup-$KERNEL_NAME.sh" <<XXX
#!/bin/bash
#
# Automatically generated script to setup kernel $KERNEL_NAME.
#
ml reset
ml use /groups/pauli/software/171020-ii2/modules/all/
cd $KERNEL_BASE_DIR
mkdir $KERNEL_NAME 2>/dev/null || true
cd $KERNEL_NAME
for module in ${MODULES[@]}; do
echo "Loading module \$module"
module --ignore-cache load "\$module"
done
echo
PYTHON=\$(which python)
PY_DIR=\$(dirname \$(dirname \$(which python)))
PY_VER_SHORT=\$(python -V 2>&1 | cut -d\ -f2 | cut -f-2 -d.)
echo "Setting up kernel $KERNEL_NAME"
cat > "kernel.json" <<YYY
{
"display_name": "$KERNEL_NAME",
"language": "python",
"argv": [
"\$PYTHON",
"-m",
"ipykernel",
"-f",
"{connection_file}"
],
"env": {
"PATH": "\$PATH",
"LD_LIBRARY_PATH": "\$LD_LIBRARY_PATH",
"PYTHONPATH": "\$PYTHONPATH",
"PYTHON_LIBRARY_DIR": "\$PY_DIR/lib",
"PYTHON_LIBRARY": "\$PY_DIR/lib/libpython\$PY_VER_SHORT.so"
}
}
YYY
XXX
chmod +x setup-$KERNEL_NAME.sh
./setup-$KERNEL_NAME.sh
command popd >/dev/null
#!/bin/bash
#
# Run jupyterhub instance inside the current directory. This can be connected
# to through a browser and used to edit and run IPython notebooks (.ipynb).
#
set -eu -o pipefail
IP=$(dig +short $(hostname).ii2.imp.ac.at A)
if [ -z "$IP" ]; then
echo >&2 "Failed to detect local IP address."
exit 1
fi
function check_port () {
IP=$1
PORT=$2
bash -c "echo >/dev/tcp/$IP/$PORT" 2>/dev/null
return $?
}
PORT=61616
while check_port $IP $PORT && check_port $IP $(expr $PORT + 2); do
echo "Port $PORT or $(expr $PORT + 2) are not available"
# FIXME: dont know the exact range that is allowed
PORT=$(shuf -i 60000-65500 -n1)
echo $PORT...
done
echo "Using ports $PORT and $(expr $PORT + 2)"
GRP=pauli
WORK_DIR="$(pwd)"
JH_MODULE=jupyterhub/0.8.1-foss-2017a-python-3.6.4
JH_DIR="$JH_MODULE"
mkdir -p "$JH_DIR"
ml reset
ml use /groups/$GRP/software/171020-ii2/modules/all
ml $JH_MODULE
ml widgetsnbextension/3.2.1-foss-2017a-python-3.6.4
jupyter nbextension install widgetsnbextension --user --py
jupyter nbextension enable widgetsnbextension --user --py
IP_KEY="$JH_DIR/$IP.key"
IP_CERT="$JH_DIR/$IP.cert"
CONFIG="$JH_DIR/$IP-config.py"
if [ ! -e "$IP_KEY" ]; then
echo "No key for this IP ($IP) and this version of Python, generating..."
openssl genrsa -out "$IP_KEY" 2048 \
&& openssl req -new -x509 -key "$IP_KEY" -out "$IP_CERT" -days 3650 -subj "/CN=$IP" \
&& echo "Done."
fi
cat >"$CONFIG" <<-HERE
c.Authenticator.admin_users = {'$USER'}
c.Authenticator.whitelist = {'$USER'}
c.JupyterHub.cookie_secret_file = '$JH_DIR/cookie_secret'
c.JupyterHub.db_url = '$JH_DIR/sqlite'
c.JupyterHub.extra_log_file = '$JH_DIR/extra_log'
c.JupyterHub.pid_file = '$JH_DIR/pid'
c.JupyterHub.hub_port = $(expr $PORT + 2)
c.ConfigurableHTTPProxy.api_url = 'http://127.0.0.1:$(expr $PORT + 1)'
c.Spawner.notebook_dir = '$WORK_DIR'
c.Spawner.env_keep = ['PATH', 'PYTHONPATH', 'LD_LIBRARY_PATH', 'VIRTUAL_ENV', 'LANG', 'LC_ALL', 'JAVA_HOME']
HERE
cd $WORK_DIR
export CONFIGPROXY_AUTH_TOKEN=`openssl rand -hex 32`
jupyterhub --ip "$IP" --port "$PORT" --ssl-key "$IP_KEY" --ssl-cert "$IP_CERT" --config "$CONFIG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment