diff --git a/start_jupyter.py b/start_jupyter.py index a2b61ee..59235c8 100644 --- a/start_jupyter.py +++ b/start_jupyter.py @@ -75,15 +75,7 @@ def _get_lc_nodes(partition: str) -> List[str]: 'broadwl': HOME_MIDWAY, 'kicp': HOME_MIDWAY, } -SHELL_SCRIPT = { - 'lgrandi': 'start_notebook_midway3.sh', - 'build': 'start_notebook_midway3.sh', - 'caslake': 'start_notebook_midway3.sh', - 'dali': 'start_notebook_dali.sh', - 'xenon1t': 'start_notebook_midway2.sh', - 'broadwl': 'start_notebook_midway2.sh', - 'kicp': 'start_notebook_midway2.sh', -} +SHELL_SCRIPT = 'start_notebook.sh' def printflush(x): """Does print(x, flush=True), also in python 2.x""" @@ -248,8 +240,8 @@ def main(): s_container = 'xenonnt-%s.simg' % args.tag batch_job = JOB_HEADER + \ "{env_starter}/{script} " \ - "{s_container} {jupyter} {nbook_dir}".format(env_starter=ENVSTARTER_PATH, - script=SHELL_SCRIPT[args.partition], + "{s_container} {jupyter} {nbook_dir} {partition}".format(env_starter=ENVSTARTER_PATH, + script=SHELL_SCRIPT, s_container=s_container, jupyter=args.jupyter, nbook_dir=args.notebook_dir, diff --git a/start_notebook.sh b/start_notebook.sh new file mode 100755 index 0000000..76ee26e --- /dev/null +++ b/start_notebook.sh @@ -0,0 +1,104 @@ +#!/bin/bash + +IMAGE_NAME=$1 +JUPYTER_TYPE=$2 +NOTEBOOK_DIR=$3 +PARTITION=$4 + +IMAGE_DIRS=("/project/lgrandi/xenonnt/singularity-images" "/project2/lgrandi/xenonnt/singularity-images" "/dali/lgrandi/xenonnt/singularity-images") + +CONTAINER="" +# If we passed the full path to an image and it exists, use that +if [ -e "${IMAGE_NAME}" ]; then + CONTAINER="${IMAGE_NAME}" +else + # Loop over the list of image directories + for IMAGE_DIR in "${IMAGE_DIRS[@]}"; do + # Check if the image exists in the current directory + if [ -e "${IMAGE_DIR}/${IMAGE_NAME}" ]; then + CONTAINER="${IMAGE_DIR}/${IMAGE_NAME}" + break + fi + done +fi +# if no container found, throw an error +if [ -z "${CONTAINER}" ]; then + echo "Error: Singularity image not found. Please provide a valid image name or path." + exit 1 +fi + +if [ "x${JUPYTER_TYPE}" = "x" ]; then + JUPYTER_TYPE='lab' +fi + +echo "Using singularity image: ${CONTAINER}" + +PORT=$((15000 + (RANDOM %= 5000))) + +if [[ "$PARTITION" == "lgrandi" || "$PARTITION" == "build" || "$PARTITION" == "caslake" ]]; then + SINGULARITY_CACHEDIR=/scratch/midway3/$USER/singularity_cache + SSH_HOST="midway3.rcc.uchicago.edu" + BIND_OPTS=("--bind /project2" "--bind /scratch/midway3/$USER" "--bind /project/lgrandi" "--bind /project/lgrandi/xenonnt/dali:/dali") +elif [[ "$PARTITION" == "dali" ]]; then + SINGULARITY_CACHEDIR=/dali/lgrandi/$USER/singularity_cache + SSH_HOST="dali-login2.rcc.uchicago.edu" + BIND_OPTS=("--bind /dali" "--bind /dali/lgrandi/xenonnt/xenon.config:/project2/lgrandi/xenonnt/xenon.config" "--bind /dali/lgrandi/grid_proxy/xenon_service_proxy:/project2/lgrandi/grid_proxy/xenon_service_proxy") +else + SINGULARITY_CACHEDIR=/scratch/midway2/$USER/singularity_cache + SSH_HOST="midway2.rcc.uchicago.edu" + BIND_OPTS=("--bind /project2" "--bind /cvmfs" "--bind /project" "--bind /scratch/midway3/$USER" "--bind /scratch/midway2/$USER" "--bind /project2/lgrandi/xenonnt/dali:/dali") +fi + +# script to run inside container +DIR=$PWD +INNER=.singularity_inner +cat >$INNER <&1 + +jupyter ${JUPYTER_TYPE} --no-browser --port=$PORT --ip=\$JUP_HOST --notebook-dir ${NOTEBOOK_DIR} 2>&1 +EOF +chmod +x $INNER + +if [[ "$PARTITION" == "dali" ]]; then + export XENON_CONFIG=/dali/lgrandi/xenonnt/xenon.config +elif [[ "$PARTITION" == "lgrandi" || "$PARTITION" == "build" || "$PARTITION" == "caslake" ]]; then + export XENON_CONFIG=/project/lgrandi/xenonnt/xenon.config +fi + +module load singularity + +# Initialize an empty string to store the singularity exec command +SINGULARITY_COMMAND="singularity exec" + +# Check each bind path and add valid ones to the command string +for bind_opt in "${BIND_OPTS[@]}"; do + bind_path=$(echo "$bind_opt" | cut -d':' -f1 | sed 's/--bind //') + if [ -e "$bind_path" ]; then + SINGULARITY_COMMAND+=" $bind_opt" + else + echo "Warning: Bind path '$bind_path' does not exist. Skipping." + fi +done + +# Append the container and script paths to the command string +SINGULARITY_COMMAND+=" $CONTAINER $DIR/$INNER" + +# Execute the singularity command using the string +eval "$SINGULARITY_COMMAND" \ No newline at end of file diff --git a/start_notebook_dali.sh b/start_notebook_dali.sh deleted file mode 100755 index 8d1bf40..0000000 --- a/start_notebook_dali.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash - -IMAGE_NAME=$1 -JUPYTER_TYPE=$2 -NOTEBOOK_DIR=$3 - -IMAGE_DIR='/dali/lgrandi/xenonnt/singularity-images' - -# if we passed the full path to an image, use that -if [ -e ${IMAGE_NAME} ]; then - CONTAINER=${IMAGE_NAME} -# otherwise check if the name exists in the standard image directory -elif [ -e ${IMAGE_DIR}/${IMAGE_NAME} ]; then - CONTAINER=${IMAGE_DIR}/${IMAGE_NAME} -# if not any of those, throw an error -else - echo "We could not find the container at its full path ${IMAGE_NAME} or in ${IMAGE_DIR}. Exiting." - exit 1 -fi - -if [ "x${JUPYTER_TYPE}" = "x" ]; then - JUPYTER_TYPE='lab' -fi - -echo "Using singularity image: ${CONTAINER}" - -PORT=$(( 15000 + (RANDOM %= 5000) )) -SINGULARITY_CACHEDIR=/dali/lgrandi/$USER/singularity_cache - -# script to run inside container -INNER=/dali/lgrandi/$USER/.singularity_inner -cat > $INNER << EOF -#!/bin/bash -JUP_HOST=\$(hostname -i) -## print tunneling instructions -echo -e " - Copy/Paste this in your local terminal to ssh tunnel with remote - ----------------------------------------------------------------- - ssh -N -f -L localhost:$PORT:\$JUP_HOST:$PORT ${USER}@dali-login2.rcc.uchicago.edu - ----------------------------------------------------------------- - - Then open a browser on your local machine to the following address - ------------------------------------------------------------------ - localhost:$PORT - ------------------------------------------------------------------ - and use the token that appears below to login. - - OR replace "$ipnip" in the address below with "localhost" and copy - to your local browser. - " 2>&1 - -jupyter ${JUPYTER_TYPE} --no-browser --port=$PORT --ip=\$JUP_HOST --notebook-dir ${NOTEBOOK_DIR} 2>&1 -EOF -chmod +x $INNER -export XENON_CONFIG=/dali/lgrandi/xenonnt/xenon.config - -module load singularity -singularity exec --bind /dali --bind /dali/lgrandi/xenonnt/xenon.config:/project2/lgrandi/xenonnt/xenon.config --bind /dali/lgrandi/grid_proxy/xenon_service_proxy:/project2/lgrandi/grid_proxy/xenon_service_proxy $CONTAINER $INNER diff --git a/start_notebook_midway2.sh b/start_notebook_midway2.sh deleted file mode 100755 index af24b40..0000000 --- a/start_notebook_midway2.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash - -IMAGE_NAME=$1 -JUPYTER_TYPE=$2 -NOTEBOOK_DIR=$3 - -IMAGE_DIR='/project2/lgrandi/xenonnt/singularity-images' - -# if we passed the full path to an image, use that -if [ -e ${IMAGE_NAME} ]; then - CONTAINER=${IMAGE_NAME} -# otherwise check if the name exists in the standard image directory -elif [ -e ${IMAGE_DIR}/${IMAGE_NAME} ]; then - CONTAINER=${IMAGE_DIR}/${IMAGE_NAME} -# if not any of those, throw an error -else - echo "We could not find the container at its full path ${IMAGE_NAME} or in ${IMAGE_DIR}. Exiting." - exit 1 -fi - -if [ "x${JUPYTER_TYPE}" = "x" ]; then - JUPYTER_TYPE='lab' -fi - -echo "Using singularity image: ${CONTAINER}" - -PORT=$(( 15000 + (RANDOM %= 5000) )) -SINGULARITY_CACHEDIR=/scratch/midway2/$USER/singularity_cache - -# script to run inside container -DIR=$PWD -INNER=.singularity_inner -cat > $INNER << EOF -#!/bin/bash -JUP_HOST=\$(hostname -i) -## print tunneling instructions -echo -e " - Copy/Paste this in your local terminal to ssh tunnel with remote - ----------------------------------------------------------------- - ssh -N -f -L localhost:$PORT:\$JUP_HOST:$PORT ${USER}@midway2.rcc.uchicago.edu - ----------------------------------------------------------------- - - Then open a browser on your local machine to the following address - ------------------------------------------------------------------ - localhost:$PORT - ------------------------------------------------------------------ - and use the token that appears below to login. - - OR replace "$ipnip" in the address below with "localhost" and copy - to your local browser. - " 2>&1 - -jupyter ${JUPYTER_TYPE} --no-browser --port=$PORT --ip=\$JUP_HOST --notebook-dir ${NOTEBOOK_DIR} 2>&1 -EOF -chmod +x $INNER - -module load singularity -singularity exec --bind /project2 --bind /cvmfs --bind /project --bind /scratch/midway3/$USER --bind /scratch/midway2/$USER --bind /project2/lgrandi/xenonnt/dali:/dali $CONTAINER $DIR/$INNER diff --git a/start_notebook_midway3.sh b/start_notebook_midway3.sh deleted file mode 100755 index 1d64e63..0000000 --- a/start_notebook_midway3.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash - -IMAGE_NAME=$1 -JUPYTER_TYPE=$2 -NOTEBOOK_DIR=$3 - -IMAGE_DIR='/project/lgrandi/xenonnt/singularity-images' - -# if we passed the full path to an image, use that -if [ -e ${IMAGE_NAME} ]; then - CONTAINER=${IMAGE_NAME} -# otherwise check if the name exists in the standard image directory -elif [ -e ${IMAGE_DIR}/${IMAGE_NAME} ]; then - CONTAINER=${IMAGE_DIR}/${IMAGE_NAME} -# if not any of those, throw an error -else - echo "We could not find the container at its full path ${IMAGE_NAME} or in ${IMAGE_DIR}. Exiting." - exit 1 -fi - -if [ "x${JUPYTER_TYPE}" = "x" ]; then - JUPYTER_TYPE='lab' -fi - -echo "Using singularity image: ${CONTAINER}" - -PORT=$(( 15000 + (RANDOM %= 5000) )) -SINGULARITY_CACHEDIR=/scratch/midway3/$USER/singularity_cache - -# script to run inside container -DIR=$PWD -INNER=.singularity_inner -cat > $INNER << EOF -#!/bin/bash -JUP_HOST=\$(hostname -i) -## print tunneling instructions -echo -e " - Copy/Paste this in your local terminal to ssh tunnel with remote - ----------------------------------------------------------------- - ssh -N -f -L localhost:$PORT:\$JUP_HOST:$PORT ${USER}@midway3.rcc.uchicago.edu - ----------------------------------------------------------------- - - Then open a browser on your local machine to the following address - ------------------------------------------------------------------ - localhost:$PORT - ------------------------------------------------------------------ - and use the token that appears below to login. - - OR replace "$ipnip" in the address below with "localhost" and copy - to your local browser. - " 2>&1 - -jupyter ${JUPYTER_TYPE} --no-browser --port=$PORT --ip=\$JUP_HOST --notebook-dir ${NOTEBOOK_DIR} 2>&1 -EOF -chmod +x $INNER - -module load singularity -singularity exec --bind /project2 --bind /scratch/midway3/$USER --bind /project/lgrandi $CONTAINER $DIR/$INNER