]> git.lizzy.rs Git - rust.git/blob - src/ci/docker/run.sh
0e1485601e96ee8815cbf96c28043879876692ea
[rust.git] / src / ci / docker / run.sh
1 #!/usr/bin/env bash
2
3 set -e
4
5 export MSYS_NO_PATHCONV=1
6
7 script=`cd $(dirname $0) && pwd`/`basename $0`
8 image=$1
9
10 docker_dir="`dirname $script`"
11 ci_dir="`dirname $docker_dir`"
12 src_dir="`dirname $ci_dir`"
13 root_dir="`dirname $src_dir`"
14
15 objdir=$root_dir/obj
16 dist=$objdir/build/dist
17
18 source "$ci_dir/shared.sh"
19
20 travis_fold start build_docker
21 travis_time_start
22
23 if [ -f "$docker_dir/$image/Dockerfile" ]; then
24     if [ "$CI" != "" ]; then
25       hash_key=/tmp/.docker-hash-key.txt
26       rm -f "${hash_key}"
27       echo $image >> $hash_key
28
29       cat "$docker_dir/$image/Dockerfile" >> $hash_key
30       # Look for all source files involves in the COPY command
31       copied_files=/tmp/.docker-copied-files.txt
32       rm -f "$copied_files"
33       for i in $(sed -n -e 's/^COPY \(.*\) .*$/\1/p' "$docker_dir/$image/Dockerfile"); do
34         # List the file names
35         find "$docker_dir/$i" -type f >> $copied_files
36       done
37       # Sort the file names and cat the content into the hash key
38       sort $copied_files | xargs cat >> $hash_key
39
40       docker --version >> $hash_key
41       cksum=$(sha512sum $hash_key | \
42         awk '{print $1}')
43
44       s3url="s3://$SCCACHE_BUCKET/docker/$cksum"
45       url="https://s3-us-west-1.amazonaws.com/$SCCACHE_BUCKET/docker/$cksum"
46       upload="aws s3 cp - $s3url"
47
48       echo "Attempting to download $url"
49       rm -f /tmp/rustci_docker_cache
50       set +e
51       retry curl -y 30 -Y 10 --connect-timeout 30 -f -L -C - -o /tmp/rustci_docker_cache "$url"
52       loaded_images=$(docker load -i /tmp/rustci_docker_cache | sed 's/.* sha/sha/')
53       set -e
54       echo "Downloaded containers:\n$loaded_images"
55     fi
56
57     dockerfile="$docker_dir/$image/Dockerfile"
58     if [ -x /usr/bin/cygpath ]; then
59         context="`cygpath -w $docker_dir`"
60         dockerfile="`cygpath -w $dockerfile`"
61     else
62         context="$docker_dir"
63     fi
64     retry docker \
65       build \
66       --rm \
67       -t rust-ci \
68       -f "$dockerfile" \
69       "$context"
70
71     if [ "$upload" != "" ]; then
72       digest=$(docker inspect rust-ci --format '{{.Id}}')
73       echo "Built container $digest"
74       if ! grep -q "$digest" <(echo "$loaded_images"); then
75         echo "Uploading finished image to $url"
76         set +e
77         docker history -q rust-ci | \
78           grep -v missing | \
79           xargs docker save | \
80           gzip | \
81           $upload
82         set -e
83       else
84         echo "Looks like docker image is the same as before, not uploading"
85       fi
86       # Record the container image for reuse, e.g. by rustup.rs builds
87       info="$dist/image-$image.txt"
88       mkdir -p "$dist"
89       echo "$url" >"$info"
90       echo "$digest" >>"$info"
91     fi
92 elif [ -f "$docker_dir/disabled/$image/Dockerfile" ]; then
93     if isCI; then
94         echo Cannot run disabled images on CI!
95         exit 1
96     fi
97     # retry messes with the pipe from tar to docker. Not needed on non-travis
98     # Transform changes the context of disabled Dockerfiles to match the enabled ones
99     tar --transform 's#^./disabled/#./#' -C $docker_dir -c . | docker \
100       build \
101       --rm \
102       -t rust-ci \
103       -f "$image/Dockerfile" \
104       -
105 else
106     echo Invalid image: $image
107     exit 1
108 fi
109
110 travis_fold end build_docker
111 travis_time_finish
112
113 mkdir -p $HOME/.cargo
114 mkdir -p $objdir/tmp
115 mkdir -p $objdir/cores
116
117 args=
118 if [ "$SCCACHE_BUCKET" != "" ]; then
119     args="$args --env SCCACHE_BUCKET"
120     args="$args --env SCCACHE_REGION"
121     args="$args --env AWS_ACCESS_KEY_ID"
122     args="$args --env AWS_SECRET_ACCESS_KEY"
123 else
124     mkdir -p $HOME/.cache/sccache
125     args="$args --env SCCACHE_DIR=/sccache --volume $HOME/.cache/sccache:/sccache"
126 fi
127
128 # Run containers as privileged as it should give them access to some more
129 # syscalls such as ptrace and whatnot. In the upgrade to LLVM 5.0 it was
130 # discovered that the leak sanitizer apparently needs these syscalls nowadays so
131 # we'll need `--privileged` for at least the `x86_64-gnu` builder, so this just
132 # goes ahead and sets it for all builders.
133 args="$args --privileged"
134
135 exec docker \
136   run \
137   --volume "$root_dir:/checkout:ro" \
138   --volume "$objdir:/checkout/obj" \
139   --workdir /checkout/obj \
140   --env SRC=/checkout \
141   $args \
142   --env CARGO_HOME=/cargo \
143   --env DEPLOY \
144   --env DEPLOY_ALT \
145   --env LOCAL_USER_ID=`id -u` \
146   --env CI \
147   --env TRAVIS \
148   --env TRAVIS_BRANCH \
149   --env TF_BUILD \
150   --env BUILD_SOURCEBRANCHNAME \
151   --env TOOLSTATE_REPO_ACCESS_TOKEN \
152   --env CI_JOB_NAME="${CI_JOB_NAME-$IMAGE}" \
153   --volume "$HOME/.cargo:/cargo" \
154   --volume "$HOME/rustsrc:$HOME/rustsrc" \
155   --init \
156   --rm \
157   rust-ci \
158   /checkout/src/ci/run.sh