forked from cnobles/chiva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·411 lines (335 loc) · 11.1 KB
/
install.sh
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env bash
__conda_url=https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
read -r -d '' __usage <<-'EOF'
-e --environment [arg] Environment to install to. Default: "chiva"
-s --chiva_dir [arg] Location of chiva source code. Default: this directory
-c --conda [arg] Location of Conda installation. Default: ${PREFIX}
-u --update [arg] Update chiva [lib]rary, package [pkg], conda [env], or [all].
-r --requirements Install from requirements (hours) rather than build (minutes).
-t --test After installation, run test to check functionality.
-v --verbose Show subcommand output
-d --debug Run in debug mode.
-h --help Display this message and exit.
EOF
read -r -d '' __helptext <<-'EOF'
This script installs or upgrades cHIVa, including Conda (if not installed).
To upgrade, pass the '--upgrade all' option, then be sure to update your config
files using 'chiva config update'.
EOF
# Load BASH3Boilerplate for command-line parsing and logging
source etc/b3bp.sh
function __err_report() {
local error_code
error_code=${?}
error "Error in ${__file} in function ${1} on line ${2}"
exit ${error_code}
}
trap '__err_report "${FUNCNAME:-.}" ${LINENO}' ERR
# help mode
if [[ "${arg_h:?}" = "1" ]]; then
# Help exists with code 1
help "Help using ${0}:"
fi
# verbose mode
if [[ "${arg_v:?}" = "1" ]]; then
LOG_LEVEL="7"
fi
# debug mode
if [[ "${arg_d:?}" = "1" ]]; then
set -o xtrace
LOG_LEVEL="7"
fi
function debug_capture () {
debug "$(echo -e "$(${@})")"
}
function installation_error () {
error "${1} failed!"
if [[ "${arg_v:?}" != 1 && "${arg_d:?}" != 1 ]]; then
error "Try re-running with -v or -d, or file an issue on Github."
fi
exit 1
}
# Set variables
__conda_path="${arg_c:-${HOME}/miniconda3}"
__chiva_dir="${arg_s:-$(readlink -f ${__dir})}"
__chiva_env="${arg_e:-chiva}"
__run_chiva_tests=false
__reqs_install=false
__update_lib=false
__update_pkg=false
__update_env=false
__req_r_version="3.4.1"
__old_path=$PATH
__output=${2-/dev/stdout}
if [[ "${arg_t:?}" = "1" ]]; then
__run_chiva_tests=true
fi
if [[ "${arg_r:?}" = "1" ]]; then
__reqs_install=true
fi
if [[ "${arg_u}" = "all" || "${arg_u}" = "env" ]]; then
__update_lib=true
__update_env=true
__update_pkg=true
elif [[ "${arg_u}" = "lib" ]]; then
__update_lib=true
__update_pkg=false
elif [[ "${arg_u}" = "pkg" ]]; then
__update_lib=false
__update_pkg=true
fi
function __test_conda () {
command -v conda &> /dev/null && echo true || echo false
}
function __detect_conda_install () {
local discovered=$(__test_conda)
if [[ $discovered = true ]]; then
local conda_path="$(which conda)"
conda_path=${conda_path%'/condabin/conda'}
echo ${conda_path%'/bin/conda'}
fi
}
function __test_env () {
if [[ $(__test_conda) = true ]]; then
$(conda env list \
| cut -f1 -d' ' \
| grep -Fxq $__chiva_env > /dev/null) && \
echo true || echo false
else
echo false
fi
}
function __test_r_version () {
activate_chiva
local sem_version=$(R --version | grep 'R version' | cut -d ' ' -f 3)
if (( ${sem_version//./} >= ${__req_r_version//./} )); then
echo true
else
echo false
fi
deactivate_chiva
}
function __test_r_packages () {
activate_chiva
$(Rscript ${__chiva_dir}/etc/check_for_required_packages.R \
&> /dev/null) && echo true || echo false
deactivate_chiva
}
function __unittest_gintools () {
if [[ $(__test_env) = true ]]; then
activate_chiva
$(Rscript ${__chiva_dir}/tools/rscripts/check_gintools.R \
&> /dev/null) && echo true || echo false
deactivate_chiva
else
echo false
fi
}
function __test_gintools () {
if [[ $(__test_env) = true ]]; then
activate_chiva
$(Rscript ${__chiva_dir}/tools/rscripts/check_pkgs.R gintools \
&> /dev/null) && echo true || echo false
deactivate_chiva
else
echo false
fi
}
function __test_chivalib () {
if [[ $(__test_env) = true ]]; then
activate_chiva
command -v chiva &> /dev/null && echo true || echo false
deactivate_chiva
else
echo false
fi
}
function __test_chiva() {
if [[ $(__test_env) = true ]]; then
$(bash ${__chiva_dir}/tests/test.sh ${__chiva_env} &> /dev/null) && \
echo true || echo false
else
echo "fail"
fi
}
function __test_bashrc () {
grep conda.sh ~/.bashrc > /dev/null && echo true || echo false
}
function activate_chiva () {
set +o nounset
conda activate $__chiva_env
set -o nounset
}
function deactivate_chiva () {
set +o nounset
conda deactivate
set -o nounset
}
function install_conda () {
local tmpdir=$(mktemp -d)
debug "Downloading miniconda..."
debug_capture wget -q ${__conda_url} -O ${tmpdir}/miniconda.sh 2>&1
debug "Installing miniconda..."
debug_capture bash ${tmpdir}/miniconda.sh -b -p ${__conda_path} 2>&1
# Source conda into path
source ${__conda_path}/etc/profile.d/conda.sh
if [[ $(__test_conda) != true ]]; then
installation_error "Environment creation"
fi
rm ${tmpdir}/miniconda.sh
}
function install_environment () {
if [[ $__reqs_install == "true" ]]; then
local install_options="--quiet --file etc/requirements.yml"
debug_capture conda env update --name=$__chiva_env ${install_options} 2>&1
else
local install_options="--quiet --yes --file etc/build.b0.2.0.txt"
debug_capture conda create --name=$__chiva_env ${install_options} 2>&1
fi
if [[ $(__test_env) != true ]]; then
installation_error "Environment creation"
fi
if [[ $(__test_r_version) != true ]]; then
installation_error "Insufficient R-program version"
fi
if [[ $(__test_r_packages) != true ]]; then
installation_error "R-package installation"
fi
}
function install_gintools () {
activate_chiva
if [[ $(__unittest_gintools) != true ]]; then
installation_error "GinTools R-package unit tests"
else
debug_capture R CMD INSTALL tools/gintools &> /dev/null
fi
if [[ $(__test_gintools) != true ]]; then
installation_error "GinTools R-package installation"
fi
deactivate_chiva
}
function install_env_vars () {
activate_chiva
echo -ne "#/bin/sh\nexport CHIVA_DIR=${__chiva_dir}" > \
${CONDA_PREFIX}/etc/conda/activate.d/env_vars.sh
mkdir -p ${CONDA_PREFIX}/etc/conda/deactivate.d/
echo -ne "#/bin/sh\nunset CHIVA_DIR" > \
${CONDA_PREFIX}/etc/conda/deactivate.d/env_vars.sh
deactivate_chiva
}
function install_chivalib () {
activate_chiva
debug_capture pip install --upgrade ${__chiva_dir}/tools/chivalib/ 2>&1
if [[ $(__test_chivalib) != true ]]; then
installation_error "Library installation"
fi
deactivate_chiva
}
info "Starting cHIVa installation..."
info " Conda path: ${__conda_path}"
info " cHIVa src : ${__chiva_dir}"
info " cHIVa env : '${__chiva_env}'"
debug "Components detected:"
__conda_installed=$(__test_conda)
debug " Conda: ${__conda_installed}"
if [[ $__conda_installed = false ]]; then
PATH=$PATH:${__conda_path}/bin
__env_exists=$(echo false)
__gintoolspkg_installed=$(echo false)
__chivalib_installed=$(echo false)
elif [[ $__conda_installed = true ]]; then
# Source conda into shell
source ${__conda_path}/etc/profile.d/conda.sh
__env_exists=$(__test_env)
debug " Environment: ${__env_exists}"
__gintoolspkg_installed=$(__test_gintools)
debug " R-package: ${__gintoolspkg_installed}"
__chivalib_installed=$(__test_chivalib)
debug " Library: ${__chivalib_installed}"
fi
__env_changed=false
# Install Conda if necessary
if [[ $__conda_installed = true ]]; then
if [[ $(__detect_conda_install) != $__conda_path ]]; then
warning "Found pre-existing Conda installation in $(__detect_conda_install)".
warning "Ignoring specified Conda path in favor of existing Conda install."
__conda_path=$(__detect_conda_install)
fi
info "Conda already installed."
else
info "Installing Conda..."
install_conda
__env_changed=true
fi
# Source conda into shell
source ${__conda_path}/etc/profile.d/conda.sh
# Create Conda environment for cHIVa
if [[ $__env_exists = true && $__update_env = false ]]; then
info "Specified environment already exists (use '--update env' to update)"
else
if [[ $__reqs_install = "true" ]]; then
__build_source="etc/requirements.yml"
else
__build_source="etc/build.b0.2.0.txt"
fi
info "Creating cHIVa environment..."
info " Building from: $__build_source"
install_environment
__env_changed=true
info "$__chiva_env environment created."
fi
# Install iguideSupport into environment if changed or requested
if [[ $__env_changed = true ]]; then
info "Environment installed/updated; (re)installing GinTools R-package..."
install_gintools
elif [[ $__gintoolspkg_installed = false ]]; then
info "Installing GinTools R-package..."
install_gintools
elif [[ $__update_pkg = true ]]; then
info "Updating GinTools R-package..."
install_gintools
else
info "GinTools R-package already installed (use '--update pkg' to update)"
fi
# Always update the env_vars.sh in the cHIVa environment
debug "Updating \$CHIVA_DIR variable to point to ${__chiva_dir}"
info "Setting environmental variables..."
install_env_vars
# Install chivalib into environment if changed or requested
if [[ $__env_changed = true ]]; then
info "Environment installed/updated; (re)installing cHIVa library..."
install_chivalib
elif [[ $__chivalib_installed = false ]]; then
info "Installing cHIVa library..."
install_chivalib
elif [[ $__update_lib = true ]]; then
info "Updating cHIVa library..."
install_chivalib
else
info "cHIVa library already installed (use '--update lib' to update)"
fi
# Run tests if requested
if [[ $__run_chiva_tests = true ]]; then
info "Running cHIVa tests...(this may take a few mins)"
__chiva_tested=$(__test_chiva)
if [[ $__chiva_tested = true ]]; then
info " cHIVa Tests: passed"
else
warning " cHIVa Tests: FAILED"
warning " Try running the test outside of the install to confirm."
warning " Just run 'bash etc/tests/test.sh $__chiva_env'."
fi
fi
# Check if sourcing conda in .bashrc
if [[ $(__test_bashrc) = false ]]; then
warning "** Conda was not detected on your PATH. **"
warning "This is normal if you have not installed Conda before."
warning "To add it to your current .bashrc to be sourced during shell"
warning "startup, run:"
warning " 'echo \"# Added to activate conda within shell\" >> ~/.bashrc"
warning " 'echo \"source ${__conda_path}/etc/profile.d/conda.sh\" >> ~/.bashrc"
warning "and close and re-open your terminal session to apply."
warning "When finished, run 'conda activate ${__chiva_env}' to begin."
else
info "Done. Run 'conda activate ${__chiva_env}' to begin."
fi