Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjkw31 committed Sep 12, 2024
0 parents commit 3411849
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
addRStudio
==========

addRStudio adds `rstudio` server files to an existing singularity image.

Requirements
============

Requires [dir2singularity](https://github.com/wtsi-hgi/dir2singularity) installed in d2s subdirectory.

Usage
=====

Use `module load` to select the desired SoftPack module, and run:

```
addRStudio /path/to/output.sif
```
70 changes: 70 additions & 0 deletions addRStudio
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

declare ARBASE="/software/hgi/installs/softpack/addRStudio";
declare INSTALL_BASE="/software/hgi/installs/softpack/rstudio/.spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/";

declare REXE="$(which R)";
declare BASE="$(dirname "$REXE")/singularity.sif";
declare OUTPUT="$1";

help() {
declare exit=0;

if [ -n "${1:-}" ]; then
exit=1;

echo -e "$1\n";
fi;

cat <<HEREDOC
Usage: $0 /path/to/outout/image.sif
This program adds rstudio to the loaded SoftPack module, saving the new Singularity image in the given output file.
HEREDOC

exit $exit;
}

if [ ! -f "$BASE" ]; then
help "No SoftPack module with R loaded.";
fi;

if [ "$OUTPUT" = "-h" -o "$OUTPUT" = "--help" ]; then
help;
elif [ -z "$OUTPUT" -o -f "$OUTPUT" ]; then
help "No output image file specified or file already exists.";
fi;

declare version="$(R -e "R.version" -s)";
declare major="$(echo "$version" | grep "^major" | sed -e 's/^[^ ]* *//' -e 's/ *$//')";
declare minor="$(echo "$version" | grep "^minor" | sed -e 's/^[^ ]* *//' -e 's/ *$//')";

declare R_VERSION="$major.$minor";
declare R_PATH="r-4.3.1-bfwldrk76z6f52upk47zepliekn7ayqz";
declare RSERVER_PATH="rstudio-2024.04.0-pvcozy3fjfw5j4ydi7j3gazf47n64w7g";

case "$R_VERSION" in
"3.6"|"3.6.0"|"3.6.1"|"3.6.2"|"3.6.3")
R_PATH="r-3.6.1-lr5l32iscxwjc66xv4vqs55kfyxqnbk7";
RSERVER_PATH="rstudio-2024.04.0-v5b3kvtyes7mfzew3dcrl46m25w4peqw";;
"4.0"|"4.0.1"|"4.0.2"|"4.0.3")
R_PATH="r-4.0.3-mtvoxefp3hv7y2cnllkrui3yurc44xt5";
RSERVER_PATH="rstudio-2024.04.0-oegivlu2623umrbfwgqslosygk24djua";;
"4.1.0")
R_PATH="r-4.1.0-5gc5uryd3fwthjljfe4clb6kuyn6vlxh";
RSERVER_PATH="rstudio-2024.04.0-ybcgmthilhwrjz57p5eeve6jmsuaierl";;
"4.1"|"4.1.1"|"4.1.2"|"4.1.3")
R_PATH="r-4.1.3-faaljkukyjzv3bgqk3zc4kmf37axsk33";
RSERVER_PATH="rstudio-2024.04.0-wf7motz63heqioazbrsqqqd2ysoegdzk";;
"4.2.2")
R_PATH="r-4.2.2-attjfhbgqd6aertwlfis62kmnkqqh3om";
RSERVER_PATH="rstudio-2024.04.0-l3zcwq34bdvyfw4ydgwvnzfco5vljiqc";;
"4.2"|"4.2.3")
R_PATH="r-4.2.3-o7l36ehxe6dbsykz4cyei4tzywdxbiph";
RSERVER_PATH="rstudio-2024.04.0-do2phksin5s36a2l7x2e7jxs2kpk5r7j";;
"4.4"|"4.4.0"|"4.4.1")
R_PATH="r-4.4.1-zf3d5qbxgbiqsk4ddke3fl6uluwcbqcu";
RSERVER_PATH="rstudio-2024.04.0-ordci3t2b3w5yvutqsc5jzzsf5tr7cnh";;
esac;

