forked from openvinotoolkit/model_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovms_wrapper
executable file
·199 lines (186 loc) · 6.11 KB
/
ovms_wrapper
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
#!/bin/bash
#
# Copyright (c) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#set -x
SIGINT_HANDLER_CALLED=0
MODEL_SERVER_PID=
NGINX_PID=
MODEL_SERVER_EXIT_CODE=
NGINX_EXIT_CODE=
# Pass SIGINT from user to Model Server:
function sigint_handler() {
echo "SigInt hanlder"
SIGINT_HANDLER_CALLED=1
MODEL_SERVER_PID=$(cat /tmp/model_server_pid)
echo "Stopping Model Server, PID: $MODEL_SERVER_PID"
{ kill -s SIGINT "${MODEL_SERVER_PID}" 2>&1; } >/dev/null
sleep 1
if [ -z "${MODEL_SERVER_PID}" ] ; then
{ kill -s KILL "${MODEL_SERVER_PID}" 2>&1; } >/dev/null
fi
NGINX_PID=$(cat /tmp/nginx_pid)
echo "Stopping nginx, PID: $NGINX_PID"
{ kill -s QUIT "${NGINX_PID}" 2>&1; } >/dev/null
sleep 1
if [ -z "${NGINX_PID}" ] ; then
{ kill -s KILL "${NGINX_PID}" 2>&1; } >/dev/null
fi
echo "Model server exit code: $MODEL_SERVER_EXIT_CODE"
exit $MODEL_SERVER_EXIT_CODE
}
trap sigint_handler SIGINT
new_args=()
ACTION_GRPC_PORT=
ACTION_REST_PORT=
DELETE_NEXT_ARG=
GRPC_ORIGINAL_PORT=
GRPC_OVMS_NEW_PORT=
REST_ORIGINAL_PORT=
REST_OVMS_NEW_PORT=
for a in "$@"
do
echo "ARG: $a"
if [ "$DELETE_NEXT_ARG" != "" ] ; then
DELETE_NEXT_ARG=
continue
fi
if [ "$ACTION_GRPC_PORT" != "" ] ; then
GRPC_ORIGINAL_PORT=$a
ACTION_GRPC_PORT=
continue
fi
if [ "$ACTION_REST_PORT" != "" ] ; then
REST_ORIGINAL_PORT=$a
ACTION_REST_PORT=
continue
fi
if [ "$a" == "--port" ] ; then
ACTION_GRPC_PORT=1
continue
fi
if [ "$a" == "--rest_port" ] ; then
ACTION_REST_PORT=1
continue
fi
if [ "$a" == "--grpc_bind_address" ] ; then
DELETE_NEXT_ARG=1
continue
fi
if [ "$a" == "--rest_bind_address" ] ; then
DELETE_NEXT_ARG=1
continue
fi
if [[ "${a}" != "$(sed 's/ //g' <<< "${a}")" ]]; then
echo "This argument: ${a} will be changed to: '${a}'"
new_args+=("'$a'")
else
new_args+=("$a")
fi
echo "current args: " "${new_args[@]}"
done
echo "${new_args[@]}"
echo "Requested ports: GRPC: $GRPC_ORIGINAL_PORT ; REST: $REST_ORIGINAL_PORT "
# select random non-allocated port:
SELECTED_PORTS=$(python -c '
import sys
import random
not_allowed = [int(x) for x in sys.argv[1:] ]
first_port = random.choice([x for x in range(7000, 7999) if x not in not_allowed])
not_allowed.append(first_port)
second_port = random.choice([x for x in range(7000, 7999) if x not in not_allowed])
print(str(first_port) + "," + str(second_port))
' "${GRPC_ORIGINAL_PORT}" "${REST_ORIGINAL_PORT}" )
GRPC_OVMS_NEW_PORT=$(echo "${SELECTED_PORTS}" | cut -d',' -f1)
REST_OVMS_NEW_PORT=$(echo "${SELECTED_PORTS}" | cut -d',' -f2)
if [ "$GRPC_ORIGINAL_PORT" != "" ] ; then
new_args+=("--port=$GRPC_OVMS_NEW_PORT")
else
# default port shall be used if it is not provided:
GRPC_ORIGINAL_PORT=9178
new_args+=("--port=$GRPC_OVMS_NEW_PORT")
fi
if [ "$REST_ORIGINAL_PORT" != "" ] ; then
new_args+=("--rest_port=$REST_OVMS_NEW_PORT --rest_bind_address 127.0.0.1")
fi
(
sleep 1
echo "Processing Nginx configuration..."
cp /model_server.conf.template /etc/nginx/conf.d/model_server.conf
sed -i "s/MODEL_SERVER_GRPC_PORT/$GRPC_OVMS_NEW_PORT/g" /etc/nginx/conf.d/model_server.conf
sed -i "s/MODEL_SERVER_REST_PORT/$REST_OVMS_NEW_PORT/g" /etc/nginx/conf.d/model_server.conf
sed -i "s/NGINX_LISTEN_GRPC_PORT/$GRPC_ORIGINAL_PORT/g" /etc/nginx/conf.d/model_server.conf
sed -i "s/NGINX_LISTEN_REST_PORT/$REST_ORIGINAL_PORT/g" /etc/nginx/conf.d/model_server.conf
echo "Starting Nginx..."
nginx -g 'daemon off;' &
NGINX_PID=$!
echo "Nginx PID: $NGINX_PID"
echo "$NGINX_PID" > /tmp/nginx_pid
wait $NGINX_PID
NGINX_EXIT_CODE=$?
echo "Nginx job finished with an exit code: $NGINX_EXIT_CODE"
echo "$NGINX_EXIT_CODE" > /tmp/nginx_exit_code
echo "nginx" >> /tmp/job_finished
) &
(
echo "Starting Model Server..."
echo "${new_args[@]}" | { xargs /ovms/bin/./ovms --grpc_bind_address 127.0.0.1; } &
MODEL_SERVER_PID=$!
echo "Model Server PID: $MODEL_SERVER_PID"
echo "${MODEL_SERVER_PID}" > /tmp/model_server_pid
wait "${MODEL_SERVER_PID}"
MODEL_SERVER_EXIT_CODE=$?
echo "Model server job finished with an exit code: $MODEL_SERVER_EXIT_CODE"
echo "$MODEL_SERVER_EXIT_CODE" > /tmp/model_server_exit_code
echo "model_server" >> /tmp/job_finished
) &
for (( ; ; )) ; do
if [ -f /tmp/job_finished ]; then
echo "Job exited: $(cat /tmp/job_finished)"
break
else
sleep 1
fi
done
if [ "$SIGINT_HANDLER_CALLED" == "0" ] ; then
if [ -f /tmp/nginx_exit_code ] ; then
NGINX_EXIT_CODE=$(cat /tmp/nginx_exit_code)
echo "Nginx crashed with exit code: $NGINX_EXIT_CODE"
MODEL_SERVER_PID=$(cat /tmp/model_server_pid)
echo "Stopping ovms, PID: $MODEL_SERVER_PID"
{ kill -s SIGINT "${MODEL_SERVER_PID}" 2>&1; } >/dev/null
sleep 1
if [ -z "${MODEL_SERVER_PID}" ] ; then
{ kill -s KILL "${MODEL_SERVER_PID}" 2>&1; } >/dev/null
fi
exit "${NGINX_EXIT_CODE}"
fi
if [ -f /tmp/model_server_exit_code ]; then # model server crashed
MODEL_SERVER_EXIT_CODE=$(cat /tmp/model_server_exit_code)
echo "Model server existed with exit code: ${MODEL_SERVER_EXIT_CODE}"
NGINX_PID=$(cat /tmp/nginx_pid)
echo "Stopping nginx, PID: ${NGINX_PID}"
{ kill -s QUIT "${NGINX_PID}" 2>&1; } >/dev/null
sleep 1
if [ -z "${NGINX_PID}" ] ; then
kill -s KILL "${NGINX_PID}"
fi
exit "${MODEL_SERVER_EXIT_CODE}"
fi
fi
echo "SIGINT handler detected, waiting for cleanup..."
sleep 15
echo "Logic error: unexpected exit condition!"
exit 1