-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvmx
executable file
·413 lines (357 loc) · 11.2 KB
/
vmx
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/bin/sh
# VMX Docker Manager by vision.ai
# Starts the vision.ai object recognition server
# Requires docker >= 1.2; and permissions to run docker
# Visit us on the web:
# https://vision.ai
# Github repository for this file:
# https://github.com/VISIONAI/vmx-docker-manager
#
##PARAMS
cmd=$1
config=$2
COMMAND=$0
PARAMS=$@
FOLDERNAME=`date +%Y%a%b%d_%H%M%S`
init () {
echo "Starting containers..."
docker run -d --name vmx-mcr visionai/mcr-2014a 2>/dev/null
docker run -d --name vmx-userdata visionai/vmx-userdata 2>/dev/null
docker run -d --name vmx-middle$VMXTAG visionai/vmx-middle 2>/dev/null
docker run -d --name vmx-server$VMXTAG visionai/vmx-server 2>/dev/null
docker run -d --name vmx-appbuilder$VMXTAG visionai/vmx-appbuilder 2>/dev/null
touch .vmx-init
}
usage () {
echo "VMX Docker Manager by vision.ai"
echo "Version: " `git describe --tags --dirty 2>/dev/null || echo no-git`
echo "Requires docker >= 1.2; and permissions to run docker"
echo "==============================================="
echo "Usage: vmx COMMAND [OPTIONS]"
echo
echo "Commands:"
echo " start PORT: Start vmx on localhost:PORT"
echo " stop: Stop vmx"
echo " enter: Shell access to inspect vmx volumes"
echo " init: Download and start the containers that have the pre-reqs, mcr, etc."
echo " (This is done automatically the first time you start vmx)"
echo " update TAG: Update your vmx (will stop vmx first)"
echo " backup: Backup your vmx data (models, session logs, config file)"
echo " restore BACKUP: Restore models/logs/config from folder BACKUP"
echo " start_process PORT: [Advanced Usage] Start a single VMXserver process on localhost:PORT"
echo " "
echo "Examples:"
echo " - Run vmx on port 3000"
echo " $ ./vmx start 3000"
echo " - Stop vmx"
echo " $ ./vmx stop"
echo " "
echo " - Backup your data to " `pwd`/.vmx-backup/
echo " $ ./vmx backup"
echo " - Restore from a backup"
echo " $ ./vmx restore .vmx-backup/2014_FriNov21_20_04"
echo " "
echo " - Update vmx to the latest version (this is the stable one)"
echo " $ ./vmx update"
echo " or"
echo " $ ./vmx update latest"
echo " "
echo " - Update vmx to dev version (this is the bleeding-edge release)"
echo " $ ./vmx update dev"
echo " "
echo " - Update the vmx-docker-manager"
echo " $ git pull"
exit 1
}
boot2docker_extra() {
IS_B2D=`uname -a | grep boot2docker`
if [ "$IS_B2D" != "" ]; then
VMIP=`ip addr show eth1 |sed -nEe 's/^[ \t]*inet[ \t]*([0-9.]+)\/.*$/\1/p'`
elif [ `uname` = "Darwin" ]; then
VMIP=`boot2docker ip 2>/dev/null`
else
VMIP=""
fi
if [ "$VMIP" != "" ]; then
echo "You are using boot2docker, your local vmx url will be:"
echo "http://"${VMIP}":"${VMXHOSTPORT}
fi
}
check_perm() {
docker run --rm busybox echo >/dev/null 2>&1
NON_ROOT_RUN_DOCKER_EXIT_CODE="$?"
if [ $NON_ROOT_RUN_DOCKER_EXIT_CODE -eq 1 ] ; then
echo "Permissions failed.."
echo "try \`sudo $COMMAND $PARAMS\`"
exit 1
elif [ $NON_ROOT_RUN_DOCKER_EXIT_CODE -ne 0 ]; then
echo "Trying to run docker resulted in exit code: $NON_ROOT_RUN_DOCKER_EXIT_CODE"
exit $NON_ROOT_RUN_DOCKER_EXIT_CODE
fi
}
restore_userdata () {
#Docker does not support copying out of volumes: https://github.com/docker/docker/issues/1992
init
echo "Restoring models.."
docker run --name vmx-tmp \
--volumes-from vmx-userdata \
-v `pwd`/$RESTOREDIR:/restore \
-t \
ubuntu \
bash -c "find /restore && find /vmx && cp -r /restore/models/* /vmx/models"
docker stop vmx-tmp
docker rm vmx-tmp
echo "Restoring license..."
docker run --name vmx-tmp \
--volumes-from vmx-server \
-v `pwd`/$RESTOREDIR:/restore \
-t \
ubuntu cp /restore/config.json /vmx/build/config.json
docker stop vmx-tmp
docker rm vmx-tmp
}
backup_userdata () {
#Docker does not support copying out of volumes: https://github.com/docker/docker/issues/1992
docker run --name vmx-tmp --volumes-from vmx-userdata:rw ubuntu /bin/bash -c "cp -r /vmx/sessions /tmp/sessions && cp -r /vmx/models /tmp/models && cd /tmp && find"
docker cp vmx-tmp:/tmp/models `pwd`/.vmx-backup/$FOLDERNAME
docker cp vmx-tmp:/tmp/sessions `pwd`/.vmx-backup/$FOLDERNAME
docker stop vmx-tmp
docker rm vmx-tmp
}
backup_config () {
echo "doing config backup..."
docker run --name vmx-tmp --volumes-from vmx-server:ro ubuntu cp /vmx/build/config.json /config.json
docker cp vmx-tmp:/config.json `pwd`/.vmx-backup/$FOLDERNAME
docker stop vmx-tmp
docker rm vmx-tmp
}
start_vmx () {
RUNNING=$(docker ps | grep 'vmx-environment' | wc -l)
if [ "$RUNNING" -ne "1" ]; then
echo "vmx starting with port $VMXHOSTPORT"
echo "--------------------------------------"
echo "visit http://localhost:$VMXHOSTPORT "
echo
echo "Ctrl-c to detach terminal from the log"
echo "./vmx stop to stop the vmx daemon"
echo
boot2docker_extra
docker run -t \
--volumes-from vmx-mcr \
--volumes-from vmx-userdata \
--volumes-from vmx-middle \
--volumes-from vmx-server \
--volumes-from vmx-appbuilder \
-p $VMXHOSTPORT:3000 \
--name vmx-environment \
--rm \
visionai/vmx-environment /bin/bash -c "/vmx/middle/vmx | tee /vmx/middle/vmx_log" 2> /dev/null
#if it fails for some reason, clean up our mess pass the error code
prev=$?
if [ "$prev" -eq "1" ]; then
echo "Failed to start vmx. Try \"./vmx init\" first"
docker rm vmx-environment 2> /dev/null
exit $prev
else
echo "Successfully started vmx"
fi
else
echo "Already running vmx-environment"
echo "You must first run ./vmx stop"
exit 1
fi
}
start_vmx_process () {
RUNNING=$(docker ps | grep 'vmx-environment' | wc -l)
if [ "$RUNNING" -ne "1" ]; then
echo "vmx starting with port $VMXHOSTPORT"
echo "--------------------------------------"
echo "visit http://localhost:$VMXHOSTPORT "
echo
echo "Ctrl-c to detach terminal from the log"
echo "./vmx stop to stop the vmx daemon"
echo
boot2docker_extra
docker run \
--volumes-from vmx-mcr \
--volumes-from vmx-userdata \
--volumes-from vmx-middle \
--volumes-from vmx-server \
--volumes-from vmx-appbuilder \
-p $VMXHOSTPORT:3000 \
--name vmx-environment \
--rm \
visionai/vmx-environment /bin/bash -c "/vmx/build/VMXserver /vmx/ session_id none :3000 | tee /vmx/log"
#if it fails for some reason, clean up our mess pass the error code
prev=$?
if [ "$prev" -eq "1" ]; then
echo "Failed to start vmx_process. Try \"./vmx init\" first"
docker rm vmx-environment 2> /dev/null
exit $prev
else
echo "Successfully started vmx_process"
fi
else
echo "Already running vmx-environment"
echo "You must first run ./vmx stop"
exit 1
fi
}
stop_vmx () {
echo 'Stopping vmx'
docker stop vmx-environment 2> /dev/null
docker rm vmx-environment 2> /dev/null
}
enter_vmx () {
RUNNING_INSPECT=$(docker ps -a| grep 'vmx-inspect-environment' | wc -l)
if [ "$RUNNING_INSPECT" -ne "1" ]; then
echo "Entering a temporary inspect container"
docker run -w /vmx/ -i -t \
--volumes-from vmx-mcr \
--volumes-from vmx-userdata \
--volumes-from vmx-middle \
--volumes-from vmx-server \
--volumes-from vmx-appbuilder \
--name vmx-inspect-environment \
--rm \
visionai/vmx-environment /bin/bash
else
echo "Stopping previous inspection container.."
docker stop vmx-inspect-environment 2>/dev/null
echo "Removing previous inspection container.."
docker rm vmx-inspect-environment 2>/dev/null
enter_vmx
fi
}
update_vmx(){
#stop vmx
docker stop vmx-environment 2>/dev/null
docker rm vmx-tmp 2>/dev/null
mkdir -p .vmx-backup/$FOLDERNAME
#store the config
echo Starting temp container 'vmx-tmp'
docker run --name vmx-tmp --volumes-from vmx-server:ro ubuntu cp /vmx/build/config.json /config.json
docker cp vmx-tmp:/config.json `pwd`/.vmx-backup/$FOLDERNAME
docker stop vmx-tmp 2>/dev/null
docker rm vmx-tmp 2>/dev/null
#stop and update
docker stop vmx-middle 2>/dev/null
docker stop vmx-server 2>/dev/null
docker stop vmx-appbuilder 2>/dev/null
docker rm vmx-middle 2>/dev/null
docker rm vmx-server 2>/dev/null
docker rm vmx-appbuilder 2>/dev/null
docker pull visionai/vmx-middle$VMXTAG
docker pull visionai/vmx-server$VMXTAG
docker pull visionai/vmx-appbuilder$VMXTAG
#Update the environment (even though it rarely changes)
docker pull visionai/vmx-environment
docker run --name vmx-middle visionai/vmx-middle$VMXTAG
A=$?
docker run --name vmx-server visionai/vmx-server$VMXTAG
B=$?
docker run --name vmx-appbuilder visionai/vmx-appbuilder$VMXTAG
C=$?
#make a temp container for copying
docker run --name vmx-tmp --volumes-from vmx-server -v `pwd`/.vmx-backup:/vmx-backup ubuntu cp /vmx-backup/$FOLDERNAME/config.json /vmx/build/config.json
docker rm vmx-tmp
docker rm vmx-environment 2> /dev/null
if [ "$A$B$C" = "000" ]; then
echo "Successfully updated, you can now restart vmx"
echo "To update the vmx-docker-manager, run: git pull"
else
echo "Problem updating vmx"
fi
exit 0
}
[ $# -lt 1 ] && {
usage
}
check_perm
case "$cmd" in
start)
if [ $# -lt 2 ]; then
echo "Must supply port"
usage
fi
if [ ! -e ".vmx-init" ]; then
echo "Downloading dependencies (this may take a while)"
init
fi
export VMXHOSTPORT=$2
start_vmx
exit 0
;;
start_process)
if [ $# -lt 2 ]; then
echo "Must supply port"
usage
fi
if [ ! -e ".vmx-init" ]; then
echo "Downloading dependencies (this may take a while)"
init
fi
export VMXHOSTPORT=$2
start_vmx_process
exit 0
;;
stop)
stop_vmx
echo "Stopped vmx"
exit 0
;;
update)
#Keep an empty TAG empy, and turn "dev" into ":dev"
export VMXTAG=$2
if [ "$VMXTAG" != "" ]; then
VMXTAG=":$VMXTAG"
fi
update_vmx
init
echo "Updated vmx"
exit 0
;;
enter)
enter_vmx
exit 0
;;
backup)
docker rm vmx-tmp > /dev/null 2> /dev/null
backup_userdata
backup_config
exit 0
;;
restore)
if [ $# -lt 2 ]; then
echo "Must supply directory"
if [ ! -d .vmx-backup ]; then
echo "No .vmx-backup found.. have you done any backups?"
else
echo "Try one of the following commands:"
for b in .vmx-backup/*
do
echo ./vmx restore $b
NMODELS=`ls $b/models 2>/dev/null | wc -l`
echo "Has ${NMODELS} models\n"
done
exit 1
fi
fi
if [ ! -f $RESTOREDIR/config.json ]; then
echo config.json not found in $RESTOREDIR
fi
RESTOREDIR=$2
restore_userdata
;;
init)
init
echo "Successfully initialized vmx!"
exit 0
;;
*)
usage
esac
do_not () {
docker stop vmx-environment
docker rm vmx-environment
}