UCSF-CBI / rstudio-server-controller

RStudio Server Controller (RSC) - A Tool for Launching a Personal Instance of the RStudio Server
https://github.com/UCSF-CBI/rstudio-server-controller
ISC License
4 stars 2 forks source link

Launch rsc via scheduler and have the job open up the port on submit host via a SSH reverse tunnel #35

Closed HenrikBengtsson closed 2 years ago

HenrikBengtsson commented 2 years ago

Background

Submitting the following job script:

{c4-log1}$ port=$(python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')}
{c4-log1}$ echo "${port}"
43211
{c4-log1}$ sbatch rsc.sh 43211

will make the RStudio Server available from the submit host on port 43211.

This is an interesting alternative that works when one cannot SSH to a compute node, but one can SSH from a compute node back to the submit host.

#! /usr/bin/env bash
#SBATCH --nodes=1

## Port on submit host
port=${1:?}

module load CBI rstudio-server-controller

## Find an available port on local machine?
rsc_port=$(python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')}
echo "rsc_port=${rsc_port}"

## Launch RStudio Server in the background
rsc start --port="${rsc_port}" &

## SSH back to submit host opening up port via a reverse tunnel
ssh -N -R "${port}:localhost:${rsc_port}" "${SLURM_SUBMIT_HOST}"

Suggestion

Add options to rsc start to launch that reverse SSH connection, e.g.

rsc start --port=random --reverse-hostname="${SLURM_SUBMIT_HOST}" --reverse-port="${port}"
HenrikBengtsson commented 2 years ago

With the new rsc wait (#36), the above can be replaced by:

#! /usr/bin/env bash
#SBATCH --nodes=1

## Port on submit host
port=${1:?}

host=${2:${SLURM_SUBMIT_HOST}}
[[ -z ${host} ]] && { echo &>2 "ERROR: Failed to infer host"; exit 1; }

module load CBI rstudio-server-controller

## Launch RStudio Server in the background
rsc start --port=random &

## Which port did rsc produce?
rsc_port=$(rsc wait | tail -1)

## SSH back to submit host opening up port via a reverse tunnel
ssh -N -R "${port}:localhost:${rsc_port}" "${host}"
HenrikBengtsson commented 2 years ago

A simpler interface would be

rsc start --port=random --reverse-tunnel="${SLURM_SUBMIT_HOST}:${port}"
HenrikBengtsson commented 2 years ago

Implemented prototypical support for:

rsc start --port=random --reverse-tunnel="${SLURM_SUBMIT_HOST}:${port}"

I've tested it on C4 and on Wynton, where it works.