istio.io/istio@v0.0.0-20240520182934-d79c90f27776/common/scripts/setup_env.sh (about) 1 #!/bin/bash 2 # shellcheck disable=SC2034 3 4 # WARNING: DO NOT EDIT, THIS FILE IS PROBABLY A COPY 5 # 6 # The original version of this file is located in the https://github.com/istio/common-files repo. 7 # If you're looking at this file in a different repo and want to make a change, please go to the 8 # common-files repo, make the change there and check it in. Then come back to this repo and run 9 # "make update-common". 10 11 # Copyright Istio Authors 12 # 13 # Licensed under the Apache License, Version 2.0 (the "License"); 14 # you may not use this file except in compliance with the License. 15 # You may obtain a copy of the License at 16 # 17 # http://www.apache.org/licenses/LICENSE-2.0 18 # 19 # Unless required by applicable law or agreed to in writing, software 20 # distributed under the License is distributed on an "AS IS" BASIS, 21 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 # See the License for the specific language governing permissions and 23 # limitations under the License. 24 25 set -e 26 27 # https://stackoverflow.com/questions/59895/how-can-i-get-the-source-directory-of-a-bash-script-from-within-the-script-itsel 28 # Note: the normal way we use in other scripts in Istio do not work when `source`d, which is why we use this approach 29 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) 30 REPO_ROOT="$(dirname "$(dirname "${SCRIPT_DIR}")")" 31 32 LOCAL_ARCH=$(uname -m) 33 34 # Pass environment set target architecture to build system 35 if [[ ${TARGET_ARCH} ]]; then 36 # Target explicitly set 37 : 38 elif [[ ${LOCAL_ARCH} == x86_64 ]]; then 39 TARGET_ARCH=amd64 40 elif [[ ${LOCAL_ARCH} == armv8* ]]; then 41 TARGET_ARCH=arm64 42 elif [[ ${LOCAL_ARCH} == arm64* ]]; then 43 TARGET_ARCH=arm64 44 elif [[ ${LOCAL_ARCH} == aarch64* ]]; then 45 TARGET_ARCH=arm64 46 elif [[ ${LOCAL_ARCH} == armv* ]]; then 47 TARGET_ARCH=arm 48 elif [[ ${LOCAL_ARCH} == s390x ]]; then 49 TARGET_ARCH=s390x 50 elif [[ ${LOCAL_ARCH} == ppc64le ]]; then 51 TARGET_ARCH=ppc64le 52 else 53 echo "This system's architecture, ${LOCAL_ARCH}, isn't supported" 54 exit 1 55 fi 56 57 LOCAL_OS=$(uname) 58 59 # Pass environment set target operating-system to build system 60 if [[ ${TARGET_OS} ]]; then 61 # Target explicitly set 62 : 63 elif [[ $LOCAL_OS == Linux ]]; then 64 TARGET_OS=linux 65 readlink_flags="-f" 66 elif [[ $LOCAL_OS == Darwin ]]; then 67 TARGET_OS=darwin 68 readlink_flags="" 69 else 70 echo "This system's OS, $LOCAL_OS, isn't supported" 71 exit 1 72 fi 73 74 # Build image to use 75 TOOLS_REGISTRY_PROVIDER=${TOOLS_REGISTRY_PROVIDER:-gcr.io} 76 PROJECT_ID=${PROJECT_ID:-istio-testing} 77 if [[ "${IMAGE_VERSION:-}" == "" ]]; then 78 IMAGE_VERSION=master-62b37843401cf50e072f1d7f4d565730b708d642 79 fi 80 if [[ "${IMAGE_NAME:-}" == "" ]]; then 81 IMAGE_NAME=build-tools 82 fi 83 84 DOCKER_GID="${DOCKER_GID:-$(grep '^docker:' /etc/group | cut -f3 -d:)}" 85 86 TIMEZONE=$(readlink "$readlink_flags" /etc/localtime | sed -e 's/^.*zoneinfo\///') 87 88 TARGET_OUT="${TARGET_OUT:-$(pwd)/out/${TARGET_OS}_${TARGET_ARCH}}" 89 TARGET_OUT_LINUX="${TARGET_OUT_LINUX:-$(pwd)/out/linux_${TARGET_ARCH}}" 90 91 CONTAINER_TARGET_OUT="${CONTAINER_TARGET_OUT:-/work/out/${TARGET_OS}_${TARGET_ARCH}}" 92 CONTAINER_TARGET_OUT_LINUX="${CONTAINER_TARGET_OUT_LINUX:-/work/out/linux_${TARGET_ARCH}}" 93 94 IMG="${IMG:-${TOOLS_REGISTRY_PROVIDER}/${PROJECT_ID}/${IMAGE_NAME}:${IMAGE_VERSION}}" 95 96 CONTAINER_CLI="${CONTAINER_CLI:-docker}" 97 98 ENV_BLOCKLIST="${ENV_BLOCKLIST:-^_\|^PATH=\|^GOPATH=\|^GOROOT=\|^SHELL=\|^EDITOR=\|^TMUX=\|^USER=\|^HOME=\|^PWD=\|^TERM=\|^RUBY_\|^GEM_\|^rvm_\|^SSH=\|^TMPDIR=\|^CC=\|^CXX=\|^MAKEFILE_LIST=}" 99 100 # Remove functions from the list of exported variables, they mess up with the `env` command. 101 for f in $(declare -F -x | cut -d ' ' -f 3); 102 do 103 unset -f "${f}" 104 done 105 106 # Set conditional host mounts 107 CONDITIONAL_HOST_MOUNTS="${CONDITIONAL_HOST_MOUNTS:-} " 108 container_kubeconfig='' 109 110 # docker conditional host mount (needed for make docker push) 111 if [[ -d "${HOME}/.docker" ]]; then 112 CONDITIONAL_HOST_MOUNTS+="--mount type=bind,source=${HOME}/.docker,destination=/config/.docker,readonly " 113 fi 114 115 # gcloud conditional host mount (needed for docker push with the gcloud auth configure-docker) 116 if [[ -d "${HOME}/.config/gcloud" ]]; then 117 CONDITIONAL_HOST_MOUNTS+="--mount type=bind,source=${HOME}/.config/gcloud,destination=/config/.config/gcloud,readonly " 118 fi 119 120 # gitconfig conditional host mount (needed for git commands inside container) 121 if [[ -f "${HOME}/.gitconfig" ]]; then 122 CONDITIONAL_HOST_MOUNTS+="--mount type=bind,source=${HOME}/.gitconfig,destination=/home/.gitconfig,readonly " 123 fi 124 125 # .netrc conditional host mount (needed for git commands inside container) 126 if [[ -f "${HOME}/.netrc" ]]; then 127 CONDITIONAL_HOST_MOUNTS+="--mount type=bind,source=${HOME}/.netrc,destination=/home/.netrc,readonly " 128 fi 129 130 # echo ${CONDITIONAL_HOST_MOUNTS} 131 132 # This function checks if the file exists. If it does, it creates a randomly named host location 133 # for the file, adds it to the host KUBECONFIG, and creates a mount for it. Note that we use a copy 134 # of the original file, so that the container can write to it. 135 add_KUBECONFIG_if_exists () { 136 if [[ -f "$1" ]]; then 137 local local_config 138 local_config="$(mktemp)" 139 cp "${1}" "${local_config}" 140 141 kubeconfig_random="$(od -vAn -N4 -tx /dev/random | tr -d '[:space:]' | cut -c1-8)" 142 container_kubeconfig+="/config/${kubeconfig_random}:" 143 CONDITIONAL_HOST_MOUNTS+="--mount type=bind,source=${local_config},destination=/config/${kubeconfig_random} " 144 fi 145 } 146 147 # This function is designed for maximum compatibility with various platforms. This runs on 148 # any Mac or Linux platform with bash 4.2+. Please take care not to modify this function 149 # without testing properly. 150 # 151 # This function will properly handle any type of path including those with spaces using the 152 # loading pattern specified by *kubectl config*. 153 # 154 # testcase: "a:b c:d" 155 # testcase: "a b:c d:e f" 156 # testcase: "a b:c:d e" 157 parse_KUBECONFIG () { 158 TMPDIR="" 159 if [[ "$1" =~ ([^:]*):(.*) ]]; then 160 while true; do 161 rematch=${BASH_REMATCH[1]} 162 add_KUBECONFIG_if_exists "$rematch" 163 remainder="${BASH_REMATCH[2]}" 164 if [[ ! "$remainder" =~ ([^:]*):(.*) ]]; then 165 if [[ -n "$remainder" ]]; then 166 add_KUBECONFIG_if_exists "$remainder" 167 break 168 fi 169 fi 170 done 171 else 172 add_KUBECONFIG_if_exists "$1" 173 fi 174 } 175 176 KUBECONFIG=${KUBECONFIG:="$HOME/.kube/config"} 177 parse_KUBECONFIG "${KUBECONFIG}" 178 if [[ "${FOR_BUILD_CONTAINER:-0}" -eq "1" ]]; then 179 KUBECONFIG="${container_kubeconfig%?}" 180 fi 181 182 # LOCAL_OUT should point to architecture where we are currently running versus the desired. 183 # This is used when we need to run a build artifact during tests or later as part of another 184 # target. 185 if [[ "${FOR_BUILD_CONTAINER:-0}" -eq "1" ]]; then 186 # Override variables with container specific 187 TARGET_OUT=${CONTAINER_TARGET_OUT} 188 TARGET_OUT_LINUX=${CONTAINER_TARGET_OUT_LINUX} 189 REPO_ROOT=/work 190 LOCAL_OUT="${TARGET_OUT_LINUX}" 191 else 192 LOCAL_OUT="${TARGET_OUT}" 193 fi 194 195 go_os_arch=${LOCAL_OUT##*/} 196 # Golang OS/Arch format 197 LOCAL_GO_OS=${go_os_arch%_*} 198 LOCAL_GO_ARCH=${go_os_arch##*_} 199 200 BUILD_WITH_CONTAINER=0 201 202 VARS=( 203 CONTAINER_TARGET_OUT 204 CONTAINER_TARGET_OUT_LINUX 205 TARGET_OUT 206 TARGET_OUT_LINUX 207 LOCAL_GO_OS 208 LOCAL_GO_ARCH 209 LOCAL_OUT 210 LOCAL_OS 211 TARGET_OS 212 LOCAL_ARCH 213 TARGET_ARCH 214 TIMEZONE 215 KUBECONFIG 216 CONDITIONAL_HOST_MOUNTS 217 ENV_BLOCKLIST 218 CONTAINER_CLI 219 DOCKER_GID 220 IMG 221 IMAGE_NAME 222 IMAGE_VERSION 223 REPO_ROOT 224 BUILD_WITH_CONTAINER 225 ) 226 227 # For non container build, we need to write env to file 228 if [[ "${1}" == "envfile" ]]; then 229 # ! does a variable-variable https://stackoverflow.com/a/10757531/374797 230 for var in "${VARS[@]}"; do 231 echo "${var}"="${!var}" 232 done 233 else 234 for var in "${VARS[@]}"; do 235 # shellcheck disable=SC2163 236 export "${var}" 237 done 238 fi