"$ARBASE"/d2s/dir2singularity -b "$BASE" -e 'PATH=/opt/rstudio/:$PATH' -o "$OUTPUT" -p "$ARBASE/bin" -p "${INSTALL_BASE}soci-4.0.2-f4igwqxeglkzgll5ldl3lqnildqnyued/" -p "${INSTALL_BASE}sqlite-3.43.2-7egypdqfrrdxkbf2zds4o4rkxnd47wwt/" -p "${INSTALL_BASE}zip-3.0-4cvgf6is23bhmllgfmqnlaun7cnjuqrw/" -p "${INSTALL_BASE}unzip-6.0-cw5vahnjqnhdsxvj3fwzht3f3ccbhmks/" -p "${INSTALL_BASE}gettext-0.22.3-ltq66wekiuczj66vmyva2puj4mbmrjef/" -p "${INSTALL_BASE}readline-8.2-7clojnhdikinks5kstq6uo7bazm6yaqr/" -p "${INSTALL_BASE}libiconv-1.17-kbgjdow6rbsxzdrjfg3eqwjxvpjih5zv/" -p "${INSTALL_BASE}icu4c-67.1-xvrp6h5wp6xbge52dinclmoxdopxor6v/" -p "${INSTALL_BASE}ncurses-6.4-vighowby4kk2yi4qzatqu2jaen5ohtq5/" -p "${INSTALL_BASE}boost-1.83.0-ugpte2erzhszosf7nnv4p2g45cvc24nk/" -p "${INSTALL_BASE}libuuid-1.0.3-fwcptc5lwr35u4iswwuexvk6ahwspemk/" -p "${INSTALL_BASE}linux-pam-1.5.3-uk6tq4vszwtzoizzyuwtgogcv3law333/" -p "${INSTALL_BASE}tclap-1.2.5-z5jfcmmw5nvhlzraxqh4hzl3fhwyj74f/" -p "${INSTALL_BASE}yaml-cpp-0.6.3-sinzpu6oh5qo4msb6rfwjkkiiuqougq3/" -p "${INSTALL_BASE}which-2.21-omcb6tpkncbbe37hsfytbvcnajkadhom/" -p "$INSTALL_BASE$R_PATH" -p "$INSTALL_BASE$RSERVER_PATH" -r "$ARBASE/bin:/opt/rstudio";
225 changes: 225 additions & 0 deletions bin/rstudio
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#!/bin/bash

printHelp() {
cat <<HEREDOC
Usage: $0 [OPTIONS]
Start RStudio server.
Options:
--home PATH home directory inside the container. [default: $HOME]
--pwd PATH initial working directory inside the container. [default: $PWD]
--r-libs-user PATH specifies additional directories for R packages. [default: ${R_LIBS_USER:-}]
--help
HEREDOC

exit "${1:-1}";
}

declare userPath="$(echo "${R_LIBS_USER:-}" | sed -e 's/:*$//')";
declare home="$HOME";
declare RSTUDIO_DEBUG=false;
declare pwd="$PWD";

checkNotEmpty() {
if [ -z "${2:-}" ]; then
echo -e "Missing $1 value\n";

printHelp;
fi;
}

while [ $# -gt 0 ]; do
k="$1";
shift;

case "$k" in
--home)
checkNotEmpty "$k" "${1:-}"

home="$1";
shift;;
--pwd)
checkNotEmpty "$k" "${1:-}"

pwd="$1";
shift;;
--r-libs-user)
checkNotEmpty "$k" "${1:-}"

userPath="$1";
shift;;
--help)
printHelp 0;;
--debug)
export RSTUDIO_DEBUG=true;;
*)
echo "Unknown argument: $k";

printHelp;;
esac;
done;

# RStudio does *not* like the PWD to be too long (not sure on exact limit, but it's significantly less than some lustre paths.
declare CONFIG_DIR=~/.rstudio-conf;
mkdir -p "$CONFIG_DIR"

export MY_PWD="$(mktemp -p "$CONFIG_DIR" -u)";

ln -s "$pwd" "$MY_PWD";

cd "$MY_PWD";

export RSTUDIO_CONF="$(mktemp -p "$CONFIG_DIR" -d)";
export RSTUDIO_LOG="$RSTUDIO_CONF/log";

cleanup() {
cd /;

# Remove PWD symlink
rm -f "$MY_PWD";

rm -rf "$RSTUDIO_CONF";
}

if ! $RSTUDIO_DEBUG; then
trap cleanup EXIT
fi;

declare PASSWORD="$(openssl rand -base64 15)";

# Determine free Port
read LOWERPORT UPPERPORT < /proc/sys/net/ipv4/ip_local_port_range
declare PORT="";
while true; do
PORT="$(shuf -i $LOWERPORT-$UPPERPORT -n 1)";
cat /proc/net/tcp | grep -q ":$(printf "%04X" $PORT) " || break;
done;

