github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/images/bootstrap/create_bazel_cache_rcs.sh (about) 1 #!/bin/bash 2 # Copyright 2018 The Kubernetes Authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 CACHE_HOST="bazel-cache.default.svc.cluster.local." 17 CACHE_PORT="8080" 18 19 # get the installed version of a debian package 20 package_to_version () { 21 dpkg-query --showformat='${Version}' --show "$1" 22 } 23 24 # look up a binary with `command -v $1` and return the debian package it belongs to 25 command_to_package () { 26 # NOTE: we resolve symlinks first because debian packages can provide alternatives 27 # by `update-alternatives` in postinit scripts, which updates a common 28 # symlink for a provided file to the backing entry. 29 # https://wiki.debian.org/DebianAlternatives 30 local binary_path 31 binary_path=$(readlink -f "$(command -v "$1")") 32 # `dpkg-query --search $file-pattern` outputs lines with the format: "$package: $file-path" 33 # where $file-path belongs to $package 34 # https://manpages.debian.org/jessie/dpkg/dpkg-query.1.en.html 35 dpkg-query --search "${binary_path}" | cut -d':' -f1 36 } 37 38 # get the installed package version relating to a binary 39 command_to_version () { 40 local package 41 package=$(command_to_package "$1") 42 package_to_version "${package}" 43 } 44 45 hash_toolchains () { 46 # if $CC is set bazel will use this to detect c/c++ toolchains, otherwise gcc 47 # https://blog.bazel.build/2016/03/31/autoconfiguration.html 48 local cc="${CC:-gcc}" 49 local cc_version 50 cc_version=$(command_to_version "$cc") 51 # NOTE: IIRC some rules call python internally, this can't hurt 52 local python_version 53 python_version=$(command_to_version python) 54 # the rpm packaging rules use rpmbuild 55 local rpmbuild_version 56 rpmbuild_version=$(command_to_version rpmbuild) 57 # combine all tool versions into a hash 58 # NOTE(bentheelder): if we change the set of tools considered we should 59 # consider prepending the hash with a """schema version""" for completeness 60 local tool_versions 61 tool_versions="CC:${cc_version},PY:${python_version},RPM:${rpmbuild_version}" 62 echo "${tool_versions}" | md5sum | cut -d" " -f1 63 } 64 65 get_workspace () { 66 # get org/repo from prow, otherwise use $PWD 67 if [[ -n "${REPO_NAME}" ]] && [[ -n "${REPO_OWNER}" ]]; then 68 echo "${REPO_OWNER}/${REPO_NAME}" 69 else 70 echo "$(basename "$(dirname "$PWD")")/$(basename "$PWD")" 71 fi 72 } 73 74 make_bazel_rc () { 75 # this is the default for recent releases but we set it explicitly 76 # since this is the only hash our cache supports 77 echo "startup --host_jvm_args=-Dbazel.DigestFunction=sha256" 78 # use remote caching for all the things 79 echo "build --experimental_remote_spawn_cache" 80 # don't fail if the cache is unavailable 81 echo "build --remote_local_fallback" 82 # point bazel at our http cache ... 83 # NOTE our caches are versioned by all path segments up until the last two 84 # IE PUT /foo/bar/baz/cas/asdf -> is in cache "/foo/bar/baz" 85 local cache_id 86 cache_id="$(get_workspace),$(hash_toolchains)" 87 local cache_url 88 cache_url="http://${CACHE_HOST}:${CACHE_PORT}/${cache_id}" 89 echo "build --remote_http_cache=${cache_url}" 90 # specifically for bazel 0.15.0 we want to set this flag 91 # our docker image now sets BAZEL_VERSION with the bazel version as installed 92 # https://github.com/bazelbuild/bazel/issues/5047#issuecomment-401295174 93 if [[ "${BAZEL_VERSION:-}" = "0.15.0" ]]; then 94 echo "build --remote_max_connections=200" 95 fi 96 } 97 98 # https://docs.bazel.build/versions/master/user-manual.html#bazelrc 99 # bazel will look for two RC files, taking the first option in each set of paths 100 # firstly: 101 # - The path specified by the --bazelrc=file startup option. If specified, this option must appear before the command name (e.g. build) 102 # - A file named .bazelrc in your base workspace directory 103 # - A file named .bazelrc in your home directory 104 bazel_rc_contents=$(make_bazel_rc) 105 echo "create_bazel_cache_rcs.sh: Configuring '${HOME}/.bazelrc' and '/etc/bazel.bazelrc' with" 106 echo "# ------------------------------------------------------------------------------" 107 echo "${bazel_rc_contents}" 108 echo "# ------------------------------------------------------------------------------" 109 echo "${bazel_rc_contents}" >> "${HOME}/.bazelrc" 110 # Aside from the optional configuration file described above, Bazel also looks for a master rc file next to the binary, in the workspace at tools/bazel.rc or system-wide at /etc/bazel.bazelrc. 111 # These files are here to support installation-wide options or options shared between users. Reading of this file can be disabled using the --nomaster_bazelrc option. 112 echo "${bazel_rc_contents}" >> "/etc/bazel.bazelrc" 113 # hopefully no repos create *both* of these ...