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