From 381a0bb275fe6a4136869f8e7592d332cf669d28 Mon Sep 17 00:00:00 2001
From: David Hoese <david.hoese@ssec.wisc.edu>
Date: Sun, 3 Feb 2019 17:00:37 +0000
Subject: [PATCH] Update scripts to find unused ports

---
 README.md                |  1 +
 start_jupyter_lab.sbatch | 18 +++++++++++++-----
 start_jupyter_lab.sh     |  4 ++--
 3 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index f904a21..6287db6 100644
--- a/README.md
+++ b/README.md
@@ -57,3 +57,4 @@ And use the password set up during installation.
   server and 8787 for dask clients by default.
 * Jupyter lab does not currently allow connecting to unix sockets (sockets
   with a pathname). See https://github.com/jupyter/notebook/issues/2503.
+
diff --git a/start_jupyter_lab.sbatch b/start_jupyter_lab.sbatch
index 2f29cd9..91a6ab2 100644
--- a/start_jupyter_lab.sbatch
+++ b/start_jupyter_lab.sbatch
@@ -10,14 +10,22 @@
 module purge
 
 oops() {
-	echo "OOPS: $*"
-	exit 1
+    echo "OOPS: $*"
+    exit 1
 }
 
 #export HDF5_USE_FILE_LOCKING="FALSE"
 #export PYTROLL_CHUNK_SIZE="2048"
 CONDA_ENV=/home/davidh/miniconda3
-
 source $CONDA_ENV/bin/activate pangeo
-echo "ssh -N -L 8888:`hostname`:8888 -L 8787:`hostname`:8787 $USER@iris" | tee -a ${HOME}/.jupyter_lab_connect.txt
-jupyter lab --no-browser --ip=`hostname` --port=8888
+
+# Hack to find two useable ports
+PORTS=$(python -c 'import socket; s1=socket.socket(); s2=socket.socket(); s1.bind(("", 0)); s2.bind(("", 0)); print(s1.getsockname()[1], s2.getsockname()[1]); s1.close(); s2.close()')
+export JLAB_PORT=$(echo $PORTS | cut -d' ' -f1)
+export DASK_PORT=$(echo $PORTS | cut -d' ' -f2)
+
+# Write out a usable SSH command to connect to jupyter lab
+echo "ssh -N -L 8888:`hostname`:${JLAB_PORT} -L 8787:`hostname`:${DASK_PORT} $USER@iris" | tee -a ${HOME}/.jupyter_lab_connect.txt
+# Start jupyter lab
+jupyter lab --no-browser --ip=`hostname` --port ${JLAB_PORT}
+
diff --git a/start_jupyter_lab.sh b/start_jupyter_lab.sh
index f5dc23e..f301c93 100755
--- a/start_jupyter_lab.sh
+++ b/start_jupyter_lab.sh
@@ -10,7 +10,7 @@ CONNECT_FILE="${HOME}/.jupyter_lab_connect.txt"
 rm -f $CONNECT_FILE
 
 # Tell slurm to run jupyter lab and write ssh command to CONNECT_FILE
-sbatch --quiet "${SCRIPT_HOME}/start_jupyter_lab.sbatch"
+sbatch --quiet "${SCRIPT_HOME}/start_jupyter_lab.sbatch" > /dev/null 2>&1
 
 # Wait for slurm to start the job on a compute node
 while [ ! -f $CONNECT_FILE ]; do
@@ -19,7 +19,7 @@ while [ ! -f $CONNECT_FILE ]; do
 done
 
 # Wait for jupyter lab to completely start
-echo "Job started, waiting 5 seconds for jupyter lab to start..."
+echo "Job started, waiting 5 seconds for jupyter lab to start..." >&2
 sleep 5
 
 # Now that we know it has started print out the SSH command the user should
-- 
GitLab