Skip to content
Snippets Groups Projects
Commit b3415357 authored by Peng Lian's avatar Peng Lian
Browse files

Add more features to the run script

parent 82afa865
No related merge requests found
*.log
.history
*.sif
*.img
......@@ -8,87 +8,200 @@
module load singularity/3.9.9
tmpDir=/tmp/$USER/rstudio
# Help function
help-info(){
cat << _EOF_
Usage:
# run the container (default external library: ./library)
./run_container.sh [-r/--run] [-l/--libfolder relative/path/to/the/library]
# prepare the outside libraries for the container (default external library: ./library)
./run_container.sh -p/--prepare [-l/--libfolder relative/path/to/the/library] [-L/--libraries "libA libB libC"]
# show the help info
./run_container.sh -h/--help
_EOF_
}
# Get the parent path of this script
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
# vizapp folder on the host machine. (Will mount as /vizapp)
VIZAPP_HOME="$tmpDir/vizapp/"
# Image settings
REPO="rstudio"
TAG="4.2.2"
IMAGE_NAME="git.biohpc.swmed.edu:5050/astrocyte/container/${REPO}:${TAG}"
# dependencies libs of vizapp, space seperated
#VIZAPP_LIBS="rmarkdown"
# name of the local image
singularity_image=${REPO}-${TAG}.img
# user specified path to install the vizapp libs inside the container
R_LIBS_USER="/vizapp/rlibrary"
# pull the container to a local image
if [ ! -e ${singularity_image} ]; then
singularity pull ${singularity_image} docker://$IMAGE_NAME
fi
# env of the vizapp. (Will mount to /mnt)
outputDir=$(pwd)
# vizapp folder on the host machine. (Will mount as /vizapp)
VIZAPP_HOME="$DIR"
# Prepare log file locations
calling_username=$(id -un)
host_name=$(hostname -s)
tmpDir=/tmp/$USER/rstudio/$(date +%s)
# prepare tmp and the library folder
mkdir -p ${VIZAPP_HOME}/rlibrary
rm -fr $tmpDir
mkdir -p $tmpDir/home
mkdir -p $tmpDir/run
mkdir -p $tmpDir/tmp
chmod -R 777 $tmpDir &> /dev/null
chmod -R 777 $tmpDir
chmod 700 $tmpDir
# build a soft link from outputDir (mounted as /mnt) to user's home
ln -s /mnt $tmpDir/home/outputDir
# version and the image to use
PACKAGE="rstudio"
VERSION="4.1.1"
IMAGE_NAME="git.biohpc.swmed.edu:5050/astrocyte/container/${PACKAGE}:${VERSION}"
# name of the local image
singularity_image=${PACKAGE}_${VERSION}.sif
# pull the container to a local image
if [ ! -e ${singularity_image} ]; then
singularity pull ${singularity_image} docker://$IMAGE_NAME
fi
# env of the vizapp. (Will mount to /mnt)
outputDir=${outputDir:-$(pwd)}
# Disable auth or not
DISABLE_AUTH=true
PASSWORD=BioHPC
DEFAULT_USER=root
VIZAPP_PORT=8123
# install vizapp libs to customize folder
for lib in $VIZAPP_LIBS; do
# Vizapp library folder refer to $VIZAPP_HOME, will be overrided by -l|--libfolder
VIZAPP_LIBFOLDER="rlibrary"
run_container(){
# Vizapp library folder on host
mkdir -p ${VIZAPP_HOME}/${VIZAPP_LIBFOLDER}
# user specified path to install the vizapp libs inside the container
R_LIBS_USER="/vizapp/${VIZAPP_LIBFOLDER}"
# mount and run
echo "Running the container with the following command ..."
cat <<_EOF_
SINGULARITYENV_R_LIBS_USER=${R_LIBS_USER} \
SINGULARITYENV_DISABLE_AUTH=$DISABLE_AUTH \
SINGULARITYENV_PASSWORD=$PASSWORD \
SINGULARITYENV_DEFAULT_USER=$DEFAULT_USER \
SINGULARITYENV_VIZAPP_PORT=$VIZAPP_PORT \
singularity exec -f -C -w -e \
-H $tmpDir/home:/root \
-B $outputDir:/mnt,$VIZAPP_HOME:/vizapp,$tmpDir/tmp:/tmp ${singularity_image} \
R -e "if (!require('$lib', character.only=TRUE)) {install.packages('$lib', dependencies=TRUE, lib='$R_LIBS_USER')}"
singularity run -f -C -w -e \
-H $tmpDir/home:/root \
-B $outputDir:/mnt,$VIZAPP_HOME:/vizapp,$tmpDir/tmp:/tmp ${singularity_image}
_EOF_
echo -e "\nPlease visit http://localhost:${VIZAPP_PORT} to connect to the RStudio server ...\n"
# run vizapp on localhost
SINGULARITYENV_R_LIBS_USER=${R_LIBS_USER} \
SINGULARITYENV_DISABLE_AUTH=$DISABLE_AUTH \
SINGULARITYENV_DEFAULT_USER=$DEFAULT_USER \
SINGULARITYENV_VIZAPP_PORT=$VIZAPP_PORT \
singularity run -f -C -w -e \
-H $tmpDir/home:/root \
-B $outputDir:/mnt,$VIZAPP_HOME:/vizapp,$tmpDir/tmp:/tmp ${singularity_image}
}
# dependencies libs (space seperated), will be overrided by -L|--libraries
VIZAPP_LIBS="rmarkdown"
prepare_libs(){
# Vizapp library folder on host
mkdir -p ${VIZAPP_HOME}/${VIZAPP_LIBFOLDER}
# user specified path to install the vizapp libs inside the container
R_LIBS_USER="/vizapp/${VIZAPP_LIBFOLDER}"
echo "Start to install libs \"${VIZAPP_LIBS}\" to \"${VIZAPP_HOME}/${VIZAPP_LIBFOLDER}\" ..."
# install vizapp libs to customize folder
for lib in $VIZAPP_LIBS; do
SINGULARITYENV_R_LIBS_USER=${R_LIBS_USER} \
SINGULARITYENV_DISABLE_AUTH=$DISABLE_AUTH \
SINGULARITYENV_DEFAULT_USER=$DEFAULT_USER \
SINGULARITYENV_VIZAPP_PORT=$VIZAPP_PORT \
singularity exec -f -C -w -e \
-H $tmpDir/home:/root \
-B $outputDir:/mnt,$VIZAPP_HOME:/vizapp,$tmpDir/tmp:/tmp ${singularity_image} \
R -e "if (!require('$lib', character.only=TRUE)) {install.packages('$lib', dependencies=TRUE, lib='$R_LIBS_USER')}"
done
}
# Default option settings
POSITIONAL_ARGS=()
LIBFOLDER="library" # A folder inside $VIZAPP_HOME
# Capture the options and positional argument
while [[ $# -gt 0 ]]; do
case $1 in
-p|--prepare)
PREPARE="YES"
shift # past argument
;;
-r|--run)
RUN="YES"
shift # past argument
;;
-L|--libraries)
LIBS="$2"
shift
shift
;;
-l|--libfolder)
LIBFOLDER="$2"
shift
shift
;;
-*|--*)
help-info
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
# Determine how to run this script
if [[ -z $PREPARE && -z $RUN && -z $POSITIONAL_ARGS ]]; then
echo "Running the container with lib \"$LIBFOLDER\" ..."
VIZAPP_LIBFOLDER=$LIBFOLDER
run_container
# clean up tmp dir from host
rm -fr $tmpDir
elif [[ $PREPARE == "YES" && $RUN == "YES" ]]; then
echo "Preparing the libraries with lib \"$LIBFOLDER\" ..."
VIZAPP_LIBFOLDER=$LIBFOLDER
VIZAPP_LIBS=$LIBS
prepare_libs
echo "Running the container with lib \"$LIBFOLDER\" ..."
run_container
# clean up tmp dir from host
rm -fr $tmpDir
elif [[ $PREPARE == "YES" ]]; then
echo "Preparing the libraries with lib \"$LIBFOLDER\" ..."
VIZAPP_LIBFOLDER=$LIBFOLDER
VIZAPP_LIBS=$LIBS
prepare_libs
elif [[ $RUN == "YES" ]]; then
echo "Running the container with lib \"$LIBFOLDER\" ..."
VIZAPP_LIBFOLDER=$LIBFOLDER
run_container
# clean up tmp dir from host
rm -fr $tmpDir
else
help-info
fi
echo -e "\nPlease visit http://localhost:${VIZAPP_PORT} to connect to the RStudio server ...\n"
# run vizapp on localhost
SINGULARITYENV_R_LIBS_USER=${R_LIBS_USER} \
SINGULARITYENV_DISABLE_AUTH=$DISABLE_AUTH \
SINGULARITYENV_PASSWORD=$PASSWORD \
SINGULARITYENV_DEFAULT_USER=$DEFAULT_USER \
SINGULARITYENV_VIZAPP_PORT=$VIZAPP_PORT \
singularity run -f -C -w -e \
-H $tmpDir/home:/root \
-B $outputDir:/mnt,$VIZAPP_HOME:/vizapp,$tmpDir/tmp:/tmp ${singularity_image}
## Enable this section if run as non-root user inside the container
## clean up the files created in the container
#SINGULARITYENV_R_LIBS_USER=${R_LIBS_USER} \
#SINGULARITYENV_DISABLE_AUTH=$DISABLE_AUTH \
#SINGULARITYENV_PASSWORD=$PASSWORD \
#SINGULARITYENV_DEFAULT_USER=$DEFAULT_USER \
#singularity exec -f -C -w -e \
#-H $tmpDir/home:/root \
#-B $tmpDir/run:/var/run,$tmpDir/tmp:/tmp ${singularity_image} \
#/bin/bash -c "rm -fr /home/rstudio/* /home/rstudio/.* /var/run/* /tmp/* "
# clean up tmp dir from host
rm -fr $tmpDir
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment