Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-06 16:41:03

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