@@ -7,10 +7,10 @@ set -euo pipefail
77# Wrapper around Singularity/Apptainer for CWL + MPI + Singularity.
88#
99# This script identifies environment variables added by an MPI launcher
10- # (e.g. srun, mpirun) and adds these environment variables as command
11- # line arguments to singularity (with --env) .
10+ # (e.g. srun, mpirun) and adds these environment variables as Singularity
11+ # environment variables using the format ``SINGULARITYENV_$KEY=$VALUE`` .
1212#
13- # This allows CWL (which uses --cleanenv) to launch MPI + Singularity.
13+ # This allows CWL (which uses `` --cleanenv`` ) to launch MPI + Singularity.
1414#
1515# USAGE
1616# singularity_wrapper.sh <baseline-env-file> <singularity-bin> <args>
@@ -28,20 +28,23 @@ set -euo pipefail
2828# EXAMPLE
2929# singularity_wrapper.sh env.txt singularity --cleanenv exec image.sif
3030#
31+ # DEPENDENCIES
32+ # It uses the following binaries:
33+ # - printenv
3134
3235usage () {
33- cat << 'EOF ' >&2
34- Usage:
35- singularity_wrapper.sh <baseline-env-file> <singularity-bin> [args...]
36-
37- Run with --help to see full documentation.
38- EOF
36+ # Print usage using the top comment of this file, below the shebang.
37+ # Regexes:
38+ # - /^#!/d -> Delete (/d) a comment starting with #! (the shebang, above);
39+ # - /^#/,$ {} -> Capture the line that starts with # until the end of the file and run {...};
40+ # - /^#/p; -> Print (/p) lines starting with # (any comment after the shebang);
41+ # - /^[^#]/q -> And quit (/q) as soon as the lines doesn't start with # (^[^#]).
42+ sed -n ' /^#!/d; /^#/,$ { /^#/p; /^[^#]/q }' " $0 " | sed ' s/^# \{0,1\}//' >&2
3943 exit 1
4044}
4145
4246if [[ " ${1:- } " == " --help" ]]; then
43- grep ' ^#' " $0 " | sed ' s/^#//' >&2
44- exit 0
47+ usage
4548fi
4649
4750[[ $# -ge 2 ]] || usage
@@ -55,32 +58,27 @@ if [[ ! -f "$BASELINE_FILE" ]]; then
5558 exit 2
5659fi
5760
58- if [[ ! -x " $SINGULARITY_BIN " ]]; then
59- echo " Error: singularity binary not executable: $SINGULARITY_BIN " >&2
60- exit 3
61- fi
62-
6361# Read baseline env into an array.
6462declare -A BASE_ENV
6563while IFS=' =' read -r k v; do
6664 [[ -n " $k " && -n " $v " ]] || continue
6765 BASE_ENV[" $k " ]=" $v "
6866done < " $BASELINE_FILE "
6967
70- # Build new environment variables.
71- ENV_ARGS=()
68+ # Build new environment variables for Singularity (i.e. ``SINGULARITYENV_KEY=VALUE``).
69+ # Excludes empty variables and variables whose name do not follow POSIX (e.g. some
70+ # Bash environments on HPC clusters such as BSC MareNostrum5, ``BASH_FUNC_module%%=``).
7271while IFS=' =' read -r k v; do
7372 [[ -n " $k " ]] || continue
74- # If the current env doesn't exist (! -v) in the given baseline env (BASE_ENV),
75- # then we want to add it as --env in singularity.
76- if [[ ! -v BASE_ENV[$k ] ]]; then
77- ENV_ARGS+=(--env " $k =$v " )
73+ [[ " $k " =~ ^[A-Za-z_][A-Za-z0-9_]* $ ]] || continue
74+ # If the current env doesn't exist (``! -z``) in the given baseline env (``BASE_ENV``),
75+ # then we want to add it as ``--env`` in singularity.
76+ if [[ -z " ${BASE_ENV[$k]+x} " ]]; then
77+ # Debug
78+ # echo "Adding env var for Singularity command: SINGULARITYENV_$k=$v" >&2
79+ export " SINGULARITYENV_$k =$v "
7880 fi
79- done < <( env)
80-
81- # Debug
82- # echo "Adding env vars into Singularity command: ${#ENV_ARGS[@]}" >&2
81+ done < <( printenv)
8382
84- # Launch wrapped singularity command with the singularity executable, followed
85- # by the new --env arguments, and finally with all the rest the CWL runner had.
86- exec " $1 " " ${ENV_ARGS[@]} " " ${@: 2} "
83+ # Launch the Singularity binary.
84+ exec " $SINGULARITY_BIN " " ${@ } "
0 commit comments