14291: Try again with vendor.json
[arvados.git] / build / run-build-docker-jobs-image.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                    version tag for docker"
12     echo >&2 "  -u, --upload                  Upload the images (docker push)"
13     echo >&2 "  --no-cache                    Don't use build cache"
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 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,no-cache,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         --no-cache)
37             NOCACHE=--no-cache
38             shift
39             ;;
40         -t | --tags)
41             case "$2" in
42                 "")
43                   echo "ERROR: --tags needs a parameter";
44                   usage;
45                   exit 1
46                   ;;
47                 *)
48                   version_tag="$2";
49                   shift 2
50                   ;;
51             esac
52             ;;
53         --)
54             shift
55             break
56             ;;
57         *)
58             usage
59             exit 1
60             ;;
61     esac
62 done
63
64 EXITCODE=0
65
66 exit_cleanly() {
67     trap - INT
68     report_outcomes
69     exit $EXITCODE
70 }
71
72 COLUMNS=80
73 . $WORKSPACE/build/run-library.sh
74
75 docker_push () {
76     # Sometimes docker push fails; retry it a few times if necessary.
77     for i in `seq 1 5`; do
78         $DOCKER push $*
79         ECODE=$?
80         if [[ "$ECODE" == "0" ]]; then
81             break
82         fi
83     done
84
85     if [[ "$ECODE" != "0" ]]; then
86         EXITCODE=$(($EXITCODE + $ECODE))
87     fi
88     checkexit $ECODE "docker push $*"
89 }
90
91 # Sanity check
92 if ! [[ -n "$WORKSPACE" ]]; then
93     echo >&2
94     echo >&2 "Error: WORKSPACE environment variable not set"
95     echo >&2
96     exit 1
97 fi
98
99 echo $WORKSPACE
100
101 # find the docker binary
102 DOCKER=`which docker.io`
103
104 if [[ "$DOCKER" == "" ]]; then
105     DOCKER=`which docker`
106 fi
107
108 if [[ "$DOCKER" == "" ]]; then
109     title "Error: you need to have docker installed. Could not find the docker executable."
110     exit 1
111 fi
112
113 # DOCKER
114 title "Starting docker build"
115
116 timer_reset
117
118 # clean up the docker build environment
119 cd "$WORKSPACE"
120
121 if [[ -z "$ARVADOS_BUILDING_VERSION" ]] && ! [[ -z "$version_tag" ]]; then
122         ARVADOS_BUILDING_VERSION="$version_tag"
123         ARVADOS_BUILDING_ITERATION="1"
124 fi
125
126 python_sdk_ts=$(cd sdk/python && timestamp_from_git)
127 cwl_runner_ts=$(cd sdk/cwl && timestamp_from_git)
128
129 python_sdk_version=$(cd sdk/python && nohash_version_from_git 0.1)
130 cwl_runner_version=$(cd sdk/cwl && nohash_version_from_git 1.0)
131
132 if [[ $python_sdk_ts -gt $cwl_runner_ts ]]; then
133     cwl_runner_version=$(cd sdk/python && nohash_version_from_git 1.0)
134 fi
135
136 echo cwl_runner_version $cwl_runner_version python_sdk_version $python_sdk_version
137
138 if [[ "${python_sdk_version}" != "${ARVADOS_BUILDING_VERSION}" ]]; then
139         python_sdk_version="${python_sdk_version}-1"
140 else
141         python_sdk_version="${ARVADOS_BUILDING_VERSION}-${ARVADOS_BUILDING_ITERATION}"
142 fi
143
144 cwl_runner_version_orig=$cwl_runner_version
145
146 if [[ "${cwl_runner_version}" != "${ARVADOS_BUILDING_VERSION}" ]]; then
147         cwl_runner_version="${cwl_runner_version}-1"
148 else
149         cwl_runner_version="${ARVADOS_BUILDING_VERSION}-${ARVADOS_BUILDING_ITERATION}"
150 fi
151
152 cd docker/jobs
153 docker build $NOCACHE \
154        --build-arg python_sdk_version=${python_sdk_version} \
155        --build-arg cwl_runner_version=${cwl_runner_version} \
156        -t arvados/jobs:$cwl_runner_version_orig .
157
158 ECODE=$?
159
160 if [[ "$ECODE" != "0" ]]; then
161     EXITCODE=$(($EXITCODE + $ECODE))
162 fi
163
164 checkexit $ECODE "docker build"
165 title "docker build complete (`timer`)"
166
167 if [[ "$ECODE" != "0" ]]; then
168   exit_cleanly
169 fi
170
171 timer_reset
172
173 if docker --version |grep " 1\.[0-9]\." ; then
174     # Docker version prior 1.10 require -f flag
175     # -f flag removed in Docker 1.12
176     FORCE=-f
177 fi
178 if ! [[ -z "$version_tag" ]]; then
179     docker tag $FORCE arvados/jobs:$cwl_runner_version_orig arvados/jobs:"$version_tag"
180 else
181     docker tag $FORCE arvados/jobs:$cwl_runner_version_orig arvados/jobs:latest
182 fi
183
184 ECODE=$?
185
186 if [[ "$ECODE" != "0" ]]; then
187     EXITCODE=$(($EXITCODE + $ECODE))
188 fi
189
190 checkexit $ECODE "docker tag"
191 title "docker tag complete (`timer`)"
192
193 title "uploading images"
194
195 timer_reset
196
197 if [[ "$ECODE" != "0" ]]; then
198     title "upload arvados images SKIPPED because build or tag failed"
199 else
200     if [[ $upload == true ]]; then
201         ## 20150526 nico -- *sometimes* dockerhub needs re-login
202         ## even though credentials are already in .dockercfg
203         docker login -u arvados
204         if ! [[ -z "$version_tag" ]]; then
205             docker_push arvados/jobs:"$version_tag"
206         else
207            docker_push arvados/jobs:$cwl_runner_version_orig
208            docker_push arvados/jobs:latest
209         fi
210         title "upload arvados images finished (`timer`)"
211     else
212         title "upload arvados images SKIPPED because no --upload option set (`timer`)"
213     fi
214 fi
215
216 exit_cleanly