-
Notifications
You must be signed in to change notification settings - Fork 3
/
start.sh
executable file
·368 lines (302 loc) · 10.5 KB
/
start.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
#!/usr/bin/env bash
# ##################################################
# Script for starting docker environment for
#
version="1.0.2" # Sets version variable
#
# Templated from https://github.com/natelandau/shell-scripts
scriptTemplateVersion="1.3.0" # Version of scriptTemplate.sh that this script is based on
#
# 2019-05-20 DATE - v1.0.0 - First Creation
# 2020-02-19 DATE - v1.0.1 - Added prompt for BL API Key
# 2021-11-25 DATE - v1.0.2 - Improved security on Windows 10.
# - Can run with WSL1&2/Ubuntu without exposing Docker port 2375.
# - Can run on MacOS now.
# - Can run multiple instances on the same system.
#
# ##################################################
# Provide a variable with the location of this script.
scriptPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Source Scripting Utilities
# -----------------------------------
# These shared utilities provide many functions which are needed to provide
# the functionality in this boilerplate. This script will fail if they can
# not be found.
# -----------------------------------
utilsLocation="${scriptPath}/src/lib/shell-scripts/utils.sh" #
if [ -f "${utilsLocation}" ]; then
source "${utilsLocation}"
else
echo "Please find the file util.sh and add a reference to it in this script. Exiting."
exit 1
fi
# trapCleanup Function
# -----------------------------------
# Any actions that should be taken if the script is prematurely
# exited. Always call this function at the top of your script.
# -----------------------------------
function trapCleanup() {
echo ""
if is_dir "${tmpDir}"; then
rm -r "${tmpDir}"
fi
die "Exit trapped." # Edit this if you like.
}
# Set Flags
# -----------------------------------
# Flags which can be overridden by user input.
# Default values are below
# -----------------------------------
quiet=0
printLog=0
verbose=0
force=0
strict=0
debug=0
configure=0
make=0
args=()
# Set Temp Directory
# -----------------------------------
# Create temp directory with three random numbers and the process ID
# in the name. This directory is removed automatically at exit.
# -----------------------------------
tmpDir="/tmp/${scriptName}.$RANDOM.$RANDOM.$RANDOM.$$"
(umask 077 && mkdir "${tmpDir}") || {
die "Could not create temporary directory! Exiting."
}
# Logging
# -----------------------------------
# Log is only used when the '-l' flag is set.
# -----------------------------------
logFile="$HOME/Library/Logs/${scriptBasename}.log"
# Docker
# -----------------------------------
shopt -s expand_aliases # for docker alias
container="dimpl"
project="dimpl"
# Check if user wants to start a separate instance.
# Parameter #1 (a string) will be appended to the container name.
# (ex. "test" or "dev")
if [ -n "$1" ]; then
container="${container}_$1"
project=${container}
fi
export DIMPL_CONTAINER="$container"
# -----------------------------------
# Determine if running on Windows Subsystem for Linux (WSL), if so, setup a "docker" ailas
# that runs the "docker.exe" installed under Windows. (to avoid having to expose TLS port 2375)
# -----------------------------------
if [ `uname -r | grep -ic "microsoft"` -gt 0 ]; then
alias docker="'$(wslpath "`powershell.exe -c "(Get-Command docker).path"`" | tr -d "\r")'"
# Pass variables to powershell
export WSLENV=$WSLENV:DIMPL_CONTAINER
fi
# MAIN
# -----------------------------------
function mainScript() {
# If the .env file is not present, configure
if [[ ${configure} = 1 ]]; then
info "Overwriting any existing .env configuration file."
configure
elif is_not_file "${scriptPath}/.env"; then
info "No .env file found. Prompting for configuration info."
configure
else
info "Configured .env file found."
fi
# Make sure docker is installed then launch notebook
if type_not_exists 'docker'; then
die "docker command not found. Please install docker"
else
# Check if docker CLI version has "compose" integration.
# "docker-compose" will be phased out in the future.
# Use "docker compose" instead.
if [ "$(docker compose version 2>/dev/null)" ]; then
docker-compose() {
docker compose $@
}
fi
info "Launching/Restarting '$container' docker container"
# Pull docker image from Dockerhub if not using local image.
if [ "`grep -c "^[ \t]*image:.*:" docker-compose.yml`" -gt 0 ]; then
docker-compose pull
fi
docker-compose -p $project up -d
sleep 2
#info "Logging into Globus"
#docker exec -it --user jovyan dimpl_container globus login --no-local-server
# Generate Access URL
token=$(docker exec $container sh -c 'jupyter notebook list' | grep -o '\?token\=[^\ ]\+')
port=$(docker port $container | head -1 | cut -d":" -f2)
echo
echo "Access DIMPL with the following URL:"
echo " http://localhost:${port}/tree/work/notebooks${token}"
echo
fi
if [[ ${make} == 1 ]]; then
info "Running command 'make data' inside docker container"
makeDataset
fi
safeExit
}
function configure() {
if is_file "${scriptPath}/.env"; then
rm ./.env
fi
touch .env
input "Enter email address (Required for data requests from NCBI E-utils): "
read ENTREZ_EMAIL
if [[ ${ENTREZ_EMAIL} != "" ]]; then
echo "ENTREZ_EMAIL=${ENTREZ_EMAIL}" >> .env
fi
input "Enter NCBI E-utils API Key (Recommended, allows faster requests from E-utils): "
read ENTREZ_APIKEY
if [[ ${ENTREZ_APIKEY} != "" ]]; then
echo "ENTREZ_APIKEY=${ENTREZ_APIKEY}" >> .env
fi
input "Enter Breaker Lab API Key (Required for lab members, others can skip it): "
read BL_APIKEY
if [[ ${BL_APIKEY} != "" ]]; then
echo "BL_APIKEY=${BL_APIKEY}" >> .env
fi
seek_confirmation "Customize Rfam database location? (Used when running local copies of Rfam): "
if is_confirmed; then
input "Enter MYSQL database name: "
read MYSQL_DATABASE;
if [[ ${MYSQL_DATABASE} != "" ]]; then
echo "MYSQL_DATABASE=${MYSQL_DATABASE}" >> .env
fi
input "Enter MYSQL host: "
read MYSQL_HOST;
if [[ ${MYSQL_HOST} != "" ]]; then
echo "MYSQL_HOST=${MYSQL_HOST}" >> .env
fi
input "Enter MYSQL port: "
read MYSQL_PORT;
if [[ ${MYSQL_PORT} != "" ]]; then
echo "MYSQL_PORT=${MYSQL_PORT}" >> .env
fi
input "Enter MYSQL username: "
read MYSQL_USER;
if [[ ${MYSQL_USER} != "" ]]; then
echo "MYSQL_USER=${MYSQL_USER}" >> .env
fi
notice "Notice: Entering the MYSQL password below saves it in plaintext in the directory's .env file. \n
For greater security, leave the line below blank and use the -p or --password option when running start.sh"
input "Enter MYSQL password: "
read MYSQL_PASSWORD;
if [[ ${MYSQL_PASSWORD} != "" ]]; then
echo "MYSQL_PASSWORD=${MYSQL_PASSWORD}" >> .env
fi
fi
}
function makeDataset() {
parse_yaml docker-compose.yml "DOCKER_" > "${tmpDir}/parsed_yaml.txt"
source "${tmpDir}/parsed_yaml.txt"
docker exec -w /home/jovyan/work --user jovyan ${DOCKER_services_notebook_container_name} make data
}
############## Begin Options and Usage ###################
# Print usage
usage() {
echo -n "${scriptName} [OPTION]... [INSTANCE NAME SUFFIX]
Instance Name Suffix: [optional]
Used for starting multiple Dimpl docker containers.
Example: 'start.sh test' would create container name of 'dimpl_test'
Options:
-m, --make Run the 'make data' command inside docker-container upon completion
-c, --configure Prompt for environment variables, even if already present
-p, --password MYSQL password
--force Skip all user interaction. Implied 'Yes' to all actions.
-q, --quiet Quiet (no output)
-l, --log Print log to file
-s, --strict Exit script with null variables. i.e 'set -o nounset'
-v, --verbose Output more information. (Items echoed to 'verbose')
-d, --debug Runs script in BASH debug mode (set -x)
-h, --help Display this help and exit
--version Output version information and exit
"
}
# Iterate over options breaking -ab into -a -b when needed and --foo=bar into
# --foo bar
optstring=h
unset options
while (($#)); do
case $1 in
# If option is of type -ab
-[!-]?*)
# Loop over each character starting with the second
for ((i=1; i < ${#1}; i++)); do
c=${1:i:1}
# Add current char to options
options+=("-$c")
# If option takes a required argument, and it's not the last char make
# the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# If option is of type --foo=bar
--?*=*) options+=("${1%%=*}" "${1#*=}") ;;
# add --endopts for --
--) options+=(--endopts) ;;
# Otherwise, nothing special
*) options+=("$1") ;;
esac
shift
done
set -- "${options[@]}"
unset options
# Print help if no arguments were passed.
# Uncomment to force arguments when invoking the script
# [[ $# -eq 0 ]] && set -- "--help"
# Read the options and set stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; safeExit ;;
--version) echo "$(basename $0) ${version}"; safeExit ;;
-c|--configure) shift; configure=1 ;;
-m|--make) shift; make=1 ;;
-p|--password) shift; echo "Enter MYSQL Password: "; stty -echo; read MYSQLPASS; stty echo;
echo ;;
-v|--verbose) verbose=1 ;;
-l|--log) printLog=1 ;;
-q|--quiet) quiet=1 ;;
-s|--strict) strict=1;;
-d|--debug) debug=1;;
--force) force=1 ;;
--endopts) shift; break ;;
*) die "invalid option: '$1'." ;;
esac
shift
done
# Store the remaining part as arguments.
args+=("$@")
############## End Options and Usage ###################
# ############# ############# #############
# ## TIME TO RUN THE SCRIPT ##
# ## ##
# ## You shouldn't need to edit anything ##
# ## beneath this line ##
# ## ##
# ############# ############# #############
# Trap bad exits with your cleanup function
trap trapCleanup EXIT INT TERM
# Exit on error. Append '||true' when you run the script if you expect an error.
set -o errexit
# Run in debug mode, if set
if [ "${debug}" == "1" ]; then
set -x
fi
# Exit on empty variable
if [ "${strict}" == "1" ]; then
set -o nounset
fi
# Bash will remember & return the highest exitcode in a chain of pipes.
# This way you can catch the error in case mysqldump fails in `mysqldump |gzip`, for example.
set -o pipefail
# Run your script
mainScript
safeExit # Exit cleanly