Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
utilities_for_rstudio_server/rstudio-server.launcher.sh
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
250 lines (202 sloc)
7.85 KB
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
#! /usr/bin/bash | |
# @ID@ # | |
PREFIX=@PREFIX@ | |
CONTAINER=@CONTAINER@ | |
# ------------------------------------------------------------------------- | |
function show_help() { | |
echo -e "\n $0 information:\n" | |
cat << __EOF | |
This is a containerized setup for rstudio-server, allowing you to run | |
rstudio on a public server and use it remotely in your web browser. | |
To avoid the risk that someone else is using your session, you will be | |
asked for your login name and a password. The password is generated | |
per session, and is shown in the terminal window when you start the | |
server. | |
Also please do not forget to stop the server with 'rstudio-server stop' | |
when you are done (just closing the browser will _not_ do the job). | |
Available commands are: | |
----------------------- | |
start that is indeed the default action, you do not | |
need to enter it. | |
stop this will stop your server and free the resources. | |
inspect you can use this to explore the container you are running, | |
probably only useful for trouble shooting. | |
force_cleanup this will kill any running rstudio-server and cleans your | |
session directory, use this when the server or R has crashed | |
and you can not start or reconnect again. | |
A shortcut is to use 'zap'. | |
help shows the page you are reading now (-h, --help also works). | |
__EOF | |
} | |
function apptainer_exec() { | |
container="$1"; shift | |
# Note for the unwary: USER_SERVER_BASE is _only_ set when the server is started, | |
# not when just inspecting! | |
# echo \ | |
apptainer exec \ | |
-B /usr:/usr \ | |
-B /pkg:/pkg \ | |
-B /project:/project \ | |
-B /scratch:/scratch \ | |
-B $USER_SERVER_BASE/var/lib:/var/lib \ | |
-B $USER_SERVER_BASE/var/run:/var/run \ | |
"$container" $@ | |
} | |
# ------------------------------------------------------------------------- | |
USER_SERVER_INFO_DIR=$HOME/.rstudio-server-app | |
FIRST_RUN= | |
STOP_SERVER= | |
# Mariux default for TMPDIR is /scratch/local | |
TMPDIR=${TMPDIR:-/scratch/local} | |
if [ "$TMPDIR" = "/scratch/local" ]; then | |
TMPDIR=/scratch/local2/$USER | |
fi | |
mkdir -p $TMPDIR | |
if [ ! -d $USER_SERVER_INFO_DIR ]; then | |
FIRST_RUN=1 | |
fi | |
mkdir -p $USER_SERVER_INFO_DIR | |
echo "[$(date +%c)] ${HOSTNAME%%.*} ${1:-START}" >> $USER_SERVER_INFO_DIR/info.log | |
case $1 in | |
force_cleanup|zap) | |
killall rserver | |
rm -rf $TMPDIR/rstudio-server-app/session_data | |
echo -e "\n# Note: Removed your rservers and/or session data ...\n" | |
exit 0 | |
;; | |
inspect) | |
apptainer inspect $PREFIX/libexec/$CONTAINER | |
echo -e "\n# Note: Leave the container with exit / [Ctrl]-[D]\n" | |
apptainer_exec $PREFIX/libexec/$CONTAINER /usr/bin/bash | |
exit 0 | |
;; | |
start) # default, so do nothing here | |
: | |
;; | |
stop) | |
STOP_SERVER=1 | |
;; | |
-h|--help|help) | |
show_help | |
exit 0 | |
;; | |
esac | |
if [ -e $TMPDIR/rstudio-server-app/session_data/rss.pid ]; then | |
pid=$(cat $TMPDIR/rstudio-server-app/session_data/rss.pid) | |
if [ -e /proc/$pid ]; then | |
if [ -r /proc/$pid/exe ]; then | |
# echo "# $pid present - need to stop the server ..." >&2 | |
exe=$(readlink /proc/$pid/exe) | |
# this is the path inside the container | |
if [ "$exe" = "/apps/rstudio-server/bin/rserver" ]; then | |
if [ -n "$STOP_SERVER" ]; then | |
echo -e "\n Note: Stopping rstudio-server (pid:$pid) ...\n" | |
kill $pid | |
exit 0 | |
else | |
echo "# Note: A rstudio-server is running here already" >&2 | |
fi | |
else | |
echo "# Note: The task '$exe' runs under the expected pid now, and not rstudio-server ..." >&2 | |
echo "# Run 'rstudio-server force_cleanup' to remove old session data." | |
fi | |
else | |
echo "# Note: Process with pid $pid does no longer belong to you ..." >&2 | |
echo "# Run 'rstudio-server force_cleanup' to remove old session data." | |
fi | |
exit 1 | |
fi | |
fi | |
if [ -n "$STOP_SERVER" ]; then | |
echo "# Error: no running server found here." >&2 | |
exit 1 | |
fi | |
rm -rf $TMPDIR/rstudio-server-app | |
mkdir -p -m 0700 $TMPDIR/rstudio-server-app | |
USER_SERVER_BASE=$TMPDIR/rstudio-server-app/session_data # will be set to 0777 by rstudio-server ... | |
USER_SERVER_INFO_DIR=$HOME/.rstudio-server-app | |
SOCKET_NAME_CHECK="$USER_SERVER_BASE/rstudio-rserver/session-server-rpc.socket" | |
if [ ${#SOCKET_NAME_CHECK} -ge 108 ]; then | |
echo "ERROR: Name of rstudio-server socket file will exceed the 108 character limit (man 7 unix)." >&2 | |
echo " You probably need to use a shorter TMPDIR ($TMPDIR [${#TMPDIR}])" >&2 | |
exit 2 | |
fi | |
# our 'normal' mechanics for the R binary does not work here, let ptype do the job | |
MAIN_R_PROGRAM=${R_CUSTOM:-/usr/bin/R} | |
if ! [ -e "$MAIN_R_PROGRAM" ]; then | |
MAIN_R_PROGRAM=$(ptype R | awk '{print($3)}') | |
if ! [ -e "$MAIN_R_PROGRAM" ]; then | |
echo "# Error: Can not find the R executable (tried on '$MAIN_R_PROGRAM')." | |
exit 1 | |
fi | |
fi | |
echo -e "\n R executable set to: $MAIN_R_PROGRAM\n" | |
# prepare server start ... | |
# create a one-time password | |
# 17 chars are a bit unfair if people don't do copy and paste | |
RSTUDIO_PASSWORD='******************************' | |
RSTUDIO_PASSWORD=$(openssl rand -base64 12 | colrm 12) | |
# also make it clickable ... | |
RSTUDIO_PASSWORD=${RSTUDIO_PASSWORD//[+\/=]/3} | |
# ... and this is passed to 'auth-pam-helper' | |
PASSWORD_DIGEST=$(echo $RSTUDIO_PASSWORD | /usr/bin/sha256sum) | |
PASSWORD_DIGEST=${PASSWORD_DIGEST%% *} | |
echo -e "\n Your password for this session is: '$RSTUDIO_PASSWORD'\n" | |
RSTUDIO_PASSWORD='******************************' | |
unset RSTUDIO_PASSWORD | |
export PASSWORD_DIGEST | |
LOWER_PORT=28787 | |
UPPER_PORT=38787 | |
RSTUDIO_SERVER_PORT=$LOWER_PORT | |
while :; do | |
# is it free? | |
ss -ltn | grep -q ":$RSTUDIO_SERVER_PORT " || break | |
RSTUDIO_SERVER_PORT=$(( RSTUDIO_SERVER_PORT+1 )) | |
if [ "$RSTUDIO_SERVER_PORT" -gt "$UPPER_PORT" ]; then | |
RSTUDIO_SERVER_PORT=$LOWER_PORT | |
fi | |
sleep 0.15 | |
done | |
echo -e " Browser address is: http://$HOSTNAME:$RSTUDIO_SERVER_PORT" | |
echo -e " or: http://localhost:8787 (when at home)\n" | |
echo -e " Note for home-users:\n" | |
echo -e " to tunnel the server connection to port 8787 on your local computer, enter\n" | |
echo -e " ssh -L 8787:$HOSTNAME:$RSTUDIO_SERVER_PORT $USER@geniux.molgen.mpg.de\n" | |
if [ -n "$FIRST_RUN" ]; then | |
echo -e " ------------------------------------------------------------------\n" | |
echo -e " Note: First time run of rstudio-server ...\n" | |
echo -e " Please mind to stop the server from the console with\n" | |
echo -e " rstudio-server stop\n" | |
echo -e " when you are done.\n" | |
echo -e " Hit [Enter] to start now, or [Ctrl]-[C] and eventually take a" | |
echo -e " look at the help page with 'rstudio-server help' ...\n" | |
echo -e " ------------------------------------------------------------------" | |
read junk | |
fi | |
# --------------------------------------------- finally start this thing -- | |
mkdir -p $USER_SERVER_BASE/var/lib | |
mkdir -p $USER_SERVER_BASE/var/run | |
apptainer_exec \ | |
$PREFIX/libexec/$CONTAINER \ | |
\ | |
/apps/rstudio-server/bin/rserver \ | |
--server-working-dir $USER_SERVER_BASE \ | |
--server-data-dir $USER_SERVER_BASE \ | |
--database-config-file $USER_SERVER_BASE/dbconf \ | |
--server-user $USER \ | |
--www-port $RSTUDIO_SERVER_PORT \ | |
--www-address $HOSTNAME \ | |
--rsession-which-r $MAIN_R_PROGRAM \ | |
--auth-minimum-user-id 100 \ | |
--auth-none 0 \ | |
--auth-pam-helper-path $PREFIX/bin/rstudio-server.pam-helper.sh \ | |
--server-pid-file $USER_SERVER_BASE/rss.pid \ | |
--server-daemonize 1 | |
sleep 0.3 | |
if [ -e $USER_SERVER_BASE/rss.pid ]; then | |
pid=$(cat $USER_SERVER_BASE/rss.pid) | |
echo -e "\n Note: rstudio-server started (pid:$pid) ...\n" | |
else | |
echo "# Warning: Could not figure out the process id, check if rstudio-server/rserver is running." >&2 | |
fi | |
# ------------------------------------------------------------------------- |