]> git.lizzy.rs Git - rust.git/blob - src/ci/docker/run.sh
Auto merge of #86977 - vakaras:body_with_borrowck_facts, r=nikomatsakis
[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
9 image=""
10 dev=0
11
12 while [[ $# -gt 0 ]]
13 do
14   case "$1" in
15     --dev)
16       dev=1
17       ;;
18     *)
19       if [ -n "$image" ]
20       then
21         echo "expected single argument for the image name"
22         exit 1
23       fi
24       image="$1"
25       ;;
26   esac
27   shift
28 done
29
30 script_dir="`dirname $script`"
31 docker_dir="${script_dir}/host-$(uname -m)"
32 ci_dir="`dirname $script_dir`"
33 src_dir="`dirname $ci_dir`"
34 root_dir="`dirname $src_dir`"
35
36 objdir=$root_dir/obj
37 dist=$objdir/build/dist
38
39 source "$ci_dir/shared.sh"
40
41 CACHE_DOMAIN="${CACHE_DOMAIN:-ci-caches.rust-lang.org}"
42
43 if [ -f "$docker_dir/$image/Dockerfile" ]; then
44     if [ "$CI" != "" ]; then
45       hash_key=/tmp/.docker-hash-key.txt
46       rm -f "${hash_key}"
47       echo $image >> $hash_key
48
49       cat "$docker_dir/$image/Dockerfile" >> $hash_key
50       # Look for all source files involves in the COPY command
51       copied_files=/tmp/.docker-copied-files.txt
52       rm -f "$copied_files"
53       for i in $(sed -n -e 's/^COPY \(.*\) .*$/\1/p' "$docker_dir/$image/Dockerfile"); do
54         # List the file names
55         find "$script_dir/$i" -type f >> $copied_files
56       done
57       # Sort the file names and cat the content into the hash key
58       sort $copied_files | xargs cat >> $hash_key
59
60       # Include the architecture in the hash key, since our Linux CI does not
61       # only run in x86_64 machines.
62       uname -m >> $hash_key
63
64       docker --version >> $hash_key
65       cksum=$(sha512sum $hash_key | \
66         awk '{print $1}')
67
68       url="https://$CACHE_DOMAIN/docker/$cksum"
69
70       echo "Attempting to download $url"
71       rm -f /tmp/rustci_docker_cache
72       set +e
73       retry curl -y 30 -Y 10 --connect-timeout 30 -f -L -C - -o /tmp/rustci_docker_cache "$url"
74       loaded_images=$(docker load -i /tmp/rustci_docker_cache | sed 's/.* sha/sha/')
75       set -e
76       echo "Downloaded containers:\n$loaded_images"
77     fi
78
79     dockerfile="$docker_dir/$image/Dockerfile"
80     if [ -x /usr/bin/cygpath ]; then
81         context="`cygpath -w $script_dir`"
82         dockerfile="`cygpath -w $dockerfile`"
83     else
84         context="$script_dir"
85     fi
86     retry docker \
87       build \
88       --rm \
89       -t rust-ci \
90       -f "$dockerfile" \
91       "$context"
92
93     if [ "$CI" != "" ]; then
94       s3url="s3://$SCCACHE_BUCKET/docker/$cksum"
95       upload="aws s3 cp - $s3url"
96       digest=$(docker inspect rust-ci --format '{{.Id}}')
97       echo "Built container $digest"
98       if ! grep -q "$digest" <(echo "$loaded_images"); then
99         echo "Uploading finished image to $url"
100         set +e
101         docker history -q rust-ci | \
102           grep -v missing | \
103           xargs docker save | \
104           gzip | \
105           $upload
106         set -e
107       else
108         echo "Looks like docker image is the same as before, not uploading"
109       fi
110       # Record the container image for reuse, e.g. by rustup.rs builds
111       info="$dist/image-$image.txt"
112       mkdir -p "$dist"
113       echo "$url" >"$info"
114       echo "$digest" >>"$info"
115     fi
116 elif [ -f "$docker_dir/disabled/$image/Dockerfile" ]; then
117     if isCI; then
118         echo Cannot run disabled images on CI!
119         exit 1
120     fi
121     # Transform changes the context of disabled Dockerfiles to match the enabled ones
122     tar --transform 's#disabled/#./#' -C $script_dir -c . | docker \
123       build \
124       --rm \
125       -t rust-ci \
126       -f "host-$(uname -m)/$image/Dockerfile" \
127       -
128 else
129     echo Invalid image: $image
130
131     # Check whether the image exists for other architectures
132     for arch_dir in "${script_dir}"/host-*; do
133         # Avoid checking non-directories and the current host architecture directory
134         if ! [[ -d "${arch_dir}" ]]; then
135             continue
136         fi
137         if [[ "${arch_dir}" = "${docker_dir}" ]]; then
138             continue
139         fi
140
141         arch_name="$(basename "${arch_dir}" | sed 's/^host-//')"
142         if [[ -f "${arch_dir}/${image}/Dockerfile" ]]; then
143             echo "Note: the image exists for the ${arch_name} host architecture"
144         elif [[ -f "${arch_dir}/disabled/${image}/Dockerfile" ]]; then
145             echo "Note: the disabled image exists for the ${arch_name} host architecture"
146         else
147             continue
148         fi
149         echo "Note: the current host architecture is $(uname -m)"
150     done
151
152     exit 1
153 fi
154
155 mkdir -p $HOME/.cargo
156 mkdir -p $objdir/tmp
157 mkdir -p $objdir/cores
158 mkdir -p /tmp/toolstate
159
160 args=
161 if [ "$SCCACHE_BUCKET" != "" ]; then
162     args="$args --env SCCACHE_BUCKET"
163     args="$args --env SCCACHE_REGION"
164     args="$args --env AWS_ACCESS_KEY_ID"
165     args="$args --env AWS_SECRET_ACCESS_KEY"
166 else
167     mkdir -p $HOME/.cache/sccache
168     args="$args --env SCCACHE_DIR=/sccache --volume $HOME/.cache/sccache:/sccache"
169 fi
170
171 # Run containers as privileged as it should give them access to some more
172 # syscalls such as ptrace and whatnot. In the upgrade to LLVM 5.0 it was
173 # discovered that the leak sanitizer apparently needs these syscalls nowadays so
174 # we'll need `--privileged` for at least the `x86_64-gnu` builder, so this just
175 # goes ahead and sets it for all builders.
176 args="$args --privileged"
177
178 # Things get a little weird if this script is already running in a docker
179 # container. If we're already in a docker container then we assume it's set up
180 # to do docker-in-docker where we have access to a working `docker` command.
181 #
182 # If this is the case (we check via the presence of `/.dockerenv`)
183 # then we can't actually use the `--volume` argument. Typically we use
184 # `--volume` to efficiently share the build and source directory between this
185 # script and the container we're about to spawn. If we're inside docker already
186 # though the `--volume` argument maps the *host's* folder to the container we're
187 # about to spawn, when in fact we want the folder in this container itself. To
188 # work around this we use a recipe cribbed from
189 # https://circleci.com/docs/2.0/building-docker-images/#mounting-folders to
190 # create a temporary container with a volume. We then copy the entire source
191 # directory into this container, and then use that copy in the container we're
192 # about to spawn. Finally after the build finishes we re-extract the object
193 # directory.
194 #
195 # Note that none of this is necessary if we're *not* in a docker-in-docker
196 # scenario. If this script is run on a bare metal host then we share a bunch of
197 # data directories to share as much data as possible. Note that we also use
198 # `LOCAL_USER_ID` (recognized in `src/ci/run.sh`) to ensure that files are all
199 # read/written as the same user as the bare-metal user.
200 if [ -f /.dockerenv ]; then
201   docker create -v /checkout --name checkout alpine:3.4 /bin/true
202   docker cp . checkout:/checkout
203   args="$args --volumes-from checkout"
204 else
205   args="$args --volume $root_dir:/checkout:ro"
206   args="$args --volume $objdir:/checkout/obj"
207   args="$args --volume $HOME/.cargo:/cargo"
208   args="$args --volume $HOME/rustsrc:$HOME/rustsrc"
209   args="$args --volume /tmp/toolstate:/tmp/toolstate"
210   args="$args --env LOCAL_USER_ID=`id -u`"
211 fi
212
213 if [ "$dev" = "1" ]
214 then
215   # Interactive + TTY
216   args="$args -it"
217   command="/bin/bash"
218 else
219   command="/checkout/src/ci/run.sh"
220 fi
221
222 if [ "$CI" != "" ]; then
223   # Get some needed information for $BASE_COMMIT
224   #
225   # This command gets the last merge commit which we'll use as base to list
226   # deleted files since then.
227   BASE_COMMIT="$(git log --author=bors@rust-lang.org -n 2 --pretty=format:%H | tail -n 1)"
228 else
229   BASE_COMMIT=""
230 fi
231
232 docker \
233   run \
234   --workdir /checkout/obj \
235   --env SRC=/checkout \
236   $args \
237   --env CARGO_HOME=/cargo \
238   --env DEPLOY \
239   --env DEPLOY_ALT \
240   --env CI \
241   --env TF_BUILD \
242   --env BUILD_SOURCEBRANCHNAME \
243   --env GITHUB_ACTIONS \
244   --env GITHUB_REF \
245   --env TOOLSTATE_REPO_ACCESS_TOKEN \
246   --env TOOLSTATE_REPO \
247   --env TOOLSTATE_PUBLISH \
248   --env RUST_CI_OVERRIDE_RELEASE_CHANNEL \
249   --env CI_JOB_NAME="${CI_JOB_NAME-$IMAGE}" \
250   --env BASE_COMMIT="$BASE_COMMIT" \
251   --init \
252   --rm \
253   rust-ci \
254   $command
255
256 if [ -f /.dockerenv ]; then
257   rm -rf $objdir
258   docker cp checkout:/checkout/obj $objdir
259 fi