forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.sh
executable file
·247 lines (223 loc) · 7.63 KB
/
docker.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
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
#!/bin/bash
ORG=scionproto
branch=
build_dir=
image_tag=
get_params() {
# If we're on a local branch, use that. If we're on a detached HEAD from a
# remote branch, or from a bare rev id, use that instead.
branch=$(LC_ALL=C git status | head -n1 |
awk '/^On branch|HEAD detached at/ {print $NF}')
build_dir="docker/_build/$branch"
image_tag=$(echo "$branch" | tr '/' '.')
}
cmd_base() {
set -e
set -o pipefail
get_params
copy_tree
docker_build "base"
docker tag scion_base:latest "$ORG/scion_base:pending"
touch docker/_build/scion_base.stamp
}
cmd_build() {
set -e
set -o pipefail
get_params
copy_tree
docker_build
}
cmd_tester() {
set -eo pipefail
docker build -t "scion_tester:latest" - < docker/Dockerfile.tester
docker build -f docker/acceptance/sig/Dockerfile -t "scion_sig_acceptance:latest" docker/acceptance/sig
}
copy_tree() {
set -e
set -o pipefail
echo "Copying current working tree for Docker image"
echo "============================================="
mkdir -p "${build_dir:?}"
# Just in case it's sitting there from a previous run
rm -rf "${build_dir}/scion.git/"
git ls-files | rsync -a --files-from=- . "${build_dir}/scion.git/"
echo
}
docker_build() {
set -e
set -o pipefail
local suffix="$1"
local image_name="scion${suffix:+_$suffix}"
local conf_rel="docker/Dockerfile${suffix:+.$suffix}"
local conf="${build_dir:?}/scion.git/$conf_rel"
local tag="$image_name:${image_tag:?}"
local log="$build_dir/build${suffix:+_$suffix}.log"
[ -n "$suffix" ] || local build_args="--build-arg SCION_UID=$(id -u) --build-arg SCION_GID=$(id -g) --build-arg DOCKER_GID=$(getent group docker | cut -f3 -d:) --build-arg GIT_VERSION=$(./tools/git-version) --build-arg GIT_TAG=$(git tag -l --points-at HEAD)"
echo "Building ${suffix:+$suffix }Docker image"
echo "=========================="
echo "Image: $tag"
echo "Config: $conf_rel"
echo "Log: $log"
echo "=========================="
echo
docker build $DOCKER_ARGS -f "$conf" -t "$tag" $build_args "$build_dir/scion.git" | tee "$log"
docker tag "$tag" "$image_name:latest"
touch docker/_build/scion.stamp
}
cmd_clean() {
if [ -z "$1" -o "$1" = "cntrs" ]; then
stop_cntrs
del_cntrs
fi
[ -z "$1" -o "$1" = "images" ] && del_imgs
rm -rf docker/_build/
}
common_args() {
local args="-h scion $DOCKER_ARGS"
args+=" -v /var/run/docker.sock:/var/run/docker.sock"
args+=" -v $SCION_MOUNT/gen:/home/scion/go/src/github.com/scionproto/scion/gen"
args+=" -v $SCION_MOUNT/logs:/home/scion/go/src/github.com/scionproto/scion/logs"
args+=" -v $SCION_MOUNT/gen-certs:/home/scion/go/src/github.com/scionproto/scion/gen-certs"
args+=" -v $SCION_MOUNT/gen-cache:/home/scion/go/src/github.com/scionproto/scion/gen-cache"
args+=" -v $SCION_MOUNT/traces:/home/scion/go/src/github.com/scionproto/scion/traces"
args+=" -v $SCION_MOUNT/htmlcov:/home/scion/go/src/github.com/scionproto/scion/python/htmlcov"
args+=" -v $SCION_MOUNT/metrics:/home/scion/go/src/github.com/scionproto/scion/metrics"
args+=" -e SCION_OUTPUT_BASE=$SCION_MOUNT"
args+=" -e SCION_UID=$(id -u)"
args+=" -e SCION_GID=$(id -g)"
args+=" -e SCION_PWD=${PWD}"
args+=" -e DOCKER_GID=$(getent group docker | cut -f3 -d:)"
args+=" -e SCION_USERSPEC=$(id -un):$(id -gn)"
args+=" -u root"
args+=" -e DOCKER0=$(./tools/docker-ip)"
echo $args
}
cmd_run() {
set -e
SCION_MOUNT=${SCION_MOUNT:-$(mktemp -d /tmp/scion_out.XXXXXX)}
echo "SCION_MOUNT directory: $SCION_MOUNT"
local img=${SCION_IMG:-scion}
local args=$(common_args)
args+=" -i -t --rm --entrypoint=/docker-entrypoint.sh"
setup_volumes
docker run $args "$img" "$@"
}
cmd_start() {
set -e
SCION_MOUNT=${SCION_MOUNT:-$(mktemp -d /tmp/scion_out.XXXXXX)}
echo "SCION_MOUNT directory: $SCION_MOUNT"
local img=${SCION_IMG:-scion}
local cntr=${SCION_CNTR:-scion}
if docker container inspect "$cntr" &>/dev/null; then
echo "Removing stale $cntr container"
./tools/quiet docker rm -f "$cntr"
fi
local args=$(common_args)
args+=" --name $cntr"
setup_volumes
./tools/quiet docker container create $args "$img" -c "tail -f /dev/null"
./tools/quiet docker start "$cntr"
# Adjust ownership of mounted dirs
docker exec "$cntr" /docker-entrypoint.sh
}
cmd_exec() {
local cntr=${SCION_CNTR:-scion}
docker exec -it -u scion "$cntr" bash -l -c "$*"
}
cmd_stop() {
local cntr=${SCION_CNTR:-scion}
echo "Stopping $cntr container"; ./tools/quiet docker stop "$cntr";
echo "Removing $cntr container"; ./tools/quiet docker rm "$cntr";
}
setup_volumes() {
set -e
for i in gen logs gen-certs gen-cache traces htmlcov metrics; do
mkdir -p "$SCION_MOUNT/$i"
# Check dir exists, and is owned by the current (effective) user. If
# it's owned by the wrong user, the docker environment won't be able to
# write to it.
[ -O "$SCION_MOUNT/$i" ] || { echo "Error: '$SCION_MOUNT/$i' dir not owned by $LOGNAME"; exit 1; }
done
# Make sure the socket dirs have the correct permissions. Unlike for the volumes we try to fix
# the permissions if necessary.
local disp_dir="/run/shm/dispatcher"
[ -d "$disp_dir" ] || mkdir "$disp_dir"
[ $(stat -c "%U" "$disp_dir") == "$LOGNAME" ] || { sudo -p "Fixing ownership of $disp_dir - [sudo] password for %p: " chown $LOGNAME: "$disp_dir"; }
local sciond_dir="/run/shm/sciond"
[ -d "$sciond_dir" ] || mkdir "$sciond_dir"
[ $(stat -c "%U" "$sciond_dir") == "$LOGNAME" ] || { sudo -p "Fixing ownership of $sciond_dir - [sudo] password for %p: " chown $LOGNAME: "$sciond_dir"; }
}
stop_cntrs() {
local running
running=$(docker ps -q)
if [ -n "$running" ]; then
echo
echo "Stopping running containers"
echo "==========================="
docker stop $running
fi
}
del_cntrs() {
local stopped
stopped=$(docker ps -aq)
if [ -n "$stopped" ]; then
echo
echo "Deleting stopped containers"
echo "==========================="
docker rm $stopped
fi
}
del_imgs() {
local images
images=$(docker images | awk '/^<none>/ {print $3}')
if [ -n "$images" ]; then
echo
echo "Deleting unamed images"
echo "======================"
docker rmi $images
fi
}
cmd_help() {
cat <<-_EOF
Usage:
$PROGRAM base
$PROGRAM build
$PROGRAM tester
$PROGRAM run
Run the Docker image.
$PROGRAM start
Start a Docker container.
$PROGRAM exec
Execute a command in a running container.
$PROGRAM stop
Stop the Docker container.
$PROGRAM clean
Remove all Docker containers and all generated images.
$PROGRAM help
Show this text.
_EOF
}
PROGRAM="${0##*/}"
COMMAND="$1"
ARG="$2"
[ $(id -u) -eq 0 ] && { echo "Error: running as root is not allowed!" && exit 1; }
if ! ( groups | grep -q "\<docker\>"; ); then
echo "Error: you must be in the 'docker' group"
exit 1
fi
if ! type -p docker &>/dev/null; then
echo "Error: you don't have docker installed. Please see docker/README.md"
exit 1
fi
case $COMMAND in
base) cmd_base ;;
build) cmd_build ;;
tester) cmd_tester ;;
clean) shift; cmd_clean "$@" ;;
run) shift; cmd_run "$@" ;;
start) cmd_start ;;
exec) shift; cmd_exec "$@" ;;
stop) shift; cmd_stop "$@" ;;
help) cmd_help ;;
*) cmd_help ;;
esac