|
| 1 | +#!/bin/bash |
| 2 | +# shellcheck disable=SC2155 |
| 3 | + |
| 4 | +# default values |
| 5 | +# shellcheck disable=SC2076 |
| 6 | +if [ -z "$PIPE_CONDOR_STATUS" ]; then |
| 7 | + export PIPE_CONDOR_STATUS=enable |
| 8 | +elif [[ ! " enable disable " =~ " $PIPE_CONDOR_STATUS " ]]; then |
| 9 | + echo "Warning: unsupported value $PIPE_CONDOR_STATUS for PIPE_CONDOR_STATUS; disabling" |
| 10 | + export PIPE_CONDOR_STATUS=disable |
| 11 | +fi |
| 12 | +if [ -z "$PIPE_CONDOR_DIR" ]; then |
| 13 | + export PIPE_CONDOR_DIR=~/nobackup/pipes |
| 14 | +fi |
| 15 | +export PIPE_CONDOR_DIR=$(readlink -f "$PIPE_CONDOR_DIR") |
| 16 | +mkdir -p "$PIPE_CONDOR_DIR" |
| 17 | +# ensure the pipe dir is bound |
| 18 | +export APPTAINER_BIND=${APPTAINER_BIND}${APPTAINER_BIND:+,}${PIPE_CONDOR_DIR} |
| 19 | + |
| 20 | +# concept based on https://stackoverflow.com/questions/32163955/how-to-run-shell-script-on-host-from-docker-container |
| 21 | + |
| 22 | +# execute command sent to host pipe; send output to container pipe; store exit code |
| 23 | +listenhost(){ |
| 24 | + # stop when host pipe is removed |
| 25 | + while [ -e "$1" ]; do |
| 26 | + # "|| true" is necessary to stop "Interrupted system call" |
| 27 | + # must be *inside* eval to ensure EOF once command finishes |
| 28 | + # now replaced with assignment of exit code to local variable (which also returns true) |
| 29 | + tmpexit=0 |
| 30 | + eval "$(cat "$1") || tmpexit="'$?' >& "$2" |
| 31 | + echo "$tmpexit" > "$3" |
| 32 | + done |
| 33 | +} |
| 34 | +export -f listenhost |
| 35 | + |
| 36 | +# creates randomly named pipe and prints the name |
| 37 | +makepipe(){ |
| 38 | + PREFIX="$1" |
| 39 | + PIPETMP=${PIPE_CONDOR_DIR}/${PREFIX}_$(uuidgen) |
| 40 | + mkfifo "$PIPETMP" |
| 41 | + echo "$PIPETMP" |
| 42 | +} |
| 43 | +export -f makepipe |
| 44 | + |
| 45 | +# to be run on host before launching each apptainer session |
| 46 | +startpipe(){ |
| 47 | + HOSTPIPE=$(makepipe HOST) |
| 48 | + CONTPIPE=$(makepipe CONT) |
| 49 | + EXITPIPE=$(makepipe EXIT) |
| 50 | + # export pipes to apptainer |
| 51 | + echo "export APPTAINERENV_HOSTPIPE=$HOSTPIPE; export APPTAINERENV_CONTPIPE=$CONTPIPE; export APPTAINERENV_EXITPIPE=$EXITPIPE" |
| 52 | +} |
| 53 | +export -f startpipe |
| 54 | + |
| 55 | +# sends function to host, then listens for output, and provides exit code from function |
| 56 | +call_host(){ |
| 57 | + if [ "${FUNCNAME[0]}" = "call_host" ]; then |
| 58 | + FUNCTMP= |
| 59 | + else |
| 60 | + FUNCTMP="${FUNCNAME[0]}" |
| 61 | + fi |
| 62 | + echo "cd $PWD; $FUNCTMP $*" > "$HOSTPIPE" |
| 63 | + cat < "$CONTPIPE" |
| 64 | + return "$(cat < "$EXITPIPE")" |
| 65 | +} |
| 66 | +export -f call_host |
| 67 | + |
| 68 | +# from https://stackoverflow.com/questions/1203583/how-do-i-rename-a-bash-function |
| 69 | +copy_function() { |
| 70 | + test -n "$(declare -f "$1")" || return |
| 71 | + eval "${_/$1/$2}" |
| 72 | + eval "export -f $2" |
| 73 | +} |
| 74 | +export -f copy_function |
| 75 | + |
| 76 | +if [ -z "$APPTAINER_ORIG" ]; then |
| 77 | + export APPTAINER_ORIG=$(which apptainer) |
| 78 | +fi |
| 79 | +# always set this (in case of nested containers) |
| 80 | +export APPTAINERENV_APPTAINER_ORIG=$APPTAINER_ORIG |
| 81 | + |
| 82 | +apptainer(){ |
| 83 | + if [ "$PIPE_CONDOR_STATUS" = "disable" ]; then |
| 84 | + ( |
| 85 | + # shellcheck disable=SC2030 |
| 86 | + export APPTAINERENV_PIPE_CONDOR_STATUS=disable |
| 87 | + $APPTAINER_ORIG "$@" |
| 88 | + ) |
| 89 | + else |
| 90 | + # in subshell to contain exports |
| 91 | + ( |
| 92 | + # shellcheck disable=SC2031 |
| 93 | + export APPTAINERENV_PIPE_CONDOR_STATUS=enable |
| 94 | + # only start pipes on host |
| 95 | + # i.e. don't create more pipes/listeners for nested containers |
| 96 | + if [ -z "$APPTAINER_CONTAINER" ]; then |
| 97 | + eval "$(startpipe)" |
| 98 | + listenhost "$APPTAINERENV_HOSTPIPE" "$APPTAINERENV_CONTPIPE" "$APPTAINERENV_EXITPIPE" & |
| 99 | + LISTENER=$! |
| 100 | + fi |
| 101 | + # actually run apptainer |
| 102 | + $APPTAINER_ORIG "$@" |
| 103 | + # avoid dangling cat process after exiting container |
| 104 | + # (again, only on host) |
| 105 | + if [ -z "$APPTAINER_CONTAINER" ]; then |
| 106 | + pkill -P "$LISTENER" |
| 107 | + rm -f "$APPTAINERENV_HOSTPIPE" "$APPTAINERENV_CONTPIPE" "$APPTAINERENV_EXITPIPE" |
| 108 | + fi |
| 109 | + ) |
| 110 | + fi |
| 111 | +} |
| 112 | +export -f apptainer |
| 113 | + |
| 114 | +# on host: get list of condor executables |
| 115 | +if [ -z "$APPTAINER_CONTAINER" ]; then |
| 116 | + export APPTAINERENV_HOSTFNS=$(compgen -c | grep ^condor_) |
| 117 | +# in container: replace with call_host versions |
| 118 | +elif [ "$PIPE_CONDOR_STATUS" = "enable" ]; then |
| 119 | + # shellcheck disable=SC2153 |
| 120 | + for HOSTFN in $HOSTFNS; do |
| 121 | + copy_function call_host "$HOSTFN" |
| 122 | + done |
| 123 | +fi |
0 commit comments