forked from c-amr/camr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.sh
executable file
·136 lines (121 loc) · 3.21 KB
/
rest.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
#!/bin/sh
image=didzis/camrrest
port=5000
function usage {
echo "CAMR REST API runner (dockerized version)"
echo
echo "usage: $0 [--image NAME] [-p PORT] [--detach] [--name NAME] [--] [options for rest.py (see below)]"
echo
echo "--image NAME specify docker image (default: $image)"
echo "--pull pull docker image from registry"
echo "-p PORT, --port PORT specify port on host system (default: $port)"
echo "--name NAME specify docker container name"
echo "-d, --detach run container in background"
echo '-r, --reuse reuse container if already exists (will first try `docker start NAME`)'
echo
echo "notes:"
echo "* requires up to 22GB of RAM"
echo "* requires at least 2GB of disk space (for docker image)"
echo
}
function check_image {
if [ "$(docker images -q $image 2> /dev/null)" = "" ]; then
read -p "Docker image $image not found, pull from registry (at least 2GB of disk space required) [Y/n] ? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Nn]$ ]]; then
exit 1
fi
elif [ $pull -eq 1 ]; then
echo
echo "Pulling from registry"
docker pull $image
fi
}
docker version > /dev/null
if [ $? -ne 0 ]; then
echo "Docker not installed or engine not running."
exit 1
fi
if [ -t 0 ]; then
docker_tty=-it
else
docker_tty=
fi
for arg in $@; do
if [ "$arg" = "--help" ] || [ "$arg" = "-h" ]; then
usage
check_image
docker run --rm $docker_tty $image --rest --help
exit $?
fi
done
detach_rm="--rm"
reuse=0
pull=0
args=()
container_args_only=0
while [ $# -gt 0 ]; do
if [ "$1" = "--" ]; then
container_args_only=1
shift
# break
elif [ $container_args_only -ne 1 ] && [ "$1" = "--image" ]; then
shift
image="$1"
shift
elif [ $container_args_only -ne 1 ] && [ "$1" = "--name" ]; then
shift
name="$1"
name_arg="--name $1"
shift
elif [ $container_args_only -ne 1 ] && ([ "$1" = "--detach" ] || [ "$1" = "-d" ]); then
detach_rm="-d"
shift
elif [ $container_args_only -ne 1 ] && ([ "$1" = "--reuse" ] || [ "$1" = "-r" ]); then
reuse=1
shift
elif [ $container_args_only -ne 1 ] && [ "$1" = "--pull" ]; then
pull=1
shift
elif [ $container_args_only -ne 1 ] && ([ "$1" = "--port" ] || [ "$1" = "-p" ]); then
shift
port="$1"
shift
else
args=("${args[@]}" "$1")
shift
fi
done
if [ $reuse -eq 1 ] && [ "$name" != "" ]; then
if [ "`docker ps -aq --format '{{.Names}}' | grep "^$name$"`" != "" ]; then
docker start $name
if [ $? -eq 0 ]; then
exit 0
else
exit 1
fi
else
echo "Container $name does not exist, will create."
echo
fi
fi
if [ $reuse -eq 1 ] && [ "$name" != "" ] && [ "$detach_rm" == "--rm" ]; then
# don't remove container if planned to be reused
detach_rm=
fi
echo Will execute: docker run $detach_rm $name_arg -p $port:5000 $docker_tty $image --rest "${args[@]}"
check_image
echo
echo REST API will be available at host IP port $port
sleep 1
echo
id=`docker run $detach_rm $name_arg -p $port:5000 $docker_tty $image --rest "${args[@]}"`
if [ "$name" != "" ]; then
id="$name"
fi
if [ "$detach_rm" == "-d" ]; then
echo
echo "Running in detached mode, to see output log, execute:"
echo "$ docker logs -f $id"
echo
fi