forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker.sh
executable file
·159 lines (143 loc) · 4.35 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
#!/bin/bash
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=$(git status | head -n1 |
awk '/^On branch|HEAD detached at/ {print $NF}')
build_dir="docker/_build/$branch"
image_tag=$(echo "$branch" | tr '/' '.')
}
cmd_build() {
set -e
set -o pipefail
make -s go
get_params
echo
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;
git submodule --quiet foreach 'git ls-files | sed "s|^|$path/|"';
} | rsync -a --files-from=- . "${build_dir}/scion.git/"
cp bin/border bin/discovery "${build_dir}/scion.git/bin"
# Needed so that the go.capnp references in proto/*.capnp don't break
cp proto/go.capnp "${build_dir}/scion.git/proto"
echo
echo "Building Docker image"
echo "====================="
docker_build "build.log"
}
docker_build() {
set -e
set -o pipefail
local log_file="$1"; shift
local image_name="scion"
local args=""
echo "Image: $image_name:$image_tag"
echo "Log: $build_dir/$log_file"
echo "============================"
echo
if [ -n "$CIRCLECI" ]; then
# We're running on CircleCI, so don't rm images and *do* use -f during tagging
docker build --rm=false -t "${image_name:?}:${image_tag:?}" "${build_dir:?}/scion.git" |
tee "$build_dir/${log_file:?}"
docker tag -f "$image_name:$image_tag" "$image_name:latest"
else
docker build $args -t "${image_name:?}:${image_tag:?}" "${build_dir:?}/scion.git" |
tee "$build_dir/${log_file:?}"
docker tag "$image_name:$image_tag" "$image_name:latest"
fi
}
cmd_clean() {
stop_cntrs
del_cntrs
del_imgs
rm -rf docker/_build/
}
cmd_run() {
local args="-i -t --privileged -h scion"
args+=" -v $PWD/htmlcov:/home/scion/go/src/github.com/netsec-ethz/scion/htmlcov"
args+=" -v $PWD/logs:/home/scion/go/src/github.com/netsec-ethz/scion/logs"
args+=" -v $PWD/sphinx-doc/_build:/home/scion/go/src/github.com/netsec-ethz/scion/sphinx-doc/_build"
# Can't use --rm in circleci, their environment doesn't allow it, so it
# just throws an error
[ -n "$CIRCLECI" ] || args+=" --rm"
setup_volumes
docker run $args scion "$@"
}
setup_volumes() {
set -e
for i in htmlcov logs sphinx-doc/_build; do
mkdir -p "$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 "$i" ] || { echo "Error: '$i' dir not owned by $LOGNAME"; exit 1; }
done
}
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 '/^scion/ {print $1":"$2}; /^<none>/ {print $3}')
if [ -n "$images" ]; then
echo
echo "Deleting all generated images"
echo "============================="
docker rmi $images
fi
}
cmd_help() {
cat <<-_EOF
Usage:
$PROGRAM build
$PROGRAM run
Run the Docker image.
$PROGRAM clean
Remove all Docker containers and all generated images.
$PROGRAM help
Show this text.
_EOF
}
PROGRAM="${0##*/}"
COMMAND="$1"
ARG="$2"
if ! ( [ $(id -u) -eq 0 ] || groups | grep -q "\<docker\>"; ); then
echo "Error: you must either be root, or 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
build) cmd_build ;;
clean) cmd_clean ;;
run) shift; cmd_run "$@" ;;
help) cmd_help ;;
*) cmd_help ;;
esac