-
Notifications
You must be signed in to change notification settings - Fork 2
/
build
executable file
·280 lines (201 loc) · 6.24 KB
/
build
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
SCRIPT_NAME=$(basename "${0}")
DEFAULT_BUILD_SOURCE="RECIPE"
usage()
{
cat <<EOF
${SCRIPT_NAME}:
This script builds either the Singularity CONTAINER image (HCPprocessPipelines.simg) or
the Singularity SANDBOX directory (HCPprocessPipelinesSandbox).
The CONTAINER image can be built either from the RECIPE file (HCPprocessPipelines)
or from the SANDBOX directory (HCPprocessPipelinesSandbox) if it exists.
The SANDBOX directory can be built from the RECIPE file (HCPprocessPipelines)
or from the CONTAINER image (HCPprocessPipelines.simg) if it exists.
Usage: ${SCRIPT_NAME} PARAMETER ...
PARAMETERs are: [ ] = optional; < > = user supplied value
[--help] : show this usage information and exit
Build Source
------------
You can specify the source of the build using one of the following
command line options:
--source=[SANDBOX|RECIPE|CONTAINER]
--from=[SANDBOX|RECIPE|CONTAINER]
--from-sandbox (equivalent to --source=SANDBOX)
--from-recipe (equivalent to --source=RECIPE)
--from-container (equivalent to --source=CONTAINER)
The source defaults to ${DEFAULT_BUILD_SOURCE}
Build Target
------------
You must specify the target of the build using one of the following
command line options:
--target=[SANDBOX|CONTAINER]
--to=[SANDBOX|CONTAINER]
--sandbox (equivalent to --target=SANDBOX)
--container (equivalent to --target=CONTAINER)
Source and target combinations that do not make sense, such as
building a container from a container, are reported as errors.
Building from a source that does not exist (e.g. trying to
build a CONTAINER from a SANDBOX when the SANDBOX does not
exist) is reported as an error.
EOF
}
abort()
{
local error_msg
error_msg="${1}"
echo ""
usage
echo "--------------------------------------------------"
echo "ERROR: ${error_msg}"
echo "--------------------------------------------------"
echo ""
exit 1
}
get_options()
{
local arguments=($@)
# initialize global output variables
g_source="${DEFAULT_BUILD_SOURCE}"
unset g_target
# parse arguments
local num_args=${#arguments[@]}
local argument
local index=0
while [ ${index} -lt ${num_args} ]; do
argument=${arguments[index]}
case ${argument} in
--help)
usage
exit 1
;;
--source=*)
g_source=${argument/*=/""}
g_source=$(echo ${g_source} | tr '[:lower:]' '[:upper:]')
index=$(( index + 1 ))
;;
--from=*)
g_source=${argument/*=/""}
g_source=$(echo ${g_source} | tr '[:lower:]' '[:upper:]')
index=$(( index + 1 ))
;;
--from-sandbox)
g_source="SANDBOX"
index=$(( index + 1 ))
;;
--from-recipe)
g_source="RECIPE"
index=$(( index + 1 ))
;;
--from-container)
g_source="CONTAINER"
index=$(( index + 1 ))
;;
--target=*)
g_target=${argument/*=/""}
g_target=$(echo ${g_target} | tr '[:lower:]' '[:upper:]')
index=$(( index + 1 ))
;;
--to=*)
g_target=${argument/*=/""}
g_target=$(echo ${g_target} | tr '[:lower:]' '[:upper:]')
index=$(( index + 1 ))
;;
--container)
g_target="CONTAINER"
index=$(( index + 1 ))
;;
--sandbox)
g_target="SANDBOX"
index=$(( index + 1 ))
;;
*)
echo "Unrecognized option: ${argument}"
exit 1
;;
esac
done
# check parameters
if [ -z "${g_source}" ]; then
abort "source (--source=[RECIPE|SANDBOX|CONTAINER], --to=[RECIPE|SANDBOX|CONTAINER], --from-sandbox, --from-recipe, --from-container) required"
fi
if [ "${g_source}" != "RECIPE" -a "${g_source}" != "SANDBOX" -a "${g_source}" != "CONTAINER" ]; then
abort "source must be RECIPE, SANDBOX, or CONTAINER"
else
echo "INFO: source: ${g_source}"
fi
if [ -z "${g_target}" ]; then
abort "target (--target=[CONTAINER|SANDBOX], --to=[CONTAINER|SANDBOX], --container, --sandbox) required"
fi
if [ "${g_target}" != "CONTAINER" -a "${g_target}" != "SANDBOX" ]; then
abort "target must be CONTAINER or SANDBOX"
else
echo "INFO: target: ${g_target}"
fi
}
main()
{
get_options "$@"
if [ "${g_source}" = "RECIPE" ]; then
if [ -e "HCPprocessPipelines" ]; then
if [ "${g_target}" = "CONTAINER" ]; then
# recipe --> container
echo "Building: RECIPE --> CONTAINER"
if [ -e "HCPprocessPipelines.simg" ]; then
rm HCPprocessPipelines.simg
fi
time sudo singularity build HCPprocessPipelines.simg HCPprocessPipelines
elif [ "${g_target}" = "SANDBOX" ]; then
# recipe --> sandbox
echo "Building: RECIPE --> SANDBOX"
if [ -e "HCPprocessPipelinesSandbox" ]; then
sudo rm -rf HCPprocessPipelinesSandbox
fi
time sudo singularity build --sandbox HCPprocessPipelinesSandbox/ HCPprocessPipelines
else
abort "Unrecognized target: ${g_target}"
fi
else
abort "Cannot build from RECIPE because it does not exist"
fi
elif [ "${g_source}" = "SANDBOX" ]; then
if [ -e "HCPprocessPipelinesSandbox" ]; then
if [ "${g_target}" = "CONTAINER" ]; then
# sandbox --> container
echo "Building: SANDBOX --> CONTAINER"
if [ -e "HCPprocessPipelines.simg" ]; then
rm HCPprocessPipelines.simg
fi
time sudo singularity build HCPprocessPipelines.simg HCPprocessPipelinesSandbox/
elif [ "${g_target}" = "SANDBOX" ]; then
# sandbox --> sandbox
abort "Cannot build SANDBOX from SANDBOX"
else
abort "Unrecognized target: ${g_target}"
fi
else
abort "Cannot build from SANDBOX because it does not exist"
fi
elif [ "${g_source}" = "CONTAINER" ]; then
if [ -e "HCPprocessPipelines.simg" ]; then
if [ "${g_target}" = "CONTAINER" ]; then
# container --> container
abort "Cannot build CONTAINER from CONTAINER"
elif [ "${g_target}" = "SANDBOX" ]; then
# container --> sandbox
echo "Building: CONTAINER --> SANDBOX"
if [ -e "HCPprocessPipelinesSandbox" ]; then
sudo rm -rf HCPprocessPipelinesSandbox
fi
time sudo singularity build --sandbox HCPprocessPipelinesSandbox/ HCPprocessPipelines.simg
else
abort "Unrecognized target: ${g_target}"
fi
else
abort "Cannot build from CONTAINER because it does not exist"
fi
else
abort "Unrecognized source: ${g_source}"
fi
}
# Invoke the main function to get things started
main "$@"