istio.io/istio@v0.0.0-20240520182934-d79c90f27776/bin/init.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2018 Istio Authors
     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  
    17  # Init script downloads or updates envoy and the go dependencies. Called from Makefile, which sets
    18  # the needed environment variables.
    19  
    20  set -o errexit
    21  set -o nounset
    22  set -o pipefail
    23  
    24  if [[ "${TARGET_OUT_LINUX:-}" == "" ]]; then
    25    echo "Environment variables not set. Make sure you run through the makefile (\`make init\`) rather than directly."
    26    exit 1
    27  fi
    28  
    29  # Setup arch suffix for envoy binary. For backwards compatibility, amd64 has no suffix.
    30  if [[ "${TARGET_ARCH}" == "amd64" ]]; then
    31  	ISTIO_ENVOY_ARCH_SUFFIX=""
    32  else
    33  	ISTIO_ENVOY_ARCH_SUFFIX="-${TARGET_ARCH}"
    34  fi
    35  
    36  # Populate the git version for istio/proxy (i.e. Envoy)
    37  PROXY_REPO_SHA="${PROXY_REPO_SHA:-$(grep PROXY_REPO_SHA istio.deps  -A 4 | grep lastStableSHA | cut -f 4 -d '"')}"
    38  
    39  # Envoy binary variables
    40  ISTIO_ENVOY_BASE_URL="${ISTIO_ENVOY_BASE_URL:-https://storage.googleapis.com/istio-build/proxy}"
    41  
    42  # If we are not using the default, assume its private and we need to authenticate
    43  if [[ "${ISTIO_ENVOY_BASE_URL}" != "https://storage.googleapis.com/istio-build/proxy" ]]; then
    44    AUTH_HEADER="Authorization: Bearer $(gcloud auth print-access-token)"
    45    export AUTH_HEADER
    46  fi
    47  
    48  SIDECAR="${SIDECAR:-envoy}"
    49  
    50  # OS-neutral vars. These currently only work for linux.
    51  ISTIO_ENVOY_VERSION="${ISTIO_ENVOY_VERSION:-${PROXY_REPO_SHA}}"
    52  ISTIO_ENVOY_DEBUG_URL="${ISTIO_ENVOY_DEBUG_URL:-${ISTIO_ENVOY_BASE_URL}/envoy-debug-${ISTIO_ENVOY_VERSION}${ISTIO_ENVOY_ARCH_SUFFIX}.tar.gz}"
    53  ISTIO_ENVOY_RELEASE_URL="${ISTIO_ENVOY_RELEASE_URL:-${ISTIO_ENVOY_BASE_URL}/envoy-alpha-${ISTIO_ENVOY_VERSION}${ISTIO_ENVOY_ARCH_SUFFIX}.tar.gz}"
    54  
    55  # Envoy Linux vars.
    56  ISTIO_ENVOY_LINUX_VERSION="${ISTIO_ENVOY_LINUX_VERSION:-${ISTIO_ENVOY_VERSION}}"
    57  ISTIO_ENVOY_LINUX_DEBUG_URL="${ISTIO_ENVOY_LINUX_DEBUG_URL:-${ISTIO_ENVOY_DEBUG_URL}}"
    58  ISTIO_ENVOY_LINUX_RELEASE_URL="${ISTIO_ENVOY_LINUX_RELEASE_URL:-${ISTIO_ENVOY_RELEASE_URL}}"
    59  # Variables for the extracted debug/release Envoy artifacts.
    60  ISTIO_ENVOY_LINUX_DEBUG_DIR="${ISTIO_ENVOY_LINUX_DEBUG_DIR:-${TARGET_OUT_LINUX}/debug}"
    61  ISTIO_ENVOY_LINUX_DEBUG_NAME="${ISTIO_ENVOY_LINUX_DEBUG_NAME:-envoy-debug-${ISTIO_ENVOY_LINUX_VERSION}}"
    62  ISTIO_ENVOY_LINUX_DEBUG_PATH="${ISTIO_ENVOY_LINUX_DEBUG_PATH:-${ISTIO_ENVOY_LINUX_DEBUG_DIR}/${ISTIO_ENVOY_LINUX_DEBUG_NAME}}"
    63  
    64  ISTIO_ENVOY_LINUX_RELEASE_DIR="${ISTIO_ENVOY_LINUX_RELEASE_DIR:-${TARGET_OUT_LINUX}/release}"
    65  ISTIO_ENVOY_LINUX_RELEASE_NAME="${ISTIO_ENVOY_LINUX_RELEASE_NAME:-${SIDECAR}-${ISTIO_ENVOY_VERSION}}"
    66  ISTIO_ENVOY_LINUX_RELEASE_PATH="${ISTIO_ENVOY_LINUX_RELEASE_PATH:-${ISTIO_ENVOY_LINUX_RELEASE_DIR}/${ISTIO_ENVOY_LINUX_RELEASE_NAME}}"
    67  
    68  # There is no longer an Istio built Envoy binary available for the Mac. Copy the Linux binary as the Mac binary was
    69  # very old and likely no one was really using it (at least temporarily).
    70  
    71  # Download Envoy debug and release binaries for Linux x86_64. They will be included in the
    72  # docker images created by Dockerfile.proxyv2.
    73  
    74  # Gets the download command supported by the system (currently either curl or wget)
    75  DOWNLOAD_COMMAND=""
    76  function set_download_command () {
    77    # Try curl.
    78    if command -v curl > /dev/null; then
    79      if curl --version | grep Protocols  | grep https > /dev/null; then
    80        DOWNLOAD_COMMAND="curl -fLSs --retry 5 --retry-delay 1 --retry-connrefused"
    81        return
    82      fi
    83      echo curl does not support https, will try wget for downloading files.
    84    else
    85      echo curl is not installed, will try wget for downloading files.
    86    fi
    87  
    88    # Try wget.
    89    if command -v wget > /dev/null; then
    90      DOWNLOAD_COMMAND="wget -qO -"
    91      return
    92    fi
    93    echo wget is not installed.
    94  
    95    echo Error: curl is not installed or does not support https, wget is not installed. \
    96         Cannot download envoy. Please install wget or add support of https to curl.
    97    exit 1
    98  }
    99  
   100  # Downloads and extract an Envoy binary if the artifact doesn't already exist.
   101  # Params:
   102  #   $1: The URL of the Envoy tar.gz to be downloaded.
   103  #   $2: The full path of the output binary.
   104  #   $3: Non-versioned name to use
   105  function download_envoy_if_necessary () {
   106    if [[ ! -f "$2" ]] ; then
   107      # Enter the output directory.
   108      mkdir -p "$(dirname "$2")"
   109      pushd "$(dirname "$2")"
   110  
   111      # Download and extract the binary to the output directory.
   112      echo "Downloading ${SIDECAR}: $1 to $2"
   113      time ${DOWNLOAD_COMMAND} --header "${AUTH_HEADER:-}" "$1" |\
   114        tar --extract --gzip --strip-components=3 --to-stdout > "$2"
   115      chmod +x "$2"
   116  
   117      # Make a copy named just "envoy" in the same directory (overwrite if necessary).
   118      echo "Copying $2 to $(dirname "$2")/${3}"
   119      cp -f "$2" "$(dirname "$2")/${3}"
   120      popd
   121    fi
   122  }
   123  
   124  mkdir -p "${TARGET_OUT}"
   125  
   126  # Set the value of DOWNLOAD_COMMAND (either curl or wget)
   127  set_download_command
   128  
   129  if [[ -n "${DEBUG_IMAGE:-}" ]]; then
   130    # Download and extract the Envoy linux debug binary.
   131    download_envoy_if_necessary "${ISTIO_ENVOY_LINUX_DEBUG_URL}" "$ISTIO_ENVOY_LINUX_DEBUG_PATH" "${SIDECAR}"
   132  else
   133    echo "Skipping envoy debug. Set DEBUG_IMAGE to download."
   134  fi
   135  
   136  # Download and extract the Envoy linux release binary.
   137  download_envoy_if_necessary "${ISTIO_ENVOY_LINUX_RELEASE_URL}" "$ISTIO_ENVOY_LINUX_RELEASE_PATH" "${SIDECAR}"
   138  ISTIO_ENVOY_NATIVE_PATH=${ISTIO_ENVOY_LINUX_RELEASE_PATH}
   139  
   140  # Copy native envoy binary to TARGET_OUT
   141  echo "Copying ${ISTIO_ENVOY_NATIVE_PATH} to ${TARGET_OUT}/${SIDECAR}"
   142  cp -f "${ISTIO_ENVOY_NATIVE_PATH}" "${TARGET_OUT}/${SIDECAR}"
   143  
   144  # Copy the envoy binary to TARGET_OUT_LINUX if the local OS is not Linux
   145  if [[ "$GOOS_LOCAL" != "linux" ]]; then
   146     echo "Copying ${ISTIO_ENVOY_LINUX_RELEASE_PATH} to ${TARGET_OUT_LINUX}/${SIDECAR}"
   147    cp -f "${ISTIO_ENVOY_LINUX_RELEASE_PATH}" "${TARGET_OUT_LINUX}/${SIDECAR}"
   148  fi