-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathbackend_entrypoint.sh
executable file
·138 lines (109 loc) · 3.22 KB
/
backend_entrypoint.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
#!/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
}
cmd_bash() {
exec bash "$@"
}
cmd_init() {
wait_for_db
~/manage.py migrate
wait_for_redis_inmem
~/manage.py migrateredis
~/manage.py syncperiodicjobs
}
_get_includes() {
declare -A merged_config
for config in ~/backend_entrypoint.d/*.conf; do
declare -A config=$(cat $config)
for key in "${!config[@]}"; do
if [ -v merged_config[$key] ]; then
fail "Duplicated component definition: $key"
fi
merged_config[$key]=${config[$key]}
done
done
extra_configs=()
for component in "$@"; do
if ! [ -v merged_config[$component] ]; then
fail "Unexpected worker: $component"
fi
for include in ${merged_config["$component"]}; 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
}
cmd_run() {
if [ "$#" -eq 0 ]; then
fail "run: at least 1 argument is expected"
fi
component="$1"
if [ "$component" = "server" ]; then
~/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 "$component")
elif [ "$component" = "worker" ]; then
if [ "$#" -eq 1 ]; then
fail "run worker: expected at least 1 queue name"
fi
queue_list="${@:2}"
echo "Workers to run: $queue_list"
export CVAT_QUEUES=$queue_list
postgres_app_name+=":${queue_list// /+}"
supervisord_includes=$(_get_includes $queue_list)
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"
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