diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f2615a6d0d203eaf0420beadf522259636be3e4..a202553b6bea453a6ad1c12fa1b76a51e4511be8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,10 +27,17 @@ endforeach() project(Installer VERSION 1.0 LANGUAGES C) # Need a language for GNUInstallDirs find_package(Git REQUIRED) +find_package(Python 3 REQUIRED COMPONENTS Interpreter) include(ExternalProject) include(GNUInstallDirs) +# Detect the user's environment. +execute_process(COMMAND "${Python_EXECUTABLE}" detect_environment.py + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE ENVIRONMENT_SETUP + OUTPUT_STRIP_TRAILING_WHITESPACE) + # Create setup.sh for users to source if they didn't provide their own install dir. # If they did, we expect them to know what they are doing. if ("${CMAKE_INSTALL_PREFIX}" STREQUAL "${CMAKE_SOURCE_DIR}/tools") diff --git a/README.md b/README.md index b42f9238179f674f1772ddab7ddff48e66a8d325..a7c9f2eb9f0573b64f2ff73130fbfcf06e8979cc 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,7 @@ In general, it is recommended that you install the software on a fast disk to en ``` git clone https://gitlab.cern.ch/cms-analysis/general/DasAnalysisSystem/gitlab-profile.git DasAnalysisSystem cd DasAnalysisSystem -source ./setup ``` -The `setup` is necessary at CERN (CH), at DESY (DE), at IIHE (BE), and for GitLab CI, but may not be necessary on all platforms (e.g. your private laptop). 2. Run the installation: ``` ./install.sh @@ -107,16 +105,16 @@ In both cases, first go to the root directory of `DasAnalysisSystem`. Just run the following: ``` -source ./setup -source ./tools/setup.sh +source tools/setup.sh ``` +This will automatically activate the LCG environment that was used for installing. To run CRAB jobs (only in a LCG environment), you will also need to set up a valid [grid certificate](https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookStartingGrid#ObtainingCert). If you already have done so, this will enable it: ``` voms-proxy-init --rfc --voms cms -valid 192:00 ``` -To make [RUCIO](https://twiki.cern.ch/twiki/bin/viewauth/CMS/Rucio) requests (only for LCG environment), the `setup` file tried to guess your RUCIO username from your local username (unless was already set up). This is not guaranteed to work and you may have to define your RUCIO username ahead for sourcing the DAS environment. +To make [RUCIO](https://twiki.cern.ch/twiki/bin/viewauth/CMS/Rucio) requests (only for LCG environment), `tools/setup.sh` tries to guess your RUCIO username from your local username (unless was already set up). This is not guaranteed to work and you may have to define your RUCIO username ahead for sourcing the DAS environment. ### With micromamba @@ -132,7 +130,6 @@ CMSSW is necessary to produce n-tuples containing CMS data. If `/cvmfs` is avail The CMSSW environment is based on no longer maintained versions of Linux, such as Enterprise Linux 8, whereas most clusters use a more recent operating system, Alma Linux 9. A compatibility layer is thus needed to run CMS software, which is provided by means of "container images". For instance, CMSSW 10 requires CentOS7 (used for UL production), whereas CMSSW 12 requires EL8 (used to compile the n-tupliser). DAS provides commands to start containers, called `cc7` and `el8`, which take no arguments. After running `el8`, you start a shell in the image, then you can source the CMSSW environment as follows: ``` -source $DAS_BASE/scripts/setup cd $DAS_BASE/CMSSW_12_4_0 cmsenv ``` diff --git a/detect_environment.py b/detect_environment.py new file mode 100644 index 0000000000000000000000000000000000000000..f063f5e9167d19a104b8084a60154b9f412f5d74 --- /dev/null +++ b/detect_environment.py @@ -0,0 +1,105 @@ +#! /usr/bin/env python + +import os +from os.path import abspath, isfile, join + + +def lcg(): + """ + Generates the code to set up an LCG environment. + """ + + # The CMAKE_PREFIX_PATH seems to point to the root of the environment. + location = os.environ.get('CMAKE_PREFIX_PATH') + setup = isfile(join(location, 'setup.sh')) + if not isfile(setup): + raise ValueError('Could not LCG setup.sh') + + return 'source ' + setup + + +def conda_like(): + r""" + Generates the code to set up a conda/mamba/micromamba environment. + + \note Only micromamba is officially supported. + """ + + env_name = os.environ['CONDA_DEFAULT_ENV'] + + # Figure out which variant we're using + if 'MAMBA_EXE' in os.environ and 'MAMBA_ROOT_PREFIX' in os.environ: + ## \todo Is this also valid for environments created by mamba? + template = ''' +# Make sure {mamba_exe_name} is set up +export MAMBA_EXE='{mamba_exe}'; +export MAMBA_ROOT_PREFIX='{mamba_root}'; +__mamba_setup="$("$MAMBA_EXE" shell hook --shell bash --root-prefix "$MAMBA_ROOT_PREFIX" 2> /dev/null)" +if [ $? -eq 0 ]; then + eval "$__mamba_setup" +else + alias {mamba_exe_name}="$MAMBA_EXE" # Fallback on help from mamba activate +fi +unset __mamba_setup + +# Enable the environment +{mamba_exe_name} activate '{env_name}' +''' + mamba_exe = os.environ['MAMBA_EXE'] + mamba_root = os.environ['MAMBA_ROOT_PREFIX'] + mamba_exe_name = 'micromamba' if mamba_exe.endswith('micromamba') else 'mamba' + return template.format(mamba_exe=mamba_exe, + mamba_root=mamba_root, + mamba_exe_name=mamba_exe_name, + env_name=env_name) + elif 'CONDA_EXE' in os.environ: + template = ''' +# Make sure conda is set up +__conda_setup="$('{conda_exe}' 'shell.bash' 'hook' 2> /dev/null)" +if [ $? -eq 0 ]; then + eval "$__conda_setup" +else + if [ -f "{conda_base}/etc/profile.d/conda.sh" ]; then + . "{conda_base}/etc/profile.d/conda.sh" + else + export PATH="{conda_base}/bin:$PATH" + fi +fi +unset __conda_setup + +# Enable the environment +conda activate '{env_name}' +''' + conda_exe = os.environ['CONDA_EXE'] + conda_root = abspath(join(conda_exe, os.pardir, os.pardir)) + return template.format(conda_exe=conda_exe, + conda_root=conda_root, + env_name=env_name) + else: + raise ValueError('Unknown conda-like environment') + + +def detect_environment(): + """ + Detect the environment in which the command is begin run, and returns code + to set it up again. Returns an empty string on failure. + """ + + # List of environment variables used to detect which environment is active. + detect = { + 'LCG_VERSION': lcg, + 'CONDA_DEFAULT_ENV': conda_like, + } + + for env_var, function in detect.items(): + if env_var in os.environ: + try: + return function() + except: + pass # Try other possibilities + + return '' + + +if __name__ == '__main__': + print(detect_environment()) diff --git a/setup b/setup deleted file mode 100644 index da20ab3cd7f1a9f5771c4fcb923b9c80bdbd8cce..0000000000000000000000000000000000000000 --- a/setup +++ /dev/null @@ -1,40 +0,0 @@ -case "$(hostname)" in - *desy.de) - tput setaf 2 - echo "Setting up environment for DESY (naf)." - tput op - source /cvmfs/sft.cern.ch/lcg/views/LCG_104/x86_64-el9-gcc13-opt/setup.sh - source /cvmfs/grid.desy.de/etc/profile.d/grid-ui-env.sh - ;; - *cern.ch) - tput setaf 2 - echo "Setting up environment for CERN (lxplus)." - tput op - source /cvmfs/sft.cern.ch/lcg/views/LCG_104/x86_64-el9-gcc13-opt/setup.sh - ;; - *iihe.ac.be) - tput setaf 2 - echo "Setting up environment for IIHE T2B cluster." - tput op - source /cvmfs/sft.cern.ch/lcg/views/LCG_104/x86_64-el9-gcc13-opt/setup.sh - ;; - *) - tput setaf 3 - echo "Unknown facility. Proceed at your own risks." - tput op - ;; -esac - -if [ "$CI" = "true" ] -then - return -fi - -source /cvmfs/cms.cern.ch/rucio/setup-py3.sh -source /cvmfs/cms.cern.ch/crab3/crab.sh - -if [ -n "$RUCIO_ACCOUNT" ] -then - echo "\$RUCIO_ACCOUNT not found in environment. Setting up with \$USER." - export RUCIO_ACCOUNT=$USER -fi diff --git a/setup.sh.in b/setup.sh.in index b16bd6eee36355652197c8339dc380c53dc8e8e1..facd5b89e267fea9d834ae382ae861830de40865 100644 --- a/setup.sh.in +++ b/setup.sh.in @@ -1,3 +1,5 @@ +@ENVIRONMENT_SETUP@ + # Technically needed export PATH=@CMAKE_INSTALL_FULL_BINDIR@:$PATH export LD_LIBRARY_PATH=@CMAKE_INSTALL_FULL_LIBDIR@:$(correction config --libdir):$LD_LIBRARY_PATH @@ -9,3 +11,33 @@ export DAS_BASE=@CMAKE_SOURCE_DIR@ export DARWIN_BASE=@CMAKE_SOURCE_DIR@/Darwin export DARWIN_TABLES=@CMAKE_SOURCE_DIR@/tables export CORE_BASE=@CMAKE_SOURCE_DIR@/Core + +# Special rules for compute centers +if [ ! "$CI" = "true" ] && # Do not run this on CI jobs + [ -d /cvmfs/grid.cern.ch ] && # Only run if /cvmfs/grid.cern.ch is mounted + [ -f /etc/redhat_release ] # Only run on Red Hat and derivatives +then + # Some sites need special instructions + case "$(hostname)" in + *desy.de) + tput setaf 2 + echo "Setting up environment for DESY (naf)." + tput op + source /cvmfs/grid.desy.de/etc/profile.d/grid-ui-env.sh + ;; + *) + # Known to be sufficient at CERN (lxplus) and IIHE (T2B). + tput setaf 2 + echo "Setting up environment for a generic facility." + tput op + ;; + esac + + # Rucio for dataset transfers + source /cvmfs/cms.cern.ch/rucio/setup-py3.sh + if [ -n "$RUCIO_ACCOUNT" ] + then + echo "\$RUCIO_ACCOUNT not found in environment. Setting up with \$USER." + export RUCIO_ACCOUNT=$USER + fi +fi