Merge branch '11309-crunchstat-summary-containers'
[arvados.git] / build / run-build-docker-images.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 function usage {
7     echo >&2
8     echo >&2 "usage: $0 [options]"
9     echo >&2
10     echo >&2 "$0 options:"
11     echo >&2 "  -t, --tags [csv_tags]         comma separated tags"
12     echo >&2 "  -u, --upload                  Upload the images (docker push)"
13     echo >&2 "  -h, --help                    Display this help and exit"
14     echo >&2
15     echo >&2 "  If no options are given, just builds the images."
16 }
17
18 upload=false
19
20 # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
21 TEMP=`getopt -o hut: \
22     --long help,upload,tags: \
23     -n "$0" -- "$@"`
24
25 if [ $? != 0 ] ; then echo "Use -h for help"; exit 1 ; fi
26 # Note the quotes around `$TEMP': they are essential!
27 eval set -- "$TEMP"
28
29 while [ $# -ge 1 ]
30 do
31     case $1 in
32         -u | --upload)
33             upload=true
34             shift
35             ;;
36         -t | --tags)
37             case "$2" in
38                 "")
39                   echo "ERROR: --tags needs a parameter";
40                   usage;
41                   exit 1
42                   ;;
43                 *)
44                   tags=$2;
45                   shift 2
46                   ;;
47             esac
48             ;;
49         --)
50             shift
51             break
52             ;;
53         *)
54             usage
55             exit 1
56             ;;
57     esac
58 done
59
60
61 EXITCODE=0
62
63 COLUMNS=80
64
65 title () {
66     printf "\n%*s\n\n" $(((${#title}+$COLUMNS)/2)) "********** $1 **********"
67 }
68
69 docker_push () {
70     if [[ ! -z "$tags" ]]
71     then
72         for tag in $( echo $tags|tr "," " " )
73         do
74              $DOCKER tag $1 $1:$tag
75         done
76     fi
77
78     # Sometimes docker push fails; retry it a few times if necessary.
79     for i in `seq 1 5`; do
80         $DOCKER push $*
81         ECODE=$?
82         if [[ "$ECODE" == "0" ]]; then
83             break
84         fi
85     done
86
87     if [[ "$ECODE" != "0" ]]; then
88         title "!!!!!! docker push $* failed !!!!!!"
89         EXITCODE=$(($EXITCODE + $ECODE))
90     fi
91 }
92
93 timer_reset() {
94     t0=$SECONDS
95 }
96
97 timer() {
98     echo -n "$(($SECONDS - $t0))s"
99 }
100
101 # Sanity check
102 if ! [[ -n "$WORKSPACE" ]]; then
103     echo >&2
104     echo >&2 "Error: WORKSPACE environment variable not set"
105     echo >&2
106     exit 1
107 fi
108
109 echo $WORKSPACE
110
111 # find the docker binary
112 DOCKER=`which docker.io`
113
114 if [[ "$DOCKER" == "" ]]; then
115     DOCKER=`which docker`
116 fi
117
118 if [[ "$DOCKER" == "" ]]; then
119     title "Error: you need to have docker installed. Could not find the docker executable."
120     exit 1
121 fi
122
123 # DOCKER
124 title "Starting docker build"
125
126 timer_reset
127
128 # clean up the docker build environment
129 cd "$WORKSPACE"
130
131 title "Starting arvbox build localdemo"
132
133 tools/arvbox/bin/arvbox build localdemo
134 ECODE=$?
135
136 if [[ "$ECODE" != "0" ]]; then
137     title "!!!!!! docker BUILD FAILED !!!!!!"
138     EXITCODE=$(($EXITCODE + $ECODE))
139 fi
140
141 title "Starting arvbox build dev"
142
143 tools/arvbox/bin/arvbox build dev
144
145 ECODE=$?
146
147 if [[ "$ECODE" != "0" ]]; then
148     title "!!!!!! docker BUILD FAILED !!!!!!"
149     EXITCODE=$(($EXITCODE + $ECODE))
150 fi
151
152 title "docker build complete (`timer`)"
153
154 title "uploading images"
155
156 timer_reset
157
158 if [[ "$EXITCODE" != "0" ]]; then
159     title "upload arvados images SKIPPED because build failed"
160 else
161     if [[ $upload == true ]]; then
162         ## 20150526 nico -- *sometimes* dockerhub needs re-login
163         ## even though credentials are already in .dockercfg
164         docker login -u arvados
165
166         docker_push arvados/arvbox-dev
167         docker_push arvados/arvbox-demo
168         title "upload arvados images complete (`timer`)"
169     else
170         title "upload arvados images SKIPPED because no --upload option set"
171     fi
172 fi
173
174 exit $EXITCODE