This repository was archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_transparent_singularity.sh
More file actions
executable file
·228 lines (194 loc) · 8.4 KB
/
run_transparent_singularity.sh
File metadata and controls
executable file
·228 lines (194 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bash
#Deploy script for singularity Containers "Transparent Singularity"
#Creates wrapper scripts for all executables in a container's $DEPLOY_PATH
# singularity needs to be available
# for downloading images from nectar it needs curl installed
#11/07/2018
#by Steffen Bollmann <Steffen.Bollmann@cai.uq.edu.au> & Tom Shaw <t.shaw@uq.edu.au>
# set -e
echo "[DEBUG] This is the run_transparent_singularity.sh script"
echo "Singularity bindpath is: $SINGULARITY_BINDPATH"
echo "Singularity bindpath should at least have vnm path!"
_script="$(readlink -f ${BASH_SOURCE[0]})" ## who am i? ##
_base="$(dirname $_script)" ## Delete last component from $_script ##
echo "making sure this is not running in a symlinked directory (singularity bug)"
echo "path: $_base"
cd $_base
_base=`pwd -P`
echo "corrected path: $_base"
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--storage)
storage="$2"
shift # past argument
shift # past value
;;
-c|--container)
container="$2"
shift # past argument
shift # past value
;;
-u|--unpack)
unpack="$2"
shift # past argument
shift # past value
;;
-o|--singularity-opts)
singularity_opts="$2"
shift # past argument
shift # past value
;;
--default)
DEFAULT=YES
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [[ -n $1 ]]; then
container="$1"
fi
if [ -z "$container" ]; then
echo "-----------------------------------------------"
echo "Select the container you would like to install:"
echo "-----------------------------------------------"
echo "singularity container list:"
curl -s https://raw.githubusercontent.com/NeuroDesk/neurodesk/master/cvmfs/log.txt
echo " "
echo "-----------------------------------------------"
echo "usage examples:"
echo "./run_transparent_singularity.sh CONTAINERNAME"
echo "./run_transparent_singularity.sh --container convert3d_1.0.0_20210104.simg --storage docker"
echo "./run_transparent_singularity.sh convert3d_1.0.0_20210104.simg"
echo "./run_transparent_singularity.sh convert3d_1.0.0_20210104 --unpack true --singularity-opts '--bind /cvmfs'"
echo "-----------------------------------------------"
exit
else
echo "-------------------------------------"
echo "installing container ${container}"
echo "-------------------------------------"
# define mount points for this system
echo "-------------------------------------"
echo 'IMPORTANT: you need to set your system specific mount points in your .bashrc!: e.g. export SINGULARITY_BINDPATH="/opt,/data"'
echo "-------------------------------------"
fi
containerName="$(cut -d'_' -f1 <<< ${container})"
echo "containerName: ${containerName}"
containerVersion="$(cut -d'_' -f2 <<< ${container})"
echo "containerVersion: ${containerVersion}"
containerDateAndFileEnding="$(cut -d'_' -f3 <<< ${container})"
containerDate="$(cut -d'.' -f1 <<< ${containerDateAndFileEnding})"
containerEnding="$(cut -d'.' -f2 <<< ${containerDateAndFileEnding})"
echo "containerDate: ${containerDate}"
# if no container extension is given, assume .simg
if [ "$containerEnding" = "$containerDate" ]; then
containerEnding="simg"
container=${containerName}_${containerVersion}_${containerDate}.${containerEnding}
fi
echo "checking for singularity ..."
qq=`which singularity`
if [[ ${#qq} -lt 1 ]]; then
echo "This script requires singularity on your path. E.g. add module load singularity/2.4.2 to your .bashrc"
echo "If you are root try again as normal user"
exit 2
fi
echo "containerEnding: ${containerEnding}"
echo "checking if $container exists in the cvmfs cache ..."
if [[ -d "/cvmfs/neurodesk.ardc.edu.au/containers/${containerName}_${containerVersion}:${containerDate}" ]]; then
echo "$container exists in cvmfs"
storage="cvmfs"
container_pull="ln -s /cvmfs/neurodesk.ardc.edu.au/containers/${containerName}_${containerVersion}:${containerDate} $container"
else
echo "$container does not exists in cvmfs. Testing Oracle Object storage next: "
if curl --output /dev/null --silent --head --fail "https://objectstorage.us-ashburn-1.oraclecloud.com/n/nrrir2sdpmdp/b/neurodesk/o/$container"; then
echo "$container exists in the oracle cache"
storage="oracle"
echo "check if aria2 is installed ..."
qq=`which aria2c`
if [[ ${#qq} -lt 1 ]]; then
echo "aria2 is not installed. Defaulting to curl."
container_pull="curl -X GET https://objectstorage.us-ashburn-1.oraclecloud.com/n/nrrir2sdpmdp/b/neurodesk/o/$container -O"
else
container_pull="aria2c https://objectstorage.us-ashburn-1.oraclecloud.com/n/nrrir2sdpmdp/b/neurodesk/o/$container https://objectstorage.eu-zurich-1.oraclecloud.com/n/nrrir2sdpmdp/b/neurodesk/o/$container https://swift.rc.nectar.org.au/v1/AUTH_dead991e1fa847e3afcca2d3a7041f5d/neurodesk/$container"
fi
else
# fallback to docker
echo "$container does not exist in any cache - loading from docker!"
storage="docker"
container_pull="singularity pull --name $container docker://vnmd/${containerName}_${containerVersion}:${containerDate}"
fi
fi
echo "deploying in $_base"
echo "checking if container needs to be downloaded"
if [[ -e $container ]]; then
echo "container downloaded already. Remove to re-download!"
else
echo "pulling image now ..."
$container_pull
fi
# echo "making container executable"
# chmod a+x $container
# if [[ ${#qq} -lt 1 ]]; then
# echo "Something went wrong when making the container executable."
# fi
echo "checking which executables exist inside container"
echo "executing: singularity exec $singularity_opts --pwd $_base $container $_base/ts_binaryFinder.sh"
singularity exec $singularity_opts --pwd $_base $container $_base/ts_binaryFinder.sh
echo "checking if commands.txt exists now"
if [[ -f $_base/commands.txt ]]; then
echo "[DEBUG] run_transparent_singularity: This worked!"
else
echo "[DEBUG] run_transparent_singularity: Trying to set singularity bindpaths:"
export SINGULARITY_BINDPATH=/data
singularity exec $singularity_opts --pwd $_base $container $_base/ts_binaryFinder.sh
if [[ -f $_base/commands.txt ]]; then
echo "[DEBUG] run_transparent_singularity: This worked!"
else
echo "[DEBUG] run_transparent_singularity: Something is wrong with the Singularity Bindpath. Please check!"
exit 2
fi
fi
echo "create singularity executable for each regular executable in commands.txt"
# $@ parses command line options.
#test executable="fslmaths"
while read executable; do \
echo $executable > $_base/${executable}; \
echo "#!/usr/bin/env bash" > $executable
echo "export PWD=\`pwd -P\`" >> $executable
echo "singularity exec $singularity_opts --pwd \$PWD $_base/$container $executable \"\$@\"" >> $executable
chmod a+x $executable
done < $_base/commands.txt
echo "creating activate script that runs deactivate first in case it is already there"
echo "#!/usr/bin/env bash" > activate_${container}.sh
echo "source deactivate_${container}.sh $_base" >> activate_${container}.sh
echo -e "export PWD=\`pwd -P\`" >> activate_${container}.sh
echo -e 'export PATH="$PWD:$PATH"' >> activate_${container}.sh
echo -e 'echo "# Container in $PWD" >> ~/.bashrc' >> activate_${container}.sh
echo -e 'echo "export PATH="$PWD:\$PATH"" >> ~/.bashrc' >> activate_${container}.sh
chmod a+x activate_${container}.sh
echo "deactivate script"
echo pathToRemove=$_base | cat - ts_deactivate_ > temp && mv temp deactivate_${container}.sh
chmod a+x deactivate_${container}.sh
if [[ $unpack = "true" ]]
then
echo "unpacking singularity file to sandbox directory:"
sudo singularity build --sandbox temp $container
rm -rf $container
mv temp $container
fi
echo "create module files one directory up"
modulePath=$_base/../modules/`echo $container | cut -d _ -f 1`
echo $modulePath
mkdir $modulePath -p
moduleName=`echo $container | cut -d _ -f 2`
echo "#%Module####################################################################" > ${modulePath}/${moduleName}
echo "module-whatis ${container}" >> ${modulePath}/${moduleName}
echo "prepend-path PATH ${_base}" >> ${modulePath}/${moduleName}
echo "rm ${modulePath}/${moduleName}" >> ts_uninstall.sh