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

[DO NOT MERGE] Starts RStudio namespacing /tmp using unshare #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
79 changes: 54 additions & 25 deletions template/script.sh.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
#!/usr/bin/env bash

# Load the required environment
setup_env () {
# Additional environment which could be moved into a module
# Change these to suit
export RSTUDIO_SERVER_IMAGE="/apps/rserver-launcher-centos7.simg"
export SINGULARITY_BINDPATH="/etc,/media,/mnt,/opt,/srv,/usr,/var"
export PATH="$PATH:/usr/lib/rstudio-server/bin"
export SINGULARITYENV_PATH="$PATH"
}
setup_env

#
# Start RStudio Server
#

# PATH including rserver
export PATH="$PATH:/usr/lib/rstudio-server/bin"

# PAM auth helper used by RStudio
export RSTUDIO_AUTH="${PWD}/bin/auth"
export RSTUDIO_AUTH="<%= session.staged_root.join('bin/auth') %>"

# Generate an `rsession` wrapper script
export RSESSION_WRAPPER_FILE="${PWD}/rsession.sh"
export RSESSION_WRAPPER_FILE="<%= session.staged_root.join('rsession.sh') %>"
(
umask 077
sed 's/^ \{2\}//' > "${RSESSION_WRAPPER_FILE}" << EOL
Expand All @@ -41,21 +33,58 @@ chmod 700 "${RSESSION_WRAPPER_FILE}"
# Set working directory to home directory
cd "${HOME}"

export TMPDIR="$(mktemp -d)"
set -x

mkdir -p "$TMPDIR/rstudio-server"
python -c 'from uuid import uuid4; print(uuid4())' > "$TMPDIR/rstudio-server/secure-cookie-key"
chmod 0600 "$TMPDIR/rstudio-server/secure-cookie-key"
# Dump environment without functions for use in rsession.sh, filtering readonly variables
(set -o posix; set) | grep -vP '^(BASHOPTS|BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID)' > "<%= session.staged_root.join('.env') %>"

set -x
# Launch the RStudio Server
echo "Starting up rserver..."
cat > "<%= session.staged_root.join('launcher.sh') %>" << SCRIPT
#!/bin/bash

singularity run -B "$TMPDIR:/tmp" "$RSTUDIO_SERVER_IMAGE" \
--www-port "${port}" \
exec > <%= session.staged_root.join('unshared_log.log') %> 2>&1

# Hide real /tmp
# tmpfs does not pre-allocate memory so a large size is safe (tm)
mount -n -o size=64M -t tmpfs tmpfs /tmp

(
cd /tmp
mkdir -p "rstudio-server"
python -c 'from uuid import uuid4; print(uuid4())' > "rstudio-server/secure-cookie-key"
chmod 0600 "rstudio-server/secure-cookie-key"
)

echo "Contents of /tmp"
tree /tmp
echo "Starting rserver on port $port"

# env | sort

source "<%= session.staged_root.join('.env') %>"

rserver --www-port "$port" \
--auth-none 0 \
--auth-pam-helper-path "${RSTUDIO_AUTH}" \
--auth-pam-helper-path "$RSTUDIO_AUTH" \
--auth-encrypt-password 0 \
--rsession-path "${RSESSION_WRAPPER_FILE}"
--rsession-path "$RSESSION_WRAPPER_FILE"

ps aux | sort

# Get the PID of the last rserver process started
rserver_pid=$( pgrep 'rserver' | tail )
echo "PID: $rserver_pid"
# As long as the PID directory exists we wait
if [[ "/proc/$rserver_pid" != "/proc/" ]]; then
while [[ -d "/proc/$rserver_pid" ]]; do
echo "Waiting on $rserver_pid"
sleep 1
done
else
echo "PID for rserver was not captured"
fi
SCRIPT

# Launch the RStudio Server
unshare -rmfp /bin/bash "<%= session.staged_root.join('launcher.sh') %>"

echo 'Singularity as exited...'
echo 'RStudio has exited...'