Repository navigation
Expand file tree
/
Copy pathcontainer-jdk.sh
More file actions
executable file
·70 lines (65 loc) · 2.48 KB
/
Copy pathcontainer-jdk.sh
File metadata and controls
executable file
·70 lines (65 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
# Make <dir> a stand-in JDK home whose bin/java and bin/javac run inside a
# container image. The Maven production legs use it for their drop-in check
# on a Java release no JDK installer offers any more (Java 6: no
# actions/setup-java distribution publishes it).
#
# scripts/container-jdk.sh <name@sha256:digest> <dir> <mount>
#
# <mount> is the one host directory the tools can see, mounted at the same
# path. The legs keep every file they hand to a JDK (the harness source, the
# two jars, the dependency jars) under their temp directory, so pass that.
# The tools run without network and as the calling user, and every run is
# bounded: 300 s inside the container, 330 s for the docker client.
#
# The image must be pinned by digest. Unless it is already present it is
# pulled here, bounded to two minutes, and one `java -version` proves the
# wrapper works before a test depends on it.
#
# Exit status: 0 when <dir> is ready; 3 when Docker is missing, the image
# cannot be pulled or it does not run here (the caller leaves that JDK out);
# 2 for a usage error.
set -euo pipefail
if [ $# -ne 3 ]; then
sed -n '2,21p' "$0" | sed 's/^# \{0,1\}//' >&2
exit 2
fi
image="$1"
dir="$2"
mount="$3"
if ! printf '%s' "$image" | grep -Eq '@sha256:[0-9a-f]{64}$'; then
echo "container-jdk.sh: pin the image by digest (name@sha256:<64 hex>), got $image" >&2
exit 2
fi
# Run "$@" for at most $1 seconds. `timeout` is not on macOS; perl is on
# every machine these suites run on.
bounded() {
perl -e 'alarm shift; exec @ARGV or die "exec $ARGV[0]: $!\n"' "$@"
}
if ! command -v docker >/dev/null 2>&1; then
echo "container-jdk.sh: docker is not installed" >&2
exit 3
fi
if ! bounded 20 docker image inspect "$image" >/dev/null 2>&1; then
if ! bounded 120 docker pull --quiet --platform linux/amd64 "$image" >&2; then
echo "container-jdk.sh: could not pull $image" >&2
exit 3
fi
fi
mkdir -p "$dir/bin" "$mount"
mount=$(cd "$mount" && pwd -P)
for tool in java javac; do
cat > "$dir/bin/$tool" <<EOF
#!/usr/bin/env bash
# Generated by scripts/container-jdk.sh: \`$tool\` from $image.
exec perl -e 'alarm shift; exec @ARGV or die "exec \$ARGV[0]: \$!\\n"' 330 \\
docker run --rm --pull=never --network none --platform linux/amd64 \\
--user "\$(id -u):\$(id -g)" -v "$mount:$mount" -w "\$PWD" \\
"$image" timeout 300 $tool "\$@"
EOF
chmod +x "$dir/bin/$tool"
done
if ! bounded 120 "$dir/bin/java" -version >&2; then
echo "container-jdk.sh: $image does not run here" >&2
exit 3
fi