2023-04-13 05:43:38 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
function setup {
|
2023-08-21 02:01:33 -04:00
|
|
|
(stopBazelServer && sleep 1) || true
|
|
|
|
|
2023-04-13 05:43:38 -04:00
|
|
|
# Ensure that the cache directories exist, so they are not created by docker with root permissions.
|
|
|
|
mkdir -p "${HOME}/.cache/bazel"
|
|
|
|
mkdir -p "${HOME}/.cache/shared_bazel_repository_cache"
|
|
|
|
mkdir -p "${HOME}/.cache/shared_bazel_action_cache"
|
2023-07-24 05:14:06 -04:00
|
|
|
|
|
|
|
if [[ ! -f "${HOME}/.docker/config.json" ]]; then
|
|
|
|
echo "ERROR: ${HOME}/.docker/config.json does not exist."
|
|
|
|
echo "Please login into your container registry to create it."
|
|
|
|
echo "echo <TOKEN> | docker login ghcr.io -u <USERNAME> --password-stdin"
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-04-13 05:43:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function startBazelServer {
|
2023-08-10 04:52:15 -04:00
|
|
|
local containerImage="ghcr.io/edgelesssys/bazel-container:v6.3.2"
|
2023-04-13 05:43:38 -04:00
|
|
|
local containerName="bazeld"
|
|
|
|
|
|
|
|
setup
|
|
|
|
|
2023-08-21 02:01:33 -04:00
|
|
|
# In-container .bazelrc overwrite.
|
|
|
|
mkdir -p "/tmp/bazel-container"
|
|
|
|
cat << EOF > "/tmp/bazel-container/.bazelrc"
|
|
|
|
startup --output_user_root=/home/${USER}/.cache/bazel/_bazel_${USER}
|
|
|
|
EOF
|
|
|
|
|
2023-04-13 05:43:38 -04:00
|
|
|
local hostWorkspaceDir
|
|
|
|
hostWorkspaceDir="$(git rev-parse --show-toplevel)"
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo Could not find git repository root. Are you in a git repository?
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo Starting bazel container as daemon...
|
|
|
|
echo You can stop this command using:
|
|
|
|
echo docker kill "${containerName}"
|
|
|
|
|
|
|
|
docker run \
|
|
|
|
--rm \
|
|
|
|
--detach \
|
|
|
|
--name "${containerName}" \
|
|
|
|
-v "${hostWorkspaceDir}":/workspace \
|
2023-08-21 02:01:33 -04:00
|
|
|
-v "${HOME}/.cache/bazel":"${HOME}/.cache/bazel" \
|
|
|
|
-v "${HOME}/.cache/shared_bazel_repository_cache":"${HOME}/.cache/shared_bazel_repository_cache" \
|
|
|
|
-v "${HOME}/.cache/shared_bazel_action_cache":"${HOME}/.cache/shared_bazel_action_cache" \
|
2023-07-07 02:25:43 -04:00
|
|
|
-v "${HOME}/.docker/config.json":"/home/builder/.docker/config.json" \
|
2023-08-21 02:01:33 -04:00
|
|
|
-v "/tmp/bazel-container/.bazelrc":"/etc/bazel.bazelrc" \
|
2023-04-13 05:43:38 -04:00
|
|
|
--entrypoint=/bin/sleep \
|
|
|
|
"${containerImage}" \
|
|
|
|
infinity || return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopBazelServer {
|
|
|
|
local containerName="bazeld"
|
|
|
|
|
|
|
|
echo Stopping bazel container...
|
|
|
|
|
|
|
|
docker kill "${containerName}" || return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
function bazel {
|
|
|
|
local containerName="bazeld"
|
|
|
|
|
|
|
|
local hostWorkspaceDir
|
|
|
|
hostWorkspaceDir="$(git rev-parse --show-toplevel)"
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo Could not find git repository root. Are you in a git repository?
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local containerWorkDir
|
|
|
|
containerWorkDir=$(realpath -m "/workspace/$(realpath --relative-base="${hostWorkspaceDir}" .)")
|
|
|
|
if [[ $? -ne 0 ]]; then
|
|
|
|
echo Could not determine container work directory.
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
docker exec \
|
|
|
|
-it \
|
|
|
|
--workdir "${containerWorkDir}" \
|
|
|
|
--env "HOST_CACHE=${HOME}/.cache" \
|
|
|
|
"${containerName}" \
|
|
|
|
bazel "$@" || return $?
|
|
|
|
}
|