-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild
executable file
·86 lines (74 loc) · 2.32 KB
/
build
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
#!/bin/bash -e
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
DATA_BASE_IMAGE_VERSION=0.9.17
usage() {
cat <<END
Usage: $progname OPTIONS
-c Required: Specify the main container name. Will only be built if it doesn't exist, unless -f is passed
-f Force a rebuild of the main container, even if it exists
-p Path to the docker root
-h Display this help text
${*:-}
END
exit 1
}
while getopts "fc:p:" OPTION ; do
case $OPTION in
c)
container_name=$OPTARG
;;
f)
force_rebuild=true
;;
p)
docker_root=$OPTARG
;;
*)
usage
;;
esac
done
if [[ -z "$container_name" ]]; then
echo "Please specify the container name with -c <container_name>"
exit 1
fi
if [[ -z "$docker_root" ]]; then
echo "Please specify the container name with -p <docker_root>"
exit 1
fi
. "$DIR/start_docker"
need_build=false
if [[ "$force_rebuild" == "true" ]]; then
need_build=true
elif [[ -z "$(docker images | grep -E '^'"$container_name"'\s+')" ]]; then
# no docker containers with the term "$container_name" in their description
need_build=true
else
if [[ ! -z "$(which md5sum)" ]]; then
md5_tool=md5sum
elif [[ ! -z "$(which md5)" ]]; then
md5_tool=md5
else
echo "No md5 tool! Forcing rebuild"
need_build=true
fi
if [[ ! -z "$md5_tool" ]]; then
current_build_md5=$(find . \( -type f \( -name Dockerfile -o -path "./configs/*" -o -path "./container_scripts/*" -o -path "./generated/*" \) \) -exec $md5_tool "{}" \; | $md5_tool | cut -d " " -f 1)
mkdir -p "$docker_root/tmp"
build_md5_file=$docker_root/tmp/build_md5
if [[ ! -f "$build_md5_file" || "$(cat $build_md5_file)" != "$current_build_md5" ]]; then
echo "build files changed, forcing rebuild"
printf $current_build_md5 >$build_md5_file
need_build=true
fi
fi
fi
running_output="$(docker ps | grep -E '\s+'"$container_name"'\s+')" || true
if [[ "$need_build" == "true" && ! -z "$running_output" ]]; then
echo "Trying to rebuild the container image but the container is already running. Stopping the existing container"
container_id=`echo "$running_output" | perl -e 'printf +(split(/\s+/, <>))[0]'`
docker stop "$container_id"
fi
if [[ "$need_build" == "true" ]]; then
docker build -t $container_name "$docker_root"
fi