10808: Avoid using the disk cache if a different user owns it (e.g., running a rake...
[arvados.git] / build / run-build-packages-one-target.sh
1 #!/bin/bash
2
3 read -rd "\000" helpmessage <<EOF
4 $(basename $0): Orchestrate run-build-packages.sh for one target
5
6 Syntax:
7         WORKSPACE=/path/to/arvados $(basename $0) [options]
8
9 --target <target>
10     Distribution to build packages for (default: debian8)
11 --command
12     Build command to execute (default: use built-in Docker image command)
13 --test-packages
14     Run package install test script "test-packages-$target.sh"
15 --debug
16     Output debug information (default: false)
17 --only-build <package>
18     Build only a specific package
19 --only-test <package>
20     Test only a specific package
21
22 WORKSPACE=path         Path to the Arvados source tree to build packages from
23
24 EOF
25
26 set -e
27
28 if ! [[ -n "$WORKSPACE" ]]; then
29   echo >&2 "$helpmessage"
30   echo >&2
31   echo >&2 "Error: WORKSPACE environment variable not set"
32   echo >&2
33   exit 1
34 fi
35
36 if ! [[ -d "$WORKSPACE" ]]; then
37   echo >&2 "$helpmessage"
38   echo >&2
39   echo >&2 "Error: $WORKSPACE is not a directory"
40   echo >&2
41   exit 1
42 fi
43
44 PARSEDOPTS=$(getopt --name "$0" --longoptions \
45     help,debug,test-packages,target:,command:,only-test:,only-build: \
46     -- "" "$@")
47 if [ $? -ne 0 ]; then
48     exit 1
49 fi
50
51 TARGET=debian8
52 COMMAND=
53 DEBUG=
54
55 eval set -- "$PARSEDOPTS"
56 while [ $# -gt 0 ]; do
57     case "$1" in
58         --help)
59             echo >&2 "$helpmessage"
60             echo >&2
61             exit 1
62             ;;
63         --target)
64             TARGET="$2"; shift
65             ;;
66         --only-test)
67             test_packages=1
68             packages="$2"; shift
69             ;;
70         --only-build)
71             ONLY_BUILD="$2"; shift
72             ;;
73         --debug)
74             DEBUG=" --debug"
75             ;;
76         --command)
77             COMMAND="$2"; shift
78             ;;
79         --test-packages)
80             test_packages=1
81             ;;
82         --)
83             if [ $# -gt 1 ]; then
84                 echo >&2 "$0: unrecognized argument '$2'. Try: $0 --help"
85                 exit 1
86             fi
87             ;;
88     esac
89     shift
90 done
91
92 set -e
93
94 if [[ -n "$test_packages" ]]; then
95     if [[ -n "$(find $WORKSPACE/packages/$TARGET -name '*.rpm')" ]] ; then
96         createrepo $WORKSPACE/packages/$TARGET
97     fi
98
99     if [[ -n "$(find $WORKSPACE/packages/$TARGET -name '*.deb')" ]] ; then
100         (cd $WORKSPACE/packages/$TARGET
101          dpkg-scanpackages .  2> >(grep -v 'warning' 1>&2) | gzip -c > Packages.gz
102         )
103     fi
104
105     COMMAND="/jenkins/package-testing/test-packages-$TARGET.sh"
106     IMAGE="arvados/package-test:$TARGET"
107 else
108     IMAGE="arvados/build:$TARGET"
109     if [[ "$COMMAND" != "" ]]; then
110         COMMAND="/usr/local/rvm/bin/rvm-exec default bash /jenkins/$COMMAND --target $TARGET$DEBUG"
111     fi
112 fi
113
114 JENKINS_DIR=$(dirname "$(readlink -e "$0")")
115
116 if [[ -n "$test_packages" ]]; then
117     pushd "$JENKINS_DIR/package-test-dockerfiles"
118 else
119     pushd "$JENKINS_DIR/package-build-dockerfiles"
120     make "$TARGET/generated"
121 fi
122
123 echo $TARGET
124 cd $TARGET
125 time docker build --tag=$IMAGE .
126 popd
127
128 if test -z "$packages" ; then
129     packages="arvados-api-server
130         arvados-docker-cleaner
131         arvados-git-httpd
132         arvados-node-manager
133         arvados-src
134         arvados-workbench
135         crunch-dispatch-local
136         crunch-dispatch-slurm
137         crunch-run
138         crunchstat
139         keep-balance
140         keep-block-check
141         keepproxy
142         keep-rsync
143         keepstore
144         keep-web
145         libarvados-perl"
146
147     case "$TARGET" in
148         *)
149             packages="$packages python-arvados-fuse
150                   python-arvados-python-client python-arvados-cwl-runner"
151             ;;
152     esac
153 fi
154
155 FINAL_EXITCODE=0
156
157 package_fails=""
158
159 mkdir -p "$WORKSPACE/apps/workbench/vendor/cache-$TARGET"
160 mkdir -p "$WORKSPACE/services/api/vendor/cache-$TARGET"
161
162 docker_volume_args=(
163     -v "$JENKINS_DIR:/jenkins"
164     -v "$WORKSPACE:/arvados"
165     -v /arvados/services/api/vendor/bundle
166     -v /arvados/apps/workbench/vendor/bundle
167     -v "$WORKSPACE/services/api/vendor/cache-$TARGET:/arvados/services/api/vendor/cache"
168     -v "$WORKSPACE/apps/workbench/vendor/cache-$TARGET:/arvados/apps/workbench/vendor/cache"
169 )
170
171 if [[ -n "$test_packages" ]]; then
172     for p in $packages ; do
173         if [[ -n "$ONLY_BUILD" ]] && [[ "$p" != "$ONLY_BUILD" ]]; then
174             continue
175         fi
176         echo
177         echo "START: $p test on $IMAGE" >&2
178         if docker run --rm \
179             "${docker_volume_args[@]}" \
180             --env ARVADOS_DEBUG=1 \
181             --env "TARGET=$TARGET" \
182             --env "WORKSPACE=/arvados" \
183             "$IMAGE" $COMMAND $p
184         then
185             echo "OK: $p test on $IMAGE succeeded" >&2
186         else
187             FINAL_EXITCODE=$?
188             package_fails="$package_fails $p"
189             echo "ERROR: $p test on $IMAGE failed with exit status $FINAL_EXITCODE" >&2
190         fi
191     done
192 else
193     echo
194     echo "START: build packages on $IMAGE" >&2
195     if docker run --rm \
196         "${docker_volume_args[@]}" \
197         --env ARVADOS_DEBUG=1 \
198         --env "ONLY_BUILD=$ONLY_BUILD" \
199         "$IMAGE" $COMMAND
200     then
201         echo
202         echo "OK: build packages on $IMAGE succeeded" >&2
203     else
204         FINAL_EXITCODE=$?
205         echo "ERROR: build packages on $IMAGE failed with exit status $FINAL_EXITCODE" >&2
206     fi
207 fi
208
209 if test -n "$package_fails" ; then
210     echo "Failed package tests:$package_fails" >&2
211 fi
212
213 exit $FINAL_EXITCODE