Last active
September 27, 2021 22:10
-
-
Save npho/7284cf9f3f9fb2ea816072724d80909b to your computer and use it in GitHub Desktop.
rstudio-server.job
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#SBATCH --account=uwit | |
#SBATCH --partition=compute | |
#SBATCH --time=08:00:00 | |
#SBATCH --signal=USR2 | |
#SBATCH --ntasks=4 | |
#SBATCH --mem=10G | |
#SBATCH --output=/mmfs1/home/%u/rstudio-server.job.%j | |
# Create temp directory for ephemeral content to bind-mount in the container | |
RSTUDIO_TMP=$(python -c 'import tempfile; print(tempfile.mkdtemp())') | |
mkdir -p -m 700 \ | |
${RSTUDIO_TMP}/run \ | |
${RSTUDIO_TMP}/tmp \ | |
${RSTUDIO_TMP}/var/lib/rstudio-server | |
cat > ${RSTUDIO_TMP}/database.conf <<END | |
provider=sqlite | |
directory=/var/lib/rstudio-server | |
END | |
# Set OMP_NUM_THREADS to prevent OpenBLAS (and any other OpenMP-enhanced | |
# libraries used by R) from spawning more threads than the number of processors | |
# allocated to the job. | |
# | |
# Set R_LIBS_USER to a path specific to rocker/rstudio to avoid conflicts with | |
# personal libraries from any R installation in the host environment | |
cat > ${RSTUDIO_TMP}/rsession.sh <<END | |
#!/bin/sh | |
export OMP_NUM_THREADS=${SLURM_JOB_CPUS_PER_NODE} | |
export R_LIBS_USER=$GSCRATCH/$USER/rocker-rstudio/4.0 | |
exec rsession "\${@}" | |
END | |
chmod +x ${RSTUDIO_TMP}/rsession.sh | |
export SINGULARITY_BIND="/gscratch:/gscratch,${RSTUDIO_TMP}/run:/run,${RSTUDIO_TMP}/tmp:/tmp,${RSTUDIO_TMP}/database.conf:/etc/rstudio/database.conf,${RSTUDIO_TMP}/rsession.sh:/etc/rstudio/rsession.sh,${RSTUDIO_TMP}/var/lib/rstudio-server:/var/lib/rstudio-server" | |
# Do not suspend idle sessions. | |
# Alternative to setting session-timeout-minutes=0 in /etc/rstudio/rsession.conf | |
export SINGULARITYENV_RSTUDIO_SESSION_TIMEOUT=0 | |
export SINGULARITYENV_USER=$(id -un) | |
export SINGULARITYENV_PASSWORD=$(openssl rand -base64 15) | |
# get unused socket per https://unix.stackexchange.com/a/132524 | |
# tiny race condition between the python & singularity commands | |
readonly PORT=$(python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()') | |
cat 1>&2 <<END | |
1. SSH tunnel from your workstation using the following command: | |
ssh -N -L 8787:${HOSTNAME}:${PORT} ${SINGULARITYENV_USER}@klone.hyak.uw.edu | |
and point your web browser to http://localhost:8787 | |
2. log in to RStudio Server using the following credentials: | |
user: ${SINGULARITYENV_USER} | |
password: ${SINGULARITYENV_PASSWORD} | |
When done using RStudio Server, terminate the job by: | |
1. Exit the RStudio Session ("power" button in the top right corner of the RStudio window) | |
2. Issue the following command on the login node: | |
scancel -f ${SLURM_JOB_ID} | |
END | |
source /etc/bashrc | |
module load singularity | |
singularity exec --cleanenv $GSCRATCH/$USER/rstudio_4.1.0.sif \ | |
rserver --www-port ${PORT} \ | |
--auth-none=0 \ | |
--auth-pam-helper-path=pam-helper \ | |
--auth-stay-signed-in-days=30 \ | |
--auth-timeout-minutes=0 \ | |
--rsession-path=/etc/rstudio/rsession.sh | |
printf 'rserver exited' 1>&2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment