-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcs300-run-docker
executable file
·170 lines (151 loc) · 4.8 KB
/
cs300-run-docker
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
#! /bin/bash
maindir=""
destdir=cs300
container_name=cs300-container
network_name=cs300-network
clean=false
verbose=false
arch="`uname -m`"
tag=
platform=
while test "$#" -ne 0; do
if test "$1" = "-C" -o "$1" = "--clean"; then
clean=true
shift
elif test "$1" = "-V" -o "$1" = "--verbose"; then
verbose=true
shift
elif test "$1" = "-a" -o "$1" = "--arm" -o "$1" = "--arm64"; then
if test "$arch" = "arm64" -o "$arch" = "aarch64"; then
platform=linux/arm64
shift
else
echo "\`cs300-run-docker --arm\` only works on ARM64 hosts" 1>&2
exit 1
fi
elif test "$1" = "-x" -o "$1" = "--x86-64" -o "$1" = "--x86_64" -o "$1" = "--amd64"; then
platform=linux/amd64
else
armtext=
if test "$arch" = "arm64" -o "$arch" = "aarch64"; then
armtext=" [-a|--arm] [-x|--x86-64]"
fi
echo "Usage: cs300-run-docker [-C|--clean]$armtext [-V|--verbose]" 1>&2
exit 1
fi
done
if test -z "$platform" -a \( "$arch" = "arm64" -o "$arch" = "aarch64" \); then
platform=linux/arm64
elif test -z "$platform"; then
platform=linux/amd64
fi
if test -z "$tag" -a "$platform" = linux/arm64; then
tag=cs300:arm64
elif test -z "$tag"; then
tag=cs300:latest
fi
vexec () {
if $verbose; then
echo "$@"
fi
exec "$@"
}
has_network() {
docker network ls | grep $network_name >/dev/null
}
has_container() {
[ $( docker ps -a | grep $container_name | wc -l ) -gt 0 ]
}
create_network() {
echo "cs300 network not found, creating one..."
docker network create -d bridge cs300-network
}
remove_containers() {
echo "Removing all existing cs300 containers..."
docker ps -a -f name=cs300 --format "{{.ID}}" | while read line ; do docker rm --force $line ; done
}
start_container() {
echo "Entering container..."
docker start $container_name
docker exec -it $container_name /bin/bash
}
start_new_container() {
echo "Starting a new container..."
vexec docker run -it \
--name $container_name \
--platform $platform \
--privileged \
--cap-add=SYS_PTRACE --cap-add=NET_ADMIN --security-opt seccomp=unconfined \
-v "$maindir/home":/home/cs300-user \
-w "/home/cs300-user" \
-p "0.0.0.0:9095:9095" \
--network "$network_name" \
$netarg$sshenvarg $tag
}
if stat --format %i / >/dev/null 2>&1; then
statformatarg="--format"
else
statformatarg="-f"
fi
myfileid=`stat $statformatarg %d:%i "${BASH_SOURCE[0]}" 2>/dev/null`
dir="`pwd`"
subdir=""
while test "$dir" != / -a "$dir" != ""; do
thisfileid=`stat $statformatarg %d:%i "$dir"/cs300-run-docker 2>/dev/null`
if test -n "$thisfileid" -a "$thisfileid" = "$myfileid"; then
maindir="$dir"
break
fi
subdir="/`basename "$dir"`$subdir"
dir="`dirname "$dir"`"
done
if test -z "$maindir" && expr "${BASH_SOURCE[0]}" : / >/dev/null 2>&1; then
maindir="`dirname "${BASH_SOURCE[0]}"`"
subdir=""
fi
ssharg=
sshenvarg=
if test -n "$SSH_AUTH_SOCK" -a "`uname`" = Darwin; then
ssharg=" -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock"
sshenvarg=" -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock"
fi
if test -n "$maindir"; then
existing_image="`docker ps -f status=running -f ancestor=$tag -f volume=/host_mnt"$maindir" --no-trunc --format "{{.CreatedAt}},{{.ID}}" | sort -r | head -n 1`"
if test -n "$existing_image"; then
created_at="`echo $existing_image | sed 's/,.*//'`"
image="`echo $existing_image | sed 's/^.*,//'`"
image12="`echo $image | head -c 12`"
echo "* Using running container $image12, created $created_at" 1>&2
echo "- To start a new container, exit then \`cs300-run-docker -f\`" 1>&2
echo "- To kill this container, exit then \`docker kill $image12\`" 1>&2
vexec docker exec -it$sshenvarg $image /bin/bash
fi
fi
netarg=
if test `uname` = Darwin; then
if ! netstat -n -a -p tcp | grep '\.6169[ ].*LISTEN' >/dev/null; then
netarg="$netarg "'--expose=6169/tcp -p 6169:6169/tcp'
fi
if ! netstat -n -a -p tcp | grep '\.12949[ ].*LISTEN' >/dev/null; then
netarg="$netarg "'--expose=12949/tcp -p 12949:12949/tcp'
fi
elif test -x /bin/netstat; then
if ! netstat -n -a -p tcp | grep '\.6169[ ].*LISTEN' >/dev/null; then
netarg="$netarg "'--expose=6169/tcp -p 6169:6169/tcp'
fi
if ! netstat -n -l -t | grep ':12949[ ]' >/dev/null; then
netarg="$netarg "'--expose=12949/tcp -p 12949:12949/tcp'
fi
fi
if test -z "$maindir"; then
echo "Error: could not determine your directory."
exit 1
fi
has_network || create_network
if $clean; then
remove_containers && start_new_container
elif has_container; then
start_container
else
start_new_container
fi