-
Notifications
You must be signed in to change notification settings - Fork 66
/
functions
341 lines (303 loc) · 8.3 KB
/
functions
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
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.
BOOTUP=color
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
STOP_TIMEOUT=15 # seconds
APP_SRC=$(readlink -f $0)
if [ "$1" == "config" ]; then _CONFIG=1; else _CONFIG=0; fi
###############################################################################
# helper functions
###############################################################################
# Infer if this is in Workbench by scanning the networks.
IN_NVWB=$(docker container inspect `hostname` | jq '.[0].NetworkSettings.Networks.workbench or false')
if [ "$IN_NVWB" == "true" ]; then
# Determine the network config for apps
DOCKER_NETWORK=" --network=workbench "
# Determine the best prefix for the app name
else
DOCKER_NETWORK=" "
fi
# resolve a mount point to the host's directory
# usage: hostpath LOCAL_PATH
function hostpath() {
local localpath
localpath=$(/bin/bash -c "cd $1; pwd")
docker inspect $(hostname) --format json | \
jq -r '.[0].HostConfig.Mounts[] | select(.Target | startswith("'$localpath'")).Source'
}
# extract settings from env variables
# usage: config_lkp VAR DEFAULT
function config_lkp() {
local varname default
varname="$1"
default="$2"
if [[ $_CONFIG == 1 ]]; then
echo "$varname=$default" >&2
fi
echo ${!varname:-$2}
}
###############################################################################
# lifecycle functions
###############################################################################
# restart the contiainer
# usage: restart CONTAINER_NAME
function restart() {
stop "$1"
start "$1"
}
# local wrapper for docker run and stop that include logging
function docker_run() {
exec "Starting the container" _docker_run $@
}
function docker_stop() {
exec "Stopping the container" _docker_stop $@
}
# ensure the container is started and running
# usage: start CONTAINER_NAME
function start() {
local status_code name
name="$1"
{ status "$name" > /dev/null; status_code=$?; } || true
case $status_code in
0) success; echo "Container is already running." >&2 ;;
1) echo "Container is stopped. Recreating." >&2; docker_stop; docker_run ;;
2) echo "Container does not exist. Starting." >&2; docker_run ;;
esac
}
# ensure the container is stopped
# usage: stop CONTAINER_NAME
function stop() {
local status_code name
name="$1"
{ status "$name" > /dev/null; status_code=$?; } || true
case $status_code in
0) echo "Container is running. Stopping." >&2; docker_stop;;
1) echo "Container is stopped. Removing." >&2; docker_stop;;
2) success; echo -n "Container does not exist.";;
*) ;;
esac
}
# check the status of the associated container image
# usage: status CONTAINER_NAME
# status codes:
# 0: Container is running
# 1: Container exists, but is not running
# 2: Container does not exist
function status() {
local name
name="$1"
state=$( (docker inspect \
--format '{{.State.Status}}' \
$name 2> /dev/null \
|| echo -n "stopped") | xargs echo)
echo "Container status: $state"
case "$state" in
stopped)
return 2
;;
running)
health "$name"
;;
created | exited | paused | dead)
return 1
;;
*)
echo "Unrecognized container state: $state" >&2
exit 1
;;
esac
}
# check the health of the associated container image
# usage: health CONTAINER_NAME
# status codes:
# 0: Container is healthy or starting
# 1: Container is not healthy
function health() {
local name
name="$1"
state=$(docker inspect \
--format '{{.State.Health.Status}}' \
$name 2>&1 | head -n 1)
echo "Container health: $state"
case "$state" in
healthy | starting | "")
return 0
;;
unhealthy)
return 1
;;
*)
echo "Unrecognized container health state: $state" >&2
exit 1
;;
esac
}
# wait for the container to finish starting
# usage: wait_for CONTAINER_NAME
function wait_for() {
local name
name="$1"
echo "Waiting for the container to finish starting."
while true; do
health "$name" | grep "starting" || return 0
sleep 5
done
}
# show the applications configuration parameters
# usage: config
function config() {
echo "$CONFIG_SCHEMA"
}
# show the application's metadata
# uage: meta
function meta() {
_meta >&2
echo "start_command: $APP_SRC start" >&2
echo "stop_command: $APP_SRC stop" >&2
echo "health_check_command: $APP_SRC status" >&2
}
# show the applications container image
# usage: image
function image() {
echo $IMAGE
}
# show the applications default tag
# usage: tag
function tag() {
echo $TAG
}
# the main entrypoint for most applications
# usage: main VERB CONTAINER_NAME
function main() {
local name verb
verb=$1
name=$2
case "$verb" in
status | start | stop | restart | wait_for | meta | image | tag)
$verb $name
;;
config)
;;
*)
echo "Usage: $0 {start|stop|restart|status|config}"
exit 1
;;
esac
}
###############################################################################
# abstract functions
###############################################################################
function _docker_stop() {
echo "$0 function is not defined in the application definition." >&2
exit 2
}
function _docker_run() {
echo "$0 function is not defined in the application definition." >&2
exit 2
}
_meta() {
cat <<-EOM
name: New Application
type: custom
class: process
user_msg: |-
Milvus is now available at:
localhost:19530
icon_url: "https://milvus.io/favicon-32x32.png"
EOM
}
###############################################################################
# logging functions
###############################################################################
echo_success() {
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
echo -n "["
[ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
echo -n $" OK "
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 0
}
echo_failure() {
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
echo -n "["
[ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
echo -n $"FAILED"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 1
}
echo_passed() {
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
echo -n "["
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo -n $"PASSED"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 1
}
echo_warning() {
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
echo -n "["
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo -n $"WARNING"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 1
}
# Log that something succeeded
success() {
echo_success
return 0
}
# Log that something failed
failure() {
local rc=$?
echo_failure
return $rc
}
# Log that something passed, but may have had errors. Useful for fsck
passed() {
local rc=$?
echo_passed
return $rc
}
# Log a warning
warning() {
local rc=$?
echo_warning
return $rc
}
# run a command and log its result
# usage: exec "LOG_MESSAGE" COMMAND TO EXEC
function exec() {
local msg
msg="$1"
echo -n "$msg" >&2
shift
"$@" && success >&2 || failure >&2
retcode=$?
echo "$msg" >&2
return $retcode
}