21700: Install Bundler system-wide in Rails postinst
[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 "  -i, --images [dev,demo]       Choose which images to build (default: dev and demo)"
13     echo >&2 "  -u, --upload                  Upload the images (docker push)"
14     echo >&2 "  -h, --help                    Display this help and exit"
15     echo >&2
16     echo >&2 "  If no options are given, just builds the images."
17 }
18
19 upload=false
20 images=dev,demo
21
22 # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
23 TEMP=`getopt -o hut:i: \
24     --long help,upload,tags:,images: \
25     -n "$0" -- "$@"`
26
27 if [ $? != 0 ] ; then echo "Use -h for help"; exit 1 ; fi
28 # Note the quotes around `$TEMP': they are essential!
29 eval set -- "$TEMP"
30
31 while [ $# -ge 1 ]
32 do
33     case $1 in
34         -u | --upload)
35             upload=true
36             shift
37             ;;
38         -i | --images)
39             case "$2" in
40                 "")
41                   echo "ERROR: --images needs a parameter";
42                   usage;
43                   exit 1
44                   ;;
45                 *)
46                   images=$2;
47                   shift 2
48                   ;;
49             esac
50             ;;
51         -t | --tags)
52             case "$2" in
53                 "")
54                   echo "ERROR: --tags needs a parameter";
55                   usage;
56                   exit 1
57                   ;;
58                 *)
59                   tags=$2;
60                   shift 2
61                   ;;
62             esac
63             ;;
64         --)
65             shift
66             break
67             ;;
68         *)
69             usage
70             exit 1
71             ;;
72     esac
73 done
74
75
76 EXITCODE=0
77
78 COLUMNS=80
79
80 title () {
81     printf "\n%*s\n\n" $(((${#title}+$COLUMNS)/2)) "********** $1 **********"
82 }
83
84 docker_push () {
85     # docker always creates a local 'latest' tag, and we don't want to push that
86     # tag in every case. Remove it.
87     docker rmi $1:latest
88
89     GITHEAD=$(cd $WORKSPACE && git log --format=%H -n1 HEAD)
90
91     if [[ ! -z "$tags" ]]
92     then
93         for tag in $(echo $tags|tr "," " " )
94         do
95              $DOCKER tag $1:$GITHEAD $1:$tag
96         done
97     fi
98
99     for tag in $(echo $tags|tr "," " " )
100     do
101         # Sometimes docker push fails; retry it a few times if necessary.
102         for i in `seq 1 5`; do
103              $DOCKER push $1:$tag
104              ECODE=$?
105              if [[ "$ECODE" == "0" ]]; then
106                  break
107              fi
108         done
109
110         if [[ "$ECODE" != "0" ]]; then
111             title "!!!!!! docker push $1:$tag failed !!!!!!"
112             EXITCODE=$(($EXITCODE + $ECODE))
113         fi
114     done
115 }
116
117 timer_reset() {
118     t0=$SECONDS
119 }
120
121 timer() {
122     echo -n "$(($SECONDS - $t0))s"
123 }
124
125 # Sanity check
126 if ! [[ -n "$WORKSPACE" ]]; then
127     echo >&2
128     echo >&2 "Error: WORKSPACE environment variable not set"
129     echo >&2
130     exit 1
131 fi
132
133 echo $WORKSPACE
134
135 # find the docker binary
136 DOCKER=`which docker.io`
137
138 if [[ "$DOCKER" == "" ]]; then
139     DOCKER=`which docker`
140 fi
141
142 if [[ "$DOCKER" == "" ]]; then
143     title "Error: you need to have docker installed. Could not find the docker executable."
144     exit 1
145 fi
146
147 # DOCKER
148 title "Starting docker build"
149
150 timer_reset
151
152 # clean up the docker build environment
153 cd "$WORKSPACE"
154
155 if [[ "$images" =~ demo ]]; then
156   title "Starting arvbox build localdemo"
157
158   tools/arvbox/bin/arvbox build localdemo
159   ECODE=$?
160
161   if [[ "$ECODE" != "0" ]]; then
162       title "!!!!!! docker BUILD FAILED !!!!!!"
163       EXITCODE=$(($EXITCODE + $ECODE))
164   fi
165 fi
166
167 if [[ "$images" =~ dev ]]; then
168   title "Starting arvbox build dev"
169
170   tools/arvbox/bin/arvbox build dev
171
172   ECODE=$?
173
174   if [[ "$ECODE" != "0" ]]; then
175       title "!!!!!! docker BUILD FAILED !!!!!!"
176       EXITCODE=$(($EXITCODE + $ECODE))
177   fi
178 fi
179
180 title "docker build complete (`timer`)"
181
182 if [[ "$EXITCODE" != "0" ]]; then
183     title "upload arvados images SKIPPED because build failed"
184 else
185     if [[ $upload == true ]]; then
186         title "uploading images"
187         timer_reset
188
189         ## 20150526 nico -- *sometimes* dockerhub needs re-login
190         ## even though credentials are already in .dockercfg
191         docker login -u arvados
192
193         if [[ "$images" =~ dev ]]; then
194           docker_push arvados/arvbox-dev
195         fi
196         if [[ "$images" =~ demo ]]; then
197           docker_push arvados/arvbox-demo
198         fi
199         title "upload arvados images complete (`timer`)"
200     else
201         title "upload arvados images SKIPPED because no --upload option set"
202     fi
203 fi
204
205 exit $EXITCODE