blob: a1449ce0d0e832fb094dc25cf40358f959dbd451 [file] [log] [blame]
Philipp Wollermann9884d5a2019-01-04 09:51:45 +01001#!/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 Wollermannb220c8a2019-08-28 16:01:26 +020017### Setup script for an Ubuntu 18.04 LTS based Docker host.
Philipp Wollermann9884d5a2019-01-04 09:51:45 +010018
19# Fail on errors.
20# Fail when using undefined variables.
21# Print all executed commands.
22# Fail when any command in a pipe fails.
23set -euxo pipefail
24
Philipp Wollermannb220c8a2019-08-28 16:01:26 +020025### Prevent dpkg / apt-get / debconf from trying to access stdin.
Philipp Wollermann9884d5a2019-01-04 09:51:45 +010026export DEBIAN_FRONTEND="noninteractive"
27
28### Install base packages.
29{
Philipp Wollermannb53d73f2019-08-03 10:28:12 +020030 apt-get -y update
31 apt-get -y dist-upgrade
Philipp Wollermannbf881522021-08-08 13:12:39 +020032 apt-get -y install python-is-python3 openjdk-11-jdk-headless unzip
Philipp Wollermann0e051dd2019-05-16 11:37:52 +020033}
34
Philipp Wollermanndff36b82019-05-28 15:50:13 +020035### Disable automatic upgrades, as they can interfere with our startup scripts.
36{
37 cat > /etc/apt/apt.conf.d/10periodic <<'EOF'
38APT::Periodic::Enable "0";
39EOF
40}
41
Philipp Wollermann9884d5a2019-01-04 09:51:45 +010042### Increase file descriptor limits
43{
Philipp Wollermann83a95012019-05-16 11:41:55 +020044 cat >> /etc/security/limits.conf <<'EOF'
Philipp Wollermann9884d5a2019-01-04 09:51:45 +010045* soft nofile 100000
46* hard nofile 100000
47EOF
48}
49
Philipp Wollermann25e88c42020-02-03 16:43:04 +010050### Patch the filesystem options to increase I/O performance
51{
Philipp Wollermannd5f7be72020-02-17 17:59:28 +010052 tune2fs -o ^acl,journal_data_writeback,nobarrier /dev/sda1
53 cat > /etc/fstab <<'EOF'
Philipp Wollermann9f99a092021-07-01 18:02:28 +020054LABEL=cloudimg-rootfs / ext4 defaults,noatime,commit=300,journal_async_commit 0 0
55LABEL=UEFI /boot/efi vfat defaults,noatime 0 0
Philipp Wollermannd5f7be72020-02-17 17:59:28 +010056EOF
Philipp Wollermann25e88c42020-02-03 16:43:04 +010057}
58
Philipp Wollermann9884d5a2019-01-04 09:51:45 +010059### Install the Buildkite Agent on production images.
60{
61 apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 \
Philipp Wollermann1403d2c2019-01-10 13:15:51 +010062 --recv-keys 32A37959C2FA5C3C99EFBC32A79206696452D198
Philipp Wollermann9884d5a2019-01-04 09:51:45 +010063 add-apt-repository -y "deb https://apt.buildkite.com/buildkite-agent stable main"
Philipp Wollermannb53d73f2019-08-03 10:28:12 +020064 apt-get -y update
65 apt-get -y install buildkite-agent
Philipp Wollermann9884d5a2019-01-04 09:51:45 +010066
Philipp Wollermann1403d2c2019-01-10 13:15:51 +010067 # Disable the Buildkite agent service, as the startup script has to mount /var/lib/buildkite-agent
68 # first.
Philipp Wollermann9884d5a2019-01-04 09:51:45 +010069 systemctl disable buildkite-agent
Philipp Wollermann501c4952020-02-17 17:57:19 +010070
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).
75Restart=no
76PermissionsStartOnly=true
77ExecStopPost=/bin/systemctl poweroff
78EOF
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.
84TasksAccounting=no
85EOF
86
87 cat > /etc/systemd/system/buildkite-agent.service.d/10-environment.conf <<'EOF'
88[Service]
89# Setup some environment variables that we need.
90Environment=ANDROID_HOME=/opt/android-sdk-linux
91Environment=ANDROID_NDK_HOME=/opt/android-ndk-r15c
92Environment=CLOUDSDK_PYTHON=/usr/bin/python
93Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
94EOF
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 Wollermann9884d5a2019-01-04 09:51:45 +0100100}
101
102### Install Docker.
103{
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200104 apt-get -y install apt-transport-https ca-certificates
Philipp Wollermann9884d5a2019-01-04 09:51:45 +0100105
Philipp Wollermann9f99a092021-07-01 18:02:28 +0200106 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 Wollermann9884d5a2019-01-04 09:51:45 +0100110
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200111 apt-get -y update
Philipp Wollermann9f99a092021-07-01 18:02:28 +0200112 apt-get -y install docker-ce docker-ce-cli containerd.io
Philipp Wollermann9884d5a2019-01-04 09:51:45 +0100113
Philipp Wollermann338db4a2019-05-18 11:21:04 +0200114 # 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]
119SocketMode=0666
120EOF
Philipp Wollermann9884d5a2019-01-04 09:51:45 +0100121
Philipp Wollermann338db4a2019-05-18 11:21:04 +0200122 # Disable the Docker service, as the startup script has to mount /var/lib/docker first.
Philipp Wollermann9884d5a2019-01-04 09:51:45 +0100123 systemctl disable docker
Philipp Wollermannec0c8982019-05-19 21:32:01 +0200124 systemctl stop docker
Philipp Wollermann9884d5a2019-01-04 09:51:45 +0100125}
126
Philipp Wollermannb220c8a2019-08-28 16:01:26 +0200127## Add our minimum uptime enforcer.
128{
129 cat > /etc/systemd/system/minimum-uptime.service <<'EOF'
130[Unit]
131Description=Ensures that the VM is running for at least one minute.
132
133[Service]
134Type=simple
135ExecStart=/usr/bin/nohup sleep 60
136TimeoutSec=60
137KillSignal=SIGHUP
138
139[Install]
140WantedBy=multi-user.target
141EOF
142 systemctl enable minimum-uptime.service
143}
144
Philipp Wollermann0e75ec32019-07-15 15:14:43 +0200145### Get rid of Ubuntu's snapd stuff and install the Google Cloud SDK the traditional way.
146{
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200147 apt-get -y remove --purge snapd
Philipp Wollermann0e75ec32019-07-15 15:14:43 +0200148 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 Wollermannb53d73f2019-08-03 10:28:12 +0200150 apt-get -y install apt-transport-https ca-certificates
151 curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | \
Philipp Wollermann0e75ec32019-07-15 15:14:43 +0200152 apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200153 apt-get -y update
154 apt-get -y install google-cloud-sdk
Philipp Wollermann0e75ec32019-07-15 15:14:43 +0200155}
156
Philipp Wollermannb220c8a2019-08-28 16:01:26 +0200157### Preseed our Git mirrors.
Philipp Wollermann9a67e0a2019-05-16 11:39:11 +0200158{
Philipp Wollermann338db4a2019-05-18 11:21:04 +0200159 mkdir -p /var/lib/gitmirrors
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200160 curl -fsSL https://storage.googleapis.com/bazel-git-mirror/bazelbuild-mirror.tar | \
161 tar x -C /var/lib/gitmirrors --strip=1
Philipp Wollermann338db4a2019-05-18 11:21:04 +0200162 chown -R buildkite-agent:buildkite-agent /var/lib/gitmirrors
163 chmod -R 0755 /var/lib/gitmirrors
Philipp Wollermann9a67e0a2019-05-16 11:39:11 +0200164}
165
Philipp Wollermannb220c8a2019-08-28 16:01:26 +0200166### Install Android NDK.
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200167{
168 cd /opt
Yun Pengd5f98442022-09-13 12:13:16 +0200169 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 Wollermannb53d73f2019-08-03 10:28:12 +0200175}
176
Philipp Wollermannb220c8a2019-08-28 16:01:26 +0200177### Install Android SDK.
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200178{
Philipp Wollermanne1018bf2021-07-01 23:30:50 +0200179 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 Wollermannb53d73f2019-08-03 10:28:12 +0200182 unzip android-sdk.zip > /dev/null
183 rm android-sdk.zip
Philipp Wollermanne1018bf2021-07-01 23:30:50 +0200184 mv cmdline-tools latest
185 yes | latest/bin/sdkmanager --licenses > /dev/null || true
Philipp Wollermann6011b4f2021-07-02 08:06:00 +0200186 latest/bin/sdkmanager --update
187 latest/bin/sdkmanager \
Philipp Wollermannd635b752020-02-03 11:46:33 +0100188 "build-tools;28.0.2" \
Philipp Wollermann6011b4f2021-07-02 08:06:00 +0200189 "build-tools;30.0.3" \
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200190 "extras;android;m2repository" \
191 "platform-tools" \
192 "platforms;android-24" \
193 "platforms;android-28" \
Philipp Wollermann5b81bb92020-08-21 19:40:56 +0200194 "platforms;android-29" \
Ben Lee24c07622022-05-02 15:51:36 -0700195 "platforms;android-30" \
Yun Pengc6bfff52023-09-29 10:05:47 +0200196 "platforms;android-31" \
Yun Peng9817b292024-01-31 17:14:25 +0100197 "platforms;android-32" > /dev/null
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200198}
199
Philipp Wollermannb220c8a2019-08-28 16:01:26 +0200200### Fix permissions in /opt.
Philipp Wollermannb53d73f2019-08-03 10:28:12 +0200201{
202 chown -R root:root /opt
203}
204
Philipp Wollermann9884d5a2019-01-04 09:51:45 +0100205### 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 Pengf3c70e82024-02-21 14:55:07 +0100212### Configure and start Docker.
213systemctl start docker
214
215### Ensure that Docker images can be downloaded from GCR.
216gcloud auth configure-docker --quiet
217
218### Pull the Docker images that we need in production.
Yun Pengf3c70e82024-02-21 14:55:07 +0100219docker pull "gcr.io/bazel-public/centos7-java11-devtoolset10" &
220docker pull "gcr.io/bazel-public/centos7-releaser" &
221docker pull "gcr.io/bazel-public/debian10-java11" &
222docker pull "gcr.io/bazel-public/debian11-java17" &
Yun Pengf3c70e82024-02-21 14:55:07 +0100223docker pull "gcr.io/bazel-public/ubuntu1804-java11" &
Chi Wang626e41c2024-03-06 13:05:47 +0100224docker pull "gcr.io/bazel-public/ubuntu2004" &
Yun Pengf3c70e82024-02-21 14:55:07 +0100225docker pull "gcr.io/bazel-public/ubuntu2004-java11-kythe" &
Chi Wang626e41c2024-03-06 13:05:47 +0100226docker pull "gcr.io/bazel-public/ubuntu2204" &
Yun Pengf3c70e82024-02-21 14:55:07 +0100227docker pull "gcr.io/bazel-public/ubuntu2204-java17" &
Yun Peng3bc08612024-10-15 16:56:00 +0200228docker pull "gcr.io/bazel-public/ubuntu2404" &
229docker pull "gcr.io/bazel-public/ubuntu2404-kythe" &
Yun Pengf3c70e82024-02-21 14:55:07 +0100230docker pull "gcr.io/bazel-public/fedora39-java17" &
Yun Peng3bc08612024-10-15 16:56:00 +0200231docker pull "gcr.io/bazel-public/fedora40-java21" &
Yun Pengf3c70e82024-02-21 14:55:07 +0100232wait
233
Philipp Wollermann9884d5a2019-01-04 09:51:45 +0100234poweroff