declare RSERVER_CONF="$RSTUDIO_CONF/rserver.conf";
declare SINGULARITY="$(dirname "$(which R)")/singularity.sif";

cat > "$RSERVER_CONF" <<HEREDOC
# RStudio Server Configuration File
www-port=$PORT
server-user=$USER
server-pid-file=$RSTUDIO_CONF/pid
server-data-dir=$RSTUDIO_CONF/data
auth-none=0
auth-timeout-minutes=0
auth-stay-signed-in-days=30
auth-pam-helper-path=$RSTUDIO_CONF/pam
database-config-file=$RSTUDIO_CONF/database.conf
secure-cookie-key-file=$RSTUDIO_CONF/cookie
rsession-which-r=$(echo /software/hgi/installs/softpack/rstudio/.spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/r-*/bin/R)
rsession-config-file=$RSTUDIO_CONF/session.conf
HEREDOC

{
cat <<HEREDOC
session-save-action-default=no
session-timeout-minutes=0
session-timeout-suspend=0
HEREDOC

declare rlibs="/opt/view/rlib/R";

# if [ -n "$userPath" ]; then
# rlibs="$rlibs:$userPath";
# fi;

export R_LIBS_USER="$rlibs";

echo -n "r-libs-user=$R_LIBS_USER";
} > "$RSTUDIO_CONF/session.conf";

cat > "$RSTUDIO_CONF/database.conf" <<HEREDOC
provider=sqlite
directory=$RSTUDIO_CONF/database
HEREDOC

cat > "$RSTUDIO_CONF/pam" <<HEREDOC
#!/bin/bash
IFS="" read -rs PASSWORD
[ "$USER" = "\${1}" ] && [ "$PASSWORD" = "\${PASSWORD}" ];
HEREDOC

if [ ! -f "$home/.config/rstudio/rstudio-prefs.json" ]; then
mkdir -p "$home/.config/rstudio/";

cat > "$home/.config/rstudio/rstudio-prefs.json" <<HEREDOC
{
"save_workspace": "never",
"load_workspace": false,
"initial_working_directory": "~",
"spelling_dictionary_language": "",
"posix_terminal_shell": "bash"
}
HEREDOC
fi;

if $RSTUDIO_DEBUG; then
echo -e "\nDEBUG OUTPUT: $RSTUDIO_CONF\n\n" | tee -a "$RSTUDIO_LOG";

echo "HOME: $home";
echo "PWD: $PWD";
echo "R_VERSION: $R_VERSION";
echo;

echo "$RSERVER_CONF"
echo "$RSERVER_CONF" | sed -e 's/./=/g';
echo;

cat $RSERVER_CONF;
echo;

FNAME="$RSTUDIO_CONF/session.conf";
echo "$FNAME"
echo "$FNAME" | sed -e 's/./=/g';
echo;

cat "$FNAME";
echo;
echo;
FNAME="$RSTUDIO_CONF/database.conf";
echo "$FNAME"
echo "$FNAME" | sed -e 's/./=/g';
echo;

cat "$FNAME";
echo;
echo;
FNAME="$home/.config/rstudio/rstudio-prefs.json";
echo "$FNAME"
echo "$FNAME" | sed -e 's/./=/g';
echo;

cat "$FNAME";
echo;
echo;
fi;

chmod +x "$RSTUDIO_CONF/pam";

export HOME="$home";
export LC_ALL="C";
export LC_COLLATE="C"
export TZ="UTC";
export HOSTNAME="$(hostname -A | tr -d ' ')";

{
tee -a "$RSTUDIO_LOG" <<HEREDOC
Point web browser to: http://$HOSTNAME:$PORT
use username: $USER
use password: $PASSWORD
----------
----------:$(echo "0:$USER:$PASSWORD:$(echo "http://$HOSTNAME:$PORT" | base64 | tr -d '\n'):$(echo "$HOME" | base64 | tr -d '\n')" | base64 | tr -d '\n')
HEREDOC
} | grep -v "^----------";

echo $PATH;
/software/hgi/installs/softpack/rstudio/.spack/linux-ubuntu22.04-x86_64_v3/gcc-11.4.0/rstudio-*/bin/rserver --config-file="$RSERVER_CONF" | tee -a "$RSTUDIO_LOG";

0 comments on commit 3411849

Please sign in to comment.