-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbazel-container
executable file
·244 lines (206 loc) · 6.06 KB
/
bazel-container
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
# Copyright 2021 The Cross-Media Measurement Authors
#
# 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.
set -eEu -o pipefail
readonly HOST_BAZEL="${BAZEL:-bazel}"
readonly IMAGE="${IMAGE:-ghcr.io/world-federation-of-advertisers/bazel:0.2.0}"
readonly DOCKER="${DOCKER:-docker}"
readonly DOCKER_HOST="${DOCKER_HOST:-unix:///var/run/docker.sock}"
readonly CONTAINER_USER='builder'
readonly CONTAINER_HOME="/${CONTAINER_USER}"
readonly OUTPUT_BASE="${PWD}/bazel-container-output"
readonly OUTPUT_USER_ROOT="${CONTAINER_HOME}/.cache/bazel/_bazel_${CONTAINER_USER}"
readonly BAZELISK_HOME="${CONTAINER_HOME}/.cache/bazelisk"
readonly GCLOUD_SDK_CONFIG="${CONTAINER_HOME}/.config/gcloud"
command_exists() {
type "$1" >/dev/null 2>&1
}
check_bash_version() {
if [[ "${BASH_VERSINFO:-0}" -lt 5 ]]; then
echo "Bash version ${BASH_VERSION:-unknown} is too old" >&2
exit 1
fi
}
check_docker_is_podman() {
if [[ "$($DOCKER -v)" == *podman* ]]; then
echo 'Podman is not supported by TestContainers' >&2
exit 1
fi
}
get_docker_socket() {
if [[ "${DOCKER_HOST}" != 'unix://'* ]]; then
echo 'DOCKER_HOST is not a Unix socket' >&2
exit 1
fi
echo "${DOCKER_HOST##unix://}"
}
is_rootless() {
[[ "$($DOCKER info --format '{{.SecurityOptions}}')" == *rootless* ]] ||
[[ "$($DOCKER version --format '{{.Client.Os}}')" == 'darwin' ]]
}
hash_md5() {
if hash md5 2>/dev/null; then
md5 -s -q "$1"
return
fi
echo -n "$1" | md5sum | cut -d ' ' -f 1
}
get_output_volume() {
local wdir_hash
wdir_hash="$(hash_md5 "${PWD}")"
echo "bazel-output-${wdir_hash}"
}
# Outputs the host's Bazel output user root.
# See https://docs.bazel.build/versions/4.2.1/output_directories.html
get_host_output_user_root() {
local output_root
if [[ "${OSTYPE}" == 'darwin'* ]]; then
output_root='/private/var/tmp'
else
output_root="${HOME}/.cache/bazel"
fi
echo "${output_root}/_bazel_${USER}"
}
get_host_cache_dir() {
if [[ -v XDG_CACHE_HOME ]]; then
echo "${XDG_CACHE_HOME}"
elif [[ "${OSTYPE}" == 'darwin'* ]]; then
echo "${HOME}/Library/Caches"
else
echo "${HOME}/.cache"
fi
}
ensure_host_bazelisk_cache_dir() {
local host_cache_dir
host_cache_dir="$(get_host_cache_dir)"
readonly host_cache_dir
readonly bazelisk_cache_dir="${host_cache_dir}/bazelisk"
mkdir -p "${bazelisk_cache_dir}"
echo "${bazelisk_cache_dir}"
}
get_host_install_base() {
command_exists "${HOST_BAZEL}" || return
${HOST_BAZEL} info install_base
}
main() {
check_bash_version
check_docker_is_podman
local docker_socket
docker_socket="$(get_docker_socket)"
readonly docker_socket
local host_output_user_root
host_output_user_root="$(get_host_output_user_root)"
readonly host_output_user_root
mkdir -p "${host_output_user_root}"
local install_base
install_base="$(get_host_install_base)"
readonly install_base
local host_bazelisk_cache_dir
host_bazelisk_cache_dir="$(ensure_host_bazelisk_cache_dir)"
readonly host_bazelisk_cache_dir
mkdir -p "${OUTPUT_BASE}"
local -a docker_options=(
'-it'
'--rm'
'--network=host'
'--entrypoint=/usr/bin/bazel'
'--env'
"HOME=${CONTAINER_HOME}"
'--env'
"BAZELISK_HOME=${BAZELISK_HOME}"
'--mount'
"type=bind,source=${PWD},target=${PWD},readonly"
'--mount'
"type=bind,source=${host_output_user_root},target=${OUTPUT_USER_ROOT}"
'--mount'
"type=bind,source=${host_bazelisk_cache_dir},target=${BAZELISK_HOME}"
'--mount'
"type=bind,source=${docker_socket},target=/var/run/docker.sock"
"--workdir=${PWD}"
)
if hash gcloud; then
local gcloud_config
gcloud_config="$(gcloud info --format='value(config.paths.global_config_dir)')"
docker_options+=(
'--mount'
"type=bind,source=${gcloud_config},target=${GCLOUD_SDK_CONFIG}"
'--env'
"CLOUDSDK_CONFIG=${GCLOUD_SDK_CONFIG}"
)
fi
if [[ -d "${HOME}/.docker" ]]; then
docker_options+=(
'--mount'
"type=bind,source=${HOME}/.docker,target=${CONTAINER_HOME}/.docker,readonly"
)
fi
if [[ "${OSTYPE}" == 'linux'* ]]; then
docker_options+=(
'--mount'
"type=bind,source=${OUTPUT_BASE},target=${OUTPUT_BASE}"
)
else
# Mount OUTPUT_BASE as a named volume to avoid potential filesystem
# incompatibilities.
local output_volume
output_volume="$(get_output_volume)"
readonly output_volume
docker_options+=(
'--mount'
"type=volume,source=${output_volume},target=${OUTPUT_BASE}"
)
fi
if ! is_rootless; then
local -r owner="${EUID}:${GROUPS[0]}"
docker_options+=(
"--user=${owner}"
"--env=USER=${CONTAINER_USER}"
)
fi
local -a startup_options=(
"--output_user_root=${OUTPUT_USER_ROOT}"
"--output_base=${OUTPUT_BASE}"
)
if [[ -n "${install_base}" ]]; then
docker_options+=(
'--mount'
"type=bind,source=${install_base},target=${install_base}"
)
startup_options+=(
"--install_base=${install_base}"
)
fi
while [[ "$1" =~ [[:space:]]*-.* ]]; do
startup_options+=("$1")
shift 1
done
local command="$1"
shift 1
local container_id
container_id=$(
"${DOCKER}" create "${docker_options[@]}" \
"${IMAGE}" \
"${startup_options[@]}" \
"${command}" \
--config=container \
--experimental_convenience_symlinks=ignore \
"$@"
)
if [[ -e "$HOME/.bazelrc" ]]; then
"${DOCKER}" cp "$HOME/.bazelrc" "${container_id}:${CONTAINER_HOME}/"
fi
echo "Running in Bazel container: ${command} $*" >&2
exec "${DOCKER}" start --attach --interactive "${container_id}"
}
main "$@"