-
Notifications
You must be signed in to change notification settings - Fork 42
/
install.sh
executable file
·251 lines (215 loc) · 7.23 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
#!/usr/bin/env bash
__conda_url=https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
__version_tag=$(if git describe --tags >/dev/null 2>&1 ; then git describe --tags; else echo v4.7.0; fi) # <--- Update this on each version release
__version_tag="${__version_tag:1}" # Remove the 'v' prefix
read -r -d '' __usage <<-'EOF'
-e --environment [arg] Environment to install to. Default: "sunbeam" followed by the version tag (e.g. sunbeam3.0.1)
-s --sunbeam_dir [arg] Location of Sunbeam source code. Default: this directory
-c --conda [arg] Location of Conda installation. Default: $CONDA_PREFIX
-u --update [arg] Update sunbeam [lib]rary, conda [env], or [all].
-v --verbose Show subcommand output.
-d --debug Run in debug mode.
-h --help Display this message and exit.
-w --version Display version information and exit.
EOF
read -r -d '' __helptext <<-'EOF'
This script installs or upgrades Sunbeam, including Conda (if not installed).
To upgrade, pass the '--upgrade all' option, then be sure to update your config
files using 'sunbeam 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 exits with code 1
help "Help using ${0}:"
fi
# version mode
if [[ "${arg_w:?}" = "1" ]]; then
echo "${__version_tag}"
exit 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}"
__sunbeam_dir="${arg_s:-$(readlink -f ${__dir})}"
__sunbeam_env="${arg_e:-sunbeam${__version_tag}}"
__update_lib=false
__update_env=false
if [[ "${arg_u}" = "all" || "${arg_u}" = "env" ]]; then
__update_lib=true
__update_env=true
elif [[ "${arg_u}" = "lib" ]]; then
__update_lib=true
fi
__old_path=$PATH
PATH=$PATH:${__conda_path}/bin
function __git_dir_exists() {
if [ -d ".git" ]; then
echo true
else
echo false
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)"
echo ${conda_path%'/bin/conda'}
fi
}
function __test_env() {
if [[ $(__test_conda) = true ]]; then
$(conda env list \
| cut -f1 -d' ' \
| grep -Fxq $__sunbeam_env > /dev/null) && \
echo true || echo false
else
echo false
fi
}
function __test_sunbeam() {
if [[ $(__test_env) = true ]]; then
activate_sunbeam
command -v sunbeam &> /dev/null && echo true || echo false
deactivate_sunbeam
else
echo false
fi
}
function enable_conda_activate () {
# Allow conda [de]activate in this script
CONDA_BASE=$(conda info --base) # see https://github.com/conda/conda/issues/7980
source $CONDA_BASE/etc/profile.d/conda.sh
}
function activate_sunbeam () {
enable_conda_activate
set +o nounset
conda activate $__sunbeam_env
set -o nounset
}
function deactivate_sunbeam () {
enable_conda_activate
set +o nounset
conda deactivate
set -o nounset
}
function install_conda () {
local tmpdir=$(mktemp -d)
debug "Downloading miniconda..."
debug_capture wget -nv ${__conda_url} -O ${tmpdir}/miniconda.sh 2>&1
debug "Installing miniconda..."
debug_capture bash ${tmpdir}/miniconda.sh -b -p ${__conda_path} 2>&1
if [[ $(__test_conda) != true ]]; then
installation_error "Environment creation"
fi
rm ${tmpdir}/miniconda.sh
}
function install_environment () {
debug_capture conda env create --name=$__sunbeam_env \
--quiet --file environment.yml
if [[ $(__test_env) != true ]]; then
installation_error "Environment creation"
fi
}
function install_env_vars () {
activate_sunbeam
mkdir -p ${CONDA_PREFIX}/etc/conda/activate.d
echo -ne "#/bin/sh\nexport SUNBEAM_DIR=${__sunbeam_dir}\nexport SUNBEAM_VER=${__version_tag}\nexport SUNBEAM_MIN_MEM_MB=8000\nexport SUNBEAM_MIN_RUNTIME=60" > \
${CONDA_PREFIX}/etc/conda/activate.d/env_vars.sh
mkdir -p ${CONDA_PREFIX}/etc/conda/deactivate.d
echo -ne "#/bin/sh\nunset SUNBEAM_DIR\nunset SUNBEAM_VER\nunset SUNBEAM_MIN_MEM_MB\nunset SUNBEAM_MIN_RUNTIME" > \
${CONDA_PREFIX}/etc/conda/deactivate.d/env_vars.sh
}
function install_sunbeamlib () {
activate_sunbeam
debug_capture pip install --upgrade $__sunbeam_dir 2>&1
if [[ $(__test_sunbeam) != true ]]; then
installation_error "Library installation"
fi
}
info "Starting Sunbeam installation..."
info " Conda path: ${__conda_path}"
info " Sunbeam src: ${__sunbeam_dir}"
info " Sunbeam env: '${__sunbeam_env}'"
debug "Components detected:"
__conda_installed=$(__test_conda)
debug " Conda: ${__conda_installed}"
__env_exists=$(__test_env)
debug " Environment: ${__env_exists}"
__sunbeam_installed=$(__test_sunbeam)
debug " Library: ${__sunbeam_installed}"
__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
conda config --set channel_priority strict # Set channel priority on new install
# Create Conda environment for Sunbeam
if [[ $__env_exists = true && $__update_env = false ]]; then
info "Specified environment already exists (use '--update env' to update)"
else
info "Creating Sunbeam environment..."
install_environment
__env_changed=true
fi
# Always update the env_vars.sh in the sunbeam environment
install_env_vars
source ${CONDA_PREFIX}/etc/conda/activate.d/env_vars.sh
info "Updated SUNBEAM_VER to ${SUNBEAM_VER}"
info "Updated SUNBEAM_DIR to ${SUNBEAM_DIR}"
# Install sunbeamlib into environment if changed or requested
if [[ $__env_changed = true ]]; then
info "Environment installed/updated; (re)installing Sunbeam library..."
install_sunbeamlib
elif [[ $__sunbeam_installed = false ]]; then
info "Installing Sunbeam library..."
install_sunbeamlib
elif [[ $__update_lib = true ]]; then
info "Updating Sunbeam library..."
install_sunbeamlib
else
info "Sunbeam library already installed (use '--update lib' to update)"
fi
info "Done. Run 'conda activate ${__sunbeam_env}' to begin."
info "Optionally, then run 'pytest tests/'"
info "to make sure the installation is working properly."
info "If the SUNBEAM_VER or SUNBEAM_DIR environment variables"
info "haven't updated, reactivate your environment."