Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2018 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Philipp Wollermann | b220c8a | 2019-08-28 16:01:26 +0200 | [diff] [blame] | 17 | ### Setup script for an Ubuntu 18.04 LTS based Docker host. |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 18 | |
| 19 | # Fail on errors. |
| 20 | # Fail when using undefined variables. |
| 21 | # Print all executed commands. |
| 22 | # Fail when any command in a pipe fails. |
| 23 | set -euxo pipefail |
| 24 | |
Philipp Wollermann | b220c8a | 2019-08-28 16:01:26 +0200 | [diff] [blame] | 25 | ### Prevent dpkg / apt-get / debconf from trying to access stdin. |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 26 | export DEBIAN_FRONTEND="noninteractive" |
| 27 | |
| 28 | ### Install base packages. |
| 29 | { |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 30 | apt-get -y update |
| 31 | apt-get -y dist-upgrade |
Philipp Wollermann | bf88152 | 2021-08-08 13:12:39 +0200 | [diff] [blame] | 32 | apt-get -y install python-is-python3 openjdk-11-jdk-headless unzip |
Philipp Wollermann | 0e051dd | 2019-05-16 11:37:52 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Philipp Wollermann | dff36b8 | 2019-05-28 15:50:13 +0200 | [diff] [blame] | 35 | ### Disable automatic upgrades, as they can interfere with our startup scripts. |
| 36 | { |
| 37 | cat > /etc/apt/apt.conf.d/10periodic <<'EOF' |
| 38 | APT::Periodic::Enable "0"; |
| 39 | EOF |
| 40 | } |
| 41 | |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 42 | ### Increase file descriptor limits |
| 43 | { |
Philipp Wollermann | 83a9501 | 2019-05-16 11:41:55 +0200 | [diff] [blame] | 44 | cat >> /etc/security/limits.conf <<'EOF' |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 45 | * soft nofile 100000 |
| 46 | * hard nofile 100000 |
| 47 | EOF |
| 48 | } |
| 49 | |
Philipp Wollermann | 25e88c4 | 2020-02-03 16:43:04 +0100 | [diff] [blame] | 50 | ### Patch the filesystem options to increase I/O performance |
| 51 | { |
Philipp Wollermann | d5f7be7 | 2020-02-17 17:59:28 +0100 | [diff] [blame] | 52 | tune2fs -o ^acl,journal_data_writeback,nobarrier /dev/sda1 |
| 53 | cat > /etc/fstab <<'EOF' |
Philipp Wollermann | 9f99a09 | 2021-07-01 18:02:28 +0200 | [diff] [blame] | 54 | LABEL=cloudimg-rootfs / ext4 defaults,noatime,commit=300,journal_async_commit 0 0 |
| 55 | LABEL=UEFI /boot/efi vfat defaults,noatime 0 0 |
Philipp Wollermann | d5f7be7 | 2020-02-17 17:59:28 +0100 | [diff] [blame] | 56 | EOF |
Philipp Wollermann | 25e88c4 | 2020-02-03 16:43:04 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 59 | ### Install the Buildkite Agent on production images. |
| 60 | { |
| 61 | apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 \ |
Philipp Wollermann | 1403d2c | 2019-01-10 13:15:51 +0100 | [diff] [blame] | 62 | --recv-keys 32A37959C2FA5C3C99EFBC32A79206696452D198 |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 63 | add-apt-repository -y "deb https://apt.buildkite.com/buildkite-agent stable main" |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 64 | apt-get -y update |
| 65 | apt-get -y install buildkite-agent |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 66 | |
Philipp Wollermann | 1403d2c | 2019-01-10 13:15:51 +0100 | [diff] [blame] | 67 | # Disable the Buildkite agent service, as the startup script has to mount /var/lib/buildkite-agent |
| 68 | # first. |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 69 | systemctl disable buildkite-agent |
Philipp Wollermann | 501c495 | 2020-02-17 17:57:19 +0100 | [diff] [blame] | 70 | |
| 71 | mkdir -p /etc/systemd/system/buildkite-agent.service.d |
| 72 | cat > /etc/systemd/system/buildkite-agent.service.d/10-oneshot-agent.conf <<'EOF' |
| 73 | [Service] |
| 74 | # Only run one job, then shutdown the machine (so that the instance group replaces it with a fresh one). |
| 75 | Restart=no |
| 76 | PermissionsStartOnly=true |
| 77 | ExecStopPost=/bin/systemctl poweroff |
| 78 | EOF |
| 79 | |
| 80 | cat > /etc/systemd/system/buildkite-agent.service.d/10-disable-tasks-accounting.conf <<'EOF' |
| 81 | [Service] |
| 82 | # Disable tasks accounting, because Bazel is prone to run into resource limits there. |
| 83 | # This fixes the "cgroup: fork rejected by pids controller" error that some CI jobs triggered. |
| 84 | TasksAccounting=no |
| 85 | EOF |
| 86 | |
| 87 | cat > /etc/systemd/system/buildkite-agent.service.d/10-environment.conf <<'EOF' |
| 88 | [Service] |
| 89 | # Setup some environment variables that we need. |
| 90 | Environment=ANDROID_HOME=/opt/android-sdk-linux |
| 91 | Environment=ANDROID_NDK_HOME=/opt/android-ndk-r15c |
| 92 | Environment=CLOUDSDK_PYTHON=/usr/bin/python |
| 93 | Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
| 94 | EOF |
| 95 | } |
| 96 | |
| 97 | ### Let 'localhost' resolve to '::1', otherwise one of Envoy's tests fails. |
| 98 | { |
| 99 | sed -i 's/^::1 .*/::1 localhost ip6-localhost ip6-loopback/' /etc/hosts |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | ### Install Docker. |
| 103 | { |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 104 | apt-get -y install apt-transport-https ca-certificates |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 105 | |
Philipp Wollermann | 9f99a09 | 2021-07-01 18:02:28 +0200 | [diff] [blame] | 106 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg |
| 107 | echo \ |
| 108 | "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ |
| 109 | $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 110 | |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 111 | apt-get -y update |
Philipp Wollermann | 9f99a09 | 2021-07-01 18:02:28 +0200 | [diff] [blame] | 112 | apt-get -y install docker-ce docker-ce-cli containerd.io |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 113 | |
Philipp Wollermann | 338db4a | 2019-05-18 11:21:04 +0200 | [diff] [blame] | 114 | # Allow everyone access to the Docker socket. Usually this would be insane from a security point |
| 115 | # of view, but these are untrusted throw-away machines anyway, so the risk is acceptable. |
| 116 | mkdir /etc/systemd/system/docker.socket.d |
| 117 | cat > /etc/systemd/system/docker.socket.d/override.conf <<'EOF' |
| 118 | [Socket] |
| 119 | SocketMode=0666 |
| 120 | EOF |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 121 | |
Philipp Wollermann | 338db4a | 2019-05-18 11:21:04 +0200 | [diff] [blame] | 122 | # Disable the Docker service, as the startup script has to mount /var/lib/docker first. |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 123 | systemctl disable docker |
Philipp Wollermann | ec0c898 | 2019-05-19 21:32:01 +0200 | [diff] [blame] | 124 | systemctl stop docker |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Philipp Wollermann | b220c8a | 2019-08-28 16:01:26 +0200 | [diff] [blame] | 127 | ## Add our minimum uptime enforcer. |
| 128 | { |
| 129 | cat > /etc/systemd/system/minimum-uptime.service <<'EOF' |
| 130 | [Unit] |
| 131 | Description=Ensures that the VM is running for at least one minute. |
| 132 | |
| 133 | [Service] |
| 134 | Type=simple |
| 135 | ExecStart=/usr/bin/nohup sleep 60 |
| 136 | TimeoutSec=60 |
| 137 | KillSignal=SIGHUP |
| 138 | |
| 139 | [Install] |
| 140 | WantedBy=multi-user.target |
| 141 | EOF |
| 142 | systemctl enable minimum-uptime.service |
| 143 | } |
| 144 | |
Philipp Wollermann | 0e75ec3 | 2019-07-15 15:14:43 +0200 | [diff] [blame] | 145 | ### Get rid of Ubuntu's snapd stuff and install the Google Cloud SDK the traditional way. |
| 146 | { |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 147 | apt-get -y remove --purge snapd |
Philipp Wollermann | 0e75ec3 | 2019-07-15 15:14:43 +0200 | [diff] [blame] | 148 | echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | \ |
| 149 | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 150 | apt-get -y install apt-transport-https ca-certificates |
| 151 | curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | \ |
Philipp Wollermann | 0e75ec3 | 2019-07-15 15:14:43 +0200 | [diff] [blame] | 152 | apt-key --keyring /usr/share/keyrings/cloud.google.gpg add - |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 153 | apt-get -y update |
| 154 | apt-get -y install google-cloud-sdk |
Philipp Wollermann | 0e75ec3 | 2019-07-15 15:14:43 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Philipp Wollermann | b220c8a | 2019-08-28 16:01:26 +0200 | [diff] [blame] | 157 | ### Preseed our Git mirrors. |
Philipp Wollermann | 9a67e0a | 2019-05-16 11:39:11 +0200 | [diff] [blame] | 158 | { |
Philipp Wollermann | 338db4a | 2019-05-18 11:21:04 +0200 | [diff] [blame] | 159 | mkdir -p /var/lib/gitmirrors |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 160 | curl -fsSL https://storage.googleapis.com/bazel-git-mirror/bazelbuild-mirror.tar | \ |
| 161 | tar x -C /var/lib/gitmirrors --strip=1 |
Philipp Wollermann | 338db4a | 2019-05-18 11:21:04 +0200 | [diff] [blame] | 162 | chown -R buildkite-agent:buildkite-agent /var/lib/gitmirrors |
| 163 | chmod -R 0755 /var/lib/gitmirrors |
Philipp Wollermann | 9a67e0a | 2019-05-16 11:39:11 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Philipp Wollermann | b220c8a | 2019-08-28 16:01:26 +0200 | [diff] [blame] | 166 | ### Install Android NDK. |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 167 | { |
| 168 | cd /opt |
Yun Peng | d5f9844 | 2022-09-13 12:13:16 +0200 | [diff] [blame] | 169 | curl -fsSL -o android-ndk-r15c.zip https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip |
| 170 | unzip android-ndk-r15c.zip > /dev/null |
| 171 | rm android-ndk-r15c.zip |
| 172 | curl -fsSL -o android-ndk-r25b.zip https://dl.google.com/android/repository/android-ndk-r25b-linux.zip |
| 173 | unzip android-ndk-r25b.zip > /dev/null |
| 174 | rm android-ndk-r25b.zip |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Philipp Wollermann | b220c8a | 2019-08-28 16:01:26 +0200 | [diff] [blame] | 177 | ### Install Android SDK. |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 178 | { |
Philipp Wollermann | e1018bf | 2021-07-01 23:30:50 +0200 | [diff] [blame] | 179 | mkdir -p /opt/android-sdk-linux/cmdline-tools |
| 180 | cd /opt/android-sdk-linux/cmdline-tools |
| 181 | curl -fsSL -o android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-7302050_latest.zip |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 182 | unzip android-sdk.zip > /dev/null |
| 183 | rm android-sdk.zip |
Philipp Wollermann | e1018bf | 2021-07-01 23:30:50 +0200 | [diff] [blame] | 184 | mv cmdline-tools latest |
| 185 | yes | latest/bin/sdkmanager --licenses > /dev/null || true |
Philipp Wollermann | 6011b4f | 2021-07-02 08:06:00 +0200 | [diff] [blame] | 186 | latest/bin/sdkmanager --update |
| 187 | latest/bin/sdkmanager \ |
Philipp Wollermann | d635b75 | 2020-02-03 11:46:33 +0100 | [diff] [blame] | 188 | "build-tools;28.0.2" \ |
Philipp Wollermann | 6011b4f | 2021-07-02 08:06:00 +0200 | [diff] [blame] | 189 | "build-tools;30.0.3" \ |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 190 | "extras;android;m2repository" \ |
| 191 | "platform-tools" \ |
| 192 | "platforms;android-24" \ |
| 193 | "platforms;android-28" \ |
Philipp Wollermann | 5b81bb9 | 2020-08-21 19:40:56 +0200 | [diff] [blame] | 194 | "platforms;android-29" \ |
Ben Lee | 24c0762 | 2022-05-02 15:51:36 -0700 | [diff] [blame] | 195 | "platforms;android-30" \ |
Yun Peng | c6bfff5 | 2023-09-29 10:05:47 +0200 | [diff] [blame] | 196 | "platforms;android-31" \ |
Yun Peng | 9817b29 | 2024-01-31 17:14:25 +0100 | [diff] [blame] | 197 | "platforms;android-32" > /dev/null |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Philipp Wollermann | b220c8a | 2019-08-28 16:01:26 +0200 | [diff] [blame] | 200 | ### Fix permissions in /opt. |
Philipp Wollermann | b53d73f | 2019-08-03 10:28:12 +0200 | [diff] [blame] | 201 | { |
| 202 | chown -R root:root /opt |
| 203 | } |
| 204 | |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 205 | ### Clean up and trim the filesystem (potentially reduces the final image size). |
| 206 | { |
| 207 | rm -rf /var/lib/apt/lists/* |
| 208 | fstrim -v / |
| 209 | sleep 3 |
| 210 | } |
| 211 | |
Yun Peng | f3c70e8 | 2024-02-21 14:55:07 +0100 | [diff] [blame] | 212 | ### Configure and start Docker. |
| 213 | systemctl start docker |
| 214 | |
| 215 | ### Ensure that Docker images can be downloaded from GCR. |
| 216 | gcloud auth configure-docker --quiet |
| 217 | |
| 218 | ### Pull the Docker images that we need in production. |
Yun Peng | f3c70e8 | 2024-02-21 14:55:07 +0100 | [diff] [blame] | 219 | docker pull "gcr.io/bazel-public/centos7-java11-devtoolset10" & |
| 220 | docker pull "gcr.io/bazel-public/centos7-releaser" & |
| 221 | docker pull "gcr.io/bazel-public/debian10-java11" & |
| 222 | docker pull "gcr.io/bazel-public/debian11-java17" & |
Yun Peng | f3c70e8 | 2024-02-21 14:55:07 +0100 | [diff] [blame] | 223 | docker pull "gcr.io/bazel-public/ubuntu1804-java11" & |
Chi Wang | 626e41c | 2024-03-06 13:05:47 +0100 | [diff] [blame] | 224 | docker pull "gcr.io/bazel-public/ubuntu2004" & |
Yun Peng | f3c70e8 | 2024-02-21 14:55:07 +0100 | [diff] [blame] | 225 | docker pull "gcr.io/bazel-public/ubuntu2004-java11-kythe" & |
Chi Wang | 626e41c | 2024-03-06 13:05:47 +0100 | [diff] [blame] | 226 | docker pull "gcr.io/bazel-public/ubuntu2204" & |
Yun Peng | f3c70e8 | 2024-02-21 14:55:07 +0100 | [diff] [blame] | 227 | docker pull "gcr.io/bazel-public/ubuntu2204-java17" & |
Yun Peng | 3bc0861 | 2024-10-15 16:56:00 +0200 | [diff] [blame] | 228 | docker pull "gcr.io/bazel-public/ubuntu2404" & |
| 229 | docker pull "gcr.io/bazel-public/ubuntu2404-kythe" & |
Yun Peng | f3c70e8 | 2024-02-21 14:55:07 +0100 | [diff] [blame] | 230 | docker pull "gcr.io/bazel-public/fedora39-java17" & |
Yun Peng | 3bc0861 | 2024-10-15 16:56:00 +0200 | [diff] [blame] | 231 | docker pull "gcr.io/bazel-public/fedora40-java21" & |
Yun Peng | f3c70e8 | 2024-02-21 14:55:07 +0100 | [diff] [blame] | 232 | wait |
| 233 | |
Philipp Wollermann | 9884d5a | 2019-01-04 09:51:45 +0100 | [diff] [blame] | 234 | poweroff |