-
Notifications
You must be signed in to change notification settings - Fork 967
/
common_startup_functions.sh
231 lines (215 loc) · 9.38 KB
/
common_startup_functions.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
#!/bin/sh
__CONDA_INFO=
parse_common_args() {
INITIALIZE_TOOL_DEPENDENCIES=1
# Pop args meant for common_startup.sh
while :
do
case "$1" in
--skip-eggs|--skip-wheels|--skip-samples|--dev-wheels|--no-create-venv|--no-replace-pip|--replace-pip|--skip-client-build)
common_startup_args="$common_startup_args $1"
shift
;;
--skip-tool-dependency-initialization)
INITIALIZE_TOOL_DEPENDENCIES=0
shift
;;
--skip-venv)
skip_venv=1
common_startup_args="$common_startup_args $1"
shift
;;
--stop-daemon|stop)
common_startup_args="$common_startup_args --stop-daemon"
paster_args="$paster_args --stop-daemon"
pid_log_paster_args="--pid-file \"$PID_FILE\""
uwsgi_args="$uwsgi_args --stop \"$PID_FILE\""
stop_daemon_arg_set=1
shift
;;
--restart|restart)
paster_args="$paster_args restart"
pid_log_paster_args="--pid-file \"$PID_FILE\" --log-file \"$LOG_FILE\""
uwsgi_args="$uwsgi_args --reload \"$PID_FILE\""
restart_arg_set=1
daemon_or_restart_arg_set=1
shift
;;
--daemon|start)
paster_args="$paster_args --daemon"
pid_log_paster_args="--pid-file \"$PID_FILE\" --log-file \"$LOG_FILE\""
# --daemonize2 waits until after the application has loaded
# to daemonize, thus it stops if any errors are found
uwsgi_args="--master --daemonize2 \"$LOG_FILE\" --pidfile2 \"$PID_FILE\" $uwsgi_args"
daemon_or_restart_arg_set=1
shift
;;
--status|status)
paster_args="$paster_args $1"
pid_log_paster_args="--pid-file \"$PID_FILE\""
shift
;;
--wait)
wait_arg_set=1
shift
;;
"")
break
;;
*)
paster_args="$paster_args $1"
uwsgi_args="$uwsgi_args $1"
shift
;;
esac
done
}
run_common_start_up() {
./scripts/common_startup.sh $common_startup_args || exit 1
}
conda_activate() {
: ${GALAXY_CONDA_ENV:="_galaxy_$(get_galaxy_major_version)"}
echo "Activating Conda environment: $GALAXY_CONDA_ENV"
# Dash is actually supported by 4.4, but not with `. /path/to/activate`, only `conda activate`, which we
# can't load unless we know the path to <conda_root>/etc/profile.d/conda.sh
if ! command -v source >/dev/null; then
echo "WARNING: Your shell is not supported with Conda, attempting to use Conda env"
echo " '$GALAXY_CONDA_ENV' with manual environment setup. To avoid this"
echo " message, use a supported shell or activate the environment before"
echo " starting Galaxy."
PATH="$(get_conda_env_path $GALAXY_CONDA_ENV)/bin:$PATH"
CONDA_DEFAULT_ENV="$GALAXY_CONDA_ENV"
CONDA_PREFIX="$(get_conda_root_path)"
else
source activate "$GALAXY_CONDA_ENV"
fi
}
setup_python() {
# If there is a .venv/ directory, assume it contains a virtualenv that we
# should run this instance in.
: ${GALAXY_VIRTUAL_ENV:=.venv}
# $GALAXY_CONDA_ENV isn't set here to avoid running the version check if not using Conda
if [ -d "$GALAXY_VIRTUAL_ENV" -a -z "$skip_venv" ]; then
[ -n "$PYTHONPATH" ] && { echo 'Unsetting $PYTHONPATH'; unset PYTHONPATH; }
echo "Activating virtualenv at $GALAXY_VIRTUAL_ENV"
. "$GALAXY_VIRTUAL_ENV/bin/activate"
elif [ -z "$skip_venv" ]; then
set_conda_exe
if [ -n "$CONDA_EXE" ] && \
check_conda_env ${GALAXY_CONDA_ENV:="_galaxy_$(get_galaxy_major_version)"}; then
# You almost surely have pip >= 8.1 and running `conda install ... pip>=8.1` every time is slow
REPLACE_PIP=0
[ -n "$PYTHONPATH" ] && { echo 'Unsetting $PYTHONPATH'; unset PYTHONPATH; }
if [ "$CONDA_DEFAULT_ENV" != "$GALAXY_CONDA_ENV" ]; then
conda_activate
fi
fi
fi
# If you are using --skip-venv we assume you know what you are doing but warn
# in case you don't.
[ -n "$PYTHONPATH" ] && echo 'WARNING: $PYTHONPATH is set, this can cause problems importing Galaxy dependencies'
python ./scripts/check_python.py || exit 1
}
set_galaxy_config_file_var() {
if [ -z "$GALAXY_CONFIG_FILE" ]; then
GALAXY_CONFIG_FILE=$(PYTHONPATH=lib python -c "from __future__ import print_function; from galaxy.util.properties import find_config_file; print(find_config_file(['galaxy', 'universe_wsgi']) or '')")
export GALAXY_CONFIG_FILE
fi
}
find_server() {
server_config=$1
server_app=$2
arg_getter_args=
default_webserver="paste"
case "$server_config" in
*.y*ml|''|none)
default_webserver="uwsgi" # paste incapable of this
;;
esac
APP_WEBSERVER=${APP_WEBSERVER:-$default_webserver}
if [ "$APP_WEBSERVER" = "uwsgi" ];
then
# Look for uwsgi
if [ -z "$skip_venv" -a -x $GALAXY_VIRTUAL_ENV/bin/uwsgi ]; then
UWSGI=$GALAXY_VIRTUAL_ENV/bin/uwsgi
elif command -v uwsgi >/dev/null 2>&1; then
UWSGI=uwsgi
else
echo 'ERROR: Could not find uwsgi executable'
exit 1
fi
[ "$server_config" != "none" ] && arg_getter_args="-c \"$server_config\""
[ -n "$server_app" ] && arg_getter_args="$arg_getter_args --app $server_app"
run_server="$UWSGI"
server_args=
if [ -z "$stop_daemon_arg_set" -a -z "$restart_arg_set" ]; then
server_args="$(eval python ./scripts/get_uwsgi_args.py $arg_getter_args)"
fi
server_args="$server_args $uwsgi_args"
pid_log_paster_args=""
elif [ "$APP_WEBSERVER" = "gunicorn" ];
then
export GUNICORN_CMD_ARGS="${GUNICORN_CMD_ARGS:-\"--bind=localhost:8080\"}"
server_args="$APP_WEBSERVER --pythonpath lib --paste \"$server_config\""
else
run_server="python"
server_args="./scripts/paster.py serve \"$server_config\" $paster_args"
fi
}
get_galaxy_major_version() {
PYTHONPATH='lib' python -c 'from galaxy.version import VERSION_MAJOR; print(VERSION_MAJOR)'
}
# Prior to Conda 4.4, the setup method was to add <conda_root>/bin to $PATH. Beginning with 4.4, that method is still
# possible, but the preferred method is to source <conda_root>/etc/profile.d/conda.sh. If the new method is used, the
# base environment will *not* be on $PATH, unlike previous versions, and `conda` is a shell function not available to
# subshells (e.g. scripts). Additionally, in Conda >= 4.4 (and sometimes in Conda < 4.4 due to bugs), an activated
# non-root/base environment will not have a symlink to `conda`. Beginning with 4.5, $CONDA_EXE will be set to the path
# to the `conda` script in the base environment. Thus in Conda 4.4, it may not be possible to locate `conda` even if you
# are using Conda.
set_conda_exe() {
[ -z "$_CONDA_EXE_SET" ] || return 0
if python -V 2>&1 | grep -q -e 'Anaconda' -e 'Continuum Analytics' || \
python -c 'import sys; print(sys.version.replace("\n", " "))' | grep -q -e 'packaged by conda-forge' ; then
: ${CONDA_EXE:=$(command -v conda)}
if [ -z "$CONDA_EXE" ]; then
echo "WARNING: \`python\` is from conda, but the \`conda\` command cannot be found."
pydir="$(dirname $(command -v python))"
for CONDA_EXE in $pydir/conda $pydir/../../../bin/conda; do
[ -x "$CONDA_EXE" ] && break || unset CONDA_EXE
done
if [ -z "$CONDA_EXE" ]; then
echo "WARNING: Unable to guess conda location, if you are using Conda 4.4, upgrade to"
echo " Conda 4.5 or activate the base environment prior to starting Galaxy:"
echo " $ conda activate base"
else
echo "Guessed conda location: $CONDA_EXE"
PATH="$(dirname $CONDA_EXE):$PATH"
fi
else
echo "Found conda at: $CONDA_EXE"
fi
_CONDA_EXE_SET=1
fi
}
set_conda_info() {
# cache conda info to avoid the cost of running it multiple times
if [ -z "$__CONDA_INFO" -o "$1" = "reset" ]; then
__CONDA_INFO="$(${CONDA_EXE:-conda} info --json)"
fi
}
get_conda_root_path() {
set_conda_info
printf "%s" "$__CONDA_INFO" \
| python -c "import json, sys; print(json.load(sys.stdin)['root_prefix'])"
}
check_conda_env() {
# envs listed in ~/.conda/environments.txt show up in envs.txt but can't be activated by name. =/
set_conda_info
printf "%s" "$__CONDA_INFO" \
| python -c "import json, os.path, sys; info=json.load(sys.stdin); sys.exit(0 if '$1' in [os.path.basename(p) for p in info['envs'] if os.path.dirname(p) in info['envs_dirs']] else 1)"
}
get_conda_env_path() {
set_conda_info
printf "%s" "$__CONDA_INFO" \
| python -c "import json, os.path, sys; info=json.load(sys.stdin); print([p for p in info['envs'] if os.path.basename(p) == '$1' and os.path.dirname(p) in info['envs_dirs']][0])"
}