go.fuchsia.dev/jiri@v0.0.0-20240502161911-b66513b29486/scripts/bootstrap_jiri (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2015 The Vanadium Authors. All rights reserved.
     3  # Use of this source code is governed by a BSD-style
     4  # license that can be found in the LICENSE file.
     5  
     6  # bootstrap_jiri initializes a root directory for jiri.  The following
     7  # directories and files will be created:
     8  #   <root_dir>                         - root directory (picked by user)
     9  #   <root_dir>/.jiri_root              - root metadata directory
    10  #   <root_dir>/.jiri_root/bin/jiri     - jiri binary
    11  #
    12  # The jiri sources are downloaded and built into a temp directory, which is
    13  # always deleted when this script finishes.  The <root_dir> is deleted on any
    14  # failure.
    15  
    16  set -euf -o pipefail
    17  
    18  # Jiri repo, from which we will download the jiri script wrapper.
    19  readonly JIRI_REPO_URL="https://fuchsia.googlesource.com/jiri"
    20  
    21  # Google Storage bucket that contains prebuilt versions of jiri.
    22  readonly CIPD_ENDPOINT_URL="https://chrome-infra-packages.appspot.com/dl/fuchsia/tools/jiri"
    23  
    24  # fatal prints an error message, followed by the usage string, and then exits.
    25  fatal() {
    26    usage='
    27  
    28  Usage:
    29     bootstrap_jiri <root_dir>
    30  
    31  A typical bootstrap workflow looks like this:
    32  
    33  $ curl -s https://fuchsia.googlesource.com/jiri/+/main/scripts/bootstrap_jiri?format=TEXT | base64 --decode | bash -s myroot
    34  $ export PATH=myroot/.jiri_root/bin:$PATH
    35  $ cd myroot
    36  $ jiri import manifest https://example.com/manifest
    37  $ jiri update'
    38    echo "ERROR: $@${usage}" 1>&2
    39    exit 1
    40  }
    41  
    42  readonly HOST_ARCH=$(uname -m)
    43  if [ "$HOST_ARCH" == "aarch64" -o "$HOST_ARCH" == "arm64" ]; then
    44    readonly ARCH="arm64"
    45  elif [ "$HOST_ARCH" == "x86_64" ]; then
    46    readonly ARCH="amd64"
    47  else
    48    echo "Arch not supported: $HOST_ARCH"
    49    exit 1
    50  fi
    51  
    52  # toabs converts the possibly relative argument into an absolute path.  Run in a
    53  # subshell to avoid changing the caller's working directory.
    54  toabs() (
    55    cd $(dirname $1)
    56    echo ${PWD}/$(basename $1)
    57  )
    58  
    59  # Check the <root_dir> argument is supplied.
    60  if [[ $# -ne 1 ]]; then
    61    fatal "need <root_dir> argument"
    62  fi
    63  
    64  readonly ROOT_DIR="$(toabs $1)"
    65  readonly BIN_DIR="${ROOT_DIR}/.jiri_root/bin"
    66  
    67  # If ROOT_DIR doesn't already exist, we will destroy it during the exit trap,
    68  # otherwise we will only destroy the BIN_DIR
    69  if [[ -d "${ROOT_DIR}" ]]; then
    70    CLEANUP_DIR="${BIN_DIR}"
    71  else
    72    CLEANUP_DIR="${ROOT_DIR}"
    73  fi
    74  mkdir -p "${BIN_DIR}"
    75  
    76  # Remove the root_dir if this script fails so as to not leave the environment in a strange half-state.
    77  trap "rm -rf -- \"${CLEANUP_DIR}\"" INT TERM EXIT
    78  
    79  # Determine and validate the version of jiri.
    80  HOST_OS=$(uname | tr '[:upper:]' '[:lower:]')
    81  if [[ ${HOST_OS} == "darwin" ]]; then
    82    HOST_OS="mac"
    83  fi
    84  readonly TARGET="${HOST_OS}-${ARCH}"
    85  readonly COMMIT_URL="${JIRI_REPO_URL}/+refs/heads/main?format=JSON"
    86  readonly LOG_URL="${JIRI_REPO_URL}/+log/refs/heads/main?format=JSON"
    87  readonly VERSION=$(curl -sSf "${COMMIT_URL}" | sed -n 's/.*"value": "\([0-9a-f]\{40\}\)"/\1/p')
    88  readonly VERSIONS=$(curl -sSf "${LOG_URL}" | sed -n 's/.*"commit": "\([0-9a-f]\{40\}\)".*/\1/p')
    89  readonly TEMP_FILE=$(mktemp)
    90  
    91  JIRI_URL=""
    92  for version in ${VERSIONS}; do
    93    url="${CIPD_ENDPOINT_URL}/${TARGET}/+/git_revision:${version}"
    94    if curl --output /dev/null --silent --fail "${url}"; then
    95      JIRI_URL="${url}"
    96  	break
    97    fi
    98  done
    99  
   100  if [[ -z "${JIRI_URL}" ]]; then
   101    echo "Cannot find prebuilt Jiri binary." 1>&2
   102    exit 1
   103  fi
   104  
   105  if ! curl -sfL -o "${TEMP_FILE}" "${JIRI_URL}"; then
   106    echo "Failed downloading prebuilt Jiri archive." 1>&2
   107    exit 1
   108  fi
   109  
   110  if ! unzip -o ${TEMP_FILE} jiri -d ${BIN_DIR}  > /dev/null; then
   111    echo "Failed unarchiving Jiri"
   112    rm -rf ${TEMP_FILE}
   113    exit 1
   114  fi
   115  
   116  rm -rf ${TEMP_FILE}
   117  chmod 755 "${BIN_DIR}/jiri"
   118  
   119  # Install cipd, which is frequently needed to use manifests.
   120  pushd "${BIN_DIR}" > /dev/null
   121  if ! "${BIN_DIR}/jiri" bootstrap cipd; then
   122    echo "Running jiri bootstrap failed." 1>&2
   123    popd > /dev/null
   124    exit 1
   125  fi
   126  popd > /dev/null
   127  
   128  echo "Please add ${BIN_DIR} to your PATH"
   129  
   130  trap - EXIT