style and fashion for classy 'if' statements
[arvados.git] / jenkins / run-docker-tests.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 "  -h, --help                    Display this help and exit"
10     echo >&2
11     echo >&2 "  If no options are given, just builds the images."
12 }
13
14 upload=false
15
16 # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
17 TEMP=`getopt -o hu \
18     --long help,upload \
19     -n "$0" -- "$@"`
20
21 if [ $? != 0 ] ; then echo "Use -h for help"; exit 1 ; fi
22 # Note the quotes around `$TEMP': they are essential!
23 eval set -- "$TEMP"
24
25 while [ $# -ge 1 ]
26 do
27     case $1 in
28         -u | --upload)
29             upload=true
30             shift
31             ;;
32         --)
33             shift
34             break
35             ;;
36         *)
37             usage
38             exit 1
39             ;;
40     esac
41 done
42
43
44 EXITCODE=0
45
46 COLUMNS=80
47
48 title () {
49     printf "\n%*s\n\n" $(((${#title}+$COLUMNS)/2)) "********** $1 **********"
50 }
51
52 docker_push () {
53   # Sometimes docker push fails; retry it a few times if necessary.
54     for i in `seq 1 5`; do
55         $DOCKER push $*
56         ECODE=$?
57         if [[ "$ECODE" == "0" ]]; then
58             break
59         fi
60     done
61
62     if [[ "$ECODE" != "0" ]]; then
63         title "!!!!!! docker push $* failed !!!!!!"
64         EXITCODE=$(($EXITCODE + $ECODE))
65     fi
66 }
67
68 timer_reset() {
69     t0=$SECONDS
70 }
71
72 timer() {
73     echo -n "$(($SECONDS - $t0))s"
74 }
75
76 # Sanity check
77 if ! [[ -n "$WORKSPACE" ]]; then
78     echo >&2
79     echo >&2 "Error: WORKSPACE environment variable not set"
80     echo >&2
81     exit 1
82 fi
83
84 echo $WORKSPACE
85
86 # find the docker binary
87 DOCKER=`which docker.io`
88
89 if [[ "$DOCKER" == "" ]]; then
90     DOCKER=`which docker`
91 fi
92
93 if [[ "$DOCKER" == "" ]]; then
94     title "Error: you need to have docker installed. Could not find the docker executable."
95     exit 1
96 fi
97
98 # DOCKER
99 title "Starting docker build"
100
101 timer_reset
102
103 # clean up the docker build environment
104 cd "$WORKSPACE"
105 cd docker
106 ./build.sh realclean
107
108 rm -f config.yml
109
110 # Get test config.yml file
111 cp $HOME/docker/config.yml .
112
113 ./build.sh
114
115 ECODE=$?
116
117 if [[ "$ECODE" != "0" ]]; then
118     title "!!!!!! docker BUILD FAILED !!!!!!"
119     EXITCODE=$(($EXITCODE + $ECODE))
120 fi
121
122 title "docker build complete (`timer`)"
123
124 title "uploading images"
125
126 timer_reset
127
128 if [[ "$ECODE" != "0" ]]; then
129     title "upload arvados images SKIPPED because build failed"
130 else
131     if [[ $upload == true ]]; then 
132         docker_push arvados/api
133         docker_push arvados/compute
134         docker_push arvados/doc
135         docker_push arvados/workbench
136         docker_push arvados/keep
137         docker_push arvados/keepproxy
138         docker_push arvados/shell
139         docker_push arvados/sso
140         title "upload arvados images complete (`timer`)"
141     else
142         title "upload arvados images SKIPPED because no --upload option set"
143     fi
144 fi
145
146 title "Starting docker java-bwa-samtools build"
147
148 timer_reset
149
150 ./build.sh java-bwa-samtools-image
151
152 ECODE=$?
153
154 if [[ "$ECODE" != "0" ]]; then
155     title "!!!!!! docker java-bwa-samtools BUILD FAILED !!!!!!"
156     EXITCODE=$(($EXITCODE + $ECODE))
157 fi
158
159 title "docker build java-bwa-samtools complete (`timer`)"
160
161 timer_reset
162
163 if [[ "$ECODE" != "0" ]]; then
164     title "upload arvados/jobs image SKIPPED because build failed"
165 else
166     if [[ $upload == true ]]; then 
167         title "upload arvados/jobs image"
168         docker_push arvados/jobs
169         title "upload arvados/jobs image complete (`timer`)"
170     else
171         title "upload arvados images SKIPPED because no --upload option set"
172     fi
173 fi
174
175 exit $EXITCODE