forked from cs61/cs61-lectures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs61-run-docker
executable file
·93 lines (83 loc) · 3.04 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
#! /bin/bash
maindir=""
destdir=cs61-lectures
fresh=
verbose=false
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
else
echo "Usage: cs61-run-docker [-f|--fresh] [-V|--verbose]" 1>&2
exit 1
fi
done
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`
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
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" -a -z "$fresh"; then
existing_image="`docker ps -f status=running -f ancestor=cs61:latest -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 \`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
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 linux/amd64 --rm --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 cs61:latest
else
vexec docker run -it --platform linux/amd64 --rm --cap-add=SYS_PTRACE --cap-add=NET_ADMIN --security-opt seccomp=unconfined $netarg$sshenvarg cs61:latest
fi