Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-17 09:03:01

0001 #!/bin/bash
0002 
0003 ## Simple setup script that installs the container
0004 ## in your local environment under $PREFIX/local/lib
0005 ## and creates a simple top-level launcher script
0006 ## that launches the container for this working directory
0007 ## with the $EIC_SHELL_PREFIX variable pointing
0008 ## to the $PREFIX/local directory
0009 
0010 CONTAINER="eic_xl"
0011 VERSION="nightly"
0012 PREFIX="$PWD"
0013 
0014 function print_the_help {
0015   echo "USAGE:  ./install.sh [-p PREFIX] [-v VERSION]"
0016   echo "OPTIONAL ARGUMENTS:"
0017   echo "          -p,--prefix     Working directory to deploy the environment (D: $PREFIX)"
0018   echo "          -t,--tmpdir     Change tmp directory (D: $([[ -z "$TMPDIR" ]] && echo "/tmp" || echo "$TMPDIR"))"
0019   echo "          -n,--no-cvmfs   Disable check for local CVMFS (D: enabled)"
0020   echo "          -c,--container  Container version (D: $CONTAINER)"
0021   echo "          -v,--version    Version to install (D: $VERSION)"
0022   echo "          -h,--help       Print this message"
0023   echo ""
0024   echo "  Set up containerized development environment."
0025   echo ""
0026   echo "EXAMPLE: ./install.sh" 
0027   exit
0028 }
0029 
0030 while [ $# -gt 0 ]; do
0031   key=$1
0032   case $key in
0033     -p|--prefix)
0034       PREFIX=$(realpath $2)
0035       shift
0036       shift
0037       ;;
0038     -t|--tmpdir)
0039       export TMPDIR=$2
0040       export SINGULARITY_TMPDIR=$2
0041       shift
0042       shift
0043       ;;
0044     -n|--no-cvmfs)
0045       DISABLE_CVMFS_USAGE=true
0046       shift
0047       ;;
0048     -c|--container)
0049       CONTAINER=$2
0050       shift
0051       shift
0052       ;;
0053     -v|--version)
0054       VERSION=$2
0055       shift
0056       shift
0057       ;;
0058     -h|--help)
0059       print_the_help
0060       exit 0
0061       ;;
0062     *)
0063       echo "ERROR: unknown argument: $key"
0064       echo "use --help for more info"
0065       exit 1
0066       ;;
0067   esac
0068 done
0069 
0070 ## create prefix if needed
0071 mkdir -p $PREFIX || exit 1
0072 pushd $PREFIX
0073 
0074 if [ ! -d $PREFIX ]; then
0075   echo "ERROR: not a valid directory: $PREFIX"
0076   echo "use --help for more info"
0077   exit 1
0078 fi
0079 
0080 echo "Setting up development environment for eicweb/$CONTAINER:$VERSION"
0081 
0082 mkdir -p $PREFIX/local/lib || exit 1
0083 
0084 function install_singularity() {
0085   SINGULARITY=
0086   ## check for a singularity install
0087   ## default singularity if new enough
0088   if [ $(type -P singularity ) ]; then
0089     SINGULARITY=$(which singularity)
0090     SINGULARITY_VERSION=`$SINGULARITY --version`
0091     if [ ${SINGULARITY_VERSION:0:1} = 2 ]; then
0092       ## too old, look for something else
0093       SINGULARITY=
0094     fi
0095   fi
0096   if [ -z $SINGULARITY ]; then
0097     ## first priority: a known good install (this one is on JLAB)
0098     if [ -d "/apps/singularity/3.7.1/bin/" ]; then
0099       SINGULARITY="/apps/singularity/3.7.1/bin/singularity"
0100     ## whatever is in the path is next
0101     elif [ $(type -P singularity ) ]; then
0102       SINGULARITY=$(which singularity)
0103     ## cvmfs singularity is last resort (sandbox mode can cause issues)
0104     elif [ -f "/cvmfs/oasis.opensciencegrid.org/mis/singularity/bin/singularity" ]; then
0105       SINGULARITY="/cvmfs/oasis.opensciencegrid.org/mis/singularity/bin/singularity"
0106     ## not good...
0107     else
0108       echo "ERROR: no singularity found, please make sure you have singularity in your \$PATH"
0109       exit 1
0110     fi
0111   fi
0112   echo " - Found singularity at $SINGULARITY"
0113 
0114   ## get singularity version
0115   ## we only care if is 2.x or not, so we can use singularity --version 
0116   ## which returns 2.xxxxx for version 2
0117   SINGULARITY_VERSION=`$SINGULARITY --version`
0118   SIF=
0119   if [ ${SINGULARITY_VERSION:0:1} = 2 ]; then
0120     SIF="$PREFIX/local/lib/${CONTAINER}-${VERSION}.simg"
0121 
0122     echo "WARNING: your singularity version $SINGULARITY_VERSION is ancient, we strongly recommend using version 3.x"
0123     echo "We will attempt to use a fall-back SIMG image to be used with this singularity version"
0124     if [ -f /gpfs02/eic/athena/${CONTAINER}-${VERSION}.simg ]; then
0125       ln -sf /gpfs02/eic/athena/${CONTAINER}-${VERSION}.simg ${SIF}
0126     else
0127       echo "Attempting last-resort singularity pull for old image"
0128       echo "This may take a few minutes..."
0129       INSIF=`basename ${SIF}`
0130       singularity pull --name "${INSIF}" docker://eicweb/$CONTAINER:$VERSION
0131       mv ${INSIF} $SIF
0132       chmod +x ${SIF}
0133       unset INSIF
0134     fi
0135   ## we are in sane territory, yay!
0136   else
0137     ## check if we can just use cvmfs for the image
0138     SIF="$PREFIX/local/lib/${CONTAINER}-${VERSION}.sif"
0139     if [ -z "$DISABLE_CVMFS_USAGE" -a -d /cvmfs/singularity.opensciencegrid.org/eicweb/${CONTAINER}:${VERSION} ]; then
0140       SIF="$PREFIX/local/lib/${CONTAINER}-${VERSION}"
0141       ## need to cleanup in this case, else it will try to make a subdirectory
0142       rm -rf ${SIF}
0143       ln -sf /cvmfs/singularity.opensciencegrid.org/eicweb/${CONTAINER}:${VERSION} ${SIF}
0144     elif [ -f /cvmfs/eic.opensciencegrid.org/singularity/athena/${CONTAINER}_v${VERSION}.sif ]; then
0145       ln -sf /cvmfs/eic.opensciencegrid.org/singularity/athena/${CONTAINER}_v${VERSION}.sif ${SIF}
0146     elif [ -f /gpfs02/cvmfst0/eic.opensciencegrid.org/singularity/athena/${CONTAINER}_v${VERSION}.sif ]; then
0147       ln -sf /gpfs02/cvmfst0/eic.opensciencegrid.org/singularity/athena/${CONTAINER}_v${VERSION}.sif ${SIF}
0148     ## check if we have an internal CI image we will use for testing purposes
0149     elif [ -f $PWD/.gitlab-ci/${CONTAINER}-${VERSION}.sif ]; then
0150       ln -sf $PWD/.gitlab-ci/${CONTAINER}-${VERSION}.sif ${SIF}
0151     ## if not, download the container to the system
0152     else
0153       ## get the python installer and run the old-style install
0154       ## work in temp directory
0155       tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)
0156       pushd $tmp_dir
0157       wget https://eic.github.io/eic-shell/install.py
0158       chmod +x install.py
0159       ./install.py -f -c $CONTAINER -v $VERSION .
0160       INSIF=lib/`basename ${SIF}`
0161       mv $INSIF $SIF
0162       chmod +x ${SIF}
0163       ## cleanup
0164       popd
0165       rm -rf $tmp_dir
0166       unset INSIF
0167     fi
0168   fi
0169 
0170   echo $SIF
0171   ls $SIF 2>&1 > /dev/null && GOOD_SIF=1 
0172   if [ -z "$SIF" -o -z "$GOOD_SIF" ]; then
0173     echo "ERROR: no singularity image found"
0174     exit 1
0175   else
0176     echo " - Deployed ${CONTAINER} image: $SIF"
0177   fi
0178 
0179   ## We want to make sure the root directory of the install directory
0180   ## is always bound. We also check for the existence of a few standard
0181   ## locations (/scratch /volatile /cache) and bind those too if found
0182   echo " - Determining additional bind paths"
0183   BINDPATH=${SINGULARITY_BINDPATH}
0184   echo "   --> system bindpath: $BINDPATH"
0185   PREFIX_ROOT="/$(realpath $PREFIX | cut -d "/" -f2)"
0186   for dir in /w /work /scratch /volatile /cache /gpfs /gpfs01 /gpfs02 $PREFIX_ROOT; do
0187     ## only add directories once
0188     if [[ ${BINDPATH} =~ $(basename $dir) ]]; then
0189       continue
0190     fi
0191     if [ -d $dir ]; then
0192       echo "   --> $dir"
0193       BINDPATH=${dir}${BINDPATH:+,$BINDPATH}
0194     fi
0195   done
0196 
0197   ## create a new top-level eic-shell launcher script
0198   ## that sets the EIC_SHELL_PREFIX and then starts singularity
0199 cat << EOF > eic-shell
0200 #!/bin/bash
0201 
0202 ## capture environment setup for upgrades
0203 CONTAINER=$CONTAINER
0204 TMPDIR=$TMPDIR
0205 VERSION=$VERSION
0206 PREFIX=$PREFIX
0207 DISABLE_CVMFS_USAGE=${DISABLE_CVMFS_USAGE}
0208 
0209 function print_the_help {
0210   echo "USAGE:  ./eic-shell [OPTIONS] [ -- COMMAND ]"
0211   echo "OPTIONAL ARGUMENTS:"
0212   echo "          -u,--upgrade    Upgrade the container to the latest version"
0213   echo "          -n,--no-cvmfs   Disable check for local CVMFS when updating. (D: enabled)"
0214   echo "          -c,--container  Container version (D: \$CONTAINER) (requires cvmfs)"
0215   echo "          -v,--version    Version to install (D: \$VERSION) (requires cvmfs)"
0216   echo "          -h,--help       Print this message"
0217   echo ""
0218   echo "  Start the eic-shell containerized software environment (Singularity version)."
0219   echo ""
0220   echo "EXAMPLES: "
0221   echo "  - Start an interactive shell: ./eic-shell" 
0222   echo "  - Upgrade the container:      ./eic-shell --upgrade"
0223   echo "  - Use different version:      ./eic-shell --version \$(date +%y.%m).0-stable"
0224   echo "  - Execute a single command:   ./eic-shell -- <COMMAND>"
0225   echo ""
0226   exit
0227 }
0228 
0229 UPGRADE=
0230 
0231 while [ \$# -gt 0 ]; do
0232   key=\$1
0233   case \$key in
0234     -u|--upgrade)
0235       UPGRADE=1
0236       shift
0237       ;;
0238     -n|--no-cvmfs)
0239       DISABLE_CVMFS_USAGE=true
0240       shift
0241       ;;
0242     -c|--container)
0243       CONTAINER=\$2
0244       export SIF=/cvmfs/singularity.opensciencegrid.org/eicweb/\${CONTAINER}:\${VERSION}
0245       shift
0246       shift
0247       ;;
0248     -v|--version)
0249       VERSION=\$2
0250       export SIF=/cvmfs/singularity.opensciencegrid.org/eicweb/\${CONTAINER}:\${VERSION}
0251       shift
0252       shift
0253       ;;
0254     -h|--help)
0255       print_the_help
0256       exit 0
0257       ;;
0258     --)
0259       shift
0260       break
0261       ;;
0262     *)
0263       echo "ERROR: unknown argument: \$key"
0264       echo "use --help for more info"
0265       exit 1
0266       ;;
0267   esac
0268 done
0269 
0270 if [ ! -z \${UPGRADE} ]; then
0271   echo "Upgrading eic-shell..."
0272   if [ -z "\$DISABLE_CVMFS_USAGE" -a -d /cvmfs/singularity.opensciencegrid.org/eicweb/\${CONTAINER}:\${VERSION} ]; then
0273     echo ""
0274     echo "Note: You cannot manually update the container as you are using the CVMFS version."
0275     echo "      The container will automatically update every 24 hours."
0276     echo "      You can override this by setting the '--no-cvmfs' flag, which will"
0277     echo "      instantiate a local version."
0278     echo "      This is only recommended for expert usage."
0279     echo ""
0280     echo "This will only upgrade the eic-shell script itself."
0281     echo ""
0282   fi
0283   FLAGS="-p \${PREFIX} -v \${VERSION}"
0284   if [ ! -z \${TMPDIR} ]; then
0285     FLAGS="\${FLAGS} -t \${TMPDIR}"
0286   fi
0287   if [ ! -z \${DISABLE_CVMFS_USAGE} ]; then
0288     FLAGS="\${FLAGS} --no-cvmfs"
0289   fi
0290   curl -L https://github.com/eic/eic-shell/raw/main/install.sh \
0291     | bash -s -- \${FLAGS}
0292   echo "eic-shell upgrade sucessful"
0293   exit 0
0294 fi
0295 
0296 export EIC_SHELL_PREFIX=$PREFIX/local
0297 export SINGULARITY_BINDPATH=$BINDPATH
0298 \${SINGULARITY:-$SINGULARITY} exec \${SINGULARITY_OPTIONS:-} \${SIF:-$SIF} eic-shell \$@
0299 EOF
0300 
0301   chmod +x eic-shell
0302 
0303   echo " - Created custom eic-shell excecutable"
0304 }
0305 
0306 function install_docker() {
0307   ## check for docker install
0308   DOCKER=$(which docker)
0309   if [ -z ${DOCKER} ]; then
0310     echo "ERROR: no docker install found, docker is required for the docker-based install"
0311   fi
0312   echo " - Found docker at ${DOCKER}"
0313 
0314   IMG=eicweb/${CONTAINER}:${VERSION}
0315   docker pull ${IMG}
0316   echo " - Deployed ${CONTAINER} image: ${IMG}"
0317 
0318   ## We want to make sure the root directory of the install directory
0319   ## is always bound. We also check for the existence of a few standard
0320   ## locations (/Volumes /Users /tmp) and bind those too if found
0321   echo " - Determining mount paths"
0322   PREFIX_ROOT="/$(realpath $PREFIX | cut -d "/" -f2)"
0323   MOUNT=""
0324   echo "   --> $PREFIX_ROOT"
0325   for dir in /Volumes /Users /tmp; do
0326     ## only add directories once
0327     if [[ ${MOUNT} =~ $(basename $dir) ]]; then
0328       continue
0329     fi
0330     if [ -d $dir ]; then
0331       echo "   --> $dir"
0332       MOUNT="$MOUNT -v $dir:$dir"
0333     fi
0334   done
0335   echo " - Docker mount directive: '$MOUNT'"
0336   PLATFORM_FLAG=''
0337   if [ `uname -m` = 'arm64' ]; then
0338     PLATFORM_FLAG='--platform linux/amd64'
0339     echo " - Additional platform flag to run on arm64"
0340   fi
0341 
0342   ## create a new top-level eic-shell launcher script
0343   ## that sets the EIC_SHELL_PREFIX and then starts singularity
0344 cat << EOF > eic-shell
0345 #!/bin/bash
0346 
0347 ## capture environment setup for upgrades
0348 CONTAINER=$CONTAINER
0349 TMPDIR=$TMPDIR
0350 VERSION=$VERSION
0351 PREFIX=$PREFIX
0352 DISABLE_CVMFS_USAGE=${DISABLE_CVMFS_USAGE}
0353 
0354 function print_the_help {
0355   echo "USAGE:  ./eic-shell [OPTIONS] [ -- COMMAND ]"
0356   echo "OPTIONAL ARGUMENTS:"
0357   echo "          -u,--upgrade    Upgrade the container to the latest version"
0358   echo "          --noX           Disable X11 forwarding on macOS"
0359   echo "          -h,--help       Print this message"
0360   echo ""
0361   echo "  Start the eic-shell containerized software environment (Docker version)."
0362   echo ""
0363   echo "EXAMPLES: "
0364   echo "  - Start an interactive shell: ./eic-shell" 
0365   echo "  - Upgrade the container:      ./eic-shell --upgrade"
0366   echo "  - Execute a single command:   ./eic-shell -- <COMMAND>"
0367   echo ""
0368   exit
0369 }
0370 
0371 UPGRADE=
0372 NOX=
0373 while [ \$# -gt 0 ]; do
0374   key=\$1
0375   case \$key in
0376     -u|--upgrade)
0377       UPGRADE=1
0378       shift
0379       ;;
0380     --noX)
0381       NOX=1
0382       shift
0383       ;;
0384      -h|--help)
0385       print_the_help
0386       exit 0
0387       ;;
0388     --)
0389       shift
0390       break
0391       ;;
0392     *)
0393       echo "ERROR: unknown argument: \$key"
0394       echo "use --help for more info"
0395       exit 1
0396       ;;
0397   esac
0398 done
0399 
0400 if [ x\${DISPLAY} == "x" ] ; then
0401   echo "No X11 display detected, disabling X11"
0402   NOX=1
0403 fi
0404 
0405 if [ ! -z \${UPGRADE} ]; then
0406   echo "Upgrading eic-shell..."
0407   docker pull $IMG || exit 1
0408   echo "eic-shell upgrade sucessful"
0409   exit 0
0410 fi
0411 EOF
0412 
0413   if [ `uname -s` = 'Darwin' ]; then
0414       echo 'if [ ! ${NOX} ]; then' >> eic-shell
0415       echo ' nolisten=`defaults find nolisten_tcp | grep nolisten | head -n 1 | awk ' "'{print" '$3}'"'" '|cut -b 1 `' >> eic-shell
0416       echo ' [[ $nolisten -ne 0 ]] && echo "For X support: In XQuartz settings --> Security --> enable \"Allow connections from network clients\" and restart (should be only once)."' >> eic-shell
0417       ## getting the following single and double quotes, escapes and backticks right was a nightmare
0418       ## But with a heredoc it was worse
0419       echo '  xhost +localhost' >> eic-shell
0420       echo '  dispnum=`ps -e |grep Xquartz | grep listen | grep -v xinit |awk ' "'{print" '$5}'"'" '`' >> eic-shell
0421       echo '  XSTUFF="-e DISPLAY=host.docker.internal${dispnum} -v /tmp/.X11-unix:/tmp/.X11-unix"' >> eic-shell
0422       echo 'fi' >> eic-shell
0423   fi
0424   echo "docker run $PLATFORM_FLAG $MOUNT \$XSTUFF -w=$PWD -it --rm -e EIC_SHELL_PREFIX=$PREFIX/local $IMG eic-shell \$@" >> eic-shell
0425 
0426   chmod +x eic-shell
0427   echo " - Created custom eic-shell excecutable"
0428 }
0429 
0430 ## detect OS
0431 OS=`uname -s`
0432 CPU=`uname -m`
0433 case ${OS} in
0434   Linux)
0435     echo " - Detected OS: Linux"
0436     echo " - Detected CPU: $CPU"
0437     if [ "$CPU" = "arm64" ]; then
0438       install_docker
0439     else
0440       install_singularity
0441     fi
0442     ;;
0443   Darwin)
0444     echo " - Detected OS: MacOS"
0445     echo " - Detected CPU: $CPU"
0446     install_docker
0447     ;;
0448   *)
0449     echo "ERROR: OS '${OS}' not currently supported"
0450     exit 1
0451     ;;
0452 esac
0453 
0454 popd
0455 echo "Environment setup succesfull"
0456 echo "You can start the development environment by running './eic-shell'"