blob: bf760cc9b15f0ab65f8d4be94ec5649eea825235 [file]
#!/bin/bash
#
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
### Setup script for an Ubuntu 18.04 LTS based Docker host.
# Fail on errors.
# Fail when using undefined variables.
# Print all executed commands.
# Fail when any command in a pipe fails.
set -euxo pipefail
### Prevent dpkg / apt-get / debconf from trying to access stdin.
export DEBIAN_FRONTEND="noninteractive"
### Install base packages.
{
apt-get -y update
apt-get -y dist-upgrade
apt-get -y install python-is-python3 openjdk-11-jdk-headless unzip
}
### Disable automatic upgrades, as they can interfere with our startup scripts.
{
cat > /etc/apt/apt.conf.d/10periodic <<'EOF'
APT::Periodic::Enable "0";
EOF
}
### Increase file descriptor limits
{
cat >> /etc/security/limits.conf <<'EOF'
* soft nofile 100000
* hard nofile 100000
EOF
}
### Patch the filesystem options to increase I/O performance
{
if [[ "$(uname -m)" != "aarch64" ]]; then
ROOT_DEV=$(findmnt -n -o SOURCE /)
tune2fs -o ^acl,journal_data_writeback,nobarrier "${ROOT_DEV}"
cat > /etc/fstab <<'EOF'
LABEL=cloudimg-rootfs / ext4 defaults,noatime,commit=300,journal_async_commit 0 0
LABEL=UEFI /boot/efi vfat defaults,noatime 0 0
EOF
fi
}
### Install the Buildkite Agent on production images.
{
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 \
--recv-keys 32A37959C2FA5C3C99EFBC32A79206696452D198
add-apt-repository -y "deb https://apt.buildkite.com/buildkite-agent stable main"
apt-get -y update
apt-get -y install buildkite-agent
# Disable the Buildkite agent service, as the startup script has to mount /var/lib/buildkite-agent
# first.
systemctl disable buildkite-agent
mkdir -p /etc/systemd/system/buildkite-agent.service.d
cat > /etc/systemd/system/buildkite-agent.service.d/10-oneshot-agent.conf <<'EOF'
[Service]
# Only run one job, then shutdown the machine (so that the instance group replaces it with a fresh one).
Restart=no
PermissionsStartOnly=true
ExecStopPost=/bin/systemctl poweroff
EOF
cat > /etc/systemd/system/buildkite-agent.service.d/10-disable-tasks-accounting.conf <<'EOF'
[Service]
# Disable tasks accounting, because Bazel is prone to run into resource limits there.
# This fixes the "cgroup: fork rejected by pids controller" error that some CI jobs triggered.
TasksAccounting=no
EOF
cat > /etc/systemd/system/buildkite-agent.service.d/10-environment.conf <<'EOF'
[Service]
# Setup some environment variables that we need.
Environment=ANDROID_HOME=/opt/android-sdk-linux
Environment=ANDROID_NDK_HOME=/opt/android-ndk-r15c
Environment=CLOUDSDK_PYTHON=/usr/bin/python
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
EOF
}
### Let 'localhost' resolve to '::1', otherwise one of Envoy's tests fails.
{
sed -i 's/^::1 .*/::1 localhost ip6-localhost ip6-loopback/' /etc/hosts
}
### Install Docker.
{
apt-get -y install apt-transport-https ca-certificates
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get -y update
apt-get -y install docker-ce docker-ce-cli containerd.io
# Disable the Docker service, as the startup script has to mount /var/lib/docker first.
systemctl disable docker
systemctl stop docker
# Add buildkite-agent to the docker group to grant it permissions to the Docker socket
usermod -aG docker buildkite-agent
}
## Add our minimum uptime enforcer.
{
cat > /etc/systemd/system/minimum-uptime.service <<'EOF'
[Unit]
Description=Ensures that the VM is running for at least one minute.
[Service]
Type=simple
ExecStart=/usr/bin/nohup sleep 60
TimeoutSec=60
KillSignal=SIGHUP
[Install]
WantedBy=multi-user.target
EOF
systemctl enable minimum-uptime.service
}
### Get rid of Ubuntu's snapd stuff and install the Google Cloud SDK the traditional way.
{
apt-get -y remove --purge snapd
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | \
tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
apt-get -y install apt-transport-https ca-certificates
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | \
apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
apt-get -y update
apt-get -y install google-cloud-cli
}
### Preseed our Git mirrors.
{
mkdir -p /var/lib/gitmirrors
curl -fsSL https://storage.googleapis.com/bazel-git-mirror/bazelbuild-mirror.tar | \
tar x -C /var/lib/gitmirrors --strip=1
chown -R buildkite-agent:buildkite-agent /var/lib/gitmirrors
chmod -R 0755 /var/lib/gitmirrors
}
### Install Android NDK.
{
cd /opt
curl -fsSL -o android-ndk-r15c.zip https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip
unzip android-ndk-r15c.zip > /dev/null
rm android-ndk-r15c.zip
curl -fsSL -o android-ndk-r25b.zip https://dl.google.com/android/repository/android-ndk-r25b-linux.zip
unzip android-ndk-r25b.zip > /dev/null
rm android-ndk-r25b.zip
}
### Install Android SDK.
{
mkdir -p /opt/android-sdk-linux/cmdline-tools
cd /opt/android-sdk-linux/cmdline-tools
curl -fsSL -o android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-7302050_latest.zip
unzip android-sdk.zip > /dev/null
rm android-sdk.zip
mv cmdline-tools latest
yes | latest/bin/sdkmanager --licenses > /dev/null || true
latest/bin/sdkmanager --update
latest/bin/sdkmanager \
"build-tools;36.1.0" \
"extras;android;m2repository" \
"platform-tools" \
"platforms;android-24" \
"platforms;android-28" \
"platforms;android-29" \
"platforms;android-30" \
"platforms;android-31" \
"platforms;android-32" > /dev/null
}
### Fix permissions in /opt.
{
chown -R root:root /opt
}
### Clean up and trim the filesystem (potentially reduces the final image size).
{
rm -rf /var/lib/apt/lists/*
fstrim -v /
sleep 3
}
### Enable containerd image store.
{
cat > /etc/docker/daemon.json <<'EOF'
{
"features": {
"containerd-snapshotter": true
}
}
EOF
}
### Configure and start Docker.
systemctl start docker
docker info -f '{{ .DriverStatus }}'
### Ensure that Docker images can be downloaded from GCR.
gcloud auth configure-docker --quiet
### Pull the Docker images that we need in production.
if [[ "$(uname -m)" == "aarch64" ]]; then
docker pull "gcr.io/bazel-public/rockylinux8@sha256:2b9a02bf3936565f97a9a6f3f2c069277cc130dc9343bcea5d6a7ff2f6008b4f" &
docker pull "gcr.io/bazel-public/rockylinux8-java11@sha256:64bdb00f8a652c248de3abb7df2cc012630dab1998c90cab21b50c2f61e50bd0" &
docker pull "gcr.io/bazel-public/rockylinux8-java11-devtoolset10@sha256:37ca2a50e2866dac7ac2bec786487702578eb8e89dc058249ced0cd721602f2d" &
docker pull "gcr.io/bazel-public/rockylinux8-releaser@sha256:671ed89d34afaddb86770a7f0a54c7bb3decf91a8e31bbf1f8e33dd8164419b5" &
docker pull "gcr.io/bazel-public/debian12@sha256:c96553a0acd5728f59539b6690a1810ae29ddd617e10545cc74c5d8722e022d8" &
docker pull "gcr.io/bazel-public/debian13@sha256:0c63a0d5c11bd40533ff785cb8c25887d740d3acf214d653b14abe7f42ec07ae" &
docker pull "gcr.io/bazel-public/ubuntu2004-java11@sha256:51962774622467357982690143105843a387e6c99d465778c212eecdd047f039" &
docker pull "gcr.io/bazel-public/ubuntu2004@sha256:91b7cac1ac7ccc22a7c814a37bb7e6c4c1cb8ccf6080d6a9136afd19f31d9a85" &
docker pull "gcr.io/bazel-public/ubuntu2204-kythe@sha256:e07ee8ad9535c5fded161149a98a8792a94e9a01f4680765b86091e047d7d342" &
docker pull "gcr.io/bazel-public/ubuntu2404-kythe@sha256:7e8d1e235dc5da1ad9527e772dd54236a98c2a57157151913e250c303291941e" &
docker pull "gcr.io/bazel-public/ubuntu2204-java17@sha256:3d77f0f05da01ad35a1338c2d97675917d95d45fd4be8778918c776f984c4587" &
docker pull "gcr.io/bazel-public/ubuntu2204@sha256:7efa0b902d648787c9c7995d1dd26672ce5bf53cefb1e6103d12e36672229997" &
docker pull "gcr.io/bazel-public/ubuntu2404@sha256:5f75cef6a0dc845b1727f03ec4856fe17873e6ab3fd91648167df9cfcb612f6c" &
else
docker pull "gcr.io/bazel-public/rockylinux8@sha256:a3a3ab64e044487bb4f4de79c1d9c49fb0962c5df788ffb7214540af8e77ebc2" &
docker pull "gcr.io/bazel-public/rockylinux8-java11@sha256:05f605f1eb3b1aecb9713057a4436a901929fcb8217b464806bb7603d2d0be49" &
docker pull "gcr.io/bazel-public/rockylinux8-java11-devtoolset10@sha256:4a483fa96812c6d20c8d07a783cf59cd02d8364e1831f8fa64221019427c781a" &
docker pull "gcr.io/bazel-public/rockylinux8-releaser@sha256:2a61b4a50ea72e02764b54ac119b6dd64f96ae513401f71e1bbdff4470335306" &
docker pull "gcr.io/bazel-public/debian10-java11@sha256:2cafd5671879a2693dfbb2abb0f5b06e808ba2a22a17b990da6ae8a3e195ed25" &
docker pull "gcr.io/bazel-public/debian11-java17@sha256:42da27bb2aa6101b3e03728c0f18e20772e70f708e84d708e4f3a28ea40560d8" &
docker pull "gcr.io/bazel-public/debian12@sha256:fd2576f95180a0fabdddc6154a2dd4af18e8bfd9b6e55f288cd0a72ab2d5019a" &
docker pull "gcr.io/bazel-public/debian13@sha256:ae003c171d39bc7164a705c4d8229633f518aaa36fcacf55c8d863f64e16c9f6" &
docker pull "gcr.io/bazel-public/ubuntu1804-java11@sha256:25cbb00de12a00485b215440d4ef58469ba305afa54283e40f5b7a8b8ca13c0f" &
docker pull "gcr.io/bazel-public/ubuntu2004-java11@sha256:da0038022a8173e05549fcd003d0af97f7e89a1ee29c6f2e19db10ae7d8dc328" &
docker pull "gcr.io/bazel-public/ubuntu2004@sha256:ffa90e8bad9b3952a40a0f0a9a42e3438faf0002aaa3f20cba6dd2a258ec1a56" &
docker pull "gcr.io/bazel-public/ubuntu2004-kythe@sha256:3862183a7674064e7e36ab851166e5a20eaccb47c8dd7205354fc8de6a6be0e9" &
docker pull "gcr.io/bazel-public/ubuntu2204-kythe@sha256:e7bee6f5437c7a3a6622a508be5000bf7cd92897d5fcb46d64afb0746673f876" &
docker pull "gcr.io/bazel-public/ubuntu2404-kythe@sha256:186b7bd1b9b65d59423550fa85c197bffbc207f2bdee05df81b4e38bbec07a9d" &
docker pull "gcr.io/bazel-public/ubuntu2204-java17@sha256:75a622ed24a18d1af44aa1e6ada0a55efaf6dbffd682ad83afa6432bcedfd3b2" &
docker pull "gcr.io/bazel-public/ubuntu2204@sha256:a8fc286aaca55230a3c2dddd3a5311770e6f609fa31f53447866c3b3bfb74398" &
docker pull "gcr.io/bazel-public/ubuntu2404@sha256:4bc65868dcac6d63d5ecca1685c7a41e8549a5f38e041ff1f2edc8101cf213b8" &
docker pull "gcr.io/bazel-public/fedora43-java25@sha256:36378b3c3c1f5b5f621bbf3160dc088ab7240fb0506d83253ae40d704b992323" &
docker pull "gcr.io/bazel-public/fedora45-java25@sha256:014e13b0ece7446646a901afb6b69434b8a7e5945e74ff0a633aae00116d1a22" &
fi
wait
poweroff