Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto check binding #56

Merged
merged 7 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions start_jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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,
Expand Down
102 changes: 102 additions & 0 deletions start_notebook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/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 <<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}@${SSH_HOST}
-----------------------------------------------------------------

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

if [[ "$PARTITION" == "dali" ]]; then
yuema137 marked this conversation as resolved.
Show resolved Hide resolved
export XENON_CONFIG=/dali/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"
58 changes: 0 additions & 58 deletions start_notebook_dali.sh

This file was deleted.

58 changes: 0 additions & 58 deletions start_notebook_midway2.sh

This file was deleted.

58 changes: 0 additions & 58 deletions start_notebook_midway3.sh

This file was deleted.