-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs2110docker.sh
executable file
·110 lines (92 loc) · 2.61 KB
/
cs2110docker.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
#!/bin/bash
release="stable"
imageBaseName="gtcs2110/cs2110docker-c"
imageName="${imageBaseName}:${release}"
define() { IFS=$'\n' read -r -d '' "${1}" || true; }
description="Run the CS 2110 C Docker container"
usage_text=""
define usage_text <<'EOF'
USAGE:
./cs2110docker-c.sh [start|stop|-h|--help]
OPTIONS:
start
Start a new nongraphical container in the background. This is the default if no options
are provided.
stop
Stop and remove any running instances of the container.
-h, --help
Show this help text.
EOF
print_help() {
>&2 echo -e "$description\n\n$usage_text"
}
print_usage() {
>&2 echo "$usage_text"
}
action=""
if [ $# -eq 0 ]; then
action="start"
elif [ $# -eq 1 ]; then
case "$1" in
start)
action="start"
;;
stop)
action="stop"
;;
-h|--help)
print_help
exit 0
;;
*)
>&2 echo "Error: unrecognized argument: $1"
>&2 echo ""
print_usage
exit 1
;;
esac
elif [ $# -gt 1 ]; then
>&2 echo "Error: too many arguments"
>&2 echo ""
print_usage
exit 1
fi
if ! docker -v >/dev/null; then
>&2 echo "ERROR: Docker not found. Please install Docker before running this script. Refer to the CS 2110 Docker Guide."
exit 1
fi
if ! docker container ls >/dev/null; then
>&2 echo "ERROR: Docker is not currently running. Please start Docker before running this script."
exit 1
fi
echo "Found Docker Installation"
if [ "$action" = "stop" ]; then
existingContainers=($(docker ps -a | grep "$imageBaseName" | awk '{print $1}'))
echo "${existingContainers[@]}"
if [ "${#existingContainers[@]}" -ne 0 ]; then
echo "Found CS 2110 containers. Stopping and removing them."
docker stop "${existingContainers[@]}" >/dev/null
docker rm "${existingContainers[@]}" >/dev/null
else
echo "No existing CS 2110 containers."
fi
echo "Successfully stopped CS 2110 containers"
exit 0
fi
echo "Pulling down most recent image of $imageName"
if ! docker pull $imageName; then
>&2 echo "ERROR: Unable to pull down the most recent image of $imageName"
fi
echo "Starting up new CS 2110 Docker Container:"
if command -v docker-machine &> /dev/null; then
# We're on legacy Docker Toolbox
# pwd -W doesn't work with Docker Toolbox
# Extra '/' fixes some mounting issues
currDir="/$(pwd)"
else
# pwd -W should correct path incompatibilites on Windows for Docker Desktop users
currDir="/$(pwd -W 2>/dev/null || pwd)"
fi
if [ "$action" = "start" ]; then
docker run --rm -v "$currDir":/cs2110/host/ --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -it "$imageName"
fi