-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-container
executable file
·320 lines (275 loc) · 6.68 KB
/
run-container
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#! /bin/bash
#set -x
maindir=""
container_name=os-demos
image_name=os-demo-container
network_name=net-cs1660
server_port=9000
version_file=".image-version"
clean=false
verbose=false
arch="`uname -m`"
tag=
platform=
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"/${BASH_SOURCE[0]} 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
if test -z "$maindir"; then
echo "Error: could not determine your directory."
exit 1
fi
vexec () {
if $verbose; then
echo "$@"
fi
"$@"
}
has_container() {
[ $( docker ps -a | grep $container_name | wc -l ) -gt 0 ]
}
remove_containers() {
local _name
_name="${1:-${container_name}}"
echo "Removing all existing ${container_name} containers..."
docker ps -a -f name="${_name}" --format "{{.ID}}" | while read line ; do docker rm --force $line ; done
}
stop_container() {
local _name
_name="${1:-${container_name}}"
docker stop "${_name}"
}
start_container() {
echo "Entering existing container"
echo "(To reset this container on startup, run with --clean)"
docker start $container_name
run_shell alice
}
run_in_container() {
user=$1
shift
docker exec -it --user "${user}" $container_name $@
}
run_shell() {
user=$1
run_in_container "${user}" "/bin/bash"
}
has_network() {
local rv
rv=0
$(docker network inspect "${network_name}" 2>&1 > /dev/null) || rv=$?
if [[ $rv == 0 ]]; then
return 0
else
return 1
fi
}
create_network() {
if ! has_network; then
echo "Creating container-local network ${network_name}"
docker network create "${network_name}"
else
echo "Network ${network_name} already exists"
fi
}
get_network_info() {
docker network inspect "${network_name}"
}
remove_network() {
docker network rm "${network_name}"
}
start_new_container() {
netarg="$netarg --expose=80/tcp -p ${server_port}:80/tcp"
create_network
vexec docker run -it \
--name $container_name \
--network "${network_name}" --network-alias "[${container_name}]" \
--volume $maindir/webroot:/var/www/html \
--platform $platform \
--privileged --security-opt seccomp=unconfined \
$netarg $tag
}
if test -n "$maindir"; then
existing_image="`docker ps -f status=running -f ancestor=$tag --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 \`$0 -f\`" 1>&2
echo "- To kill this container, exit then \`docker kill $image12\`" 1>&2
vexec docker exec -it $image /bin/bash
fi
fi
do_start_or_run()
{
if $clean; then
remove_containers && start_new_container
elif has_container; then
start_container
else
start_new_container
fi
}
__check_platform()
{
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="${image_name}:arm64"
elif test -z "$tag"; then
tag="${image_name}:latest"
fi
}
get_version_string()
{
commit=""
if ! command -v git > /dev/null ; then
commit="NOGIT"
else
commit=$(git rev-parse --short HEAD)
fi
ts="$(date -Iseconds)"
echo "${image_name}-${commit}-${ts}"
}
build_image()
{
# Add a version string so we can track which one students are using
echo "${container_name} $(get_version_string)" > "${version_file}"
if test $platform = linux/arm64; then
docker build -t "$tag" -f Dockerfile --platform linux/arm64 .
else
docker build -t "$tag" -f Dockerfile --platform linux/amd64 .
fi
}
remove_image()
{
docker image rm "$tag"
}
do_help()
{
cat <<EOF
Usage $0: [--verbose] [--clean] [--debug] [--arm] [command] [command opts...]
Top-level options:
--verbose Execute docker with verbose output
--clean Discard current container state when using 'start'
--debug Use to show execution of this script for debugging
--arm Force building for ARM64 platform (ARM64/M1 macs only)
Commands:
setup Download the container image (first time setup)
- If container image exists, runs 'clean' and updates to latest
start Start the container (requires 'setup' first)
stop Stop the container
clean Remove container state (revert state to image built with 'setup')
reset Remove container state and restart (same as 'clean+start')
update-image Remove container state and update image to latest version
clean-image Remove all container state and the image (must 'setup' again)
reset-all Same as clean-image+setup+start
clean-old Discard containers generated with old versions of this script
If no command is specified, the default is 'start'.
EOF
}
main()
{
# Top-level args
case $1 in
--verbose|-v)
shift
verbose=true
;;
--clean|-C)
shift
clean=true
;;
--debug)
shift
set -x
;;
-a|--arm|--arm64|--aarch64)
shift
if [[ ( "${arch}" == "arm64" ) || ( "${arch}" == "aarch64" ) ]]; then
platform=linux/arm64
else
echo "$0 --arm only works on arm64 hosts (platform is ${arch})" 1>&2
exit 1
fi
;;
-x|--x86-64)
shift
platform=linux/amd64
;;
--help)
shift
do_help
exit 0
esac
__check_platform
# Default subcommand
if [[ $# == 0 ]]; then
do_start_or_run
exit 0
fi
# Subcommands
case $1 in
help)
do_help
exit 0
;;
start|run)
shift
do_start_or_run $@
;;
shell)
shift
run_shell $@
;;
stop)
shift
stop_container $@
;;
clean)
shift
remove_containers $@
;;
reset)
shift
remove_containers
do_start_or_run $@
;;
setup|build-image)
shift
build_image $@
;;
remove-image|clean-image|reset-image)
shift
remove_containers
remove_image $@
;;
*)
echo "Invalid command $1"
do_help
exit 1
;;
esac
}
main $@