-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathbackend_entrypoint.sh
More file actions
executable file
·209 lines (168 loc) · 5.26 KB
/
Copy pathbackend_entrypoint.sh
File metadata and controls
executable file
·209 lines (168 loc) · 5.26 KB
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
#!/usr/bin/env bash
set -eu
fail() {
printf >&2 "%s: %s\n" "$0" "$1"
exit 1
}
wait_for_db() {
wait-for-it "${CVAT_POSTGRES_HOST}:${CVAT_POSTGRES_PORT:-5432}" -t 0
}
wait_for_redis_inmem() {
wait-for-it "${CVAT_REDIS_INMEM_HOST}:${CVAT_REDIS_INMEM_PORT:-6379}" -t 0
}
wait_for_clickhouse() {
wait-for-it "${CLICKHOUSE_HOST}:${CLICKHOUSE_PORT:-8123}" -t 0
}
account_for_internal_proxy() {
CVAT_NUM_PROXIES="${CVAT_NUM_PROXIES:-0}"
if ! [[ "$CVAT_NUM_PROXIES" =~ ^[0-9]+$ ]]; then
fail "CVAT_NUM_PROXIES must be a non-negative integer"
fi
# The user-facing setting counts proxies before the backend container.
# Add the internal nginx hop that proxies requests to uvicorn.
export CVAT_NUM_PROXIES=$((CVAT_NUM_PROXIES + 1))
}
cmd_bash() {
exec bash "$@"
}
cmd_init() {
wait_for_db
~/manage.py migrate
wait_for_redis_inmem
~/manage.py migrateredis
~/manage.py syncperiodicjobs
if [[ "${CVAT_ANALYTICS:-0}" == "1" ]]; then
wait_for_clickhouse
python components/analytics/clickhouse/init.py
fi
}
_load_component_config() {
declare -gA merged_config=()
for config_file in ~/backend_entrypoint.d/*.conf; do
declare -A config=$(cat $config_file)
for key in "${!config[@]}"; do
if [[ -n ${merged_config[$key]+_} ]]; then
fail "Duplicated component definition: $key"
fi
merged_config[$key]=${config[$key]}
done
unset config
done
}
_get_includes() {
extra_configs=()
for key in "$@"; do
if [[ -z ${merged_config[$key]+_} ]]; then
fail "Unexpected component: $key"
fi
for include in ${merged_config["$key"]}; do
if ! [[ ${extra_configs[@]} =~ $include ]] && \
( ! [[ "$include" == "clamav" ]] || [[ "${CLAM_AV:-}" == "yes" ]] ); then
extra_configs+=("$include")
fi
done
done
if [ ${#extra_configs[@]} -gt 0 ]; then
printf 'reusable/%s.conf ' "${extra_configs[@]}"
fi
}
_get_reusable_includes() {
extra_configs=()
for include in "$@"; do
if ! [ -r "$HOME/supervisord/reusable/$include.conf" ]; then
fail "Unexpected supervisor include: $include"
fi
if ! [[ ${extra_configs[@]} =~ $include ]]; then
extra_configs+=("$include")
fi
done
if [ ${#extra_configs[@]} -gt 0 ]; then
printf 'reusable/%s.conf ' "${extra_configs[@]}"
fi
}
cmd_run() {
if [ "$#" -eq 0 ]; then
fail "run: at least 1 argument is expected"
fi
component="$1"
_load_component_config
case "$component" in
server|worker|worker-pool|nginx) ;;
*) fail "Unexpected run component: $component" ;;
esac
if [ "$component" = "nginx" ]; then
exec supervisord -c "supervisord/nginx.conf"
fi
if [ "$component" = "server" ]; then
account_for_internal_proxy
~/manage.py collectstatic --no-input
fi
wait_for_db
echo "waiting for migrations to complete..."
while ! ~/manage.py migrate --check; do
sleep 10
done
wait_for_redis_inmem
echo "waiting for Redis migrations to complete..."
while ! ~/manage.py migrateredis --check; do
sleep 10
done
supervisord_includes=""
postgres_app_name="cvat:$component"
if [ "$component" = "server" ]; then
supervisord_includes="$(_get_includes "server")$(_get_reusable_includes "${@:2}")"
elif [ "$component" = "worker" ] || [ "$component" = "worker-pool" ]; then
if [ "$#" -eq 1 ]; then
fail "run worker: expected at least 1 queue name"
fi
queues=()
extra_flags=()
for arg in "${@:2}"; do
if [[ "$arg" == --* ]]; then
extra_flags+=("$arg")
else
queues+=("$arg")
fi
done
if [ ${#queues[@]} -eq 0 ]; then
fail "run worker: expected at least 1 queue name"
fi
queue_list="${queues[*]}"
echo "Workers to run: $queue_list"
if [ ${#extra_flags[@]} -gt 0 ]; then
echo "Extra rqworker flags: ${extra_flags[*]}"
fi
export CVAT_QUEUES=$queue_list
export CVAT_RQWORKER_EXTRA_FLAGS="${extra_flags[*]:-}"
postgres_app_name+=":${queue_list// /+}"
supervisord_includes=$(_get_includes "${queues[@]}")
fi
echo "Additional supervisor configs that will be included: $supervisord_includes"
export CVAT_POSTGRES_APPLICATION_NAME=$postgres_app_name
export CVAT_SUPERVISORD_INCLUDES=$supervisord_includes
exec supervisord -c "supervisord/$component.conf"
}
if [ $# -eq 0 ]; then
echo >&2 "$0: at least one subcommand required"
echo >&2 ""
echo >&2 "available subcommands:"
echo >&2 " bash <bash args...>"
echo >&2 " init"
echo >&2 " run server [additional components]"
echo >&2 " run nginx"
echo >&2 " run worker <list of queues>"
exit 1
fi
for init_script in /etc/cvat/init.d/*; do
if [ -r "$init_script" ]; then
. "$init_script"
fi
done
while [ $# -ne 0 ]; do
if [ "$(type -t "cmd_$1")" != "function" ]; then
fail "unknown subcommand: $1"
fi
cmd_name="$1"
shift
"cmd_$cmd_name" "$@"
done