forked from sunbeam-labs/sunbeam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·238 lines (202 loc) · 6.58 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
#!/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: "sunbeam"
-s --sunbeam_dir [arg] Location of Sunbeam source code. Default: this directory
-c --conda [arg] Location of Conda installation. Default: ${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.
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 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}"
__sunbeam_dir="${arg_s:-$(readlink -f ${__dir})}"
__sunbeam_env="${arg_e:-sunbeam}"
__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 __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 update --name=$__sunbeam_env \
--quiet --file environment.yml
if [[ $(__test_env) != true ]]; then
installation_error "Environment creation"
fi
}
function install_env_vars () {
activate_sunbeam
echo -ne "#/bin/sh\nexport SUNBEAM_DIR=${__sunbeam_dir}" > \
${CONDA_PREFIX}/etc/conda/activate.d/env_vars.sh
echo -ne "#/bin/sh\nunset SUNBEAM_DIR" > \
${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
# 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
# Check again to ensure success
# 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
# Always update the env_vars.sh in the sunbeam environment
debug "Updating \$SUNBEAM_DIR variable to point to ${__sunbeam_dir}"
install_env_vars
# Check if on pre-existing path
if [[ $__old_path != *"${__conda_path}/bin"* ]]; then
warning "** Conda was not detected on your PATH. **"
warning "This is normal if you haven't installed Conda before."
warning "To add it to your path, run "
warning " 'echo \"export PATH=\$PATH:${__conda_path}/bin\" >> ~/.bashrc'"
warning "and close and re-open your terminal session to apply."
warning "When finished, run 'conda activate ${__sunbeam_env}' to begin."
else
info "Done. Run 'conda activate ${__sunbeam_env}' to begin."
fi