-
Notifications
You must be signed in to change notification settings - Fork 84
/
cs61-run-docker
executable file
·143 lines (132 loc) · 4.88 KB
/
cs61-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
#! /bin/bash
maindir=""
destdir=cs61-lectures
fresh=
verbose=false
arch="`arch`"
tag=
platform=
container=
while test "$#" -ne 0; do
if test "$1" = "-f" -o "$1" = "--fresh"; then
fresh=1
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 "\`cs61-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
shift
elif test "(" "$1" = "-t" -o "$1" = "--tag" ")" -a "$#" -gt 1; then
tag="$2"
shift 2
elif test -z "$container" -a -z "$fresh" -a -n "$1" && expr "$1" : '[0-9a-f][0-9a-f][0-9a-f]*$' >/dev/null 2>&1; then
container="$1"
shift
else
armtext=
if test "$arch" = "arm64" -o "$arch" = "aarch64"; then
armtext=" [-a|--arm] [-x|--x86-64]"
fi
echo "Usage: cs61-run-docker [-f|--fresh]$armtext [-V|--verbose] [CONTAINERID]" 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=cs61:arm64
elif test -z "$tag"; then
tag=cs61:latest
fi
vexec () {
if $verbose; then
echo "$@"
fi
exec "$@"
}
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`
ssharg=
sshenvarg=
if test -n "$SSH_AUTH_SOCK" -a "`uname`" = Darwin; then
ssharg=" --mount type=bind,src=/run/host-services/ssh-auth.sock,target=/run/host-services/ssh-auth.sock"
sshenvarg=" -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock"
fi
dir="`pwd`"
subdir=""
while test "$dir" != / -a "$dir" != ""; do
thisfileid=`stat $statformatarg %d:%i "$dir"/cs61-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
existing_image=""
if test -n "$container"; then
existing_image="`docker ps -f "id=$container" --no-trunc --format "{{.CreatedAt}},{{.ID}}" | sort -r | head -n 1`"
if test -z "$existing_image"; then
echo "* Container $container not found" 1>&2
echo "- To start a new container, try \`cs61-run-docker -f\`" 1>&2
echo "- To list executing containers, try \`docker ps\`" 1>&2
exit 1
elif test "`echo "$existing_image" | wc -l`" -gt 1; then
echo "* Container $container ambiguous" 1>&2
echo "- To start a new container, try \`cs61-run-docker -f\`" 1>&2
echo "- To list executing containers, try \`docker ps\`" 1>&2
exit 1
fi
elif test -n "$maindir" -a -z "$fresh"; 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`"
fi
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 \`cs61-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
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 -n "$maindir"; then
vexec docker run -it --platform $platform --rm --privileged --cap-add=SYS_PTRACE --cap-add=NET_ADMIN --security-opt seccomp=unconfined -v "$maindir":/home/cs61-user/$destdir$ssharg -w "/home/cs61-user/$destdir$subdir" $netarg$sshenvarg $tag
else
vexec docker run -it --platform $platform --rm --privileged --cap-add=SYS_PTRACE --cap-add=NET_ADMIN --security-opt seccomp=unconfined $netarg$sshenvarg $tag
fi