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